Hooks tests and CRDs
Hooks run at selected lifecycle points
A hook is a templated resource with a lifecycle annotation. pre-upgrade runs before ordinary upgrade resources are updated. The example Job only prints a fixed message; it does not migrate a database. A lower hook weight runs before a higher one in the same event. A failing Job hook can fail the release operation. Use deadlines, bounded retries and least privilege; a migration should be idempotent and compatible with the old and new application versions.
apiVersion: batch/v1kind: Jobmetadata: name: {{ printf "%s-precheck" .Release.Name | quote }} annotations: "helm.sh/hook": pre-upgrade "helm.sh/hook-weight": "-5" "helm.sh/hook-delete-policy": before-hook-creation,hook-succeededspec: backoffLimit: 0 activeDeadlineSeconds: 30 ttlSecondsAfterFinished: 300 template: spec: automountServiceAccountToken: false restartPolicy: Never containers: - name: precheck image: registry.example.invalid/study/check:1.0 command: ["/bin/sh", "-c", "printf 'synthetic precheck'"] resources: requests: {cpu: 10m, memory: 16Mi} limits: {cpu: 50m, memory: 32Mi} securityContext: runAsNonRoot: true runAsUser: 10001 allowPrivilegeEscalation: false capabilities: {drop: [ALL]} seccompProfile: {type: RuntimeDefault}Hook resources need their own cleanup
Hook-created resources are not managed like ordinary release resources. before-hook-creation removes an old hook resource before its next creation; hook-succeeded removes a successful one. A Job TTL can clean up completed or failed Jobs later. Capture diagnostic evidence before cleanup and never assume uninstall removes all hook artifacts. In this chart, the test hook runs a synthetic check only, with no cluster credentials.
annotations: "helm.sh/hook": test "helm.sh/hook-delete-policy": before-hook-creation,hook-succeededTest a release, not just a chart
helm test runs test hooks for a deployed release, not a local template-only check. A zero exit code proves only what the test actually asserts. A real application test should check expected response, permissions and important failure cases. The fixture image is deliberately non-deployable, so do not run the test command on any cluster. Future authorized labs must replace and pin a reviewed image and verify exact context first.
# Illustrative only; not executedhelm --kubeconfig ./lab.kubeconfig --kube-context study -n helm-study test demoCRDs have a separate lifecycle
A CustomResourceDefinition registers a new API resource. The separate crd-demo chart places a fictional ReadingList CRD in crds/ and an instance in templates/. Files under crds/ are not templated; Helm installs missing CRDs before ordinary resources on install. Existing CRDs are not upgraded or deleted by the normal crds/ lifecycle, including rollback and uninstall. A namespaced instance still depends on a cluster-scoped definition. No controller is supplied; registration alone does not implement application behavior.
apiVersion: apiextensions.k8s.io/v1kind: CustomResourceDefinitionmetadata: name: readinglists.study.example.invalidspec: group: study.example.invalid scope: Namespaced names: plural: readinglists singular: readinglist kind: ReadingList versions: - name: v1 served: true storage: true schema: openAPIV3Schema: type: object properties: spec: type: object properties: title: type: string required: [title]Plan upgrades and rollback with data safety
Use helm template --include-crds to inspect the definition and instance without installing. A dry-run cannot establish a previously absent CRD; API discovery/admission may fail for its instances. A CRD update requires a separately reviewed compatibility, stored-version, conversion and recovery plan. Do not delete CRDs as cleanup: their instances can be lost across namespaces. A release rollback does not undo external database changes or restore data. Review backup/restore, stop conditions, and test hooks separately.
helm template demo ./crd-demo --include-crds -n helm-studyCheck Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Hooks tests and CRDs — knowledge check
Apply concepts to the original offline chart fixtures.
When does a pre-upgrade hook run?
What does hook-succeeded request?
What does a zero exit code in the synthetic test prove?
What happens to an existing CRD in crds/ on normal upgrade?
What does a release rollback not guarantee?