Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/sourcegraph-executor/dind/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ In addition to the documented values, the `executor` and `private-docker-registr
| sourcegraph.image.repository | string | Sourcegraph's public Docker Hub registry | Global docker image registry or prefix |
| sourcegraph.image.useGlobalTagAsDefault | bool | `false` | When set to true, sourcegraph.image.defaultTag is used as the default defaultTag for all services, instead of service-specific default defaultTags |
| sourcegraph.imagePullSecrets | list | `[]` | Mount named secrets containing docker credentials |
| sourcegraph.labels | object | `{}` | Add a global label to all resources |
| sourcegraph.labels | object | `{}` | Add a global label to every resource the chart creates. The chart does not add it to pod templates (use `sourcegraph.podLabels`), or to the volume claim template of the `private-docker-registry` StatefulSet, which Kubernetes does not allow changing |
| sourcegraph.nameOverride | string | `""` | Set a custom name for the app.kubernetes.io/name annotation |
| sourcegraph.nodeSelector | object | `{}` | NodeSelector, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#nodeselector) |
| sourcegraph.podAnnotations | object | `{}` | Add extra annotations to attach to all pods |
Expand Down
50 changes: 23 additions & 27 deletions charts/sourcegraph-executor/dind/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ 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 }}
app: {{ .name }}
deploy: sourcegraph
sourcegraph-resource-requires: no-cluster-admin
app.kubernetes.io/component: executor
{{- include "sourcegraph.uniqueKeys" (list
(include "sourcegraph.labels" $r)
$r.Values.executor.labels
(printf "app: %s" .name)
"deploy: sourcegraph"
"sourcegraph-resource-requires: no-cluster-admin"
"app.kubernetes.io/component: executor"
) | nindent 4 }}
spec:
selector:
matchLabels:
Expand All @@ -160,26 +160,22 @@ spec:
template:
metadata:
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.uniqueKeys" (list
"kubectl.kubernetes.io/default-container: executor"
(printf "checksum/docker-config: %s" (include "dind.daemonConfig" $r | sha256sum))
$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 }}
app: {{ .name }}
deploy: sourcegraph
sourcegraph-resource-requires: no-cluster-admin
app.kubernetes.io/component: executor
{{- include "sourcegraph.uniqueKeys" (list
(include "sourcegraph.selectorLabels" $r)
$r.Values.sourcegraph.podLabels
$r.Values.executor.podLabels
(printf "app: %s" .name)
"deploy: sourcegraph"
"sourcegraph-resource-requires: no-cluster-admin"
"app.kubernetes.io/component: executor"
) | nindent 8 }}
spec:
{{- with $r.Values.executor.podSecurityContext }}
securityContext:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{{/*

Render label or annotation maps as one map, with each key once.

Strict YAML parsers, such as Kustomize, reject a map that repeats a key. Helm
and Kubernetes accept it and keep the last value. This keeps the last value
too, so the objects of an installed release do not change.

Pass a list of maps, lowest precedence first. Each item is a map from the
values, or the text of one or more `key: value` lines. The lines are passed
through as written, so when no key repeats, the output is the same text as
rendering the items one after the other.

labels:
{{- include "sourcegraph.uniqueKeys" (list
(include "sourcegraph.labels" .)
.Values.sourcegraph.podLabels
"deploy: sourcegraph"
) | nindent 2 }}

*/}}

{{- define "sourcegraph.uniqueKeys" -}}
{{- /* Split every item into entries: one `key: value`, with any lines of a multi-line value. */ -}}
{{- $entries := list -}}
{{- range $source := . -}}
{{- $text := "" -}}
{{- if and $source (kindIs "map" $source) -}}
{{- $text = toYaml $source -}}
{{- else if kindIs "string" $source -}}
{{- $text = trim $source -}}
{{- end -}}
{{- if $text -}}
{{- range $line := splitList "\n" $text -}}
{{- if and $entries (or (not $line) (hasPrefix " " $line)) -}}
{{- $entries = append (initial $entries) (printf "%s\n%s" (last $entries) $line) -}}
{{- else -}}
{{- $entries = append $entries $line -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- /* Keep the last entry of each key. */ -}}
{{- $seen := dict -}}
{{- $unique := list -}}
{{- range $entry := reverse $entries -}}
{{- $key := splitList ":" $entry | first | trimAll "\"'" -}}
{{- if not (hasKey $seen $key) -}}
{{- $_ := set $seen $key true -}}
{{- $unique = prepend $unique $entry -}}
{{- end -}}
{{- end -}}
{{- join "\n" $unique -}}
{{- end -}}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ apiVersion: v1
kind: ConfigMap
metadata:
labels:
app: executor
deploy: sourcegraph
app.kubernetes.io/component: executor
{{- include "sourcegraph.uniqueKeys" (list
.Values.sourcegraph.labels
"app: executor"
"deploy: sourcegraph"
"app.kubernetes.io/component: executor"
) | nindent 4 }}
name: docker-config
data:
daemon.json: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ metadata:
{{- toYaml .Values.privateDockerRegistry.serviceAnnotations | nindent 4 }}
{{- end }}
labels:
app: private-docker-registry
deploy: sourcegraph
app.kubernetes.io/component: private-docker-registry
{{- include "sourcegraph.uniqueKeys" (list
.Values.sourcegraph.labels
"app: private-docker-registry"
"deploy: sourcegraph"
"app.kubernetes.io/component: private-docker-registry"
) | nindent 4 }}
name: private-docker-registry
spec:
ports:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ kind: StatefulSet
metadata:
name: private-docker-registry
labels:
{{- include "sourcegraph.labels" . | nindent 4 }}
{{- if .Values.privateDockerRegistry.labels }}
{{- toYaml .Values.privateDockerRegistry.labels | nindent 4 }}
{{- end }}
deploy: sourcegraph
app.kubernetes.io/component: private-docker-registry
{{- include "sourcegraph.uniqueKeys" (list
(include "sourcegraph.labels" .)
.Values.privateDockerRegistry.labels
"deploy: sourcegraph"
"app.kubernetes.io/component: private-docker-registry"
) | nindent 4 }}
spec:
replicas: 1
serviceName: private-docker-registry
Expand All @@ -22,23 +22,19 @@ spec:
template:
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.uniqueKeys" (list
"kubectl.kubernetes.io/default-container: private-docker-registry"
.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 }}
deploy: sourcegraph
app: private-docker-registry
{{- include "sourcegraph.uniqueKeys" (list
(include "sourcegraph.selectorLabels" .)
.Values.sourcegraph.podLabels
.Values.privateDockerRegistry.podLabels
"deploy: sourcegraph"
"app: private-docker-registry"
) | nindent 8 }}
spec:
containers:
- name: private-docker-registry
Expand Down
80 changes: 80 additions & 0 deletions charts/sourcegraph-executor/dind/tests/labels_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# sourcegraph.labels is documented as a label for every resource the chart
# creates. The first suite asserts on every template, so a new template that
# leaves the labels out fails here.
suite: sourcegraph.labels on every resource
templates:
- "*/*.yaml"
set:
queues:
- name: batches
- name: codeintel
sourcegraph:
labels:
example.com/cost-center: "1234"
tests:
- it: should add the labels to every resource
asserts:
- equal:
path: metadata.labels["example.com/cost-center"]
value: "1234"

- it: should not add the labels to pod templates and the volume claim template
template: private-docker-registry/private-docker-registry.Statefulset.yaml
asserts:
- notExists:
path: spec.template.metadata.labels["example.com/cost-center"]
- notExists:
path: spec.volumeClaimTemplates[0].metadata.labels["example.com/cost-center"]
---
# helm-unittest fails a test when any rendered manifest repeats a key, as
# Kustomize does.
suite: unique keys
set:
executor:
queueName: test
tests:
- it: should render a key set in several values once
set:
sourcegraph:
labels: &labels
example.com/team: platform
app: from-values
deploy: from-values
helm.sh/chart: from-values
podLabels: &podLabels
example.com/team: platform
app: from-values
deploy: from-values
podAnnotations: &annotations
example.com/owner: platform
kubectl.kubernetes.io/default-container: from-values
executor: &component
labels:
<<: *labels
example.com/team: component
podLabels:
<<: *podLabels
example.com/team: component
podAnnotations:
<<: *annotations
example.com/owner: component
privateDockerRegistry: *component
templates:
- executor/executor.Deployment.yaml
- private-docker-registry/private-docker-registry.Statefulset.yaml
asserts:
- equal:
path: metadata.labels["example.com/team"]
value: component
- equal:
path: metadata.labels.deploy
value: sourcegraph
- equal:
path: spec.template.metadata.labels["example.com/team"]
value: component
- equal:
path: spec.template.metadata.labels.deploy
value: sourcegraph
- equal:
path: spec.template.metadata.annotations["example.com/owner"]
value: component
4 changes: 3 additions & 1 deletion charts/sourcegraph-executor/dind/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ sourcegraph:
useGlobalTagAsDefault: false
# -- Mount named secrets containing docker credentials
imagePullSecrets: []
# -- Add a global label to all resources
# -- Add a global label to every resource the chart creates. The chart does not add it to pod templates
# (use `sourcegraph.podLabels`), or to the volume claim template of the `private-docker-registry` StatefulSet,
# which Kubernetes does not allow changing
labels: {}
# -- Set a custom name for the app.kubernetes.io/name annotation
nameOverride: ""
Expand Down
54 changes: 54 additions & 0 deletions charts/sourcegraph-executor/k8s/templates/_helpers/_uniqueKeys.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{{/*

Render label or annotation maps as one map, with each key once.

Strict YAML parsers, such as Kustomize, reject a map that repeats a key. Helm
and Kubernetes accept it and keep the last value. This keeps the last value
too, so the objects of an installed release do not change.

Pass a list of maps, lowest precedence first. Each item is a map from the
values, or the text of one or more `key: value` lines. The lines are passed
through as written, so when no key repeats, the output is the same text as
rendering the items one after the other.

labels:
{{- include "sourcegraph.uniqueKeys" (list
(include "sourcegraph.labels" .)
.Values.sourcegraph.podLabels
"deploy: sourcegraph"
) | nindent 2 }}

*/}}

{{- define "sourcegraph.uniqueKeys" -}}
{{- /* Split every item into entries: one `key: value`, with any lines of a multi-line value. */ -}}
{{- $entries := list -}}
{{- range $source := . -}}
{{- $text := "" -}}
{{- if and $source (kindIs "map" $source) -}}
{{- $text = toYaml $source -}}
{{- else if kindIs "string" $source -}}
{{- $text = trim $source -}}
{{- end -}}
{{- if $text -}}
{{- range $line := splitList "\n" $text -}}
{{- if and $entries (or (not $line) (hasPrefix " " $line)) -}}
{{- $entries = append (initial $entries) (printf "%s\n%s" (last $entries) $line) -}}
{{- else -}}
{{- $entries = append $entries $line -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- /* Keep the last entry of each key. */ -}}
{{- $seen := dict -}}
{{- $unique := list -}}
{{- range $entry := reverse $entries -}}
{{- $key := splitList ":" $entry | first | trimAll "\"'" -}}
{{- if not (hasKey $seen $key) -}}
{{- $_ := set $seen $key true -}}
{{- $unique = prepend $unique $entry -}}
{{- end -}}
{{- end -}}
{{- join "\n" $unique -}}
{{- end -}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ kind: ConfigMap
metadata:
name: {{ include "executor.name" . }}
labels:
{{- include "executor.labels" . | nindent 4 }}
{{- include "sourcegraph.uniqueKeys" (list
.Values.sourcegraph.labels
(include "executor.labels" .)
) | nindent 4 }}
data:
EXECUTOR_USE_FIRECRACKER: "false"
EXECUTOR_FRONTEND_URL: "{{ .Values.executor.frontendUrl }}"
Expand Down
Loading
Loading