From 1f897abf42d9f5be229682d68e540934293a2c51 Mon Sep 17 00:00:00 2001 From: Shubham Pampattiwar Date: Fri, 18 Oct 2024 19:55:57 -0700 Subject: [PATCH] Add Backup warning for inclusion of NS managed by ArgoCD (#354) make update Signed-off-by: Shubham Pampattiwar --- pkg/backup/backup.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pkg/backup/backup.go b/pkg/backup/backup.go index ed68862365..dab850cf2f 100644 --- a/pkg/backup/backup.go +++ b/pkg/backup/backup.go @@ -27,6 +27,8 @@ import ( "path/filepath" "time" + corev1api "k8s.io/api/core/v1" + "github.com/pkg/errors" "github.com/sirupsen/logrus" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -62,6 +64,9 @@ const BackupVersion = 1 // BackupFormatVersion is the current backup version for Velero, including major, minor, and patch. const BackupFormatVersion = "1.1.0" +// ArgoCD managed by namespace label key +const ArgoCDManagedByNamespaceLabel = "argocd.argoproj.io/managed-by" + // Backupper performs backups. type Backupper interface { // Backup takes a backup using the specification in the velerov1api.Backup and writes backup and log data @@ -203,6 +208,17 @@ func (kb *kubernetesBackupper) BackupWithResolvers(log logrus.FieldLogger, log.Infof("Including namespaces: %s", backupRequest.NamespaceIncludesExcludes.IncludesString()) log.Infof("Excluding namespaces: %s", backupRequest.NamespaceIncludesExcludes.ExcludesString()) + // check if there are any namespaces included in the backup which are managed by argoCD + // We will check for the existence of a ArgoCD label in the includedNamespaces and add a warning + // so that users are at least aware about the existence of argoCD managed ns in their backup + if len(backupRequest.Spec.IncludedNamespaces) > 0 { + nsManagedByArgoCD := getNamespacesManagedByArgoCD(kb.kbClient, backupRequest.Spec.IncludedNamespaces, log) + + if len(nsManagedByArgoCD) > 0 { + log.Warnf("backup operation may encounter complications and potentially produce undesirable results due to the inclusion of namespaces %v managed by ArgoCD in the backup.", nsManagedByArgoCD) + } + } + if collections.UseOldResourceFilters(backupRequest.Spec) { backupRequest.ResourceIncludesExcludes = collections.GetGlobalResourceIncludesExcludes(kb.discoveryHelper, log, backupRequest.Spec.IncludedResources, @@ -716,3 +732,21 @@ type tarWriter interface { Write([]byte) (int, error) WriteHeader(*tar.Header) error } + +func getNamespacesManagedByArgoCD(kbClient kbclient.Client, includedNamespaces []string, log logrus.FieldLogger) []string { + var nsManagedByArgoCD []string + + for _, nsName := range includedNamespaces { + ns := corev1api.Namespace{} + if err := kbClient.Get(context.Background(), kbclient.ObjectKey{Name: nsName}, &ns); err != nil { + log.WithError(err).Errorf("error getting namespace %s", nsName) + continue + } + + nsLabels := ns.GetLabels() + if len(nsLabels[ArgoCDManagedByNamespaceLabel]) > 0 { + nsManagedByArgoCD = append(nsManagedByArgoCD, nsName) + } + } + return nsManagedByArgoCD +}