Skip to content

feat(helm): apply CRDs on helm upgrade via pre-upgrade Job - #385

Closed
WentingWu666666 wants to merge 1 commit into
documentdb:mainfrom
WentingWu666666:developer/wentingwu/helm-crd-upgrade-hook
Closed

WentingWu666666 wants to merge 1 commit into
documentdb:mainfrom
WentingWu666666:developer/wentingwu/helm-crd-upgrade-hook

Conversation

@WentingWu666666

Copy link
Copy Markdown
Collaborator

Summary

Adds a pre-install / pre-upgrade hook Job that applies the chart's CRDs against the cluster, so chart upgrades that bump CRD schemas don't silently leave the old schema in place.

Why

Helm 3 installs files from a chart's crds/ directory only on the first helm install and then refuses to touch them on subsequent helm upgrade calls (see Helm docs: Limitations of CRDs). Today, bumping the chart with a schema change ships the new CRD in crds/ but the live cluster keeps the old one users hit silent unknown field errors on CRs and assume the operator is broken.

What

  • New template templates/00_crd_upgrade.yaml renders, as pre-install + pre-upgrade hooks (weight -15 / -10):
    • ConfigMap containing all CRDs from crds/*.yaml via .Files.Glob
    • ServiceAccount + ClusterRole + ClusterRoleBinding scoped to CRD get/list/create/patch/update
    • Job that loops over the CRDs and runs kubectl apply --server-side --force-conflicts
  • All resources use helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded so they clean themselves up.
  • Gated by a new values block crdUpgradeJob.{enabled,image,imagePullSecrets,backoffLimit,ttlSecondsAfterFinished}, default enabled: true.
  • The chart keeps the existing crds/ directory so fresh installs still bootstrap CRDs the standard Helm way; the Job is then a no-op on first install and the upgrade fix on subsequent runs.

Deliberately out of scope

  • CRD deletion on uninstall. Removing a CRD cascades-deletes every DocumentDB / Backup / ScheduledBackup CR in the cluster a data-loss footgun. Users who want that behavior should kubectl delete crd explicitly.

Test

  • helm lint
  • helm template renders 4 hook-annotated resources
  • Live helm upgrade on local kind:
    • Job ran, applied all 3 CRDs server-side
    • kubectl get crd dbs.documentdb.io -o jsonpath='{.metadata.managedFields[*].manager}' reports helm kubectl kube-apiserver the new kubectl manager confirms the Job patched the CRD
    • Hook resources cleaned up via hook-succeeded delete policy

Notes for reviewers

  • Image: alpine/k8s:1.30.5 (multi-arch, has shell + kubectl). Override via crdUpgradeJob.image if you mirror images internally.
  • Pin the tag to your control-plane minor where possible.

Refs #381

Helm 3 installs files from the chart's crds/ directory on first install
but never touches them on subsequent `helm upgrade` calls. CRD schema
bumps in a new chart release therefore silently fail to reach the
cluster, causing 'unknown field' errors on CRs.

Add a pre-install / pre-upgrade hook Job that renders the chart's CRDs
into a ConfigMap and runs `kubectl apply --server-side --force-conflicts`
against each one. The Job is gated by `crdUpgradeJob.enabled` (default
true) so GitOps setups managing CRDs separately can opt out.

CRD deletion on `helm uninstall` is intentionally NOT wired up: it
would cascade-delete every DocumentDB / Backup / ScheduledBackup CR and
silently destroy data.

Refs documentdb#381

Co-authored-by: Copilot <[email protected]>
@documentdb-triage-tool

Copy link
Copy Markdown

🤖 Auto-triaged by documentdb-triage-tool.

Applied: enhancement
Project fields suggested: Component manifests · Priority P2 · Effort M · Status In Progress
Confidence: 0.85 (mixed)

Reasoning

effort from diff stats (185+0 LOC, 2 files); LLM: Adds a new Helm pre-upgrade hook Job and supporting RBAC resources to handle CRD schema upgrades that Helm 3 natively skips, touching chart templates and values config within one component.

If a label is wrong, remove it manually and ping @patty-chow so the rules can be tuned. The bot will not re-label items that already have component labels.

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

Labels

enhancement New feature or request triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants