Skip to content

sourcegraph: render opt-in labels on every object, each key once - #1038

Open
michaellzc wants to merge 7 commits into
mainfrom
michael/opt-in-labels
Open

michaellzc wants to merge 7 commits into
mainfrom
michael/opt-in-labels

Conversation

@michaellzc

@michaellzc michaellzc commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Customers moving from Kustomize need their own labels on every object the chart renders, so their admission policies accept the install. Today sourcegraph.labels reaches only some templates, sourcegraph.serviceLabels is never read, and overlapping labels render as duplicate keys that kubeconform -strict and Kustomize reject. This is a smaller alternative to the #1008 stack: existing labels, selectors, and pod templates stay as they are.

User labels now come from three helpers. Each merges its values so a key renders once, and fails the render on a key the chart sets itself:

  • sourcegraph.objectLabels: sourcegraph.labels, then <service>.labels, on every object except Services and pods.
  • sourcegraph.serviceLabels: sourcegraph.serviceLabels, then <service>.serviceLabels, on every Service. Services never get sourcegraph.labels, because Prometheus copies Service labels onto metrics.
  • sourcegraph.podLabels: sourcegraph.podLabels, then <service>.podLabels, on pod templates, after the selector labels. It fails on a label that would change a selector label.

The convention around them, now in AGENTS.md and enforced by tests/labelConvention*_test.yaml on every rendered object: chart-owned labels stay literal lines in templates; selectors and volumeClaimTemplates never get user labels; the standard Helm labels (sourcegraph.helmLabels) go only on workloads, plus the few templates that already had them.

Also: frontendInternal configures the sourcegraph-frontend-internal Service separately from sourcegraph-frontend, the otel-collector Service gets app: otel-collector, and openTelemetry.gateway takes podSecurityContext, extraVolumes, and extraVolumeMounts.

Checklist

Test plan

  • Sourcegraph Cloud: a read-only kubectl diff and server-side dry-run against a Cloud dev instance change only one label, app: otel-collector on the otel-collector Service. Nothing else changes.
  • A customer's Kustomize-deployed install: rendered with values that reproduce it, every immutable field (selectors, volumeClaimTemplates, serviceName) matches, so Helm can adopt it without recreating PVCs or StatefulSets. What still differs is chart-hardcoded, such as port names and probes.

Source: https://ampcode.com/threads/T-01a20fe9-432c-754e-af86-b9c6a249e1d1

sourcegraph.labels is documented as adding labels to all resources, and
sourcegraph.serviceLabels to all Services, but the first reached only the
workloads and a few other objects, and the second was not used at all. A
customer moving from Kustomize needs their labels on every object, and a
label key set in two values rendered twice, which Kustomize and
kubeconform -strict reject.

Each template keeps its own labels as literal lines and renders the user's
labels with one helper: sourcegraph.objectLabels (sourcegraph.labels, then
<serviceName>.labels) on a component's workloads, ConfigMaps, Secrets,
standalone PVCs, ServiceAccounts, RBAC objects, PodDisruptionBudgets, and
Ingress; sourcegraph.serviceLabels on Services; sourcegraph.podLabels on
pods. They merge the values so each key renders once, and fail the render
on a key the chart sets itself, or a pod label that changes a selector label;
before, such a key rendered twice and Helm kept the last one.

Renders with none of these values set are byte-identical.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a20fe9-432c-754e-af86-b9c6a249e1d1
@github-actions

Copy link
Copy Markdown

michaellzc and others added 6 commits November 26, 2026 00:20
Every other Service, and the otel-collector pods, already have an app
label. The otel-collector Service did not, and user labels cannot add it
because app is a key the chart sets itself.

Amp-Thread-ID: https://ampcode.com/threads/T-01a20fe9-432c-754e-af86-b9c6a249e1d1
Co-authored-by: Amp <amp@ampcode.com>
…rvice

frontend.serviceLabels went on both frontend Services, so an install
could not label sourcegraph-frontend without labeling
sourcegraph-frontend-internal the same way. frontendInternal.serviceLabels,
serviceAnnotations, and serviceType now configure the internal Service;
annotations and type fall back to frontend.serviceAnnotations and
frontend.internalServiceType as before.
Sourcegraph Cloud's controller sets <service>.podLabels
app.kubernetes.io/component on every component, and its monitoring
selects on those values, some of which differ from the object label
(indexed-searcher, syntax-highlighter). The chart does not set that key
on pods, so the reserved-key check failed every Cloud render. Pod labels
are now checked only against the keys pod templates set: app, deploy,
group, type, and worker-replica, plus the selector labels.

frontendInternal.serviceLabels now falls back to frontend.serviceLabels,
which main renders on sourcegraph-frontend-internal, so existing
releases keep those labels.

Amp-Thread-ID: https://ampcode.com/threads/T-01a20fe9-432c-754e-af86-b9c6a249e1d1
Co-authored-by: Amp <amp@ampcode.com>
frontendInternal.serviceLabels and serviceAnnotations fell back to the
frontend values whenever they were empty, so the internal Service could
not drop annotations meant for the public one, such as cloud load
balancer annotations. They now default to null, which still falls back,
and {} renders none.

Amp-Thread-ID: https://ampcode.com/threads/T-01a20fe9-432c-754e-af86-b9c6a249e1d1
Co-authored-by: Amp <amp@ampcode.com>
Every other workload takes podSecurityContext, extraVolumes, and
extraVolumeMounts; otel-collector did not, so an extraInitContainers
container, such as one fetching an exporter API key, had no volume to
share with the collector. The pod security context renders only when
set, so default renders do not change.
Which objects get the standard Helm labels, user labels, Service labels,
and pod labels grew by accident. Write the rules into AGENTS.md and
enforce them with two helm-unittest suites that render every template
with every optional object enabled, so a new template is checked
without being listed. Templates that predate the rules are listed as
exceptions.

Amp-Thread-ID: https://ampcode.com/threads/T-01a20fe9-432c-754e-af86-b9c6a249e1d1
Co-authored-by: Amp <amp@ampcode.com>
@michaellzc
michaellzc marked this pull request as ready for review November 26, 2026 03:46

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