Skip to content

Commit

Permalink
Fix vmware-tanzu#6752: add namespace exclude check.
Browse files Browse the repository at this point in the history
Add PSA audit and warn labels.

Signed-off-by: Xun Jiang <[email protected]>
  • Loading branch information
Xun Jiang committed Sep 5, 2023
1 parent 8d3a675 commit 2c41244
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/6760-blackpiglet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix #6752: add namespace exclude check.
26 changes: 4 additions & 22 deletions pkg/backup/item_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (r *itemCollector) getResourceItems(log logrus.FieldLogger, gv schema.Group
return nil, errors.WithStack(err)
}

items := r.backupNamespaces(unstructuredList, namespacesToList, gr, preferredGVR, log)
items := r.backupNamespaces(unstructuredList, r.backupRequest.NamespaceIncludesExcludes, gr, preferredGVR, log)

return items, nil
}
Expand Down Expand Up @@ -533,31 +533,13 @@ func (r *itemCollector) listItemsForLabel(unstructuredItems []unstructured.Unstr

// backupNamespaces process namespace resource according to namespace filters.
func (r *itemCollector) backupNamespaces(unstructuredList *unstructured.UnstructuredList,
namespacesToList []string, gr schema.GroupResource, preferredGVR schema.GroupVersionResource,
ie *collections.IncludesExcludes, gr schema.GroupResource, preferredGVR schema.GroupVersionResource,
log logrus.FieldLogger) []*kubernetesResource {
var items []*kubernetesResource
for index, unstructured := range unstructuredList.Items {
found := false
if len(namespacesToList) == 0 {
// No namespace found. By far, this condition cannot be triggered. Either way,
// namespacesToList is not empty.
log.Debug("Skip namespace resource, because no item found by namespace filters.")
break
} else if len(namespacesToList) == 1 && namespacesToList[0] == "" {
// All namespaces are included.
log.Debugf("Backup namespace %s due to full cluster backup.", unstructured.GetName())
found = true
} else {
for _, ns := range namespacesToList {
if unstructured.GetName() == ns {
log.Debugf("Backup namespace %s due to namespace filters setting.", unstructured.GetName())
found = true
break
}
}
}
if ie.ShouldInclude(unstructured.GetName()) {
log.Debugf("Backup namespace %s due to namespace filters setting.", unstructured.GetName())

if found {
path, err := r.writeToFile(&unstructuredList.Items[index])
if err != nil {
log.WithError(err).Error("Error writing item to file")
Expand Down
4 changes: 4 additions & 0 deletions pkg/install/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ func Namespace(namespace string) *corev1.Namespace {

ns.Labels["pod-security.kubernetes.io/enforce"] = "privileged"
ns.Labels["pod-security.kubernetes.io/enforce-version"] = "latest"
ns.Labels["pod-security.kubernetes.io/audit"] = "privileged"
ns.Labels["pod-security.kubernetes.io/audit-version"] = "latest"
ns.Labels["pod-security.kubernetes.io/warn"] = "privileged"
ns.Labels["pod-security.kubernetes.io/warn-version"] = "latest"

return ns
}
Expand Down

0 comments on commit 2c41244

Please sign in to comment.