Skip to content

Commit 4c8073a

Browse files
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 <amp@ampcode.com>
1 parent c802ece commit 4c8073a

34 files changed

Lines changed: 241 additions & 400 deletions

‎charts/sourcegraph-executor/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<!-- START CHANGELOG -->
44
## Unreleased
55

6+
- 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.
7+
68
## 8.0.0
79

810
* 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.

‎charts/sourcegraph-executor/dind/templates/_helpers.tpl‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
2727
{{- end }}
2828
{{- end }}
2929

30+
{{/*
31+
Merges a list of maps, such as the labels or annotations set globally and per
32+
component, so each key is rendered once. A later map wins when a key is set in
33+
more than one. Kustomize rejects a key rendered twice in one map. Renders
34+
nothing when every map is empty.
35+
36+
{{ include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.frontend.podLabels) }}
37+
*/}}
38+
{{- define "sourcegraph.mergedMaps" -}}
39+
{{- $merged := dict -}}
40+
{{- range . -}}
41+
{{- range $key, $value := . -}}
42+
{{- $_ := set $merged $key $value -}}
43+
{{- end -}}
44+
{{- end -}}
45+
{{- with $merged -}}
46+
{{- toYaml . -}}
47+
{{- end -}}
48+
{{- end }}
49+
3050
{{/*
3151
Selector labels
3252
*/}}
@@ -136,10 +156,7 @@ metadata:
136156
description: Runs sourcegraph executors
137157
kubectl.kubernetes.io/default-container: executor
138158
labels:
139-
{{- include "sourcegraph.labels" $r | nindent 4 }}
140-
{{- if $r.Values.executor.labels }}
141-
{{- toYaml $r.Values.executor.labels | nindent 4 }}
142-
{{- end }}
159+
{{- include "sourcegraph.mergedMaps" (list (include "sourcegraph.labels" $r | fromYaml) $r.Values.executor.labels) | nindent 4 }}
143160
app: {{ .name }}
144161
deploy: sourcegraph
145162
sourcegraph-resource-requires: no-cluster-admin
@@ -162,20 +179,10 @@ spec:
162179
annotations:
163180
kubectl.kubernetes.io/default-container: executor
164181
checksum/docker-config: {{ include "dind.daemonConfig" $r | sha256sum }}
165-
{{- if $r.Values.sourcegraph.podAnnotations }}
166-
{{- toYaml $r.Values.sourcegraph.podAnnotations | nindent 8 }}
167-
{{- end }}
168-
{{- if $r.Values.executor.podAnnotations }}
169-
{{- toYaml $r.Values.executor.podAnnotations | nindent 8 }}
170-
{{- end }}
182+
{{- include "sourcegraph.mergedMaps" (list $r.Values.sourcegraph.podAnnotations $r.Values.executor.podAnnotations) | nindent 8 }}
171183
labels:
172184
{{- include "sourcegraph.selectorLabels" $r | nindent 8 }}
173-
{{- if $r.Values.sourcegraph.podLabels }}
174-
{{- toYaml $r.Values.sourcegraph.podLabels | nindent 8 }}
175-
{{- end }}
176-
{{- if $r.Values.executor.podLabels }}
177-
{{- toYaml $r.Values.executor.podLabels | nindent 8 }}
178-
{{- end }}
185+
{{- include "sourcegraph.mergedMaps" (list $r.Values.sourcegraph.podLabels $r.Values.executor.podLabels) | nindent 8 }}
179186
app: {{ .name }}
180187
deploy: sourcegraph
181188
sourcegraph-resource-requires: no-cluster-admin

‎charts/sourcegraph-executor/dind/templates/private-docker-registry/private-docker-registry.Statefulset.yaml‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ kind: StatefulSet
44
metadata:
55
name: private-docker-registry
66
labels:
7-
{{- include "sourcegraph.labels" . | nindent 4 }}
8-
{{- if .Values.privateDockerRegistry.labels }}
9-
{{- toYaml .Values.privateDockerRegistry.labels | nindent 4 }}
10-
{{- end }}
7+
{{- include "sourcegraph.mergedMaps" (list (include "sourcegraph.labels" . | fromYaml) .Values.privateDockerRegistry.labels) | nindent 4 }}
118
deploy: sourcegraph
129
app.kubernetes.io/component: private-docker-registry
1310
spec:
@@ -23,20 +20,10 @@ spec:
2320
metadata:
2421
annotations:
2522
kubectl.kubernetes.io/default-container: private-docker-registry
26-
{{- if .Values.sourcegraph.podAnnotations }}
27-
{{- toYaml .Values.sourcegraph.podAnnotations | nindent 8 }}
28-
{{- end }}
29-
{{- if .Values.privateDockerRegistry.podAnnotations }}
30-
{{- toYaml .Values.privateDockerRegistry.podAnnotations | nindent 8 }}
31-
{{- end }}
23+
{{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podAnnotations .Values.privateDockerRegistry.podAnnotations) | nindent 8 }}
3224
labels:
3325
{{- include "sourcegraph.selectorLabels" . | nindent 8 }}
34-
{{- if .Values.sourcegraph.podLabels }}
35-
{{- toYaml .Values.sourcegraph.podLabels | nindent 8 }}
36-
{{- end }}
37-
{{- if .Values.privateDockerRegistry.podLabels }}
38-
{{- toYaml .Values.privateDockerRegistry.podLabels | nindent 8 }}
39-
{{- end }}
26+
{{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.privateDockerRegistry.podLabels) | nindent 8 }}
4027
deploy: sourcegraph
4128
app: private-docker-registry
4229
spec:
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Used by CI: the "Render charts through kustomize" GitHub Actions job
2+
# (.github/workflows/kustomize_render.yml) renders this chart with this file
3+
# through scripts/ci/kustomize-render.sh.
4+
#
5+
# The same label and annotation keys at the global and per-component levels,
6+
# where the per-component value should win. Without deduplication, the chart
7+
# renders the key twice in one map, which kustomize rejects.
8+
9+
sourcegraph:
10+
labels:
11+
example.com/team: platform
12+
podLabels:
13+
example.com/team: platform
14+
podAnnotations:
15+
metrics.example.com/port: "9090"
16+
17+
executor:
18+
frontendUrl: https://sourcegraph.example.com
19+
frontendExistingSecret: example-secret
20+
queueName: batches
21+
labels:
22+
example.com/team: executors
23+
podLabels:
24+
example.com/team: executors
25+
podAnnotations:
26+
metrics.example.com/port: "6060"
27+
28+
privateDockerRegistry:
29+
labels:
30+
example.com/team: registry
31+
podLabels:
32+
example.com/team: registry
33+
podAnnotations:
34+
metrics.example.com/port: "6060"

‎charts/sourcegraph-executor/k8s/templates/_helpers.tpl‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
2727
{{- end }}
2828
{{- end }}
2929

30+
{{/*
31+
Merges a list of maps, such as the labels or annotations set globally and per
32+
component, so each key is rendered once. A later map wins when a key is set in
33+
more than one. Kustomize rejects a key rendered twice in one map. Renders
34+
nothing when every map is empty.
35+
36+
{{ include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.frontend.podLabels) }}
37+
*/}}
38+
{{- define "sourcegraph.mergedMaps" -}}
39+
{{- $merged := dict -}}
40+
{{- range . -}}
41+
{{- range $key, $value := . -}}
42+
{{- $_ := set $merged $key $value -}}
43+
{{- end -}}
44+
{{- end -}}
45+
{{- with $merged -}}
46+
{{- toYaml . -}}
47+
{{- end -}}
48+
{{- end }}
49+
3050
{{/*
3151
Selector labels
3252
*/}}

‎charts/sourcegraph-executor/k8s/templates/executor.Deployment.yaml‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,10 @@ spec:
3030
metadata:
3131
annotations:
3232
kubectl.kubernetes.io/default-container: executor
33-
{{- if .Values.sourcegraph.podAnnotations }}
34-
{{- toYaml .Values.sourcegraph.podAnnotations | nindent 8 }}
35-
{{- end }}
36-
{{- if .Values.executor.podAnnotations }}
37-
{{- toYaml .Values.executor.podAnnotations | nindent 8 }}
38-
{{- end }}
33+
{{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podAnnotations .Values.executor.podAnnotations) | nindent 8 }}
3934
labels:
4035
{{- include "sourcegraph.selectorLabels" . | nindent 8 }}
41-
{{- if .Values.sourcegraph.podLabels }}
42-
{{- toYaml .Values.sourcegraph.podLabels | nindent 8 }}
43-
{{- end }}
44-
{{- if .Values.executor.podLabels }}
45-
{{- toYaml .Values.executor.podLabels | nindent 8 }}
46-
{{- end }}
36+
{{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.executor.podLabels) | nindent 8 }}
4737
{{- include "executor.labels" . | nindent 8 }}
4838
spec:
4939
securityContext:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Used by CI: the "Render charts through kustomize" GitHub Actions job
2+
# (.github/workflows/kustomize_render.yml) renders this chart with this file
3+
# through scripts/ci/kustomize-render.sh.
4+
#
5+
# The same label and annotation keys at the global and per-component levels,
6+
# where the per-component value should win. Without deduplication, the chart
7+
# renders the key twice in one map, which kustomize rejects.
8+
9+
sourcegraph:
10+
labels:
11+
example.com/team: platform
12+
podLabels:
13+
example.com/team: platform
14+
podAnnotations:
15+
metrics.example.com/port: "9090"
16+
17+
executor:
18+
frontendUrl: https://sourcegraph.example.com
19+
frontendExistingSecret: example-secret
20+
queueName: batches
21+
labels:
22+
example.com/team: executors
23+
podLabels:
24+
example.com/team: executors
25+
podAnnotations:
26+
metrics.example.com/port: "6060"

‎charts/sourcegraph-migrator/templates/_helpers.tpl‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
2727
{{- end }}
2828
{{- end }}
2929

30+
{{/*
31+
Merges a list of maps, such as the labels or annotations set globally and per
32+
component, so each key is rendered once. A later map wins when a key is set in
33+
more than one. Kustomize rejects a key rendered twice in one map. Renders
34+
nothing when every map is empty.
35+
36+
{{ include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.frontend.podLabels) }}
37+
*/}}
38+
{{- define "sourcegraph.mergedMaps" -}}
39+
{{- $merged := dict -}}
40+
{{- range . -}}
41+
{{- range $key, $value := . -}}
42+
{{- $_ := set $merged $key $value -}}
43+
{{- end -}}
44+
{{- end -}}
45+
{{- with $merged -}}
46+
{{- toYaml . -}}
47+
{{- end -}}
48+
{{- end }}
49+
3050
{{/*
3151
Selector labels
3252
*/}}

‎charts/sourcegraph-migrator/templates/migrator/sourcegraph-migrator.Job.yaml‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ metadata:
44
annotations:
55
description: Performs Sourcegraph database migrations.
66
labels:
7-
{{- include "sourcegraph.labels" . | nindent 4 }}
8-
{{- if .Values.migrator.labels }}
9-
{{- toYaml .Values.migrator.labels | nindent 4 }}
10-
{{- end }}
7+
{{- include "sourcegraph.mergedMaps" (list (include "sourcegraph.labels" . | fromYaml) .Values.migrator.labels) | nindent 4 }}
118
deploy: sourcegraph
129
app.kubernetes.io/component: migrator
1310
name: {{ default "migrator" .Values.migrator.name }}-{{ randAlphaNum 5 | lower }}
@@ -17,20 +14,10 @@ spec:
1714
metadata:
1815
annotations:
1916
kubectl.kubernetes.io/default-container: migrator
20-
{{- if .Values.sourcegraph.podAnnotations }}
21-
{{- toYaml .Values.sourcegraph.podAnnotations | nindent 8 }}
22-
{{- end }}
23-
{{- if .Values.migrator.podAnnotations }}
24-
{{- toYaml .Values.migrator.podAnnotations | nindent 8 }}
25-
{{- end }}
17+
{{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podAnnotations .Values.migrator.podAnnotations) | nindent 8 }}
2618
labels:
2719
{{- include "sourcegraph.selectorLabels" . | nindent 8 }}
28-
{{- if .Values.sourcegraph.podLabels }}
29-
{{- toYaml .Values.sourcegraph.podLabels | nindent 8 }}
30-
{{- end }}
31-
{{- if .Values.migrator.podLabels }}
32-
{{- toYaml .Values.migrator.podLabels | nindent 8 }}
33-
{{- end }}
20+
{{- include "sourcegraph.mergedMaps" (list .Values.sourcegraph.podLabels .Values.migrator.podLabels) | nindent 8 }}
3421
job: migrator
3522
deploy: sourcegraph
3623
spec:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Used by CI: the "Render charts through kustomize" GitHub Actions job
2+
# (.github/workflows/kustomize_render.yml) renders this chart with this file
3+
# through scripts/ci/kustomize-render.sh.
4+
#
5+
# The same label and annotation keys at the global and per-component levels,
6+
# where the per-component value should win. Without deduplication, the chart
7+
# renders the key twice in one map, which kustomize rejects.
8+
9+
sourcegraph:
10+
labels:
11+
example.com/team: platform
12+
podLabels:
13+
example.com/team: platform
14+
podAnnotations:
15+
metrics.example.com/port: "9090"
16+
17+
migrator:
18+
labels:
19+
example.com/team: database
20+
podLabels:
21+
example.com/team: database
22+
podAnnotations:
23+
metrics.example.com/port: "6060"

0 commit comments

Comments
 (0)