github.com/umeshredd/helm@v3.0.0-alpha.1+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. The Helm library install API is called 49 3. After some verification, the library renders the `foo` templates 50 4. The library loads the resulting resources into Kubernetes 51 5. The library returns the release object (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. The Helm library install API is called 60 3. After some verification, the library renders the `foo` templates 61 4. The library prepares to execute the `pre-install` hooks (loading hook resources into 62 Kubernetes) 63 5. The library sorts hooks by weight (assigning a weight of 0 by default) and by name for those hooks with the same weight in ascending order. 64 6. The library then loads the hook with the lowest weight first (negative to positive) 65 7. The library waits until the hook is "Ready" (except for CRDs) 66 8. The library loads the resulting resources into Kubernetes. Note that if the `--wait` 67 flag is set, the library will wait until all resources are in a ready state 68 and will not run the `post-install` hook until they are ready. 69 9. The library executes the `post-install` hook (loading hook resources) 70 10. The library waits until the hook is "Ready" 71 11. The library returns the release object (and other data) to the client 72 12. The client exits 73 74 What does it mean to wait until a hook is ready? This depends on the 75 resource declared in the hook. If the resources is a `Job` kind, the library 76 will wait until the job successfully runs to completion. And if the job 77 fails, the release will fail. This is a _blocking operation_, so the 78 Helm client will pause while the Job is run. 79 80 For all other kinds, as soon as Kubernetes marks the resource as loaded 81 (added or updated), the resource is considered "Ready". When many 82 resources are declared in a hook, the resources are executed serially. If they 83 have hook weights (see below), they are executed in weighted order. Otherwise, 84 ordering is not guaranteed. (In Helm 2.3.0 and after, they are sorted 85 alphabetically. That behavior, though, is not considered binding and could change 86 in the future.) It is considered good practice to add a hook weight, and set it 87 to `0` if weight is not important. 88 89 90 ### Hook resources are not managed with corresponding releases 91 92 The resources that a hook creates are not tracked or managed as part of the 93 release. Once Helm verifies that the hook has reached its ready state, it 94 will leave the hook resource alone. 95 96 Practically speaking, this means that if you create resources in a hook, you 97 cannot rely upon `helm uninstall` to remove the resources. To destroy such 98 resources, you need to either write code to perform this operation in a `pre-delete` 99 or `post-delete` hook or add `"helm.sh/hook-delete-policy"` annotation to the hook template file. 100 101 ## Writing a Hook 102 103 Hooks are just Kubernetes manifest files with special annotations in the 104 `metadata` section. Because they are template files, you can use all of 105 the normal template features, including reading `.Values`, `.Release`, 106 and `.Template`. 107 108 For example, this template, stored in `templates/post-install-job.yaml`, 109 declares a job to be run on `post-install`: 110 111 ```yaml 112 apiVersion: batch/v1 113 kind: Job 114 metadata: 115 name: "{{.Release.Name}}" 116 labels: 117 app.kubernetes.io/managed-by: {{.Release.Service | quote }} 118 app.kubernetes.io/instance: {{.Release.Name | quote }} 119 app.kubernetes.io/version: {{ .Chart.AppVersion }} 120 helm.sh/chart: "{{.Chart.Name}}-{{.Chart.Version}}" 121 annotations: 122 # This is what defines this resource as a hook. Without this line, the 123 # job is considered part of the release. 124 "helm.sh/hook": post-install 125 "helm.sh/hook-weight": "-5" 126 "helm.sh/hook-delete-policy": hook-succeeded 127 spec: 128 template: 129 metadata: 130 name: "{{.Release.Name}}" 131 labels: 132 app.kubernetes.io/managed-by: {{.Release.Service | quote }} 133 app.kubernetes.io/instance: {{.Release.Name | quote }} 134 helm.sh/chart: "{{.Chart.Name}}-{{.Chart.Version}}" 135 spec: 136 restartPolicy: Never 137 containers: 138 - name: post-install-job 139 image: "alpine:3.3" 140 command: ["/bin/sleep","{{default "10" .Values.sleepyTime}}"] 141 142 ``` 143 144 What makes this template a hook is the annotation: 145 146 ``` 147 annotations: 148 "helm.sh/hook": post-install 149 ``` 150 151 One resource can implement multiple hooks: 152 153 ``` 154 annotations: 155 "helm.sh/hook": post-install,post-upgrade 156 ``` 157 158 Similarly, there is no limit to the number of different resources that 159 may implement a given hook. For example, one could declare both a secret 160 and a config map as a pre-install hook. 161 162 When subcharts declare hooks, those are also evaluated. There is no way 163 for a top-level chart to disable the hooks declared by subcharts. 164 165 It is possible to define a weight for a hook which will help build a 166 deterministic executing order. Weights are defined using the following annotation: 167 168 ``` 169 annotations: 170 "helm.sh/hook-weight": "5" 171 ``` 172 173 Hook weights can be positive or negative numbers but must be represented as 174 strings. When Helm starts the execution cycle of hooks of a particular Kind it 175 will sort those hooks in ascending order. 176 177 It is also possible to define policies that determine when to delete corresponding hook resources. Hook deletion policies are defined using the following annotation: 178 179 ``` 180 annotations: 181 "helm.sh/hook-delete-policy": hook-succeeded 182 ``` 183 184 You can choose one or more defined annotation values: 185 * `"hook-succeeded"` specifies Helm should delete the hook after the hook is successfully executed. 186 * `"hook-failed"` specifies Helm should delete the hook if the hook failed during execution. 187 * `"before-hook-creation"` specifies Helm should delete the previous hook before the new hook is launched. 188 189 ### Automatically uninstall hook from previous release 190 191 When helm release being updated it is possible, that hook resource already exists in cluster. By default helm will try to create resource and fail with `"... already exists"` error. 192 193 One might choose `"helm.sh/hook-delete-policy": "before-hook-creation"` over `"helm.sh/hook-delete-policy": "hook-succeeded,hook-failed"` because: 194 195 * It is convenient to keep failed hook job resource in kubernetes for example for manual debug. 196 * It may be necessary to keep succeeded hook resource in kubernetes for some reason. 197 * At the same time it is not desirable to do manual resource deletion before helm release upgrade. 198 199 `"helm.sh/hook-delete-policy": "before-hook-creation"` annotation on hook causes Helm to remove the hook from previous release if there is one before the new hook is launched and can be used with another policy.