Skip to content

fix(installer): add keep annotation to Namespace and update migration script#2122

Merged
kvaps merged 1 commit intomainfrom
fix/remove-namespace-from-installer
Feb 28, 2026
Merged

fix(installer): add keep annotation to Namespace and update migration script#2122
kvaps merged 1 commit intomainfrom
fix/remove-namespace-from-installer

Conversation

@kvaps
Copy link
Member

@kvaps kvaps commented Feb 28, 2026

What this PR does

Adds helm.sh/resource-policy: keep annotation to the cozy-system Namespace resource
in the installer helm chart. This prevents Helm from deleting the namespace (and all
HelmReleases within it) when the installer release is removed.

Also updates the v1.0 migration script to annotate the cozy-system namespace and
cozystack-version ConfigMap with the same policy before generating the Package resource.

Release note

[platform] Add helm.sh/resource-policy=keep annotation to cozy-system Namespace in installer chart to prevent namespace deletion on HelmRelease removal. Update migration script to protect namespace and cozystack-version ConfigMap before migration.

Summary by CodeRabbit

  • Chores
    • Enhanced migration process with an interactive step to safeguard critical resources during system upgrades.
    • Added resource protection mechanisms to prevent unintended removal during Helm operations.
    • Improved control flow in the upgrade script with explicit user confirmation prompts.

… script

Add helm.sh/resource-policy=keep annotation to the cozy-system Namespace
in the installer helm chart. This prevents Helm from deleting the
namespace when the HelmRelease is removed, which would otherwise destroy
all other HelmReleases within it.

Update the migration script to annotate the cozy-system namespace and
cozystack-version ConfigMap with helm.sh/resource-policy=keep before
generating the Package resource.

Co-Authored-By: Claude <[email protected]>
Signed-off-by: Andrei Kvapil <[email protected]>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 28, 2026

📝 Walkthrough

Walkthrough

These changes add resource protection mechanisms to prevent critical Kubernetes resources from being deleted during Helm operations. A migration script now includes an interactive step to annotate resources with the helm.sh/resource-policy: keep annotation, and the cozystack-operator template applies this annotation to the cozy-system Namespace.

Changes

Cohort / File(s) Summary
Resource Protection
hack/migrate-to-version-1.0.sh, packages/core/installer/templates/cozystack-operator.yaml
Adds helm.sh/resource-policy: keep annotations to protect critical resources (Namespace and ConfigMap) from Helm deletion. Migration script includes a new interactive Step 0 with user confirmation before applying annotations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 With helm.sh protection, we stand tall,
No deletion shall come to our hall,
Annotations secured, resources kept safe,
Step zero ensures a graceful phase,
Cozystack thrives, preserved with care! ✨

🚥 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 'fix(installer): add keep annotation to Namespace and update migration script' accurately summarizes the main changes: adding Helm keep annotations to prevent namespace deletion and updating the migration script accordingly.
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 (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/remove-namespace-from-installer

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.

@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 enhances the robustness of the system by implementing a mechanism to prevent the accidental deletion of critical Kubernetes resources during Helm uninstallations. It ensures that essential components like the cozy-system namespace and a key configuration map are preserved, thereby safeguarding the application's state and preventing service disruptions.

Highlights

  • Helm Resource Policy for Namespace: The cozy-system Namespace in the installer Helm chart now includes the helm.sh/resource-policy: keep annotation. This ensures that the namespace and its contents are not deleted when the Helm installer release is uninstalled, preventing accidental data loss.
  • Migration Script Update: The v1.0 migration script has been updated to prompt users to apply the helm.sh/resource-policy: keep annotation to both the cozy-system Namespace and the cozystack-version ConfigMap. This proactive measure protects these critical resources during the migration process before the Package resource is generated.

🧠 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.

Changelog
  • hack/migrate-to-version-1.0.sh
    • Added a new 'Step 0' to the migration script.
    • Implemented a user prompt to confirm annotation of the cozy-system Namespace and cozystack-version ConfigMap with helm.sh/resource-policy: keep.
    • Included kubectl annotate commands to apply the keep policy, with overwrite functionality and error handling for the ConfigMap.
  • packages/core/installer/templates/cozystack-operator.yaml
    • Added helm.sh/resource-policy: keep annotation to the metadata.annotations section of the cozy-system Namespace resource definition.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

@kvaps kvaps marked this pull request as ready for review February 28, 2026 10:48
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Feb 28, 2026
@kvaps kvaps added backport Should change be backported on previus release and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Feb 28, 2026
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

This pull request correctly adds the helm.sh/resource-policy: keep annotation to prevent accidental deletion of the cozy-system namespace and its contents. The changes in the Helm chart and the migration script are logical and well-implemented. I have one suggestion for the migration script to improve its error handling robustness, making it fail explicitly on unexpected errors instead of silently continuing.

Comment on lines +49 to +50
echo "Annotating ConfigMap cozystack-version..."
kubectl annotate configmap -n "$NAMESPACE" cozystack-version helm.sh/resource-policy=keep --overwrite 2>/dev/null || echo " ConfigMap cozystack-version not found, skipping."
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation to annotate the cozystack-version ConfigMap suppresses all errors from kubectl and prints a "not found, skipping" message. This could be misleading if the failure is due to other reasons, such as insufficient permissions. A more robust approach is to first verify the ConfigMap's existence before attempting to annotate it. This ensures that only "not found" cases are skipped, while other errors will correctly cause the script to fail due to the set -e directive.

Suggested change
echo "Annotating ConfigMap cozystack-version..."
kubectl annotate configmap -n "$NAMESPACE" cozystack-version helm.sh/resource-policy=keep --overwrite 2>/dev/null || echo " ConfigMap cozystack-version not found, skipping."
if kubectl get configmap -n "$NAMESPACE" cozystack-version &>/dev/null; then
echo "Annotating ConfigMap cozystack-version..."
kubectl annotate configmap -n "$NAMESPACE" cozystack-version helm.sh/resource-policy=keep --overwrite
else
echo " ConfigMap cozystack-version not found, skipping."
fi

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Feb 28, 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 `@hack/migrate-to-version-1.0.sh`:
- Line 50: Replace the blind 2>/dev/null suppression on the kubectl annotate
command for the ConfigMap named cozystack-version and instead capture its
stderr/stdout; if the command fails, inspect the captured output for a NotFound
(or "not found") message and only then echo the "ConfigMap ... not found,
skipping" message, otherwise print the real error and exit non‑zero so
RBAC/connection errors (and other failures) are surfaced; locate the kubectl
annotate invocation that uses the NAMESPACE and adjust error handling
accordingly.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between daa3905 and c83e41e.

📒 Files selected for processing (2)
  • hack/migrate-to-version-1.0.sh
  • packages/core/installer/templates/cozystack-operator.yaml

echo "Annotating namespace $NAMESPACE..."
kubectl annotate namespace "$NAMESPACE" helm.sh/resource-policy=keep --overwrite
echo "Annotating ConfigMap cozystack-version..."
kubectl annotate configmap -n "$NAMESPACE" cozystack-version helm.sh/resource-policy=keep --overwrite 2>/dev/null || echo " ConfigMap cozystack-version not found, skipping."
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Check the exact line 50 and surrounding context
echo "=== Line 50 and context ==="
sed -n '45,55p' hack/migrate-to-version-1.0.sh

echo ""
echo "=== Searching for the kubectl annotate pattern ==="
rg -n 'kubectl annotate configmap' hack/migrate-to-version-1.0.sh

echo ""
echo "=== Checking for 2>/dev/null error masking pattern ==="
rg -n -C2 'kubectl annotate configmap.*2>/dev/null.*echo' hack/migrate-to-version-1.0.sh || true

Repository: cozystack/cozystack

Length of output: 1368


Differentiate between "not found" and other kubectl failures.

The current approach masks all kubectl annotate errors with 2>/dev/null, then assumes any failure means the ConfigMap doesn't exist. This silently hides RBAC denials, connectivity issues, or other errors while falsely reporting them as "not found," potentially leaving the deployment in an inconsistent state.

Capture the error output and only treat NotFound errors as expected; fail explicitly on other errors:

Proposed fix
-    kubectl annotate configmap -n "$NAMESPACE" cozystack-version helm.sh/resource-policy=keep --overwrite 2>/dev/null || echo "  ConfigMap cozystack-version not found, skipping."
+    annotate_err=""
+    if ! annotate_err=$(kubectl annotate configmap -n "$NAMESPACE" cozystack-version helm.sh/resource-policy=keep --overwrite 2>&1); then
+        if grep -q "NotFound" <<<"$annotate_err"; then
+            echo "  ConfigMap cozystack-version not found, skipping."
+        else
+            echo "$annotate_err" >&2
+            exit 1
+        fi
+    fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
kubectl annotate configmap -n "$NAMESPACE" cozystack-version helm.sh/resource-policy=keep --overwrite 2>/dev/null || echo " ConfigMap cozystack-version not found, skipping."
annotate_err=""
if ! annotate_err=$(kubectl annotate configmap -n "$NAMESPACE" cozystack-version helm.sh/resource-policy=keep --overwrite 2>&1); then
if grep -q "NotFound" <<<"$annotate_err"; then
echo " ConfigMap cozystack-version not found, skipping."
else
echo "$annotate_err" >&2
exit 1
fi
fi
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@hack/migrate-to-version-1.0.sh` at line 50, Replace the blind 2>/dev/null
suppression on the kubectl annotate command for the ConfigMap named
cozystack-version and instead capture its stderr/stdout; if the command fails,
inspect the captured output for a NotFound (or "not found") message and only
then echo the "ConfigMap ... not found, skipping" message, otherwise print the
real error and exit non‑zero so RBAC/connection errors (and other failures) are
surfaced; locate the kubectl annotate invocation that uses the NAMESPACE and
adjust error handling accordingly.

@kvaps kvaps merged commit 018aae7 into main Feb 28, 2026
12 checks passed
@kvaps kvaps deleted the fix/remove-namespace-from-installer branch February 28, 2026 10:52
@github-actions
Copy link

kvaps added a commit that referenced this pull request Feb 28, 2026
…ce and update migration script (#2123)

# Description
Backport of #2122 to `release-1.0`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Should change be backported on previus release 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