Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(backups): restore should not require existing target app#2239

Open
androndo wants to merge 1 commit intomaincozystack/cozystack:mainfrom
fix/restore-not-requires-target-appcozystack/cozystack:fix/restore-not-requires-target-appCopy head branch name to clipboard
Open

fix(backups): restore should not require existing target app#2239
androndo wants to merge 1 commit intomaincozystack/cozystack:mainfrom
fix/restore-not-requires-target-appcozystack/cozystack:fix/restore-not-requires-target-appCopy head branch name to clipboard

Conversation

@androndo
Copy link
Contributor

@androndo androndo commented Mar 18, 2026

What this PR does

Release note

- fix in-place restores

Summary by CodeRabbit

  • Refactor
    • Simplified application template context during restoration operations. Application data now includes only essential metadata (name, namespace, and kind) instead of fetching the complete Kubernetes application object.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 18, 2026

📝 Walkthrough

Walkthrough

The createVeleroRestore function in the VeleroStrategy controller is being refactored to use a simplified synthetic Application map containing only metadata (name, namespace, and kind) instead of fetching and injecting the actual Kubernetes application object, removing associated REST mapping resolution logic and error paths.

Changes

Cohort / File(s) Summary
VeleroStrategy Controller
internal/backupcontroller/velerostrategy_controller.go
Simplified template context population by replacing actual Kubernetes object injection with a synthetic Application map containing only essential metadata fields (name, namespace, kind) and removed REST mapping resolution logic.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A simpler path we now pursue,
With metadata light and templates true,
No mappings to fetch, no objects to chase,
Just essence and name in their rightful place! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main change: removing the requirement for an existing target application during restore operations, which aligns with the code changes that eliminate target object retrieval logic.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/restore-not-requires-target-app
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can disable sequence diagrams in the walkthrough.

Disable the reviews.sequence_diagrams setting to disable sequence diagrams in the walkthrough.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical issue in the backup and restore functionality where in-place restores would fail if the target application was not already present in the cluster. The changes ensure that the restore process can successfully proceed by adjusting how the application's context is prepared for Velero, making it resilient to scenarios where the original application might be missing or when restoring to a new environment.

Highlights

  • Restore Process: Modified the restore logic to no longer require the target application to exist in the cluster before an in-place restore can proceed. This fixes issues where restores would fail if the application was not present.
  • Template Context Generation: Simplified the construction of the 'Application' field within the Velero restore template context, now deriving its details directly from the backup specification rather than attempting to fetch a live application object.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The createVeleroRestore function in velerostrategy_controller.go was updated to simplify how the Application field is populated within the template context. Previously, the code would fetch the full target application object from the Kubernetes API server. This has been replaced by directly constructing a map containing only the application's name, namespace, and kind from the backup.Spec.ApplicationRef and backup.Namespace, thereby removing an unnecessary API call and streamlining the restore process.

@androndo androndo marked this pull request as ready for review March 19, 2026 12:16
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Mar 19, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@internal/backupcontroller/velerostrategy_controller.go`:
- Around line 438-445: Do a best-effort fetch of the actual application object
and put the real object into the templateContext under the "Application" key
when available; if the GET (using
backup.Spec.ApplicationRef.Name/Kind/Namespace) fails, fall back to a minimal
synthetic map but only include metadata.name and include metadata.namespace only
if the application ref actually provided a namespace (do not unconditionally set
it to backup.Namespace), and ensure templates continue to receive the full
unstructured object (e.g. the unstructured.Unstructured.Object) when the
resource is found; update the code around templateContext and the application
lookup to use this fetch-then-fallback pattern so existing restore templates
that read .Application.spec or labels still work.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: af4e23da-8211-4f35-be25-0c9c24222998

📥 Commits

Reviewing files that changed from the base of the PR and between f1cbb38 and 6b00cef.

📒 Files selected for processing (1)
  • internal/backupcontroller/velerostrategy_controller.go

Comment on lines 438 to +445
templateContext := map[string]interface{}{
"Application": app.Object,
"Application": map[string]interface{}{
"metadata": map[string]interface{}{
"name": backup.Spec.ApplicationRef.Name,
"namespace": backup.Namespace,
},
"kind": backup.Spec.ApplicationRef.Kind,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

Preserve restore template compatibility while making app lookup optional

Line 439 now always supplies a reduced synthetic .Application, which can break existing restore templates that read other fields (for example .Application.spec.* or labels). Also, Line 442 always sets .Application.metadata.namespace = backup.Namespace, which is incorrect for cluster-scoped apps and can be wrong if app namespace differs from the Backup namespace.

Consider restoring a best-effort object fetch: use full object when available; fall back to synthetic metadata-only context only when the app is missing.

Proposed compatibility-safe approach
-	templateContext := map[string]interface{}{
-		"Application": map[string]interface{}{
-			"metadata": map[string]interface{}{
-				"name":      backup.Spec.ApplicationRef.Name,
-				"namespace": backup.Namespace,
-			},
-			"kind": backup.Spec.ApplicationRef.Kind,
-		},
-		"Parameters": map[string]string{},
-	}
+	application := map[string]interface{}{
+		"metadata": map[string]interface{}{
+			"name": backup.Spec.ApplicationRef.Name,
+		},
+		"kind": backup.Spec.ApplicationRef.Kind,
+	}
+
+	// Best-effort enrichment: keep restore independent from target app existence.
+	if backup.Spec.ApplicationRef.APIGroup != nil {
+		if mapping, err := r.RESTMapping(schema.GroupKind{
+			Group: *backup.Spec.ApplicationRef.APIGroup,
+			Kind:  backup.Spec.ApplicationRef.Kind,
+		}); err == nil {
+			ns := ""
+			if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
+				ns = backup.Namespace
+				application["metadata"].(map[string]interface{})["namespace"] = ns
+			}
+			if app, err := r.Resource(mapping.Resource).Namespace(ns).Get(ctx, backup.Spec.ApplicationRef.Name, metav1.GetOptions{}); err == nil {
+				application = app.Object
+			}
+		}
+	}
+
+	templateContext := map[string]interface{}{
+		"Application": application,
+		"Parameters":  map[string]string{},
+	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/backupcontroller/velerostrategy_controller.go` around lines 438 -
445, Do a best-effort fetch of the actual application object and put the real
object into the templateContext under the "Application" key when available; if
the GET (using backup.Spec.ApplicationRef.Name/Kind/Namespace) fails, fall back
to a minimal synthetic map but only include metadata.name and include
metadata.namespace only if the application ref actually provided a namespace (do
not unconditionally set it to backup.Namespace), and ensure templates continue
to receive the full unstructured object (e.g. the
unstructured.Unstructured.Object) when the resource is found; update the code
around templateContext and the application lookup to use this
fetch-then-fallback pattern so existing restore templates that read
.Application.spec or labels still work.

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

Labels

bug Something isn't working size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.