Hoist namespace exclusion check, fix stale nsTracker doc

Address review feedback from adam-jian-zhang: rule b's doc comment
still described union semantics, which no longer matches the
precedence-based implementation (explicit exclusion > label-selector
match). Also hoist the repeated ShouldInclude check to the top of the
loop instead of duplicating it in each branch.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: Tiger Kaovilai <tkaovila@redhat.com>
This commit is contained in:
Tiger Kaovilai
2026-09-16 16:38:19 -04:00
co-authored by Claude Sonnet 5
parent 844cab71f8
commit 39d33ca0a3
+16 -11
View File
@@ -66,8 +66,9 @@ type itemCollector struct {
// The namespaces, which do not have backup including resources,
// are not collected.
//
// b. If the namespace I/E filters and the (Or)LabelSelectors selected
// namespaces are different. The tracker takes the union of them.
// b. Explicit namespace exclusion takes precedence over the
// (Or)LabelSelectors: an excluded namespace is never tracked, even
// when its own labels (or a resource within it) match the selector(s).
type nsTracker struct {
singleLabelSelector labels.Selector
orLabelSelector []labels.Selector
@@ -125,9 +126,15 @@ func (nt *nsTracker) init(
continue
}
// Explicit namespace exclusion takes precedence over the
// (Or)LabelSelectors, so check it upfront once instead of
// repeating it in every branch below.
if !nt.namespaceFilter.ShouldInclude(namespace.GetName()) {
continue
}
if nt.singleLabelSelector != nil &&
nt.singleLabelSelector.Matches(labels.Set(namespace.GetLabels())) &&
nt.namespaceFilter.ShouldInclude(namespace.GetName()) {
nt.singleLabelSelector.Matches(labels.Set(namespace.GetLabels())) {
nt.logger.Debugf("Track namespace %s, because its labels match backup LabelSelector.",
namespace.GetName(),
)
@@ -136,7 +143,7 @@ func (nt *nsTracker) init(
continue
}
if len(nt.orLabelSelector) > 0 && nt.namespaceFilter.ShouldInclude(namespace.GetName()) {
if len(nt.orLabelSelector) > 0 {
for _, selector := range nt.orLabelSelector {
if selector.Matches(labels.Set(namespace.GetLabels())) {
nt.logger.Debugf("Track namespace %s, because its labels match the backup OrLabelSelector.",
@@ -157,12 +164,10 @@ func (nt *nsTracker) init(
continue
}
if nt.namespaceFilter.ShouldInclude(namespace.GetName()) {
nt.logger.Debugf("Track namespace %s, because its name match the backup namespace filter.",
namespace.GetName(),
)
nt.track(namespace.GetName())
}
nt.logger.Debugf("Track namespace %s, because its name match the backup namespace filter.",
namespace.GetName(),
)
nt.track(namespace.GetName())
}
}