Skip to content

Commit

Permalink
Add Backup warning for inclusion of NS managed by ArgoCD
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Pampattiwar <spampatt@redhat.com>

add changelog file

Signed-off-by: Shubham Pampattiwar <spampatt@redhat.com>
  • Loading branch information
shubham-pampattiwar committed Oct 1, 2024
1 parent 42de654 commit 86d0f0d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/8257-shubham-pampattiwar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add Backup warning for inclusion of NS managed by ArgoCD
31 changes: 31 additions & 0 deletions pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"encoding/json"
"fmt"
"io"
corev1api "k8s.io/api/core/v1"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -71,6 +72,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
Expand Down Expand Up @@ -244,6 +248,15 @@ func (kb *kubernetesBackupper) BackupWithResolvers(
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 label "argocd.argoproj.io/managed-by" in the includedNamespaces and add a warning
// so that users are at least aware about the existence of argoCD managed ns in their backup
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,
Expand Down Expand Up @@ -1107,3 +1120,21 @@ func putVolumeInfos(

return backupStore.PutBackupVolumeInfos(backupName, backupVolumeInfoBuf)
}

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
}

0 comments on commit 86d0f0d

Please sign in to comment.