Skip to content

feat(chart): add CRD upgrader hook - #199

Merged
AviadHayumi merged 3 commits into
mainfrom
AviadHayumi/crd-upgrader-hook
Aug 2, 2026
Merged

AviadHayumi merged 3 commits into
mainfrom
AviadHayumi/crd-upgrader-hook

Conversation

@AviadHayumi

@AviadHayumi AviadHayumi commented Jul 30, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Helm applies the CRD under crds/ only on the first helm install, never on helm upgrade. So once the chart is installed, bumping the kartas.run.ai schema in a later release never reaches the cluster, and users hit a missing field with no error to explain it.

This adds a pre-install,pre-upgrade Job that runs kubectl apply --server-side --force-conflicts over the bundled CRD, so helm upgrade rolls the schema forward. --force-conflicts is there so the Job can take the field manager over from Helm's install-time apply. The CRD still ships in crds/, so a fresh install gets it up front, and since Helm never deletes crds/ CRDs on uninstall, existing Kartas survive an uninstall without a resource-policy: keep annotation.

The Job reads the CRD from a ConfigMap (.Files.Get) and runs the stock registry.k8s.io/kubectl image, so there is no custom image to build or publish. make helm-validate (run in CI) fails if that ConfigMap grows past ~1MB, so a CRD that would outgrow the etcd object limit is caught in review instead of at install. RBAC is a ServiceAccount plus a ClusterRole scoped by resourceNames to kartas.run.ai with create and patch. Gated by crdUpgrader.enabled (default true), independent of operator.enabled so a CRD-only install still upgrades its CRD.

Same approach as KAI-Scheduler, which ships the same hook. It bakes the CRDs into a dedicated image because its CRDs are large; ours is one ~110KB CRD so a ConfigMap is enough:

https://github.com/NVIDIA/KAI-Scheduler/blob/main/deployments/kai-scheduler/templates/hooks/pre/crd-upgrader.yaml

  • KAI's crd-upgrader Job and RBAC, the pattern this is modeled on

New files live under charts/karta/templates/hooks/pre/, plus the crdUpgrader block in charts/karta/values.yaml.

Testing

Ran it on kind: fresh install, upgrade rolls the schema forward (with the hook off it stays stale, which is the bug), installed real Kartas from docs/catalog/ and upgraded the CRD while they were live (they survive with the same uid, storedVersions unchanged), the --skip-crds create path, and uninstall keeps the CRD. make check passes.

Related issue(s)

Fixes #43

Checklist

  • All commits are signed off with DCO (git commit -s)
  • New/modified files have SPDX license and copyright headers
  • Documentation updated (if applicable)
  • Tests pass (make check)
  • No proprietary or internal information included

Summary by CodeRabbit

  • New Features

    • Added optional automatic CRD installation and upgrades during Helm installs and upgrades.
    • Added configurable upgrader settings, including image, retries, resource limits, security options, and scheduling preferences.
    • Added permissions and cleanup handling for the CRD upgrade process.
  • Bug Fixes

    • Improved reliability of CRD application using server-side updates.
  • Chores

    • Helm validation now reports CRD package size and fails when it exceeds the configured limit.

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds a configurable pre-install/pre-upgrade Helm hook that stores bundled CRDs in a ConfigMap, grants narrowly scoped RBAC, and runs a hardened kubectl Job to server-side apply the CRDs.

Changes

CRD upgrader hook

Layer / File(s) Summary
CRD bundle and upgrader configuration
charts/karta/values.yaml, charts/karta/templates/hooks/pre/crd-upgrader-configmap.yaml
Adds crdUpgrader settings and conditionally renders bundled crds/*.yaml files into a hook ConfigMap.
Hook execution and RBAC
charts/karta/templates/hooks/pre/crd-upgrader-rbac.yaml, charts/karta/templates/hooks/pre/crd-upgrader-job.yaml
Adds hook ServiceAccount, narrowly scoped ClusterRole and binding, plus a configurable Job that applies mounted CRDs with server-side force-conflict handling.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Helm
  participant CRDConfigMap
  participant CRDUpgraderJob
  participant KubernetesAPI
  Helm->>CRDConfigMap: Render bundled CRDs
  Helm->>CRDUpgraderJob: Run pre-install/pre-upgrade hook
  CRDUpgraderJob->>KubernetesAPI: Apply CRDs server-side
Loading

Suggested reviewers: isan-rivkin

Poem

A bunny packs CRDs with care,
Into a ConfigMap’s tidy lair.
Kubectl hops through every chart,
Applying schemas part by part.
RBAC guards the upgrade trail—
“Squeak!” says the hook, and all shall sail.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning Core hook, ConfigMap, and RBAC pieces are present, but the linked scope also asks for pre-rollback handling and status.storedVersions cleanup, which aren't shown. Implement the pre-rollback hook and status.storedVersions pruning, or revise the linked issue scope if those items are no longer required.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately describes the main change: adding a CRD upgrader hook to the chart.
Out of Scope Changes check ✅ Passed All listed changes support the CRD upgrader hook; no unrelated code changes are indicated.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch AviadHayumi/crd-upgrader-hook

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

@coderabbitai

coderabbitai Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Adds a configurable pre-install/pre-upgrade Helm hook that stores bundled CRDs in a ConfigMap, grants narrowly scoped RBAC, and runs a hardened kubectl Job to apply the CRDs server-side. helm-validate checks the rendered ConfigMap size.

Changes

CRD upgrader hook

Layer / File(s) Summary
CRD bundle and upgrader configuration
charts/karta/values.yaml, charts/karta/templates/hooks/pre/crd-upgrader-configmap.yaml
Adds crdUpgrader settings and conditionally renders bundled crds/*.yaml files into a hook ConfigMap.
Hook execution and RBAC
charts/karta/templates/hooks/pre/crd-upgrader-rbac.yaml, charts/karta/templates/hooks/pre/crd-upgrader-job.yaml
Adds hook RBAC resources and a configurable Job that applies mounted CRDs with server-side force-conflict handling.
Rendered ConfigMap validation
Makefile
Updates helm-validate to report the rendered ConfigMap size and fail when it exceeds CRD_CONFIGMAP_MAX_BYTES.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Helm
  participant CRDConfigMap
  participant CRDUpgraderJob
  participant KubernetesAPI
  Helm->>CRDConfigMap: Render bundled CRDs
  Helm->>CRDUpgraderJob: Run pre-install/pre-upgrade hook
  CRDUpgraderJob->>KubernetesAPI: Apply CRDs server-side
Loading

Suggested reviewers: isan-rivkin

Poem

A rabbit bundles schemas with care,
In a ConfigMap kept square.
RBAC grants the needed part,
Kubectl applies each chart.
A size check guards the trim—
Hooks prepare upgrades to begin.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The implementation covers the Job, ConfigMap, narrow RBAC, cleanup policies, and validation, but omits the requested pre-rollback hook. Add the pre-rollback hook event to the Job, ConfigMap, and RBAC hook annotations, then validate rollback behavior.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: adding a CRD upgrader hook to the Helm chart.
Out of Scope Changes check ✅ Passed The changes support CRD hook configuration and validation and do not show unrelated code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch AviadHayumi/crd-upgrader-hook

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

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@charts/karta/templates/hooks/pre/crd-upgrader-job.yaml`:
- Line 34: Update the hook Job command in the pre-CRD upgrader manifest to use
the kubectl image’s actual executable path, /kubectl, or remove the command
override so the image entrypoint is used.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: b6de0ab2-521e-4907-9a05-84f1b06a2fad

📥 Commits

Reviewing files that changed from the base of the PR and between e55319a and 023bb52.

📒 Files selected for processing (4)
  • charts/karta/templates/hooks/pre/crd-upgrader-configmap.yaml
  • charts/karta/templates/hooks/pre/crd-upgrader-job.yaml
  • charts/karta/templates/hooks/pre/crd-upgrader-rbac.yaml
  • charts/karta/values.yaml

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Makefile`:
- Line 143: Update the CRD ConfigMap size-check recipe around the helm template
pipeline so Helm renders into a temporary file first, explicitly fails when helm
template returns a nonzero status, and only then measures the file size with wc.
Preserve the existing size-limit validation and error messaging, and clean up
the temporary file regardless of the render or size-check outcome.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: 65e6cfaf-3aa4-46dd-bc02-68bc359360ae

📥 Commits

Reviewing files that changed from the base of the PR and between 023bb52 and 2b86329.

📒 Files selected for processing (1)
  • Makefile

Comment thread Makefile Resolved
Helm applies files in crds/ only on first install and never upgrades
them, so bumping the kartas.run.ai schema in a new release leaves
existing clusters on the old CRD. Add a pre-install/pre-upgrade Job that
runs kubectl apply --server-side --force-conflicts over the bundled CRD,
so every helm upgrade rolls the schema forward and the Job takes field
ownership from Helm's install-time apply.

The CRD stays in crds/ (Helm still installs it on first install and never
deletes it on uninstall, so user Kartas survive). The Job reads the CRD
from a ConfigMap built with .Files.Get and runs a stock
registry.k8s.io/kubectl image, so no custom image is needed. RBAC is a
ServiceAccount plus a ClusterRole scoped by resourceNames to
kartas.run.ai. All hook resources use hook-delete-policy
hook-succeeded,before-hook-creation, which keeps repeated helm upgrade
runs idempotent.

Gated by crdUpgrader.enabled (default true), independent of
operator.enabled so a CRD-only install still upgrades its CRD.

helm-validate fails if the CRD ConfigMap grows past the ~1 MiB etcd
object limit, so a too-large CRD is caught at PR time.

Refs #43

Signed-off-by: aviadh <aviad.hayumi@gmail.com>
@AviadHayumi
AviadHayumi force-pushed the AviadHayumi/crd-upgrader-hook branch from 5a9c9b4 to e5b34d6 Compare August 2, 2026 12:35

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
charts/karta/templates/hooks/pre/crd-upgrader-rbac.yaml (1)

11-14: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Add hook-failed to the Job hook only.

hook-failed deletes only the hook resource that fails. It cleans up a failed Job, but it does not delete the earlier successful ConfigMap or RBAC resources. Use a separate cleanup mechanism for failed-upgrade RBAC cleanup.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@charts/karta/templates/hooks/pre/crd-upgrader-rbac.yaml` around lines 11 -
14, Add hook-failed only to the Job hook annotations in
charts/karta/templates/hooks/pre/crd-upgrader-job.yaml (lines 11-14). Leave the
existing hook-delete-policy unchanged in
charts/karta/templates/hooks/pre/crd-upgrader-rbac.yaml (lines 11-14) and
charts/karta/templates/hooks/pre/crd-upgrader-configmap.yaml (lines 11-14); use
a separate mechanism for failed-upgrade RBAC cleanup.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@charts/karta/templates/hooks/pre/crd-upgrader-rbac.yaml`:
- Line 23: Update the CRD upgrader RBAC resource name using a release- and
namespace-specific naming helper or values that include both release identity
and namespace, rather than the current karta.fullname expression. Apply the same
unique naming consistently to the related ClusterRole, ServiceAccount, and
ClusterRoleBinding references so each release binds only its own upgrader
account.

---

Nitpick comments:
In `@charts/karta/templates/hooks/pre/crd-upgrader-rbac.yaml`:
- Around line 11-14: Add hook-failed only to the Job hook annotations in
charts/karta/templates/hooks/pre/crd-upgrader-job.yaml (lines 11-14). Leave the
existing hook-delete-policy unchanged in
charts/karta/templates/hooks/pre/crd-upgrader-rbac.yaml (lines 11-14) and
charts/karta/templates/hooks/pre/crd-upgrader-configmap.yaml (lines 11-14); use
a separate mechanism for failed-upgrade RBAC cleanup.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: 1a7cb4d4-bff4-40ee-8f84-3d922fa89001

📥 Commits

Reviewing files that changed from the base of the PR and between 5a9c9b4 and e5b34d6.

📒 Files selected for processing (5)
  • Makefile
  • charts/karta/templates/hooks/pre/crd-upgrader-configmap.yaml
  • charts/karta/templates/hooks/pre/crd-upgrader-job.yaml
  • charts/karta/templates/hooks/pre/crd-upgrader-rbac.yaml
  • charts/karta/values.yaml
🚧 Files skipped from review as they are similar to previous changes (2)
  • Makefile
  • charts/karta/values.yaml

karta.fullname ignores the release name and namespace, so the
crd-upgrader ClusterRole and ClusterRoleBinding were named the same
across releases. Two installs in different namespaces would collide on
those cluster-scoped objects, and before-hook-creation could delete one
release's RBAC and bind the wrong ServiceAccount. Suffix the ClusterRole
and ClusterRoleBinding (and its roleRef) with .Release.Namespace so each
install gets its own; the ServiceAccount stays namespaced.

Refs #43

Signed-off-by: aviadh <aviad.hayumi@gmail.com>
The crd-upgrader hook resources were named from karta.fullname, which
ignores the release name and namespace, so two releases could share the
cluster-scoped ClusterRole and ClusterRoleBinding (and same-namespace
releases the namespaced ones too). before-hook-creation could then
delete one release's hook resources and rebind another's.

Add a karta.crdUpgrader.name helper that folds in the release namespace
and name, and use it for every hook resource (ServiceAccount, ConfigMap,
Job, ClusterRole, ClusterRoleBinding, and their references) so each
release gets its own set.

Refs #43

Signed-off-by: aviadh <aviad.hayumi@gmail.com>
@AviadHayumi
AviadHayumi merged commit 250cb94 into main Aug 2, 2026
2 checks passed
@AviadHayumi
AviadHayumi deleted the AviadHayumi/crd-upgrader-hook branch August 2, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add CRD upgrader hook to Helm chart

2 participants