Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

sshende-catalogicsoftware

Description

This PR fixes #7519 where namespace metadata (labels and annotations) from backup bundles was not being restored even when --existing-resource-policy was set to "update".

Fix Details

The fix adds logic to:

  1. Check if the namespace exists and if existingResourcePolicy is "update"
  2. Retrieve the namespace definition from the backup bundle
  3. Attempt to patch the existing namespace with backup metadata
  4. Include a fallback mechanism for manual label/annotation updates if patching fails

Testing Done

  1. Created a namespace with custom labels and annotations
  2. Backed up the namespace using Velero
  3. Modified the namespace labels/annotations
  4. Restored with --existing-resource-policy=update
  5. Verified that original labels and annotations were restored

Related PRs or Issues

Reviewer Instructions

Please pay special attention to:

  • Error handling in namespace restoration
  • Backward compatibility
  • The fallback mechanism for manual updates

Please indicate you've done the following:

@sshende-catalogicsoftware
Copy link
Author

@kaovilai , Thanks for the suggestions! I've updated both comments to be clearer:

Please let me know if you'd like to see any other improvements!

kaovilai
kaovilai previously approved these changes Jan 15, 2025
resourceClient,
)

// Fall back to manual label/annotation update if the patch fails
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was more of defensive programming to cover any unknown scenarios where the patch might fail. I'll remove it.

if groupResource == kuberesource.Namespaces {
if existingNamespaces.Has(targetNS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can make condition like

if existingNamespaces.Has(targetNS) && velerov1api.PolicyTypeUpdate == ctx.restore.Spec.ExistingResourcePolicy {...}

and ignore printing the "Skipping ...." log message.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepted. Will make the change.

@@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this log message needed?

It can also be confusing if ctx.restore.Spec.IncludeClusterResources is set to true

@@ -0,0 +1,7 @@
kind: bug
Copy link
Contributor

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accept. Will change this to a single line.

@sshende-catalogicsoftware
Copy link
Author

Thanks for the review. I shall be updating the code based on the latest review comments. Awakening this PR after a long time, but will try to finish it now.

This change ensures that when restoring namespaces with
existing-resource-policy set to 'update', the namespace metadata
(labels and annotations) from the backup bundle is properly restored.

Fixes vmware-tanzu#7519

Signed-off-by: Swanand Shende <[email protected]>
Thanks. Accept this suggestion

Co-authored-by: Tiger Kaovilai <[email protected]>
Signed-off-by: sshende-catalogicsoftware <[email protected]>
Thanks. Accept this suggestion as well.

Co-authored-by: Tiger Kaovilai <[email protected]>
Signed-off-by: sshende-catalogicsoftware <[email protected]>
@sshende-catalogicsoftware sshende-catalogicsoftware force-pushed the 7519-namespace-metadata-restore branch 2 times, most recently from 1ce3fdd to 4e7e853 Compare March 28, 2025 12:55
@sshende-catalogicsoftware
Copy link
Author

sshende-catalogicsoftware commented Mar 28, 2025

@reasonerjt : thanks for your review comments again. I have implemented the suggested changes in the latest commit : 4e7e853

@reasonerjt , @kaovilai : Can you please let me know if the changes seem correct and OK to be merged?

@kaovilai
Copy link
Member

@sshende-catalogicsoftware DCO your commits :)

- Removed the fallback mechanism and rely upon the PATCH operation to add the labels and annotations to the namespace resoure.

- Since we are relying on the PATCH operation, we need to add restore labels to backup namespace object so generatePatch detects intended changes.

- Cosmetic changes: Remove certain log statements that could cause some confusion.

Signed-off-by: Swanand Shende <[email protected]>
@sshende-catalogicsoftware sshende-catalogicsoftware force-pushed the 7519-namespace-metadata-restore branch from 4e7e853 to 507391a Compare March 31, 2025 14:18
@sshende-catalogicsoftware
Copy link
Author

Done, thanks for bringing it to my notice @kaovilai. I have added the signature to the latest commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can not update a namespace even if ExistingResourcePolicy is set to "update"
3 participants