|
| 1 | +--- |
| 2 | +hip: 9999 |
| 3 | +title: "Handling unknown hook-delete-policy values" |
| 4 | +authors: [ "Marcin Owsiany <[email protected]>" ] |
| 5 | +created: "2025-07-23" |
| 6 | +type: "feature" |
| 7 | +status: "draft" |
| 8 | +--- |
| 9 | + |
| 10 | +## Abstract |
| 11 | + |
| 12 | +Currently, unknown _values_ of `helm.sh/hook-delete-policy` annotations are silently permitted. |
| 13 | +They result in the given hook _never_ being deleted. |
| 14 | +However this behaviour seems to be by coincidence, rather than intentional. |
| 15 | +This HIP proposes how to handle them going forward. |
| 16 | + |
| 17 | +## Motivation |
| 18 | + |
| 19 | +There are [a number of _documented_ hook deletion policies][Docs]. |
| 20 | +All of them result in hook deletion _at some point_. |
| 21 | +A default hook delete policy (namely `before-hook-creation`) |
| 22 | +is [applied when the list of hook policies for a resource is empty][Code]. |
| 23 | + |
| 24 | +However, when an _unrecognized_ value (e.g. `badger`) is specified: |
| 25 | +* Helm does not complain in any way, and |
| 26 | +* a hook resource annotated this way is **never deleted**. |
| 27 | + |
| 28 | +It seems that the current behaviour is a coincidence or mistake, rather than intended. |
| 29 | + |
| 30 | +## Rationale |
| 31 | + |
| 32 | +In line with [Hyrum's Law][Hyrum's Law], at least [one project][StackRox chart] depends on the current behaviour. |
| 33 | +Namely, it specifies a hook deletion policy of `never` on most of its storage-related resources |
| 34 | +(such as `PersistentVolumeClaim`). |
| 35 | +These resources are applied as hooks, in order to guard against data loss, |
| 36 | +see [appendix A](#appendix-a-why-create-storage-resources-as-hooks) if you would like to know more. |
| 37 | + |
| 38 | +I was able to find [one more unrelated chart][Anance personal chart] that also specifies the same value. |
| 39 | + |
| 40 | +Some charts also allow the users to specify the policy using a helm var, |
| 41 | +and it's not clear whether they validate the value before using it in a template. |
| 42 | + |
| 43 | +The current Helm behaviour in this case is unspecified. |
| 44 | +There is a concern that a change in the current implementation |
| 45 | +(for example applying the default policy more aggressively) could cause a catastrophic data loss. |
| 46 | + |
| 47 | +## Specification |
| 48 | + |
| 49 | +This proposal: |
| 50 | +- suggests keeping status quo for helm 3, for the sake of backwards compatibility, |
| 51 | +- outlines a few options for helm 4. |
| 52 | + |
| 53 | +### For Helm 3 |
| 54 | + |
| 55 | +- No semantic changes to production code. |
| 56 | +- Add a regression test to make sure that the current behaviour is not changed by mistake. |
| 57 | + This is probably easiest to do by extracting the defaulting code to a separate unexported function and |
| 58 | + adding a unit test for the function. |
| 59 | + |
| 60 | +### For Helm 4 |
| 61 | + |
| 62 | +There are a few dimensions in which we can make a change, that are somewhat interconnected: |
| 63 | +- whether to accept a deletion disabling policy at all, or break compatibility and reject it, |
| 64 | +- if accepted, whether support it officially (documented, etc), or deprecate it, |
| 65 | +- what to do about (other) undocumented hook deletion policy values, |
| 66 | + |
| 67 | +#### 1. Official support for a policy which disables hook deletion |
| 68 | + |
| 69 | +Add a new, documented hook deletion policy value: `never`. |
| 70 | +When specified, such hook resource is not deleted. |
| 71 | +Effectively, the same as suggestion for Helm 3, but explicitly supported. |
| 72 | + |
| 73 | +Pros: |
| 74 | +- Keeps compatibility with Helm 3. |
| 75 | +- Allows a notion of resources which are installed by helm, but afterwards not managed by it in any way. |
| 76 | +- StackRox chart continues to work as before without changing. |
| 77 | +- Other charts (if any) which happen depend on current undocumented behaviour could easily be made to work by changing |
| 78 | + whatever value they use to `never`. |
| 79 | + |
| 80 | +Cons: |
| 81 | +- It seems that hooks were generally intended to be resources whose previous instances are cleaned up |
| 82 | + before (subsequent) chart applications. Supporting this policy breaks this assumption and |
| 83 | + introduces some complexity when reasoning about possible scenarios. For example such `pre-install` |
| 84 | + hook resources need to be guarded by a `if $.Release.IsInstall` condition in order not to break upgrades. |
| 85 | + |
| 86 | +#### 2. Keep the status quo |
| 87 | + |
| 88 | +The same as described above for Helm 3. |
| 89 | + |
| 90 | +Pros: |
| 91 | +- Keeps compatibility with Helm 3. |
| 92 | +- StackRox chart continues to work as before without changing. |
| 93 | + |
| 94 | +#### 3. Reject all undocumented hook deletion policies (including `never`) |
| 95 | + |
| 96 | +Make it an error to use undocumented hook deletion policies, including `never`. |
| 97 | + |
| 98 | +Pros: |
| 99 | +- Mental model for hook deletion stays simple: they are always deleted (at _some_ point). |
| 100 | + |
| 101 | +Cons: |
| 102 | +- Breaks compatibility with Helm 3. In particular: |
| 103 | + - StackRox helm chart stops working as is. This would most likely force StackRox to stop supporting direct usage of `helm` |
| 104 | + for installation; StackRox operator would still use Helm library internally, and manage the storage resources differently, |
| 105 | + as it does now. |
| 106 | + |
| 107 | +#### 4. Reject undocumented policies (_other than_ `never`) |
| 108 | + |
| 109 | +Make it an error to use undocumented hook deletion policies, except `never` (which would be treated |
| 110 | +as described in point either 1 or 2). |
| 111 | + |
| 112 | +Pros: |
| 113 | +- _Mostly_ keeps compatibility with Helm 3. Technically breaks it, but unlike (3), |
| 114 | + there is a migration path for charts which use an undocumented value other than `never` - |
| 115 | + they need to change it to `never`). |
| 116 | +- A little easier to reason about the behaviour, since there are fewer options. |
| 117 | + |
| 118 | +Cons: |
| 119 | +- If an older chart exists out there that uses an undocumented value other than `never`, |
| 120 | + then its legacy versions would be incompatible with Helm 4. |
| 121 | + |
| 122 | +#### 5. Warn about undocumented policies |
| 123 | + |
| 124 | +Add a linter check that complains if an undocumented value for hook deletion policy is used. |
| 125 | + |
| 126 | +Pros: |
| 127 | +- Raises awareness about this issue. |
| 128 | + |
| 129 | +Cons: |
| 130 | +- None? |
| 131 | + |
| 132 | +## Backwards compatibility |
| 133 | + |
| 134 | +Described above, this HIP is all about compatibility. |
| 135 | + |
| 136 | +## Security implications |
| 137 | + |
| 138 | +None. |
| 139 | + |
| 140 | +## How to teach this |
| 141 | + |
| 142 | +Documentation and linter concerns mentioned above. |
| 143 | + |
| 144 | +## Reference implementation |
| 145 | + |
| 146 | +N/A ATM. |
| 147 | + |
| 148 | +## Rejected ideas |
| 149 | + |
| 150 | +N/A |
| 151 | + |
| 152 | +## Open issues |
| 153 | + |
| 154 | +See alternative points for Helm 4 above. |
| 155 | + |
| 156 | +## Appendix A: Why create storage resources as hooks? |
| 157 | + |
| 158 | +Disclaimer: some of the reasoning below are conjectures since the motivation is lost in the mists of time. |
| 159 | + |
| 160 | +A Helm chart was introduced for the StackRox project around 2020. |
| 161 | +At that time, there was a strong concern that a user mistake might lead to data loss, |
| 162 | +in case the `PersistentVolume` or `PersistentVolumeClaim` were deleted in case the helm release was deleted accidentally. |
| 163 | + |
| 164 | +Because of that it looks like all possible safeguards were applied, including `helm.sh/resource-policy: keep` |
| 165 | +**and** `helm.sh/hook: pre-install,pre-upgrade` together with `helm.sh/hook-delete-policy: never`. |
| 166 | +I do not know why this undocumented value was chosen. |
| 167 | +I assume it might have been a mistake that was not caught until recently, since the result worked as desired. |
| 168 | + |
| 169 | +According to [the documentation][Resource policy docs], using `helm.sh/resource-policy: keep` alone |
| 170 | +would be sufficient to prevent deletion on uninstallation. |
| 171 | +However, it is not completely clear (to me), given how helm behaves during `--force` rollbacks. |
| 172 | +Deleting and recreating a `PersistentVolumeClaim` in that case would lead to data loss. |
| 173 | + |
| 174 | +## References |
| 175 | + |
| 176 | +- [Docs]: https://helm.sh/docs/topics/charts_hooks/#hook-deletion-policies |
| 177 | +- [Code]: https://github.com/helm/helm/blob/bd897c96fbaf7546d6a5c57be009f16f9d38d6de/pkg/action/hooks.go#L47 |
| 178 | +- [Resource policy docs]: https://helm.sh/docs/howto/charts_tips_and_tricks/#tell-helm-not-to-uninstall-a-resource |
| 179 | +- [StackRox chart]: https://github.com/stackrox/stackrox/tree/master/image/templates/helm/stackrox-central |
| 180 | +- [Anance personal chart]: https://github.com/ananace/personal-charts/blob/0e60e96373c8d827c0723ec0bfaa336bd09ffb35/charts/matrix-synapse/templates/signing-key-job.yaml#L176 |
| 181 | +- [Hyrum's Law]: https://www.hyrumslaw.com/ |
0 commit comments