Why would you give a Helm chart hook a negative helm.sh/hook-weight?
answer
- Nothing marks the start of the scale
- The default is crowded
- Someone else's hook is in your pool
- You cannot edit a dependency's annotations
- Leave room to insert later
basics
~10 sBecause 0 is the default, every hook that omits the annotation sits at 0 - including hooks from subcharts you do not control. Only a negative weight is guaranteed to run ahead of them.
solid answer
~50 sThe scale has no floor marker and no keyword for "first": Helm just sorts integers ascending, and an unannotated hook is 0. That makes 0 a crowded default rather than a starting line. Anything you want to run ahead of the unannotated crowd has to be negative - and the crowd includes hooks you did not write, because hooks from enabled subcharts are flattened into the same release and sorted into the same pool for that event. You cannot edit a dependency's annotations, so a negative weight on your own hook is the only lever you have. The same reasoning drives the usual convention of spacing weights out - `-20`, `-10`, `0`, `10` - rather than using consecutive numbers: the gaps leave room to insert a hook later without renumbering the chart and without accidentally recreating a tie.
code
yaml · 15 linesapiVersion: batch/v1
kind: Job
metadata:
name: fanout-claim-shards
annotations:
"helm.sh/hook": pre-install
"helm.sh/hook-weight": "-20"
spec:
template:
spec:
restartPolicy: Never
containers:
- name: claim
image: registry.example.internal/fanout-tools:1.4.2
args: ["claim-shards"]go deeper
Just hold the fact: weights are plain integers, negatives are legal, and a hook with no weight annotation is 0. That is enough to read someone else's chart without being surprised by a minus sign.
Explain the reason rather than the syntax - 0 is the default so it is crowded, and hooks from enabled subcharts land in the same pool for that event, which is the case a negative weight actually solves.
Be ready to talk about weights you do not control: negotiating upstream versus weighting around a dependency, and publishing spaced, documented weights when your own chart is the one being depended on.
Treat hook weights as part of a shared chart's public interface. Set the band convention across the estate, decide what the platform's own hooks reserve, and be honest that heavy hook sequencing is a smell worth designing out.
### Zero is a default, not a starting line Helm sorts a hook event's manifests by their `helm.sh/hook-weight` values, ascending, and treats a manifest with no such annotation as weight 0. It offers no `first`, no `last`, and no notion of a lowest legal weight. That combination has a consequence people miss: 0 is the *middle* of the scale and it is where everything unannotated piles up. If you want to be genuinely first, the only expression of that is a number below 0. ### The case that makes it necessary: hooks you do not own Inside a chart you control, negatives are a convenience - you could renumber everything to `0`, `10`, `20` instead. The case where a negative weight is the *only* answer is a chart with dependencies. Hooks contributed by enabled subcharts are flattened into the release and sorted into the same pool for the event they fire on. If a dependency ships a `pre-install` Job with no weight annotation, it is at 0, and you cannot change that - you do not own the subchart's templates, and a subchart's annotations are not something a parent's values can generally rewrite. Your options are to negotiate a change upstream, fork the dependency, or put your own hook at `"-10"` and be certain it runs first. The last option is a two-character edit in your own chart. The same holds in reverse for a chart intended for other people to depend on. If your chart's hooks all sit at 0, every consumer who needs to run something before them has to go negative. Publishing your hooks at deliberate, spaced weights - and documenting them in the chart's README - is a courtesy to consumers, and it is one of the few places where an annotation choice is really an interface decision. ### Spacing, not sequence The second reason to reach below zero is room. A chart that numbers its hooks `0`, `1`, `2` has to renumber to insert anything between the first two, and a hurried author who cannot be bothered will just duplicate a weight and recreate a tie. Numbering `-20`, `-10`, `0`, `10`, `20` leaves nine free slots on each side of every hook. Since the numbers are purely relative - only their order matters, never their magnitude - the gaps cost nothing. ```yaml annotations: "helm.sh/hook": pre-install "helm.sh/hook-weight": "-20" # ahead of anything unannotated, ours or a dependency's ``` Remember the value is a quoted string. `helm.sh/hook-weight: -20` unquoted is a YAML integer, and an annotation value must be a string, so the object is rejected when Helm applies it - a mistake that survives rendering and only fails in the cluster. ### What a negative weight does not buy you It does not move the hook out of its phase. A `pre-install` hook at `"-500"` is still a `pre-install` hook: it runs in that phase, after the release record exists and before the chart's ordinary manifests are applied, and no weight reaches earlier than the phase itself. It does not run before hooks on a different event either, because different events are different phases with their own pools. And it does not create a dependency: Helm will run the negative-weighted hook first, but nothing about the annotation says the later hook requires it - the only linkage is that a failed hook aborts the operation, so the rest of the phase never runs. ### The honest scope of this question Nobody is turned down for not knowing that weights can be negative; the mechanism is a one-line fact and the ordering it produces is obvious once stated. What separates a good answer is the reason: naming the unannotated-subchart-hook problem shows the candidate has run a chart with real dependencies and has hit the case where their own numbering was not enough. That is the differentiator, not the minus sign.
- Can a parent chart change the hook weight on a hook that comes from one of its subcharts?Not in general. The annotation is written in the subchart's own template, so unless the subchart deliberately exposes it through a value, the parent cannot rewrite it. The practical levers are to weight your own hooks around it, to send a patch upstream, or to vendor and modify the dependency - the first is usually the cheapest.
- Is there any functional difference between weights -5 and -5000?None, unless something else sits between them. The values are compared only against each other, so magnitude carries no meaning and there is no lowest legal weight to reserve. Very large numbers are just a convention some authors use to mean 'this should stay first'; the sort treats them like any other integer.
saying these in an interview costs you the question
- Says negative weights are invalid or a hack
- Assumes an unannotated hook runs last
- Thinks a negative weight escapes the hook's phase
- Believes the parent can rewrite a subchart hook's weight
- Numbers hooks consecutively with no room to insert
- Treats magnitude as meaningful rather than order