From eb08bdeb62dc53611dfb008f87f9cf4791e3c4a6 Mon Sep 17 00:00:00 2001 From: allenxu404 Date: Wed, 3 Aug 2022 20:00:49 +0800 Subject: [PATCH 1/2] fix issue#2413 by treating namespaces with exclude label as excludedNamespaces Signed-off-by: allenxu404 --- changelogs/unreleased/5178-allenxu404 | 2 ++ pkg/controller/backup_controller.go | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/5178-allenxu404 diff --git a/changelogs/unreleased/5178-allenxu404 b/changelogs/unreleased/5178-allenxu404 new file mode 100644 index 0000000000..2c1b4b0e04 --- /dev/null +++ b/changelogs/unreleased/5178-allenxu404 @@ -0,0 +1,2 @@ +Treat namespaces with exclude label as excludedNamespaces +Related issue: #2413 diff --git a/pkg/controller/backup_controller.go b/pkg/controller/backup_controller.go index 3f54e8f4f3..387e922bd6 100644 --- a/pkg/controller/backup_controller.go +++ b/pkg/controller/backup_controller.go @@ -70,6 +70,7 @@ import ( "github.com/vmware-tanzu/velero/pkg/util/logging" "github.com/vmware-tanzu/velero/pkg/volume" + corev1api "k8s.io/api/core/v1" kbclient "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -257,7 +258,6 @@ func (c *backupController) processBackup(key string) error { log.Debug("Preparing backup request") request := c.prepareBackupRequest(original) - if len(request.Status.ValidationErrors) > 0 { request.Status.Phase = velerov1api.BackupPhaseFailedValidation } else { @@ -436,6 +436,15 @@ func (c *backupController) prepareBackupRequest(backup *velerov1api.Backup) *pkg request.Annotations[velerov1api.SourceClusterK8sMajorVersionAnnotation] = c.discoveryHelper.ServerVersion().Major request.Annotations[velerov1api.SourceClusterK8sMinorVersionAnnotation] = c.discoveryHelper.ServerVersion().Minor + // Add namespaces with label velero.io/exclude-from-backup=true into request.Spec.ExcludedNamespaces + // Essentially, adding the label velero.io/exclude-from-backup=true to a namespace would be equivalent to setting spec.ExcludedNamespaces + namespaces, excludeLabel := corev1api.NamespaceList{}, "velero.io/exclude-from-backup" + if err := c.kbClient.List(context.Background(), &namespaces, kbclient.MatchingLabels{excludeLabel: "true"}); err == nil { + for _, ns := range namespaces.Items { + request.Spec.ExcludedNamespaces = append(request.Spec.ExcludedNamespaces, ns.Name) + } + } + // validate the included/excluded resources for _, err := range collections.ValidateIncludesExcludes(request.Spec.IncludedResources, request.Spec.ExcludedResources) { request.Status.ValidationErrors = append(request.Status.ValidationErrors, fmt.Sprintf("Invalid included/excluded resource lists: %v", err)) From e5d828a2a4e77e28301454638f312883659dcfcf Mon Sep 17 00:00:00 2001 From: allenxu404 Date: Mon, 8 Aug 2022 12:05:08 +0800 Subject: [PATCH 2/2] modify variables defination and expose err Signed-off-by: allenxu404 --- pkg/controller/backup_controller.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/controller/backup_controller.go b/pkg/controller/backup_controller.go index 387e922bd6..5363e2baff 100644 --- a/pkg/controller/backup_controller.go +++ b/pkg/controller/backup_controller.go @@ -438,11 +438,13 @@ func (c *backupController) prepareBackupRequest(backup *velerov1api.Backup) *pkg // Add namespaces with label velero.io/exclude-from-backup=true into request.Spec.ExcludedNamespaces // Essentially, adding the label velero.io/exclude-from-backup=true to a namespace would be equivalent to setting spec.ExcludedNamespaces - namespaces, excludeLabel := corev1api.NamespaceList{}, "velero.io/exclude-from-backup" - if err := c.kbClient.List(context.Background(), &namespaces, kbclient.MatchingLabels{excludeLabel: "true"}); err == nil { + namespaces := corev1api.NamespaceList{} + if err := c.kbClient.List(context.Background(), &namespaces, kbclient.MatchingLabels{"velero.io/exclude-from-backup": "true"}); err == nil { for _, ns := range namespaces.Items { request.Spec.ExcludedNamespaces = append(request.Spec.ExcludedNamespaces, ns.Name) } + } else { + request.Status.ValidationErrors = append(request.Status.ValidationErrors, fmt.Sprintf("error getting namespace list: %v", err)) } // validate the included/excluded resources