Skip to content

Commit 8d667bc

Browse files
Create global container and pod security context configs
Add .Values.sourcegraph.containerSecurityContext and .Values.sourcegraph.podSecurityContext so customers (e.g. OpenShift) can set runAsUser / runAsGroup / fsGroup once for every container and pod. Rename each component's shipped defaults to defaultContainerSecurityContext / defaultPodSecurityContext and add empty containerSecurityContext / podSecurityContext override keys. Templates render the 3-tier merge (component default -> global -> component override) through shared helpers in templates/_helpers/_securityContext.tpl, copied into all four charts. - precise-code-intel-worker derives fsGroup from the merged container runAsGroup - cadvisor gates /dev/kmsg on the merged privileged value - k8s executor keeps the deprecated executor.securityContext pod fallback - dind executor keeps dind.gVisor.securityContext as a full replacement - otel-agent and otel-collector pods gain pod security context keys Amp-Thread-ID: https://ampcode.com/threads/T-01a18eb0-b6fb-7675-8f04-2fc05d2fe567 Co-authored-by: Amp <amp@ampcode.com>
1 parent a6d7ab7 commit 8d667bc

45 files changed

Lines changed: 976 additions & 524 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎charts/sourcegraph-executor/dind/README.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,19 @@ In addition to the documented values, the `executor` and `private-docker-registr
5959
| Key | Type | Default | Description |
6060
|-----|------|---------|-------------|
6161
| dind.command | list | `["dockerd"]` | Command for the dind container. |
62+
| dind.containerSecurityContext | object | `{}` | Override container security context for the dind container. Ignored when gVisor is enabled. |
6263
| dind.daemonConfig | object | `{"hosts":["tcp://127.0.0.1:2375"],"insecure-registries":["private-docker-registry:5000"],"mtu":1200,"registry-mirrors":["http://private-docker-registry:5000"],"tls":false}` | Docker daemon configuration passed as daemon.json to the dind sidecar. Learn more from: https://docs.docker.com/reference/cli/dockerd/#on-linux |
64+
| dind.defaultContainerSecurityContext | object | `{"privileged":true}` | Default container security context for the dind container. Ignored when gVisor is enabled. |
6365
| dind.gVisor.command | list | `["/bin/sh","-c","ip link del docker0 2>/dev/null || true\necho 1 > /proc/sys/net/ipv4/ip_forward\ndev=$(ip route show default | awk '{for(i=1;i<=NF;i++) if($i==\"dev\"){print $(i+1); exit}}')\naddr=$(ip addr show dev \"$dev\" | awk '/inet /{gsub(/\\/.*/, \"\", $2); print $2; exit}')\niptables-legacy -t nat -A POSTROUTING -o \"$dev\" -j SNAT --to-source \"$addr\" -p tcp || true\niptables-legacy -t nat -A POSTROUTING -o \"$dev\" -j SNAT --to-source \"$addr\" -p udp || true\nexec dockerd\n"]` | Command for the dind container when gVisor is enabled. Overrides dind.command. Prepares the network environment that gVisor does not initialise automatically before handing off to dockerd. |
6466
| dind.gVisor.daemonConfig | object | `{"features":{"containerd-snapshotter":false},"ip6tables":false,"iptables":false,"storage-driver":"vfs"}` | Extra daemon.json settings merged into dind.daemonConfig when gVisor is enabled. These defaults configure Docker to work within gVisor's kernel constraints. |
6567
| dind.gVisor.enabled | bool | `false` | Enable gVisor sandbox (GKE only). Requires the GKE node pool to have sandbox type set to gvisor. When enabled, sets runtimeClassName: gvisor on executor pods and replaces privileged: true with explicit capabilities — these are intercepted in-sandbox and never granted to the host kernel. See: https://gvisor.dev/docs/tutorials/docker-in-gvisor/ |
6668
| dind.gVisor.securityContext | object | `{"capabilities":{"add":["NET_ADMIN","SYS_ADMIN","AUDIT_WRITE","CHOWN","DAC_OVERRIDE","FOWNER","FSETID","KILL","MKNOD","NET_BIND_SERVICE","NET_RAW","SETFCAP","SETGID","SETPCAP","SETUID","SYS_CHROOT","SYS_PTRACE"]}}` | securityContext for the dind container when gVisor is enabled. Replaces privileged: true — gVisor intercepts these capabilities in-sandbox and never grants them to the host kernel. |
6769
| dind.image.registry | string | `"index.docker.io"` | |
6870
| dind.image.repository | string | `"docker"` | |
6971
| dind.image.tag | string | `"29.5.3-dind"` | |
72+
| executor.containerSecurityContext | object | `{}` | Override container security context for the executor container |
73+
| executor.defaultContainerSecurityContext | object | `{}` | Default container security context for the executor container |
74+
| executor.defaultPodSecurityContext | object | `{"fsGroup":101}` | Default pod security context for executor pods. fsGroup: 101 ensures the scratch volume is writable by the sourcegraph user (uid=100, gid=101) when running in Docker mode (dind). Set to {} to disable. |
7075
| executor.env | object | `{}` | Extra environment variables to set on the executor container. |
7176
| executor.frontendExistingSecret | string | `""` | Name of existing k8s Secret to use for frontend password. The k8s Secret must contain the key EXECUTOR_FRONTEND_PASSWORD matching the site config executors.accessToken value. frontendPassword is ignored if this is set. |
7277
| executor.frontendPassword | string | `""` | The shared secret configured in the Sourcegraph instance site config under executors.accessToken. Required if frontendExistingSecret is not configured. |
@@ -77,7 +82,7 @@ In addition to the documented values, the `executor` and `private-docker-registr
7782
| executor.log.level | string | `"warn"` | Possible values are dbug, info, warn, eror, crit. |
7883
| executor.maximumNumJobs | int | `10` | The maximum amount of jobs that can be executed concurrently. |
7984
| executor.maximumRuntimePerJob | string | `"30m"` | The maximum wall time that can be spent on a single job. |
80-
| executor.podSecurityContext | object | `{"fsGroup":101}` | Pod-level security context applied to executor pods. fsGroup: 101 ensures the scratch volume is writable by the sourcegraph user (uid=100, gid=101) when running in Docker mode (dind). Set to {} to disable. |
85+
| executor.podSecurityContext | object | `{}` | Override pod security context for executor pods |
8186
| executor.queueName | string | `""` | The name of the queue to pull jobs from. Possible values: batches and codeintel. Either this or queueNames is required (when not using queues). |
8287
| executor.queueNames | list | `[]` | The names of multiple queues to pull jobs from. Possible values: batches and codeintel. Either this or queueName is required (when not using queues). |
8388
| executor.replicaCount | int | `1` | |
@@ -93,6 +98,7 @@ In addition to the documented values, the `executor` and `private-docker-registr
9398
| privateDockerRegistry.storageSize | string | `"10Gi"` | |
9499
| queues | list | `[]` | Optional list of queues to deploy as standalone Deployments. When set, the single executor Deployment is not rendered. Each entry supports: name (required) — used as the deployment name suffix (executor-<name>) queueName — sets EXECUTOR_QUEUE_NAME; defaults to name if omitted queueNames — sets EXECUTOR_QUEUE_NAMES (comma-joined); takes precedence over queueName when set replicaCount, resources, env (merged with executor.env, queue overrides) |
95100
| sourcegraph.affinity | object | `{}` | Affinity, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity) |
101+
| sourcegraph.containerSecurityContext | object | `{}` | Global container security context override applied to all containers. Merges with component defaults; component-specific overrides take precedence. |
96102
| sourcegraph.image.defaultTag | string | `"{{ .Chart.AppVersion }}"` | Global docker image tag |
97103
| sourcegraph.image.pullPolicy | string | `"IfNotPresent"` | Global docker image pull policy |
98104
| sourcegraph.image.repository | string | Sourcegraph's public Docker Hub registry | Global docker image registry or prefix |
@@ -103,6 +109,7 @@ In addition to the documented values, the `executor` and `private-docker-registr
103109
| sourcegraph.nodeSelector | object | `{}` | NodeSelector, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#nodeselector) |
104110
| sourcegraph.podAnnotations | object | `{}` | Add extra annotations to attach to all pods |
105111
| sourcegraph.podLabels | object | `{}` | Add extra labels to attach to all pods |
112+
| sourcegraph.podSecurityContext | object | `{}` | Global pod security context override applied to all pods. Merges with component defaults; component-specific overrides take precedence. |
106113
| sourcegraph.priorityClassName | string | `""` | Assign a priorityClass to all pods (daemonSets, deployments, and statefulSets) |
107114
| sourcegraph.tolerations | list | `[]` | Tolerations, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/) |
108115
| storageClass.allowedTopologies | object | `{}` | Persistent volumes topology configuration, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/concepts/storage/storage-classes/#allowed-topologies) |

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,12 @@ spec:
181181
sourcegraph-resource-requires: no-cluster-admin
182182
app.kubernetes.io/component: executor
183183
spec:
184-
{{- with $r.Values.executor.podSecurityContext }}
185-
securityContext:
186-
{{- toYaml . | nindent 8 }}
187-
{{- end }}
184+
{{- include "sourcegraph.podSecurityContext" (list $r "executor" 6) }}
188185
containers:
189186
- name: executor
190187
image: {{ include "sourcegraph.image" (list $r "executor") }}
191188
imagePullPolicy: {{ $r.Values.sourcegraph.image.pullPolicy }}
189+
{{- include "sourcegraph.containerSecurityContext" (list $r "executor" 10) }}
192190
livenessProbe:
193191
httpGet:
194192
path: /healthz
@@ -260,12 +258,12 @@ spec:
260258
- name: dind
261259
image: "{{ $r.Values.dind.image.registry }}/{{ $r.Values.dind.image.repository }}:{{ $r.Values.dind.image.tag }}"
262260
imagePullPolicy: {{ $r.Values.sourcegraph.image.pullPolicy }}
261+
{{- if $r.Values.dind.gVisor.enabled }}
263262
securityContext:
264-
{{- if $r.Values.dind.gVisor.enabled }}
265263
{{- toYaml $r.Values.dind.gVisor.securityContext | nindent 12 }}
266-
{{- else }}
267-
privileged: true
268-
{{- end }}
264+
{{- else }}
265+
{{- include "sourcegraph.containerSecurityContext" (list $r "dind" 10) }}
266+
{{- end }}
269267
command:
270268
{{- if $r.Values.dind.gVisor.enabled }}
271269
{{- toYaml $r.Values.dind.gVisor.command | nindent 12 }}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{{/*
2+
3+
Security context helpers for container and pod security contexts.
4+
5+
These helpers implement a 3-tier merge precedence:
6+
1. Component default (e.g., .Values.frontend.defaultContainerSecurityContext)
7+
2. Global override (e.g., .Values.sourcegraph.containerSecurityContext)
8+
3. Component override (e.g., .Values.frontend.containerSecurityContext)
9+
10+
Later values override earlier ones, allowing customers to:
11+
- Set global security context settings that apply to all components
12+
- Override specific components as needed
13+
- Retain Sourcegraph's secure defaults when no overrides are specified
14+
15+
Supports nested paths like "openTelemetry" "gateway" or "codeInsightsDB" "init".
16+
17+
*/}}
18+
19+
{{/*
20+
Internal: walk the component path and merge default, global, and component
21+
override security contexts. Returns the merged map as YAML.
22+
23+
Parameters:
24+
- $ (root context)
25+
- default key, e.g. "defaultContainerSecurityContext"
26+
- override key, e.g. "containerSecurityContext"; also the global key under .Values.sourcegraph
27+
- component path segments (one or more strings)
28+
*/}}
29+
{{- define "sourcegraph.mergeSecurityContext" -}}
30+
{{- $root := index . 0 -}}
31+
{{- $defaultKey := index . 1 -}}
32+
{{- $overrideKey := index . 2 -}}
33+
{{- $component := $root.Values -}}
34+
{{- range slice . 3 -}}
35+
{{- $component = index $component . | default dict -}}
36+
{{- end -}}
37+
{{- $default := index $component $defaultKey | default dict -}}
38+
{{- $global := index $root.Values.sourcegraph $overrideKey | default dict -}}
39+
{{- $override := index $component $overrideKey | default dict -}}
40+
{{- mustMergeOverwrite (deepCopy $default) $global $override | toYaml -}}
41+
{{- end -}}
42+
43+
{{/*
44+
Merged container security context as YAML. Pipe through fromYaml to read fields.
45+
46+
Usage:
47+
{{- $merged := include "sourcegraph.mergedContainerSecurityContext" (list . "frontend") | fromYaml }}
48+
49+
Parameters:
50+
- $ (root context)
51+
- component path segments (one or more strings)
52+
*/}}
53+
{{- define "sourcegraph.mergedContainerSecurityContext" -}}
54+
{{- include "sourcegraph.mergeSecurityContext" (concat (list (index . 0) "defaultContainerSecurityContext" "containerSecurityContext") (slice . 1)) -}}
55+
{{- end -}}
56+
57+
{{/*
58+
Merged pod security context as YAML. Pipe through fromYaml to read fields.
59+
60+
Usage:
61+
{{- $merged := include "sourcegraph.mergedPodSecurityContext" (list . "frontend") | fromYaml }}
62+
63+
Parameters:
64+
- $ (root context)
65+
- component path segments (one or more strings)
66+
*/}}
67+
{{- define "sourcegraph.mergedPodSecurityContext" -}}
68+
{{- include "sourcegraph.mergeSecurityContext" (concat (list (index . 0) "defaultPodSecurityContext" "podSecurityContext") (slice . 1)) -}}
69+
{{- end -}}
70+
71+
{{/*
72+
Internal: render a map as a "securityContext:" block, or nothing if empty.
73+
The output includes a leading newline for proper YAML formatting.
74+
75+
Parameters:
76+
- merged security context map
77+
- indent level (integer)
78+
*/}}
79+
{{- define "sourcegraph.renderSecurityContext" -}}
80+
{{- $merged := index . 0 -}}
81+
{{- $indent := int (index . 1) -}}
82+
{{- if $merged }}
83+
{{ "securityContext:" | indent $indent }}
84+
{{ toYaml $merged | indent (int (add $indent 2)) -}}
85+
{{- end -}}
86+
{{- end -}}
87+
88+
{{/*
89+
Container security context with 3-tier merge, rendered as a "securityContext:" block.
90+
91+
Usage:
92+
{{- include "sourcegraph.containerSecurityContext" (list . "frontend" 8) }}
93+
94+
Parameters:
95+
- $ (root context)
96+
- component path segments (one or more strings)
97+
- indent level (integer) as the last parameter
98+
*/}}
99+
{{- define "sourcegraph.containerSecurityContext" -}}
100+
{{- $last := sub (len .) 1 -}}
101+
{{- $merged := include "sourcegraph.mergedContainerSecurityContext" (slice . 0 $last) | fromYaml -}}
102+
{{- include "sourcegraph.renderSecurityContext" (list $merged (index . $last)) -}}
103+
{{- end -}}
104+
105+
{{/*
106+
Pod security context with 3-tier merge, rendered as a "securityContext:" block.
107+
108+
Usage:
109+
{{- include "sourcegraph.podSecurityContext" (list . "frontend" 6) }}
110+
111+
Parameters:
112+
- $ (root context)
113+
- component path segments (one or more strings)
114+
- indent level (integer) as the last parameter
115+
*/}}
116+
{{- define "sourcegraph.podSecurityContext" -}}
117+
{{- $last := sub (len .) 1 -}}
118+
{{- $merged := include "sourcegraph.mergedPodSecurityContext" (slice . 0 $last) | fromYaml -}}
119+
{{- include "sourcegraph.renderSecurityContext" (list $merged (index . $last)) -}}
120+
{{- end -}}

‎charts/sourcegraph-executor/dind/values.yaml‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ sourcegraph:
3333
podLabels: {}
3434
# -- Assign a priorityClass to all pods (daemonSets, deployments, and statefulSets)
3535
priorityClassName: ""
36+
# -- Global container security context override applied to all containers.
37+
# Merges with component defaults; component-specific overrides take precedence.
38+
containerSecurityContext: {}
39+
# -- Global pod security context override applied to all pods.
40+
# Merges with component defaults; component-specific overrides take precedence.
41+
podSecurityContext: {}
3642

3743

3844
storageClass:
@@ -131,17 +137,28 @@ executor:
131137
# -- StorageClass for the ephemeral volume. Only used when type is ephemeral.
132138
# Defaults to the cluster default StorageClass when empty.
133139
class: ""
134-
# -- Pod-level security context applied to executor pods.
140+
# -- Default pod security context for executor pods.
135141
# fsGroup: 101 ensures the scratch volume is writable by the sourcegraph user (uid=100, gid=101)
136142
# when running in Docker mode (dind). Set to {} to disable.
137-
podSecurityContext:
143+
defaultPodSecurityContext:
138144
fsGroup: 101
145+
# -- Override pod security context for executor pods
146+
podSecurityContext: {}
147+
# -- Default container security context for the executor container
148+
defaultContainerSecurityContext: {}
149+
# -- Override container security context for the executor container
150+
containerSecurityContext: {}
139151

140152
dind:
141153
image:
142154
registry: index.docker.io
143155
repository: docker
144156
tag: 29.5.3-dind
157+
# -- Default container security context for the dind container. Ignored when gVisor is enabled.
158+
defaultContainerSecurityContext:
159+
privileged: true
160+
# -- Override container security context for the dind container. Ignored when gVisor is enabled.
161+
containerSecurityContext: {}
145162
# -- Command for the dind container.
146163
command:
147164
- dockerd

0 commit comments

Comments
 (0)