From 360095a2e76105e33b45aecd7f6834169c8e70aa Mon Sep 17 00:00:00 2001 From: Joseph Antony Vaikath Date: Wed, 6 May 2026 04:30:36 -0400 Subject: [PATCH] Merge pull request #9558 from Joeavaikath/wildcard-ns-bugfix Wildcard namespaces: Log warning on empty resolution --- changelogs/unreleased/9558-Joeavaikath | 1 + pkg/backup/backup.go | 41 ++++++++++++++----- pkg/util/collections/includes_excludes.go | 1 - .../docs/main/namespace-glob-patterns.md | 2 + 4 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 changelogs/unreleased/9558-Joeavaikath diff --git a/changelogs/unreleased/9558-Joeavaikath b/changelogs/unreleased/9558-Joeavaikath new file mode 100644 index 000000000..c9be4839e --- /dev/null +++ b/changelogs/unreleased/9558-Joeavaikath @@ -0,0 +1 @@ +Wildcard namespaces: Log warning on empty resolution diff --git a/pkg/backup/backup.go b/pkg/backup/backup.go index 1a1c54247..9edaf6a85 100644 --- a/pkg/backup/backup.go +++ b/pkg/backup/backup.go @@ -167,15 +167,15 @@ func NewKubernetesBackupper( }, nil } -// getNamespaceIncludesExcludesAndArgoCDNamespaces returns an IncludesExcludes list containing which namespaces to -// include and exclude from the backup and a list of namespaces managed by ArgoCD. -func getNamespaceIncludesExcludesAndArgoCDNamespaces(backup *velerov1api.Backup, kbClient kbclient.Client) (*collections.NamespaceIncludesExcludes, []string, error) { +// getNamespaceIncludesExcludes returns an IncludesExcludes list containing which namespaces to +// include and exclude from the backup. +func getNamespaceIncludesExcludes(backup *velerov1api.Backup, kbClient kbclient.Client) (*collections.NamespaceIncludesExcludes, error) { nsList := corev1api.NamespaceList{} - activeNamespaces := []string{} - nsManagedByArgoCD := []string{} if err := kbClient.List(context.Background(), &nsList); err != nil { - return nil, nsManagedByArgoCD, err + return nil, err } + + activeNamespaces := []string{} for _, ns := range nsList.Items { activeNamespaces = append(activeNamespaces, ns.Name) } @@ -188,10 +188,20 @@ func getNamespaceIncludesExcludesAndArgoCDNamespaces(backup *velerov1api.Backup, // Expand wildcards if needed if err := includesExcludes.ExpandIncludesExcludes(); err != nil { - return nil, []string{}, err + return nil, err } - // Check for ArgoCD managed namespaces in the namespaces that will be included + return includesExcludes, nil +} + +// getArgoCDManagedNamespaces returns a list of namespaces managed by ArgoCD that should be included in the backup. +func getArgoCDManagedNamespaces(kbClient kbclient.Client, includesExcludes *collections.NamespaceIncludesExcludes) ([]string, error) { + nsList := corev1api.NamespaceList{} + if err := kbClient.List(context.Background(), &nsList); err != nil { + return nil, err + } + + nsManagedByArgoCD := []string{} for _, ns := range nsList.Items { nsLabels := ns.GetLabels() if len(nsLabels[ArgoCDManagedByNamespaceLabel]) > 0 && includesExcludes.ShouldInclude(ns.Name) { @@ -199,7 +209,7 @@ func getNamespaceIncludesExcludesAndArgoCDNamespaces(backup *velerov1api.Backup, } } - return includesExcludes, nsManagedByArgoCD, nil + return nsManagedByArgoCD, nil } func getResourceHooks(hookSpecs []velerov1api.BackupResourceHookSpec, discoveryHelper discovery.Helper) ([]hook.ResourceHook, error) { @@ -274,13 +284,18 @@ func (kb *kubernetesBackupper) BackupWithResolvers( return errors.WithStack(err) } var err error - var nsManagedByArgoCD []string - backupRequest.NamespaceIncludesExcludes, nsManagedByArgoCD, err = getNamespaceIncludesExcludesAndArgoCDNamespaces(backupRequest.Backup, kb.kbClient) + backupRequest.NamespaceIncludesExcludes, err = getNamespaceIncludesExcludes(backupRequest.Backup, kb.kbClient) if err != nil { log.WithError(err).Errorf("error getting namespace includes/excludes") return err } + nsManagedByArgoCD, err := getArgoCDManagedNamespaces(kb.kbClient, backupRequest.NamespaceIncludesExcludes) + if err != nil { + log.WithError(err).Errorf("error getting ArgoCD managed namespaces") + return err + } + if backupRequest.NamespaceIncludesExcludes.IsWildcardExpanded() { expandedIncludes := backupRequest.NamespaceIncludesExcludes.GetIncludes() expandedExcludes := backupRequest.NamespaceIncludesExcludes.GetExcludes() @@ -292,6 +307,10 @@ func (kb *kubernetesBackupper) BackupWithResolvers( return err } + if len(wildcardResult) == 0 { + log.Warnf("no namespaces matched the resolution of wildcard patterns ") + } + log.WithFields(logrus.Fields{ "expandedIncludes": expandedIncludes, "expandedExcludes": expandedExcludes, diff --git a/pkg/util/collections/includes_excludes.go b/pkg/util/collections/includes_excludes.go index ab63eaa72..f326a4124 100644 --- a/pkg/util/collections/includes_excludes.go +++ b/pkg/util/collections/includes_excludes.go @@ -173,7 +173,6 @@ func (nie *NamespaceIncludesExcludes) ExpandIncludesExcludes() error { } // ResolveNamespaceList returns a list of all namespaces which will be backed up. -// The second return value indicates whether wildcard expansion was performed. func (nie *NamespaceIncludesExcludes) ResolveNamespaceList() ([]string, error) { // Check if this is being called by non-backup processing e.g. backup queue controller if !nie.wildcardExpanded { diff --git a/site/content/docs/main/namespace-glob-patterns.md b/site/content/docs/main/namespace-glob-patterns.md index 4695124ea..c4cea9195 100644 --- a/site/content/docs/main/namespace-glob-patterns.md +++ b/site/content/docs/main/namespace-glob-patterns.md @@ -5,6 +5,8 @@ layout: docs When using `--include-namespaces` and `--exclude-namespaces` flags with backup and restore commands, you can use glob patterns to match multiple namespaces. +Note: If the resolution of namespace patterns results in no namespaces, the backup will succeed with a warning. + ## Supported Patterns Velero supports the following glob pattern characters: