-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Design: namespace selection by label selector #9223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
311a12f
a4b4cd8
3ed43fa
04ebf07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Design: namespace selection by label selector |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,330 @@ | ||||||||
| # Design proposal: Namespace Selection by Label Selector for Velero Backups | ||||||||
|
|
||||||||
| ## Abstract | ||||||||
|
|
||||||||
| This proposal modifies Velero's backup behavior to treat namespaces selected by `LabelSelector` or `OrLabelSelectors` as if they were explicitly listed in `includedNamespaces`, causing all resources within those namespaces to be included in the backup. | ||||||||
| Currently, when a namespace matches a label selector, only the namespace object itself and individually labeled resources are backed up, which is not the expected behavior for most users who want complete namespace backups. | ||||||||
|
|
||||||||
| ## Background | ||||||||
|
|
||||||||
| Users frequently need to backup entire namespaces dynamically based on labels rather than maintaining static lists in `includedNamespaces`. | ||||||||
| The current `LabelSelector` behavior only includes resources that individually match the selector, making it difficult to achieve complete namespace backups through labeling. | ||||||||
| This creates operational overhead where users must either manually maintain namespace lists or label every resource within target namespaces. | ||||||||
|
|
||||||||
| Issue #7492 requests this functionality with strong community support (+5 reactions) and multiple real-world use cases from users managing multi-cluster environments where namespace lists vary dynamically. | ||||||||
|
|
||||||||
| ## Goals | ||||||||
|
|
||||||||
| - Enable complete namespace backup through namespace label selection | ||||||||
| - Provide clear precedence rules when combining inclusion/exclusion methods | ||||||||
| - Support full Kubernetes label selector syntax including complex operators | ||||||||
|
|
||||||||
| ## Non Goals | ||||||||
|
|
||||||||
| - Changing behavior of resource-level label selectors within namespaces (resources still respect individual selectors within selected namespaces) | ||||||||
| - Adding new API fields (we will modify existing `LabelSelector` behavior) | ||||||||
| - Supporting per-resource granular control within label-selected namespaces | ||||||||
|
|
||||||||
| ## High-Level Design | ||||||||
|
|
||||||||
| When a namespace matches `spec.labelSelector` or any `spec.orLabelSelectors`, Velero will treat that namespace as if it were explicitly listed in `spec.includedNamespaces`. | ||||||||
| All resources within matching namespaces will be included in the backup, regardless of their individual labels. | ||||||||
| The final namespace selection follows the precedence: `(includedNamespaces ∪ labelSelector_matches ∪ orLabelSelector_matches) - excludedNamespaces`. | ||||||||
|
|
||||||||
| ## Detailed Design | ||||||||
|
|
||||||||
| ### Namespace Selection Logic | ||||||||
|
|
||||||||
| The namespace selection algorithm follows this formula: | ||||||||
|
|
||||||||
| ```text | ||||||||
| Final Namespaces = (ExplicitIncludes ∪ LabelSelectorMatches ∪ OrLabelSelectorMatches) - ExplicitExcludes | ||||||||
| ``` | ||||||||
|
|
||||||||
| Where: | ||||||||
|
|
||||||||
| - `ExplicitIncludes`: Namespaces in `spec.includedNamespaces` | ||||||||
| - `LabelSelectorMatches`: Namespaces matching `spec.labelSelector` | ||||||||
| - `OrLabelSelectorMatches`: Namespaces matching any selector in `spec.orLabelSelectors[]` | ||||||||
| - `ExplicitExcludes`: Namespaces in `spec.excludedNamespaces` (highest precedence) | ||||||||
|
|
||||||||
| ### Implementation Changes | ||||||||
|
|
||||||||
| #### Core Logic Modification | ||||||||
|
|
||||||||
| The primary change occurs in `pkg/backup/item_collector.go` in the `nsTracker.init()` method (lines 102-166). | ||||||||
| Currently, this method tracks namespaces for filtering but doesn't change the fundamental inclusion behavior. | ||||||||
|
|
||||||||
| **Current behavior:** | ||||||||
|
|
||||||||
| ```go | ||||||||
| // Namespace matches selector -> track for resource filtering only | ||||||||
| if nt.singleLabelSelector != nil && nt.singleLabelSelector.Matches(labels.Set(namespace.GetLabels())) { | ||||||||
| nt.track(namespace.GetName()) | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
| **New behavior:** | ||||||||
|
|
||||||||
| ```go | ||||||||
| // Namespace matches selector -> track AND include all resources in namespace | ||||||||
| if nt.singleLabelSelector != nil && nt.singleLabelSelector.Matches(labels.Set(namespace.GetLabels())) { | ||||||||
| nt.track(namespace.GetName()) | ||||||||
| // This namespace now behaves like it's in includedNamespaces | ||||||||
|
||||||||
| // This namespace now behaves like it's in includedNamespaces | |
| nt.includeNamespace(namespace.GetName()) // NEW: treat as if in includedNamespaces |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proposed status fields (status.namespaces.included, status.namespaces.explicitlyIncluded, status.namespaces.selectedByLabels, status.namespaces.excluded) would require adding new fields to the BackupStatus struct. However, the "API Schema" section at lines 107-117 states "No Changes Required", which is contradictory. This design should explicitly document the new status fields as an API addition, similar to how the wildcard namespace design documents status field additions (see design/wildcard-namespace-support-design.md:46-69).
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue 2 is incomplete - it identifies a concern but doesn't include a "Potential Solution" like Issue 1 does. Consider adding potential mitigation strategies such as: providing clear documentation with examples, implementing validation warnings for overly complex selectors, or offering CLI commands to preview namespace selection before execution.
| Users may create overly complex selectors that are difficult to understand or maintain. | |
| Users may create overly complex selectors that are difficult to understand or maintain. | |
| **Potential Solution:** Provide clear documentation with practical examples of label selectors, implement validation warnings or linter tools to alert users about overly complex or ambiguous selectors, and offer CLI commands or UI features to preview which namespaces would be selected by a given selector before executing a backup. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The design describes
labelSelectorandorLabelSelectorsworking together in the union formula, and line 197 mentions a warning for "Using both labelSelector and orLabelSelectors". However, the current API documentation inpkg/apis/velero/v1/backup_types.go:98-99explicitly states: "LabelSelector as well as OrLabelSelectors cannot co-exist in backup request, only one of them can be used." This design must explicitly address whether this mutual exclusivity constraint is being removed as part of this change, or if the formula should be adjusted to reflect the OR relationship between these fields.