From 4c8073ad9d1069850b33509bff06cda1604c1d3e Mon Sep 17 00:00:00 2001 From: Marc LeBlanc <7050295+marcleblanc2@users.noreply.github.com> Date: Wed, 25 Nov 2026 16:41:46 -0500 Subject: [PATCH] charts: render each label and pod annotation key once A key set both globally (sourcegraph.labels, podLabels, podAnnotations) and per component was rendered twice in one map. Helm keeps the last one, but kustomize rejects the manifest. Add a sourcegraph.mergedMaps helper to each chart, which merges the maps with later ones winning, and render the global and component maps through it. The merge order matches the old rendering order, so Helm releases render the same objects. Add kustomizeRender_labelOverrides.yaml to the migrator and executor charts, so the kustomize render check covers them too. Amp-Thread-ID: https://ampcode.com/threads/T-01a2140d-bf64-751f-9812-0c8757baf6fd Co-authored-by: Amp --- charts/sourcegraph-executor/CHANGELOG.md | 2 + .../dind/templates/_helpers.tpl | 39 +++++++++++-------- .../private-docker-registry.Statefulset.yaml | 19 ++------- .../tests/kustomizeRender_labelOverrides.yaml | 34 ++++++++++++++++ .../k8s/templates/_helpers.tpl | 20 ++++++++++ .../k8s/templates/executor.Deployment.yaml | 14 +------ .../tests/kustomizeRender_labelOverrides.yaml | 26 +++++++++++++ .../templates/_helpers.tpl | 20 ++++++++++ .../migrator/sourcegraph-migrator.Job.yaml | 19 ++------- .../tests/kustomizeRender_labelOverrides.yaml | 23 +++++++++++ charts/sourcegraph/CHANGELOG.md | 1 + charts/sourcegraph/templates/_helpers.tpl | 20 ++++++++++ charts/sourcegraph/templates/_worker.tpl | 19 ++------- .../blobstore/blobstore.Deployment.yaml | 19 ++------- .../cadvisor/cadvisor.DaemonSet.yaml | 19 ++------- .../codeinsights-db.StatefulSet.yaml | 19 ++------- .../codeintel-db.StatefulSet.yaml | 19 ++------- .../sourcegraph-frontend.Deployment.yaml | 19 ++------- .../gitserver/gitserver.StatefulSet.yaml | 19 ++------- .../grafana/grafana.StatefulSet.yaml | 19 ++------- .../indexed-search.StatefulSet.yaml | 19 ++------- .../templates/jaeger/jaeger.Deployment.yaml | 19 ++------- .../node-exporter.DaemonSet.yaml | 19 ++------- .../otel-collector/otel-agent.DaemonSet.yaml | 19 ++------- .../otel-collector.Deployment.yaml | 19 ++------- .../otel-collector.Service.yaml | 5 +-- .../templates/pgsql/pgsql.StatefulSet.yaml | 19 ++------- .../precise-code-intel/worker.Deployment.yaml | 19 ++------- .../prometheus/prometheus.Deployment.yaml | 19 ++------- .../redis/redis-cache.Deployment.yaml | 19 ++------- .../redis/redis-store.Deployment.yaml | 19 ++------- .../searcher/searcher.StatefulSet.yaml | 19 ++------- .../worker.Deployment.yaml | 19 ++------- .../syntect-server.Deployment.yaml | 19 ++------- 34 files changed, 241 insertions(+), 400 deletions(-) create mode 100644 charts/sourcegraph-executor/dind/tests/kustomizeRender_labelOverrides.yaml create mode 100644 charts/sourcegraph-executor/k8s/tests/kustomizeRender_labelOverrides.yaml create mode 100644 charts/sourcegraph-migrator/tests/kustomizeRender_labelOverrides.yaml diff --git a/charts/sourcegraph-executor/CHANGELOG.md b/charts/sourcegraph-executor/CHANGELOG.md index 5e2f4fb18..5f542ed08 100644 --- a/charts/sourcegraph-executor/CHANGELOG.md +++ b/charts/sourcegraph-executor/CHANGELOG.md @@ -3,6 +3,8 @@ ## Unreleased +- Fixed a label or pod annotation key set both in `sourcegraph.labels`, `sourcegraph.podLabels`, or `sourcegraph.podAnnotations` and in the `executor` or `privateDockerRegistry` equivalent being rendered twice in one map. Helm used the last one, but kustomize rejected the manifest. Each key is now rendered once, with the component's value, so Helm releases render the same objects as before. + ## 8.0.0 * Added a `network-policy` example to the `sourcegraph-executor-k8s` and `sourcegraph-executor-dind` charts, with `CiliumNetworkPolicy` resources which limit Executor and job pods to the frontend API. diff --git a/charts/sourcegraph-executor/dind/templates/_helpers.tpl b/charts/sourcegraph-executor/dind/templates/_helpers.tpl index 8b87b5d24..d6859dcff 100644 --- a/charts/sourcegraph-executor/dind/templates/_helpers.tpl +++ b/charts/sourcegraph-executor/dind/templates/_helpers.tpl @@ -27,6 +27,26 @@ app.kubernetes.io/managed-by: {{ .Release.Service }} {{- end }} {{- end }} +{{/* +Merges a list of maps, such as the labels or annotations set globally and per +component, so each key is rendered once. A later map wins when a key is set in +more than one. Kustomize rejects a key rendered twice in one map. Renders +nothing when every map is empty. + +{{ include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.frontend.podLabels) }} +*/}} +{{- define "sourcegraph.mergedMaps" -}} +{{- $merged := dict -}} +{{- range . -}} +{{- range $key, $value := . -}} +{{- $_ := set $merged $key $value -}} +{{- end -}} +{{- end -}} +{{- with $merged -}} +{{- toYaml . -}} +{{- end -}} +{{- end }} + {{/* Selector labels */}} @@ -136,10 +156,7 @@ metadata: description: Runs sourcegraph executors kubectl.kubernetes.io/default-container: executor labels: - {{- include "sourcegraph.labels" $r | nindent 4 }} - {{- if $r.Values.executor.labels }} - {{- toYaml $r.Values.executor.labels | nindent 4 }} - {{- end }} + {{- include "sourcegraph.mergedMaps" (list (include "sourcegraph.labels" $r | fromYaml) $r.Values.executor.labels) | nindent 4 }} app: {{ .name }} deploy: sourcegraph sourcegraph-resource-requires: no-cluster-admin @@ -162,20 +179,10 @@ spec: annotations: kubectl.kubernetes.io/default-container: executor checksum/docker-config: {{ include "dind.daemonConfig" $r | sha256sum }} - {{- if $r.Values.sourcegraph.podAnnotations }} - {{- toYaml $r.Values.sourcegraph.podAnnotations | nindent 8 }} - {{- end }} - {{- if $r.Values.executor.podAnnotations }} - {{- toYaml $r.Values.executor.podAnnotations | nindent 8 }} - {{- end }} + {{- include "sourcegraph.mergedMaps" (list $r.Values.sourcegraph.podAnnotations $r.Values.executor.podAnnotations) | nindent 8 }} labels: {{- include "sourcegraph.selectorLabels" $r | nindent 8 }} - {{- if $r.Values.sourcegraph.podLabels }} - {{- toYaml $r.Values.sourcegraph.podLabels | nindent 8 }} - {{- end }} - {{- if $r.Values.executor.podLabels }} - {{- toYaml $r.Values.executor.podLabels | nindent 8 }} - {{- end }} + {{- include "sourcegraph.mergedMaps" (list $r.Values.sourcegraph.podLabels $r.Values.executor.podLabels) | nindent 8 }} app: {{ .name }} deploy: sourcegraph sourcegraph-resource-requires: no-cluster-admin diff --git a/charts/sourcegraph-executor/dind/templates/private-docker-registry/private-docker-registry.Statefulset.yaml b/charts/sourcegraph-executor/dind/templates/private-docker-registry/private-docker-registry.Statefulset.yaml index 71f3194e8..828d51c8e 100644 --- a/charts/sourcegraph-executor/dind/templates/private-docker-registry/private-docker-registry.Statefulset.yaml +++ b/charts/sourcegraph-executor/dind/templates/private-docker-registry/private-docker-registry.Statefulset.yaml @@ -4,10 +4,7 @@ kind: StatefulSet metadata: name: private-docker-registry labels: - {{- include "sourcegraph.labels" . | nindent 4 }} - {{- if .Values.privateDockerRegistry.labels }} - {{- toYaml .Values.privateDockerRegistry.labels | nindent 4 }} - {{- end }} + {{- include "sourcegraph.mergedMaps" (list (include "sourcegraph.labels" . | fromYaml) .Values.privateDockerRegistry.labels) | nindent 4 }} deploy: sourcegraph app.kubernetes.io/component: private-docker-registry spec: @@ -23,20 +20,10 @@ spec: metadata: annotations: kubectl.kubernetes.io/default-container: private-docker-registry - {{- if .Values.sourcegraph.podAnnotations }} - {{- toYaml .Values.sourcegraph.podAnnotations | nindent 8 }} - {{- end }} - {{- if .Values.privateDockerRegistry.podAnnotations }} - {{- toYaml .Values.privateDockerRegistry.podAnnotations | nindent 8 }} - {{- end }} + {{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podAnnotations .Values.privateDockerRegistry.podAnnotations) | nindent 8 }} labels: {{- include "sourcegraph.selectorLabels" . | nindent 8 }} - {{- if .Values.sourcegraph.podLabels }} - {{- toYaml .Values.sourcegraph.podLabels | nindent 8 }} - {{- end }} - {{- if .Values.privateDockerRegistry.podLabels }} - {{- toYaml .Values.privateDockerRegistry.podLabels | nindent 8 }} - {{- end }} + {{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.privateDockerRegistry.podLabels) | nindent 8 }} deploy: sourcegraph app: private-docker-registry spec: diff --git a/charts/sourcegraph-executor/dind/tests/kustomizeRender_labelOverrides.yaml b/charts/sourcegraph-executor/dind/tests/kustomizeRender_labelOverrides.yaml new file mode 100644 index 000000000..17f366583 --- /dev/null +++ b/charts/sourcegraph-executor/dind/tests/kustomizeRender_labelOverrides.yaml @@ -0,0 +1,34 @@ +# Used by CI: the "Render charts through kustomize" GitHub Actions job +# (.github/workflows/kustomize_render.yml) renders this chart with this file +# through scripts/ci/kustomize-render.sh. +# +# The same label and annotation keys at the global and per-component levels, +# where the per-component value should win. Without deduplication, the chart +# renders the key twice in one map, which kustomize rejects. + +sourcegraph: + labels: + example.com/team: platform + podLabels: + example.com/team: platform + podAnnotations: + metrics.example.com/port: "9090" + +executor: + frontendUrl: https://sourcegraph.example.com + frontendExistingSecret: example-secret + queueName: batches + labels: + example.com/team: executors + podLabels: + example.com/team: executors + podAnnotations: + metrics.example.com/port: "6060" + +privateDockerRegistry: + labels: + example.com/team: registry + podLabels: + example.com/team: registry + podAnnotations: + metrics.example.com/port: "6060" diff --git a/charts/sourcegraph-executor/k8s/templates/_helpers.tpl b/charts/sourcegraph-executor/k8s/templates/_helpers.tpl index 70f75b28c..008838d52 100644 --- a/charts/sourcegraph-executor/k8s/templates/_helpers.tpl +++ b/charts/sourcegraph-executor/k8s/templates/_helpers.tpl @@ -27,6 +27,26 @@ app.kubernetes.io/managed-by: {{ .Release.Service }} {{- end }} {{- end }} +{{/* +Merges a list of maps, such as the labels or annotations set globally and per +component, so each key is rendered once. A later map wins when a key is set in +more than one. Kustomize rejects a key rendered twice in one map. Renders +nothing when every map is empty. + +{{ include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.frontend.podLabels) }} +*/}} +{{- define "sourcegraph.mergedMaps" -}} +{{- $merged := dict -}} +{{- range . -}} +{{- range $key, $value := . -}} +{{- $_ := set $merged $key $value -}} +{{- end -}} +{{- end -}} +{{- with $merged -}} +{{- toYaml . -}} +{{- end -}} +{{- end }} + {{/* Selector labels */}} diff --git a/charts/sourcegraph-executor/k8s/templates/executor.Deployment.yaml b/charts/sourcegraph-executor/k8s/templates/executor.Deployment.yaml index 1cb35d2c4..da3098b90 100644 --- a/charts/sourcegraph-executor/k8s/templates/executor.Deployment.yaml +++ b/charts/sourcegraph-executor/k8s/templates/executor.Deployment.yaml @@ -30,20 +30,10 @@ spec: metadata: annotations: kubectl.kubernetes.io/default-container: executor - {{- if .Values.sourcegraph.podAnnotations }} - {{- toYaml .Values.sourcegraph.podAnnotations | nindent 8 }} - {{- end }} - {{- if .Values.executor.podAnnotations }} - {{- toYaml .Values.executor.podAnnotations | nindent 8 }} - {{- end }} + {{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podAnnotations .Values.executor.podAnnotations) | nindent 8 }} labels: {{- include "sourcegraph.selectorLabels" . | nindent 8 }} - {{- if .Values.sourcegraph.podLabels }} - {{- toYaml .Values.sourcegraph.podLabels | nindent 8 }} - {{- end }} - {{- if .Values.executor.podLabels }} - {{- toYaml .Values.executor.podLabels | nindent 8 }} - {{- end }} + {{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.executor.podLabels) | nindent 8 }} {{- include "executor.labels" . | nindent 8 }} spec: securityContext: diff --git a/charts/sourcegraph-executor/k8s/tests/kustomizeRender_labelOverrides.yaml b/charts/sourcegraph-executor/k8s/tests/kustomizeRender_labelOverrides.yaml new file mode 100644 index 000000000..764976963 --- /dev/null +++ b/charts/sourcegraph-executor/k8s/tests/kustomizeRender_labelOverrides.yaml @@ -0,0 +1,26 @@ +# Used by CI: the "Render charts through kustomize" GitHub Actions job +# (.github/workflows/kustomize_render.yml) renders this chart with this file +# through scripts/ci/kustomize-render.sh. +# +# The same label and annotation keys at the global and per-component levels, +# where the per-component value should win. Without deduplication, the chart +# renders the key twice in one map, which kustomize rejects. + +sourcegraph: + labels: + example.com/team: platform + podLabels: + example.com/team: platform + podAnnotations: + metrics.example.com/port: "9090" + +executor: + frontendUrl: https://sourcegraph.example.com + frontendExistingSecret: example-secret + queueName: batches + labels: + example.com/team: executors + podLabels: + example.com/team: executors + podAnnotations: + metrics.example.com/port: "6060" diff --git a/charts/sourcegraph-migrator/templates/_helpers.tpl b/charts/sourcegraph-migrator/templates/_helpers.tpl index f6ce9b36d..9473283a9 100644 --- a/charts/sourcegraph-migrator/templates/_helpers.tpl +++ b/charts/sourcegraph-migrator/templates/_helpers.tpl @@ -27,6 +27,26 @@ app.kubernetes.io/managed-by: {{ .Release.Service }} {{- end }} {{- end }} +{{/* +Merges a list of maps, such as the labels or annotations set globally and per +component, so each key is rendered once. A later map wins when a key is set in +more than one. Kustomize rejects a key rendered twice in one map. Renders +nothing when every map is empty. + +{{ include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.frontend.podLabels) }} +*/}} +{{- define "sourcegraph.mergedMaps" -}} +{{- $merged := dict -}} +{{- range . -}} +{{- range $key, $value := . -}} +{{- $_ := set $merged $key $value -}} +{{- end -}} +{{- end -}} +{{- with $merged -}} +{{- toYaml . -}} +{{- end -}} +{{- end }} + {{/* Selector labels */}} diff --git a/charts/sourcegraph-migrator/templates/migrator/sourcegraph-migrator.Job.yaml b/charts/sourcegraph-migrator/templates/migrator/sourcegraph-migrator.Job.yaml index c77ec3b33..e2f23144f 100644 --- a/charts/sourcegraph-migrator/templates/migrator/sourcegraph-migrator.Job.yaml +++ b/charts/sourcegraph-migrator/templates/migrator/sourcegraph-migrator.Job.yaml @@ -4,10 +4,7 @@ metadata: annotations: description: Performs Sourcegraph database migrations. labels: - {{- include "sourcegraph.labels" . | nindent 4 }} - {{- if .Values.migrator.labels }} - {{- toYaml .Values.migrator.labels | nindent 4 }} - {{- end }} + {{- include "sourcegraph.mergedMaps" (list (include "sourcegraph.labels" . | fromYaml) .Values.migrator.labels) | nindent 4 }} deploy: sourcegraph app.kubernetes.io/component: migrator name: {{ default "migrator" .Values.migrator.name }}-{{ randAlphaNum 5 | lower }} @@ -17,20 +14,10 @@ spec: metadata: annotations: kubectl.kubernetes.io/default-container: migrator - {{- if .Values.sourcegraph.podAnnotations }} - {{- toYaml .Values.sourcegraph.podAnnotations | nindent 8 }} - {{- end }} - {{- if .Values.migrator.podAnnotations }} - {{- toYaml .Values.migrator.podAnnotations | nindent 8 }} - {{- end }} + {{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podAnnotations .Values.migrator.podAnnotations) | nindent 8 }} labels: {{- include "sourcegraph.selectorLabels" . | nindent 8 }} - {{- if .Values.sourcegraph.podLabels }} - {{- toYaml .Values.sourcegraph.podLabels | nindent 8 }} - {{- end }} - {{- if .Values.migrator.podLabels }} - {{- toYaml .Values.migrator.podLabels | nindent 8 }} - {{- end }} + {{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.migrator.podLabels) | nindent 8 }} job: migrator deploy: sourcegraph spec: diff --git a/charts/sourcegraph-migrator/tests/kustomizeRender_labelOverrides.yaml b/charts/sourcegraph-migrator/tests/kustomizeRender_labelOverrides.yaml new file mode 100644 index 000000000..ed347e40c --- /dev/null +++ b/charts/sourcegraph-migrator/tests/kustomizeRender_labelOverrides.yaml @@ -0,0 +1,23 @@ +# Used by CI: the "Render charts through kustomize" GitHub Actions job +# (.github/workflows/kustomize_render.yml) renders this chart with this file +# through scripts/ci/kustomize-render.sh. +# +# The same label and annotation keys at the global and per-component levels, +# where the per-component value should win. Without deduplication, the chart +# renders the key twice in one map, which kustomize rejects. + +sourcegraph: + labels: + example.com/team: platform + podLabels: + example.com/team: platform + podAnnotations: + metrics.example.com/port: "9090" + +migrator: + labels: + example.com/team: database + podLabels: + example.com/team: database + podAnnotations: + metrics.example.com/port: "6060" diff --git a/charts/sourcegraph/CHANGELOG.md b/charts/sourcegraph/CHANGELOG.md index a8b444320..12f07b577 100644 --- a/charts/sourcegraph/CHANGELOG.md +++ b/charts/sourcegraph/CHANGELOG.md @@ -8,6 +8,7 @@ Use `**BREAKING**:` to denote a breaking change ## Unreleased +- Fixed a label or pod annotation key set both globally (`sourcegraph.labels`, `sourcegraph.podLabels`, `sourcegraph.podAnnotations`) and per component (`.labels`, `.podLabels`, `.podAnnotations`, or `openTelemetry.gateway.serviceLabels`) being rendered twice in one map. Helm used the last one, but kustomize rejected the manifest. Each key is now rendered once, with the component's value, so Helm releases render the same objects as before. - The `correct-data-dir-permissions` init containers of `pgsql`, `codeintel-db`, and `codeinsights-db` now run the database's own image instead of `sourcegraph/alpine-3.14`, which was based on an end-of-life Alpine release. The chart no longer pulls `alpine-3.14`, so private registries no longer need to mirror it. `alpine.image` has been removed and is ignored if set; `alpine.containerSecurityContext`, `alpine.resources`, and `codeInsightsDB.init.containerSecurityContext` still apply. - Added `sourcegraph.selectorLabelsOverride`, `.extraSelectorLabels`, and `storageLabels` on `gitserver`, `grafana`, `indexedSearch`, and `searcher`, so the chart can render the selectors and volume claim templates of workloads created outside it, such as with Kustomize, and a Helm release can take them over without deleting them. All default to empty, so existing releases render the same selectors. Never change them on an installed release: Kubernetes does not allow changing these fields. See [Adopting existing workloads](README.md#adopting-existing-workloads). - Fixed `frontend.command` and `migrator.command` rendering a list as a single element, e.g. `["sh", "-c", "