Skip to content

fix(keycloak-configure): patch HelmRelease in release namespace on teardown - #3372

Merged
Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
fix/keycloak-configure-teardown-namespace
Jul 28, 2026
Merged

fix(keycloak-configure): patch HelmRelease in release namespace on teardown#3372
Andrei Kvapil (kvaps) merged 1 commit into
mainfrom
fix/keycloak-configure-teardown-namespace

Conversation

@lexfrei

@lexfrei Aleksei Sviridkin (lexfrei) commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

What this PR does

The keycloak-configure pre-delete teardown Job clears the Flux HelmRelease finalizers as its final step so the release can be uninstalled. That final kubectl patch targeted a hardcoded namespace that does not match where the release actually installs. The release installs into cozy-keycloak, and the teardown Job's own RBAC — a Role + RoleBinding granting patch on the HelmRelease by release name — is created in the release namespace, but the patch was aimed at cozy-system.

The result is a deadlock: the Job's ServiceAccount is Forbidden to patch the HelmRelease in the mismatched namespace, so the Job errors and retries forever, the HelmRelease sticks in Terminating, and the Helm release wedges in status uninstalling. This blocks any teardown or reinstall of the component.

The fix templates both the release name and the namespace from {{ .Release.Name }} / {{ .Release.Namespace }}, so the patch targets the namespace where the HelmRelease lives and where the teardown RBAC grants access. I also added a helm-unittest that pins the rendered target, guards against the hardcoded namespace regressing, and asserts the patch tracks the release name and namespace.

Screenshots

N/A — no UI changes.

Release note

fix(keycloak-configure): fix the pre-delete teardown Job so uninstalling the release no longer wedges — it now patches the HelmRelease in the release namespace instead of a hardcoded one

Summary by CodeRabbit

  • Bug Fixes
    • Improved Helm chart deletion cleanup to patch the correct HelmRelease using the chart’s release name and namespace instead of fixed values.
    • Reduced risk of teardown targeting resources in an unintended namespace.
  • Tests
    • Added a new delete/teardown YAML test suite to verify correct parameterization and that only the intended namespace is modified.

@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 9817d7e0-a086-4562-8147-a59acb0c7ca7

📥 Commits

Reviewing files that changed from the base of the PR and between f192234 and fd568de.

📒 Files selected for processing (2)
  • packages/system/keycloak-configure/templates/delete.yaml
  • packages/system/keycloak-configure/tests/delete_test.yaml
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/system/keycloak-configure/tests/delete_test.yaml
  • packages/system/keycloak-configure/templates/delete.yaml

📝 Walkthrough

Walkthrough

The Keycloak configure chart’s pre-delete Job now patches the HelmRelease using the current Helm release name and namespace. New tests verify default and custom release metadata and reject mismatched namespaces.

Changes

Keycloak teardown cleanup

Layer / File(s) Summary
Dynamic HelmRelease cleanup
packages/system/keycloak-configure/templates/delete.yaml, packages/system/keycloak-configure/tests/delete_test.yaml
The teardown Job uses templated release metadata for its kubectl patch command, with tests covering default and custom release name/namespace values.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested reviewers: androndo

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main fix: patching the HelmRelease in the release namespace during teardown.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/keycloak-configure-teardown-namespace

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.

@gemini-code-assist

Copy link
Copy Markdown
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 resolves a deadlock issue occurring during the uninstallation of the keycloak-configure component. Previously, the teardown Job attempted to patch a HelmRelease in a hardcoded namespace, which caused permission errors and left the release in a 'Terminating' state. By parameterizing the patch command with Helm template variables, the Job now correctly targets the release's actual namespace, allowing for successful cleanup.

Highlights

  • Fixing HelmRelease teardown deadlock: Updated the pre-delete teardown Job to use dynamic Helm release name and namespace instead of hardcoded values, preventing 'Forbidden' errors during uninstallation.
  • Added regression testing: Introduced a new helm-unittest suite to verify that the patch command correctly targets the release namespace and name, ensuring future stability.
New Features

🧠 You can now enable Memory (public preview) 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 (@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 the 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 counterproductive. You can react with 👍 and 👎 on Gemini (@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.

@dosubot dosubot Bot added area/platform Issues or PRs related to platform infrastructure (bundle, flux, talos, installer) backport Should change be backported on previous release kind/bug Categorizes issue or PR as related to a bug labels Jul 20, 2026

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

Copy link
Copy Markdown
Contributor

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 parameterizes the teardown job in keycloak-configure to patch the HelmRelease using the dynamic release name and namespace instead of hardcoded values, and adds a test suite to verify this behavior. The review feedback suggests making the patch command idempotent by checking if the HelmRelease exists before patching, which prevents the job from failing and retrying indefinitely if the resource has already been deleted.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

done

kubectl patch hr keycloak-configure -n cozy-system --type=merge -p '{"metadata":{"finalizers":[]}}'
kubectl patch hr {{ .Release.Name }} -n {{ .Release.Namespace }} --type=merge -p '{"metadata":{"finalizers":[]}}'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Since this kubectl patch is the last command in the shell script, its exit status determines the exit status of the entire container/Job. If the HelmRelease has already been deleted (e.g., in a previous or concurrent teardown attempt, or if it was manually cleaned up), kubectl patch will fail with a NotFound error (exit code 1), causing the teardown Job to fail and retry indefinitely.

To make the teardown Job idempotent and robust, we should check if the HelmRelease exists before attempting to patch it. If it doesn't exist, the script can exit successfully (exit code 0).

                if kubectl get hr {{ .Release.Name }} -n {{ .Release.Namespace }} >/dev/null 2>&1; then
                  kubectl patch hr {{ .Release.Name }} -n {{ .Release.Namespace }} --type=merge -p '{"metadata":{"finalizers":[]}}'
                fi

@github-actions github-actions Bot added area/uncategorized PR auto-labeler could not map title scope to a known area/*; please review size/M This PR changes 30-99 lines, ignoring generated files labels Jul 20, 2026
…ardown

The pre-delete teardown Job clears the HelmRelease finalizers as its
final step, but targeted a hardcoded namespace that does not match the
namespace where the release and its RBAC live. The Job's Role and
RoleBinding are created in the release namespace and grant patch on the
HelmRelease by release name, so the ServiceAccount was Forbidden to patch
the HelmRelease in the hardcoded namespace. The Job errored and retried
forever, the HelmRelease stuck in Terminating, and the Helm release
wedged in "uninstalling", blocking any teardown or reinstall.

Target the release name and namespace so the patch matches the RBAC that
grants it. Add a helm-unittest pinning the rendered namespace.

Assisted-By: Claude <noreply@anthropic.com>
Signed-off-by: Aleksei Sviridkin <f@lex.la>
@lexfrei
Aleksei Sviridkin (lexfrei) force-pushed the fix/keycloak-configure-teardown-namespace branch from f192234 to fd568de Compare July 27, 2026 21:11

@kvaps Andrei Kvapil (kvaps) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM — verified. This fixes a real teardown deadlock: the pre-delete hook patched the HelmRelease in cozy-system, but the release installs into its own namespace (cozy-keycloak), so the patch was Forbidden and the uninstall hung. Patching -n {{ .Release.Namespace }} now matches the namespaced Role grant; the hook is pre-delete-only, idempotent (--type=merge clearing finalizers), and covered by render + anti-regression tests. The red E2E is the known node-join flake, and this pre-delete hook is not exercised by a normal install.

@kvaps
Andrei Kvapil (kvaps) merged commit 9fe6ae2 into main Jul 28, 2026
15 of 16 checks passed
@kvaps
Andrei Kvapil (kvaps) deleted the fix/keycloak-configure-teardown-namespace branch July 28, 2026 15:45
@github-actions

Copy link
Copy Markdown

myasnikovdaniil added a commit that referenced this pull request Aug 4, 2026
…release namespace on teardown (#3478)

# Description
Backport of #3372 to `release-1.6`.
@myasnikovdaniil myasnikovdaniil added the backport-previous Backport target — previous release line label Aug 17, 2026
@github-actions

Copy link
Copy Markdown

myasnikovdaniil added a commit that referenced this pull request Aug 19, 2026
…release namespace on teardown (#3877)

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

Labels

area/platform Issues or PRs related to platform infrastructure (bundle, flux, talos, installer) area/uncategorized PR auto-labeler could not map title scope to a known area/*; please review backport Should change be backported on previous release backport-previous Backport target — previous release line kind/bug Categorizes issue or PR as related to a bug size/M This PR changes 30-99 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants