github.com/cloudposse/helm@v2.2.3+incompatible/docs/charts_hooks.md (about) 1 # Hooks 2 3 Helm provides a _hook_ mechanism to allow chart developers to intervene 4 at certain points in a release's life cycle. For example, you can use 5 hooks to: 6 7 - Load a ConfigMap or Secret during install before any other charts are 8 loaded. 9 - Execute a Job to back up a database before installing a new chart, 10 and then execute a second job after the upgrade in order to restore 11 data. 12 - Run a Job before deleting a release to gracefully take a service out 13 of rotation before removing it. 14 15 Hooks work like regular templates, but they have special annotations 16 that cause Helm to utilize them differently. In this section, we cover 17 the basic usage pattern for hooks. 18 19 ## The Available Hooks 20 21 The following hooks are defined: 22 23 - pre-install: Executes after templates are rendered, but before any 24 resources are created in Kubernetes. 25 - post-install: Executes after all resources are loaded into Kubernetes 26 - pre-delete: Executes on a deletion request before any resources are 27 deleted from Kubernetes. 28 - post-delete: Executes on a deletion request after all of the release's 29 resources have been deleted. 30 - pre-upgrade: Executes on an upgrade request after templates are 31 rendered, but before any resources are loaded into Kubernetes (e.g. 32 before a Kubernetes apply operation). 33 - post-upgrade: Executes on an upgrade after all resources have been 34 upgraded. 35 - pre-rollback: Executes on a rollback request after templates are 36 rendered, but before any resources have been rolled back. 37 - post-rollback: Executes on a rollback request after all resources 38 have been modified. 39 40 ## Hooks and the Release Lifecycle 41 42 Hooks allow you, the chart developer, an opportunity to perform 43 operations at strategic points in a release lifecycle. For example, 44 consider the lifecycle for a `helm install`. By default, the lifecycle 45 looks like this: 46 47 1. User runs `helm install foo` 48 2. Chart is loaded into Tiller 49 3. After some verification, Tiller renders the `foo` templates 50 4. Tiller loads the resulting resources into Kubernetes 51 5. Tiller returns the release name (and other data) to the client 52 6. The client exits 53 54 Helm defines two hooks for the `install` lifecycle: `pre-install` and 55 `post-install`. If the developer of the `foo` chart implements both 56 hooks, the lifecycle is altered like this: 57 58 1. User runs `helm install foo` 59 2. Chart is loaded into Tiller 60 3. After some verification, Tiller renders the `foo` templates 61 4. Tiller executes the `pre-install` hook (loading hook resources into 62 Kubernetes) 63 5. Tiller waits until the hook is "Ready" 64 6. Tiller loads the resulting resources into Kubernetes 65 7. Tiller executes the `post-install` hook (loading hook resources) 66 8. Tiller waits until the hook is "Ready" 67 9. Tiller returns the release name (and other data) to the client 68 10. The client exits 69 70 What does it mean to wait until a hook is ready? This depends on the 71 resource declared in the hook. If the resources is a `Job` kind, Tiller 72 will wait until the job successfully runs to completion. And if the job 73 fails, the release will fail. This is a _blocking operation_, so the 74 Helm client will pause while the Job is run. 75 76 For all other kinds, as soon as Kubernetes marks the resource as loaded 77 (added or updated), the resource is considered "Ready". When many 78 resources are declared in a hook, the resources are executed serially, 79 but the order of their execution is not guaranteed. 80 81 ### Hook resources are unmanaged 82 83 The resources that a hook creates are not tracked or managed as part of the 84 release. Once Tiller verifies that the hook has reached its ready state, it 85 will leave the hook resource alone. 86 87 Practically speaking, this means that if you create resources in a hook, you 88 cannot rely upon `helm delete` to remove the resources. To destroy such 89 resources, you need to write code to perform this operation in a `pre-delete` 90 or `post-delete` hook. 91 92 ## Writing a Hook 93 94 Hooks are just Kubernetes manifest files with special annotations in the 95 `metadata` section. Because they are template files, you can use all of 96 the normal template features, including reading `.Values`, `.Release`, 97 and `.Template`. 98 99 For example, this template, stored in `templates/post-install-job.yaml`, 100 declares a job to be run on `post-install`: 101 102 ```yaml 103 apiVersion: batch/v1 104 kind: Job 105 metadata: 106 name: "{{.Release.Name}}" 107 labels: 108 heritage: {{.Release.Service | quote }} 109 release: {{.Release.Name | quote }} 110 chart: "{{.Chart.Name}}-{{.Chart.Version}}" 111 annotations: 112 # This is what defines this resource as a hook. Without this line, the 113 # job is considered part of the release. 114 "helm.sh/hook": post-install 115 spec: 116 template: 117 metadata: 118 name: "{{.Release.Name}}" 119 labels: 120 heritage: {{.Release.Service | quote }} 121 release: {{.Release.Name | quote }} 122 chart: "{{.Chart.Name}}-{{.Chart.Version}}" 123 spec: 124 restartPolicy: Never 125 containers: 126 - name: post-install-job 127 image: "alpine:3.3" 128 command: ["/bin/sleep","{{default "10" .Values.sleepyTime}}"] 129 130 ``` 131 132 What makes this template a hook is the annotation: 133 134 ``` 135 annotations: 136 "helm.sh/hook": post-install 137 ``` 138 139 One resource can implement multiple hooks: 140 141 ``` 142 annotations: 143 "helm.sh/hook": post-install,post-upgrade 144 ``` 145 146 Similarly, there is no limit to the number of different resources that 147 may implement a given hook. For example, one could declare both a secret 148 and a config map as a pre-install hook. It is important to keep in mind, 149 though, that there are no ordering guarantees about hooks. 150 151 When subcharts declare hooks, those are also evaluated. There is no way 152 for a top-level chart to disable the hooks declared by subcharts. And 153 again, there is no guaranteed ordering. 154