Skip to content

fix/labels: Apply label values as documented, and render each key once - #1033

Draft
marcleblanc2 wants to merge 10 commits into
mainfrom
marc/labels-match-docs
Draft

marcleblanc2 wants to merge 10 commits into
mainfrom
marc/labels-match-docs

Conversation

@marcleblanc2

Copy link
Copy Markdown
Contributor

A customer's clusters reject any resource that lacks their service labels, so they need labels on every resource these charts create. The READMEs say sourcegraph.labels does that, but it reached 31 of 108 objects in the sourcegraph chart, 0 of 7 in sourcegraph-executor/k8s, and 2 of 4 in sourcegraph-executor/dind. sourcegraph.serviceLabels did nothing.

The charts also render a label or annotation key twice when it is set in two values, such as sourcegraph.labels and frontend.labels, or in a value and by the chart. Helm and Kubernetes accept that and keep the last value. Kustomize and kubeconform -strict reject it.

This PR makes the charts do what the READMEs say, and render each key once. It adds no values and changes no defaults.

What changes for an installed release

  • Nothing, unless a label value is set. With default values, helm template renders the same text as main for all four charts.
  • No pod restarts and no immutable-field changes. No workload's selector, pod template, or volume claim templates change. Compared with main for 749 workloads across 46 value sets.
  • With sourcegraph.labels set, these gain the labels: Services, ServiceAccounts, ConfigMaps, Secrets, PersistentVolumeClaims, PodDisruptionBudgets, PriorityClasses, the Ingress, the Prometheus RBAC objects, and every object of the k8s and dind executor charts. The labels come first in each block, so a label the chart or another value already sets keeps its value.
  • With sourcegraph.serviceLabels set, every Service gains the labels. The value had no effect before.
  • Metrics gain the labels put on Services. The bundled Prometheus copies a Service's labels onto the metrics it scrapes through it, as it already does for <serviceName>.serviceLabels. The app, job, instance, and ns metric labels cannot change: the chart's app label wins on the Service, and Prometheus sets the others after it copies the labels.
  • A key that is set twice keeps the value it has on the cluster today, and is rendered once.

Review commit by commit

Commit What it does Rendered output compared with main
helpers: add sourcegraph.uniqueKeys to each chart Adds the helper. Nothing calls it. Same
sourcegraph: render each label key once Lists the items of each labels block in the helper, in the same order Same, unless a key repeats
sourcegraph: render each annotation key once The same for annotations blocks Same, unless a key repeats
sourcegraph: add sourcegraph.labels to every resource One more item per block, on 78 templates Same, unless sourcegraph.labels is set
sourcegraph: add sourcegraph.serviceLabels to every Service One more item on 23 Services Same, unless sourcegraph.serviceLabels is set
sourcegraph: add sourcegraph.podLabels to the helm test pod One more item on the test pod Same, unless sourcegraph.podLabels is set
sourcegraph-executor/k8s: add sourcegraph.labels to every resource Both changes for this chart's 7 objects Same, unless sourcegraph.labels is set or a key repeats
sourcegraph-executor/dind: add sourcegraph.labels to every resource Both changes; the ConfigMap and Service gain the labels Same, unless sourcegraph.labels is set or a key repeats
sourcegraph-migrator: render each label and annotation key once The Job's blocks use the helper Same, unless a key repeats
sourcegraph: document what jaeger and the per-component label values do Comments in values.yaml and one README row Same

The helper

sourcegraph.uniqueKeys takes the items of one labels or annotations block, lowest precedence first, and renders each key once with its last value. A block lists the same lines it had before:

  labels:
    {{- include "sourcegraph.uniqueKeys" (list
      (include "sourcegraph.labels" .)
      .Values.frontend.labels
      "deploy: sourcegraph"
      "app.kubernetes.io/component: frontend"
    ) | nindent 4 }}

It passes lines through as written instead of merging maps, so the rendered text is the same as before when no key repeats. Each chart has its own copy in templates/_helpers/_uniqueKeys.tpl, like _priorityClassName.tpl.

Not changed

  • Pod templates do not get sourcegraph.labels. They have sourcegraph.podLabels, and adding labels there would restart every pod.
  • StatefulSet volume claim templates do not get sourcegraph.labels. Kubernetes rejects an upgrade that changes them. The READMEs now say so.
  • extraResources are rendered as the user wrote them.
  • jaeger selectors. The README says sourcegraph.selectorLabelsOverride and <serviceName>.extraSelectorLabels apply to every component, but jaeger ignores them. Making it honour them would change its selector on installed releases, so values.yaml now says what it does.
  • No new values. The per-component storageLabels and serviceAccount.labels values stay in feat/labels: Add labels to PVCs, ServiceAccounts, and RBAC objects #1008, which will need a rebase onto this.
  • jaeger.serviceAccount.create: true fails to render on main, and still does.

Tests

  • Rendered output: 46 value sets (defaults, every optional object enabled, every examples/*/override.yaml, selector overrides, executor queues) rendered from main and from this branch with Helm 4.3.1 and 3.9.1. 43 render the same text, apart from whitespace-only lines main renders when selectorLabelsOverride is set. The other 3 set sourcegraph.labels, and differ only by those labels added to metadata.labels.
  • Coverage: with every optional object enabled, sourcegraph.labels reaches 108 of 108 objects in sourcegraph, 7 of 7 in sourcegraph-executor/k8s, and 5 of 5 in sourcegraph-executor/dind.
  • Strict parsers: with the same keys set in every label and annotation value (tests/uniqueKeys_everyValue.yaml), kubeconform -strict accepts 108 of 108 objects. On main it rejects 54. kubectl kustomize builds that render; on main it fails.
  • Unit tests: 306 pass with helm-unittest 1.0.2 and 1.2.1. helm-unittest fails on a repeated key, as Kustomize does, so the new tests fail on main. The suites for sourcegraph.labels, sourcegraph.serviceLabels, and sourcegraph.podLabels assert on every template, so a new template that leaves them out fails.
  • Each commit passes the unit tests on its own.
  • scripts/ci/lint.sh and scripts/helm-docs.sh are clean.
  • Not yet run on a cluster.

Design decisions

  • Additions only. Nothing that renders today changes its value. New label sources have the lowest precedence in each block.
  • The READMEs are the contract. Where a README says a label is applied, the chart now applies it. Where the chart cannot safely do what the README says (volume claim templates, jaeger selectors), the README says what the chart does.
  • One helper, no hand-built maps. The review of feat/labels: Apply sourcegraph.labels to all objects in all four charts #1023 asked for less drift and simpler templates. Each block passes the lines it already had, in the same order.
  • Keep the last value of a repeated key. That is what Helm and Kubernetes do today, so no live object changes.

marcleblanc2 and others added 10 commits November 25, 2026 15:20
Kustomize and other strict YAML parsers reject a map that repeats a key.
The charts repeat a label or annotation key when it is set in two values,
such as sourcegraph.labels and frontend.labels, or in a value and by the
chart itself.

sourcegraph.uniqueKeys takes the maps of one labels or annotations block,
lowest precedence first, and renders each key once with its last value.
That is the value Helm and Kubernetes already keep, so installed objects
do not change. Lines pass through as written, so the rendered text is the
same as before when no key repeats.

Nothing calls the helper yet. Each chart gets the same file, like
_priorityClassName.tpl.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1effc-8aa2-756b-b724-e1218ffff085
A label key set in two values, such as sourcegraph.labels and
frontend.labels, or in a value and by the chart, was rendered twice in the
same labels block. Kustomize rejects that.

Every labels block that takes labels from more than one place now lists
the same items in the same order in sourcegraph.uniqueKeys. The last value
of a key still wins, so no object of an installed release changes, and
`helm template` renders the same text as before unless a key repeats.

helm-unittest fails on a repeated key, as Kustomize does, so the new tests
fail without this change. uniqueKeys_everyValue.yaml sets the same keys in
every label value, with every optional object enabled.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1effc-8aa2-756b-b724-e1218ffff085
An annotation key set in sourcegraph.podAnnotations and in a component's
podAnnotations, or in a value and by the chart, such as prometheus.io/port
in serviceAnnotations, was rendered twice in the same annotations block.

Every annotations block that takes annotations from more than one place
now lists the same items in the same order in sourcegraph.uniqueKeys. As
with labels, the last value still wins and `helm template` renders the
same text as before unless a key repeats.

Three pod templates add a checksum annotation only when the chart manages
the config. They build that line in a variable first, because a list item
cannot sit inside an `if`.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1effc-8aa2-756b-b724-e1218ffff085
The README says sourcegraph.labels adds labels to all resources, but only
the workloads, the redis and jaeger ConfigMaps, the frontend Role and
RoleBinding, the StorageClass, and the test pod got them: 31 of 108
objects with everything enabled.

Add them to the Services, ServiceAccounts, ConfigMaps, Secrets,
PersistentVolumeClaims, PodDisruptionBudgets, PriorityClasses, the
Ingress, and the Prometheus RBAC objects. They come first in each block,
so a label the chart or another value already sets keeps its value.

Not added, and now documented:
- Pod templates, which have sourcegraph.podLabels. Adding labels there
  would restart every pod.
- StatefulSet volume claim templates. Kubernetes rejects an upgrade that
  changes them.
- extraResources, which the chart renders as the user wrote them.

Nothing changes unless sourcegraph.labels is set. When it is, the bundled
Prometheus copies Service labels onto the metrics it scrapes through a
Service, as it already does for <serviceName>.serviceLabels, so those
metrics gain the labels.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1effc-8aa2-756b-b724-e1218ffff085
The README documents sourcegraph.serviceLabels as extra labels for all
Services, but no template read the value, so it did nothing.

Add it to all 23 Services, after sourcegraph.labels, so it wins over the
labels for every resource. A label the chart or a component's
serviceLabels already sets keeps its value.

Nothing changes unless sourcegraph.serviceLabels is set, and it had no
effect before. As with any Service label, the bundled Prometheus copies
these labels onto the metrics it scrapes through the Service.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1effc-8aa2-756b-b724-e1218ffff085
The README documents sourcegraph.podLabels as labels for all pods. Every
workload's pod template has them, but the pod that `helm test` creates did
not, so a cluster that requires a label on every pod rejects it.

The new test also asserts the labels on every workload's pod template,
which already had them.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1effc-8aa2-756b-b724-e1218ffff085
Co-authored-by: Amp <amp@ampcode.com>
The README says sourcegraph.labels adds a global label to all resources,
but no template read the value, so it did nothing.

Add it to all seven objects: the Deployment, ConfigMap, Secret, Service,
ServiceAccount, Role, and RoleBinding. It comes first in each block, so a
label the chart or another value already sets keeps its value. The pod
template is unchanged; it has sourcegraph.podLabels.

Also list the items of the label and annotation blocks that take values
from more than one place in sourcegraph.uniqueKeys, so a key set twice is
rendered once. `helm template` renders the same text as before unless
sourcegraph.labels is set or a key repeats.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1effc-8aa2-756b-b724-e1218ffff085
Co-authored-by: Amp <amp@ampcode.com>
The README says sourcegraph.labels adds a global label to all resources,
but the docker-config ConfigMap and the private-docker-registry Service
did not get it. Add it to both, first in the block, so the labels the
chart sets keep their values.

It is not added to pod templates, which have sourcegraph.podLabels, or to
the registry's volume claim template, which Kubernetes does not allow
changing. The README now says so.

Also list the items of the label and annotation blocks that take values
from more than one place in sourcegraph.uniqueKeys, so a key set twice is
rendered once. `helm template` renders the same text as before unless
sourcegraph.labels is set or a key repeats.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1effc-8aa2-756b-b724-e1218ffff085
Co-authored-by: Amp <amp@ampcode.com>
List the items of the Job's label and annotation blocks in
sourcegraph.uniqueKeys, so a key set in two values, or in a value and by
the chart, is rendered once. The last value still wins, and
`helm template` renders the same text as before unless a key repeats.

sourcegraph.labels already reached the Job, the chart's only object.

The commented example values listed serviceLabels and serviceAnnotations,
but the chart has no Service. Remove them.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1effc-8aa2-756b-b724-e1218ffff085
Comments in values.yaml, and the README row generated from them:

- sourcegraph.selectorLabelsOverride is documented for every component,
  but jaeger has its own selector helper and ignores it, as it ignores
  extraSelectorLabels. Changing jaeger's selectors would break upgrades of
  installed releases, so say what it does instead.
- The example component block said labels, podLabels, and serviceLabels
  add labels to "various resources". Say which resources each reaches,
  and that jaeger's Services use collector.serviceLabels and
  query.serviceLabels.

No template changes.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1effc-8aa2-756b-b724-e1218ffff085
Co-authored-by: Amp <amp@ampcode.com>
@github-actions

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant