-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix namespace metadata not being restored with existing-resource-policy #8607
base: main
Are you sure you want to change the base?
Changes from all commits
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,7 @@ | ||
kind: bug | ||
area: restore | ||
title: Fix namespace metadata not being restored with existing-resource-policy | ||
issue: 7519 | ||
note: | | ||
Fixes an issue where namespace labels and annotations from backup bundles | ||
were not being restored even when --existing-resource-policy was set to "update" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -742,8 +742,90 @@ func (ctx *restoreContext) processSelectedResource( | |
// have to try to create them multiple times. | ||
existingNamespaces.Insert(targetNS) | ||
} | ||
// For namespaces resources we don't need to following steps | ||
// For namespaces resources we restore metadata when existing resource policy is update, else continue | ||
if groupResource == kuberesource.Namespaces { | ||
if existingNamespaces.Has(targetNS) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can make condition like
and ignore printing the "Skipping ...." log message. |
||
// Skip updating namespace metadata if the existing resource policy is not set to 'update' | ||
if len(ctx.restore.Spec.ExistingResourcePolicy) == 0 || ctx.restore.Spec.ExistingResourcePolicy != velerov1api.PolicyTypeUpdate { | ||
ctx.log.Infof("Skipping update for existing namespace %s because existing resource policy is not 'update'", targetNS) | ||
continue | ||
} | ||
|
||
// Fetch the current namespace from the cluster | ||
existingNS, err := ctx.namespaceClient.Get(go_context.TODO(), targetNS, metav1.GetOptions{}) | ||
if err != nil { | ||
errs.AddVeleroError(errors.Wrap(err, "fetching existing namespace")) | ||
continue | ||
} | ||
|
||
// Retrieve the backup namespace definition | ||
backupNS := getNamespace( | ||
ctx.log.WithField("namespace", namespace), | ||
archive.GetItemFilePath(ctx.restoreDir, "namespaces", "", namespace), | ||
targetNS, | ||
) | ||
|
||
// Convert both namespaces to unstructured for patching | ||
existingNSUnstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(existingNS) | ||
if err != nil { | ||
errs.AddVeleroError(errors.Wrap(err, "converting existing namespace to unstructured")) | ||
continue | ||
} | ||
backupNSUnstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(backupNS) | ||
if err != nil { | ||
errs.AddVeleroError(errors.Wrap(err, "converting backup namespace to unstructured")) | ||
continue | ||
} | ||
|
||
// Construct the GroupResource for namespaces | ||
namespaceGR := schema.GroupResource{Group: "", Resource: "namespaces"} | ||
|
||
// Use getResourceClient to obtain a dynamic client for the namespace resource | ||
resourceClient, err := ctx.getResourceClient(namespaceGR, &unstructured.Unstructured{Object: backupNSUnstructured}, "") | ||
if err != nil { | ||
errs.AddVeleroError(errors.Wrap(err, "getting dynamic client for Namespace resource")) | ||
continue | ||
} | ||
|
||
// Process the update policy using the existing function | ||
warningsFromUpdateRP, errsFromUpdateRP := ctx.processUpdateResourcePolicy( | ||
&unstructured.Unstructured{Object: existingNSUnstructured}, | ||
&unstructured.Unstructured{Object: existingNSUnstructured}, // Pass existingNS with restore labels for the second parameter | ||
&unstructured.Unstructured{Object: backupNSUnstructured}, | ||
targetNS, | ||
resourceClient, | ||
) | ||
|
||
// Fall back to manual label/annotation update if the patch fails | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this fallback necessary? In what case the patch will fail but the update will succeed? |
||
if !errsFromUpdateRP.IsEmpty() { | ||
ctx.log.Warnf("Patch failed for namespace %s, falling back to manual label/annotation update", targetNS) | ||
|
||
// Ensure existingNS.Labels and Annotations are not nil | ||
if existingNS.Labels == nil { | ||
existingNS.Labels = make(map[string]string) | ||
} | ||
if existingNS.Annotations == nil { | ||
existingNS.Annotations = make(map[string]string) | ||
} | ||
|
||
// Merge labels and annotations | ||
for k, v := range backupNS.Labels { | ||
existingNS.Labels[k] = v | ||
} | ||
for k, v := range backupNS.Annotations { | ||
existingNS.Annotations[k] = v | ||
} | ||
|
||
// Apply the updated namespace | ||
_, err = ctx.namespaceClient.Update(go_context.TODO(), existingNS, metav1.UpdateOptions{}) | ||
if err != nil { | ||
errs.AddVeleroError(errors.Wrap(err, "updating namespace manually")) | ||
} | ||
} | ||
|
||
warnings.Merge(&warningsFromUpdateRP) | ||
errs.Merge(&errsFromUpdateRP) | ||
} | ||
continue | ||
} | ||
|
||
|
@@ -2243,7 +2325,9 @@ func (ctx *restoreContext) getOrderedResourceCollection( | |
continue | ||
} | ||
|
||
if namespace == "" && !boolptr.IsSetToTrue(ctx.restore.Spec.IncludeClusterResources) && !ctx.namespaceIncludesExcludes.IncludeEverything() { | ||
if groupResource.Resource == "namespaces" { | ||
ctx.log.Infof("Including resource namespaces despite being cluster-scoped") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this log message needed? It can also be confusing if |
||
} else if namespace == "" && !boolptr.IsSetToTrue(ctx.restore.Spec.IncludeClusterResources) && !ctx.namespaceIncludesExcludes.IncludeEverything() { | ||
ctx.log.Infof("Skipping resource %s because it's cluster-scoped and only specific namespaces are included in the restore", resource) | ||
continue | ||
} | ||
|
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.
This is trivial but conventionally the changelog is a one-liner, otherwise the format of the combined changelogs may be inconsistent.