Skip to content

Commit

Permalink
fix itemcollector
Browse files Browse the repository at this point in the history
  • Loading branch information
kaovilai committed Oct 23, 2024
1 parent 4043e9a commit 7e85aaf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
1 change: 0 additions & 1 deletion pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ func (kb *kubernetesBackupper) BackupWithResolvers(
if err := kb.writeBackupVersion(tw); err != nil {
return errors.WithStack(err)
}

backupRequest.NamespaceIncludesExcludes = getNamespaceIncludesExcludes(backupRequest.Backup)
log.Infof("Including namespaces: %s", backupRequest.NamespaceIncludesExcludes.IncludesString())
log.Infof("Excluding namespaces: %s", backupRequest.NamespaceIncludesExcludes.ExcludesString())
Expand Down
6 changes: 4 additions & 2 deletions pkg/backup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,12 @@ func TestBackupOldResourceFiltering(t *testing.T) {
),
},
want: []string{
"resources/pods/namespaces/foo/bar.json",
"resources/deployments.apps/namespaces/foo/bar.json",
"resources/pods/v1-preferredversion/namespaces/foo/bar.json",
"resources/deployments.apps/v1-preferredversion/namespaces/foo/bar.json",
"resources/pods/v1-preferredversion/namespaces/foo/bar.json",
"resources/pods/namespaces/foo/bar.json",
"resources/namespaces/cluster/foo.json",
"resources/namespaces/v1-preferredversion/cluster/foo.json",
},
},
// {
Expand Down
35 changes: 30 additions & 5 deletions pkg/backup/item_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import (
type itemCollector struct {
log logrus.FieldLogger
backupRequest *Request
// Namespaces that are included by the backup's labelSelector will backup all resources in that namespace even if resources are not labeled.
namespacesIncludedByLabelSelector map[string]struct{}
discoveryHelper discovery.Helper
dynamicFactory client.DynamicFactory
cohabitatingResources map[string]*cohabitatingResource
Expand Down Expand Up @@ -436,13 +438,29 @@ func (r *itemCollector) getResourceItems(
// Namespace are filtered by namespace include/exclude filters,
// backup LabelSelectors and OrLabelSelectors are checked too.
if gr == kuberesource.Namespaces {
return r.collectNamespaces(
namespaces, err := r.collectNamespaces(
resource,
gv,
gr,
preferredGVR,
log,
)
// The namespaces collected contains namespaces selected by label selector.
// Per https://github.com/vmware-tanzu/velero/issues/7492#issuecomment-1986146411 we want these to act as IncludedNamespaces so add to stringset.
for i := range namespaces {
if namespaces[i] != nil {
if r.backupRequest.NamespaceIncludesExcludes.ShouldInclude(namespaces[i].name) {
// this namespace is included by the backup's label selector, not the namespace include/exclude filter
// this is a special case where we want to include this namespace in the backup and all resources in it regardless of the resource's label selector
if r.namespacesIncludedByLabelSelector == nil {
r.namespacesIncludedByLabelSelector = make(map[string]struct{})
}
r.namespacesIncludedByLabelSelector[namespaces[i].name] = struct{}{}
}
r.backupRequest.NamespaceIncludesExcludes.Includes(namespaces[i].name)
}
}
return namespaces, err
}

clusterScoped := !resource.Namespaced
Expand Down Expand Up @@ -509,9 +527,13 @@ func (r *itemCollector) listResourceByLabelsPerNamespace(
logger.WithError(err).Error("Error getting dynamic client")
return nil, err
}

ignoreItemLabels := false
if _, namespaceSelectedByLabelSelector := r.namespacesIncludedByLabelSelector[namespace]; namespaceSelectedByLabelSelector {
logger.Infof("Listing all items in namespace %s because ns was selected by the backup's label selector", namespace)
ignoreItemLabels = true
}
var orLabelSelectors []string
if r.backupRequest.Spec.OrLabelSelectors != nil {
if r.backupRequest.Spec.OrLabelSelectors != nil && !ignoreItemLabels {
for _, s := range r.backupRequest.Spec.OrLabelSelectors {
orLabelSelectors = append(orLabelSelectors, metav1.FormatLabelSelector(s))
}
Expand All @@ -537,7 +559,7 @@ func (r *itemCollector) listResourceByLabelsPerNamespace(
}

var labelSelector string
if selector := r.backupRequest.Spec.LabelSelector; selector != nil {
if selector := r.backupRequest.Spec.LabelSelector; selector != nil && !ignoreItemLabels {
labelSelector = metav1.FormatLabelSelector(selector)
}

Expand Down Expand Up @@ -594,7 +616,8 @@ func sortCoreGroup(group *metav1.APIResourceList) {
// pod is backed up, we can perform a pre hook, then process pvcs and pvs (including taking a
// snapshot), then perform a post hook on the pod.
const (
pod = iota
namespaces = iota
pod
pvc
pv
other
Expand All @@ -604,6 +627,8 @@ const (
// pods, pvcs, pvs, everything else.
func coreGroupResourcePriority(resource string) int {
switch strings.ToLower(resource) {
case "namespaces":
return namespaces
case "pods":
return pod
case "persistentvolumeclaims":
Expand Down

0 comments on commit 7e85aaf

Please sign in to comment.