Skip to content

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

Merged
marcleblanc2 merged 10 commits into
mainfrom
michael/opt-in-labels
Dec 1, 2026
Merged

marcleblanc2 merged 10 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

github-actions Bot commented Nov 26, 2026 •

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
…stom exporters

The config the chart renders for openTelemetry.gateway.config.traces.exporters
left the collector's own metrics on 127.0.0.1:8888, so Prometheus could not
scrape them. With the app: otel-collector label on the Service, that failed
scrape fires the otel-collector "pods available" critical alert.

Add the service.telemetry.metrics.readers block the image's jaeger.yaml has.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a21663-f060-7148-8fdc-8eed219fa092
…bel from the node-exporter Service (#1040)

Stacked on #1039, which merges into #1038 (`michael/opt-in-labels`)

## Context

- The old deprecated Kustomize deployment method
(sourcegraph/deploy-sourcegraph) labeled each resource with
`sourcegraph-resource-requires: cluster-admin | no-cluster-admin`

- This chart started as a copy of those manifests

- #7 removed the label from all 69 objects in the Helm chart

- #194 brought one copy back on the `node-exporter` Service

- Nothing in the sourcegraph org selects on it: no selector, script,
code, or docs page

## Problem

- With #1038, `sourcegraph.serviceLabels` and
`nodeExporter.serviceLabels` reach the `node-exporter` Service, and the
hard-coded `sourcegraph-resource-requires` line comes after them, so
setting that key in either value renders it twice

- Helm and kubectl accept it; Kustomize and `kubeconform -strict` reject
the duplicate key

- #1038's changelog says each key renders once

- Customers who have previously deployed on the old Kustomize repo may
be carrying this label forward with them, which would fail deployment
via Kustomize

## Solution

- Remove the hard-coded label from the `node-exporter` Service, the last
object in this chart that set it

- Users who want it set it in `sourcegraph.labels`,
`sourcegraph.serviceLabels`, or `nodeExporter.serviceLabels`, and it
renders once

- `node-exporter` metrics lose the `sourcegraph_resource_requires`
label, which no bundled dashboard or alert queries, so their series are
replaced once on upgrade

## Testing

- Two new tests in `nodeExporter_test.yaml` fail on #1039's head (one
with `mapping key "sourcegraph-resource-requires" already defined`) and
pass here; `helm unittest` (313 tests, 120 snapshots) and `helm lint`
pass

- On EKS with #1038's head, the duplicate rendered as described above:
client-side and server-side apply accepted it, `kubeconform -strict` and
Kustomize did not

## Amp threads

- https://ampcode.com/threads/T-01a21663-f060-7148-8fdc-8eed219fa092

## Changelog

Removed the deprecated `sourcegraph-resource-requires: no-cluster-admin`
label from the `node-exporter` Service; set it in
`sourcegraph.serviceLabels` or `nodeExporter.serviceLabels` if needed to
keep it

Co-authored-by: Amp <amp@ampcode.com>
@marcleblanc2
marcleblanc2 merged commit 484fdc1 into main Dec 1, 2026
6 checks passed
@marcleblanc2
marcleblanc2 deleted the michael/opt-in-labels branch December 1, 2026 09:23
marcleblanc2 added a commit that referenced this pull request Dec 1, 2026
…count when openTelemetry.agent.kind is Deployment (#1041)

## Context

- #863 added `openTelemetry.agent.kind: Deployment`, which skips the
`otel-agent` DaemonSet, so application pods send traces straight to
`otel-collector:4317`

- The `otel-agent` DaemonSet is the only pod that mounts the
`otel-agent` ConfigMap and uses the `otel-agent` ServiceAccount

## Problem

- With `kind: Deployment`, the chart still renders the `otel-agent`
ConfigMap, which nothing uses

- Found rendering a customer's override with `kind: Deployment` and
#1038's label values: the orphan ConfigMap was the only object of 49
without their per-component label, since they set no
`openTelemetry.agent.labels` for an agent they don't run

- With `openTelemetry.agent.serviceAccount.create: true`, the
`otel-agent` ServiceAccount renders regardless of `kind` and
`openTelemetry.enabled`, so it is also unused when no DaemonSet renders

## Solution

- Render the `otel-agent` ConfigMap and ServiceAccount under the same
condition as the DaemonSet: `openTelemetry.enabled` and
`openTelemetry.agent.kind` of `DaemonSet`, the default. The
ServiceAccount still also needs `serviceAccount.create`

- On upgrade, Helm deletes the unused objects from releases that set
`kind: Deployment`. Renders with the default `kind: DaemonSet` do not
change

## Testing

- Four new tests in `otelAgentKind_test.yaml`: the ConfigMap and
ServiceAccount each render by default, and not with `kind: Deployment`.
The two `kind: Deployment` tests fail on `main` and pass here; `helm
unittest` (317 tests, 120 snapshots) and `helm lint` pass

- `helm template` renders byte-identical output on `main` and this
branch with the default values, and with
`openTelemetry.agent.serviceAccount.create: true`

- With `serviceAccount.create: true`, the ServiceAccount renders on
`main` with `kind: Deployment` or `openTelemetry.enabled: false`, and
not here

- The customer override with `kind: Deployment` renders 49 objects on
`main`, including the `otel-agent` ConfigMap, and 48 here, without it

- Not deployed on a cluster

## Amp threads

- https://ampcode.com/threads/T-01a21663-f060-7148-8fdc-8eed219fa092

## Changelog

The `otel-agent` ConfigMap and ServiceAccount are no longer rendered
when `openTelemetry.agent.kind` is `Deployment`

Co-authored-by: Amp <amp@ampcode.com>
DaedalusG added a commit that referenced this pull request Dec 1, 2026
)

## Context

- #1039 (in #1038) changed the `otel-collector` config so its own
metrics are served on `0.0.0.0:8888`, and #1038 added `app:
otel-collector` to the Service, which makes the `otel-collector` "pods
available" alert see that scrape target.
- The collector reads its config only at startup, and its pod template
does not change when the ConfigMap does. Running collectors keep the old
config (metrics on `127.0.0.1:8888`), so the scrape fails and the
critical alert fires.

## Problem

- On Sourcegraph Cloud, every instance that renders the new insiders
chart gets the label and the fixed ConfigMap in the same apply, without
a pod restart, so `critical_otel-collector_pods_available_percentage`
pages. This happened to about 33 instances this morning.
- A second wave started at 16:01 UTC after the controller upgrade
reconciled instances that were still on the pre-#1038 chart
(morgan-stanley-labs, miovision, typeform, dropbox, depop so far). At
11:56 UTC, 74 instances were still on the old chart. Restarting their
collectors does not help until they reconcile, so each one would need a
restart timed after its reconcile.

## Solution

- Add a `checksum/otel-collector-config` pod annotation that hashes
`openTelemetry.gateway.config.traces` (per review; like the `pgsql` auth
checksums). The annotation is new, so the reconcile that brings this
chart rolls every collector onto the fixed config, with no manual
restarts. Later, a change to the traces values rolls the pods. A change
to the chart's config template alone, with the same values, does not.
- Every `otel-collector` Deployment rolls once on the upgrade that adds
the annotation.

### Checklist

- [x] Follow the [manual testing
process](https://github.com/sourcegraph/deploy-sourcegraph-helm/blob/main/TEST.md)
- [x] Update
[changelog](https://github.com/sourcegraph/deploy-sourcegraph-helm/blob/main/charts/sourcegraph/CHANGELOG.md)
- [ ] Update [Kubernetes update
doc](https://docs.sourcegraph.com/admin/updates/kubernetes): not needed

### Test plan

- `helm unittest ./charts/sourcegraph` (helm-unittest v1.0.2, as in CI):
46 suites and 318 tests pass, plus all snapshots.
- New test `should roll the pod when the collector config changes` pins
the hash, computed independently as the sha256 of the merged `traces`
values in sorted-key JSON. It fails on `main`'s template (1 failed, 3
passed) and passes with this change.
- `./scripts/helm-docs.sh` makes no changes.

---------

Co-authored-by: Amp <amp@ampcode.com>
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.

2 participants