From b7c3c020a1cb448ce5e40e006d40fcacd51870ef Mon Sep 17 00:00:00 2001 From: Keegan Smith Date: Tue, 10 Nov 2026 15:36:16 +0000 Subject: [PATCH] sourcegraph: run DB permission init containers on the DB images The correct-data-dir-permissions init containers on pgsql, codeintel-db and codeinsights-db used sourcegraph/alpine-3.14, which is a frozen 2023 build of an Alpine release that went end-of-life in May 2023 and gets flagged by image scanners. The init container only needs sh and chmod, which the database images already ship, so run it on the same image as the database container. That drops an image from the chart entirely and keeps the init container patched with each release. alpine.containerSecurityContext and alpine.resources keep their names so existing overrides, such as the OpenShift example, still apply. Amp-Thread-ID: https://ampcode.com/threads/T-01a1c5fe-5015-76c2-bf19-d42868d2b979 Co-authored-by: Amp --- charts/sourcegraph/CHANGELOG.md | 1 + charts/sourcegraph/README.md | 8 ++-- .../private-image-registry/override.yaml | 9 ++--- .../codeinsights-db.StatefulSet.yaml | 2 +- .../codeintel-db.StatefulSet.yaml | 2 +- .../templates/pgsql/pgsql.StatefulSet.yaml | 2 +- .../dataDirPermissionsInitContainer_test.yaml | 38 +++++++++++++++++++ charts/sourcegraph/values.yaml | 14 ++----- 8 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 charts/sourcegraph/tests/dataDirPermissionsInitContainer_test.yaml diff --git a/charts/sourcegraph/CHANGELOG.md b/charts/sourcegraph/CHANGELOG.md index 9f8ad6c40..16aa7cf50 100644 --- a/charts/sourcegraph/CHANGELOG.md +++ b/charts/sourcegraph/CHANGELOG.md @@ -8,6 +8,7 @@ Use `**BREAKING**:` to denote a breaking change ## Unreleased +- 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 `.extraInitContainers` to every workload, rendered before any init containers the chart defines, so e.g. a secrets-fetching init container on `frontend` runs before `migrator`. Use `restartPolicy: Always` for a native sidecar that must be started before the main container, such as one that writes a `grafana` datasource credential file read by `datasources.yml`. - Added `.extraServicePorts` to append ports to each component's primary Service (`sourcegraph-frontend`, `indexed-search`, and `worker`, not `sourcegraph-frontend-internal`, `indexed-search-indexer`, or `worker-executors`), e.g. to expose a sidecar's port - Removed the hardcoded `PRECISE_CODE_INTEL_UPLOAD_BACKEND=blobstore` and `PRECISE_CODE_INTEL_UPLOAD_AWS_ENDPOINT=http://blobstore:9000` environment variables from `frontend`, `worker`, `precise-code-intel-worker`, and `syntactic-code-intel-worker`. They matched Sourcegraph's built-in defaults, so default deployments are unchanged. `SOURCEGRAPH_UPLOAD_*` in `*.env` is now the only object storage configuration in the chart, as in the `external-object-storage` examples. Previously, with `blobstore.enabled: true`, the chart's values were rendered after `*.env` and overrode any `PRECISE_CODE_INTEL_UPLOAD_*` set there. Helm's three-way merge removes every duplicate of those names on this upgrade, including the `*.env` override, and the override is applied on the next `helm upgrade` after this one, when code-intel uploads switch to its bucket. Before that, make sure the bucket exists, or move the settings to `SOURCEGRAPH_UPLOAD_*`, or remove them. Run a second `helm upgrade` right away to make the switch at a known time. diff --git a/charts/sourcegraph/README.md b/charts/sourcegraph/README.md index 31e0d10fb..b4027e48d 100644 --- a/charts/sourcegraph/README.md +++ b/charts/sourcegraph/README.md @@ -89,10 +89,8 @@ In addition to the documented values, all services also support the following va | Key | Type | Default | Description | |-----|------|---------|-------------| -| alpine.containerSecurityContext | object | `{"allowPrivilegeEscalation":false,"readOnlyRootFilesystem":true,"runAsGroup":999,"runAsUser":999}` | Security context for the `alpine` initContainer, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-container) | -| alpine.image.defaultTag | string | `@sha256:`, pinned by each release | Docker image tag for the `alpine` image | -| alpine.image.name | string | `"alpine-3.14"` | Docker image name for the `alpine` image | -| alpine.resources | object | `{"limits":{"cpu":"10m","memory":"50Mi"},"requests":{"cpu":"10m","memory":"50Mi"}}` | Resource requests & limits for the `alpine` initContainer, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/) | +| alpine.containerSecurityContext | object | `{"allowPrivilegeEscalation":false,"readOnlyRootFilesystem":true,"runAsGroup":999,"runAsUser":999}` | Security context for the `correct-data-dir-permissions` initContainer of `pgsql` and `codeintel-db`, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-container) | +| alpine.resources | object | `{"limits":{"cpu":"10m","memory":"50Mi"},"requests":{"cpu":"10m","memory":"50Mi"}}` | Resource requests & limits for the `correct-data-dir-permissions` initContainer of `pgsql`, `codeintel-db`, and `codeinsights-db`, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/) | | blobstore.containerSecurityContext | object | `{"allowPrivilegeEscalation":false,"runAsGroup":101,"runAsUser":100}` | Security context for the `blobstore` container, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-container) | | blobstore.enabled | bool | `true` | Enable `blobstore` (S3 compatible storage) | | blobstore.env | object | the chart will add some default environment values | Environment variables for the `blobstore` container | @@ -128,7 +126,7 @@ In addition to the documented values, all services also support the following va | codeInsightsDB.existingConfig | string | `""` | Name of existing ConfigMap for `codeinsights-db`. It must contain a `postgresql.conf` key. | | codeInsightsDB.image.defaultTag | string | `@sha256:`, pinned by each release | Docker image tag for the `codeinsights-db` image | | codeInsightsDB.image.name | string | `"postgresql-16-codeinsights"` | Docker image name for the `codeinsights-db` image | -| codeInsightsDB.init.containerSecurityContext | object | `{"allowPrivilegeEscalation":false,"readOnlyRootFilesystem":true,"runAsGroup":70,"runAsUser":70}` | Security context for the `alpine` initContainer, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-container) | +| codeInsightsDB.init.containerSecurityContext | object | `{"allowPrivilegeEscalation":false,"readOnlyRootFilesystem":true,"runAsGroup":70,"runAsUser":70}` | Security context for the `correct-data-dir-permissions` initContainer, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-container) | | codeInsightsDB.name | string | `"codeinsights-db"` | Name used by resources. Does not affect service names or PVCs. | | codeInsightsDB.podSecurityContext | object | `{"fsGroup":70,"fsGroupChangePolicy":"OnRootMismatch","runAsUser":70}` | Security context for the `codeinsights-db` pod, learn more from the [Kubernetes documentation](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod) | | codeInsightsDB.postgresExporter | object | `{}` | Configuration for the `pgsql-exporter` sidecar container | diff --git a/charts/sourcegraph/examples/private-image-registry/override.yaml b/charts/sourcegraph/examples/private-image-registry/override.yaml index f6e91ca92..1c91907ec 100644 --- a/charts/sourcegraph/examples/private-image-registry/override.yaml +++ b/charts/sourcegraph/examples/private-image-registry/override.yaml @@ -7,10 +7,10 @@ # image: custom-repo.ecr.us-east-1.amazonaws.com/sourcegraph/frontend:3.35.1 # # Original image reference: -# image: index.docker.io/sourcegraph/alpine-3.12:3.35.1@sha256:a26a8820b8794c2d85c67f7b61b00b4d468af722047b8093235a827c08ea878a +# image: index.docker.io/sourcegraph/postgresql-16:3.35.1@sha256: # # Using override, with custom image name: -# image: custom-repo.ecr.us-east-1.amazonaws.com/sourcegraph/alpine:3.12 +# image: custom-repo.ecr.us-east-1.amazonaws.com/sourcegraph/sg-postgresql-16:3.35.1 sourcegraph: image: @@ -21,10 +21,9 @@ sourcegraph: imagePullSecrets: - name: regcred # Pre-existing secret that contains the credentials needed to pull from the image registry -alpine: +pgsql: image: - name: alpine # Use a custom name instead of the Sourcegraph name - i.e. alpine-3.12 - tag: 3.12 # Use a custom tag instead of the default - i.e. 3.35.1 + name: sg-postgresql-16 # Use a custom name instead of the Sourcegraph name - i.e. postgresql-16 # Allow Executors to reuse the same registry credentials to pull the needed images for Batch Changes and Auto-indexing executor: diff --git a/charts/sourcegraph/templates/codeinsights-db/codeinsights-db.StatefulSet.yaml b/charts/sourcegraph/templates/codeinsights-db/codeinsights-db.StatefulSet.yaml index b2868f32e..80af9ea2b 100644 --- a/charts/sourcegraph/templates/codeinsights-db/codeinsights-db.StatefulSet.yaml +++ b/charts/sourcegraph/templates/codeinsights-db/codeinsights-db.StatefulSet.yaml @@ -48,7 +48,7 @@ spec: {{- toYaml . | nindent 6 }} {{- end }} - name: correct-data-dir-permissions - image: {{ include "sourcegraph.image" (list . "alpine") }} + image: {{ include "sourcegraph.image" (list . "codeInsightsDB") }} imagePullPolicy: {{ .Values.sourcegraph.image.pullPolicy }} command: ["sh", "-c", "if [ -d /var/lib/postgresql/data/pgdata ]; then chmod 750 /var/lib/postgresql/data/pgdata; fi"] volumeMounts: diff --git a/charts/sourcegraph/templates/codeintel-db/codeintel-db.StatefulSet.yaml b/charts/sourcegraph/templates/codeintel-db/codeintel-db.StatefulSet.yaml index 9e537e749..f7ae5a1c0 100644 --- a/charts/sourcegraph/templates/codeintel-db/codeintel-db.StatefulSet.yaml +++ b/charts/sourcegraph/templates/codeintel-db/codeintel-db.StatefulSet.yaml @@ -48,7 +48,7 @@ spec: {{- toYaml . | nindent 6 }} {{- end }} - name: correct-data-dir-permissions - image: {{ include "sourcegraph.image" (list . "alpine") }} + image: {{ include "sourcegraph.image" (list . "codeIntelDB") }} imagePullPolicy: {{ .Values.sourcegraph.image.pullPolicy }} command: ["sh", "-c", "if [ -d /data/pgdata-12 ]; then chmod 750 /data/pgdata-12; fi"] volumeMounts: diff --git a/charts/sourcegraph/templates/pgsql/pgsql.StatefulSet.yaml b/charts/sourcegraph/templates/pgsql/pgsql.StatefulSet.yaml index 5e8aa7d27..007e1d3b9 100644 --- a/charts/sourcegraph/templates/pgsql/pgsql.StatefulSet.yaml +++ b/charts/sourcegraph/templates/pgsql/pgsql.StatefulSet.yaml @@ -48,7 +48,7 @@ spec: {{- toYaml . | nindent 6 }} {{- end }} - name: correct-data-dir-permissions - image: {{ include "sourcegraph.image" (list . "alpine") }} + image: {{ include "sourcegraph.image" (list . "pgsql") }} imagePullPolicy: {{ .Values.sourcegraph.image.pullPolicy }} command: ["sh", "-c", "if [ -d /data/pgdata-12 ]; then chmod 750 /data/pgdata-12; fi"] volumeMounts: diff --git a/charts/sourcegraph/tests/dataDirPermissionsInitContainer_test.yaml b/charts/sourcegraph/tests/dataDirPermissionsInitContainer_test.yaml new file mode 100644 index 000000000..df28d96c5 --- /dev/null +++ b/charts/sourcegraph/tests/dataDirPermissionsInitContainer_test.yaml @@ -0,0 +1,38 @@ +suite: dataDirPermissionsInitContainer +release: + name: sourcegraph + namespace: sourcegraph +tests: +- it: should run the pgsql init container on the pgsql image + template: pgsql/pgsql.StatefulSet.yaml + set: + pgsql: + image: + name: custom-postgres + defaultTag: test + asserts: + - equal: + path: spec.template.spec.initContainers[?(@.name == "correct-data-dir-permissions")].image + value: index.docker.io/sourcegraph/custom-postgres:test +- it: should run the codeintel-db init container on the codeintel-db image + template: codeintel-db/codeintel-db.StatefulSet.yaml + set: + codeIntelDB: + image: + name: custom-postgres + defaultTag: test + asserts: + - equal: + path: spec.template.spec.initContainers[?(@.name == "correct-data-dir-permissions")].image + value: index.docker.io/sourcegraph/custom-postgres:test +- it: should run the codeinsights-db init container on the codeinsights-db image + template: codeinsights-db/codeinsights-db.StatefulSet.yaml + set: + codeInsightsDB: + image: + name: custom-postgres + defaultTag: test + asserts: + - equal: + path: spec.template.spec.initContainers[?(@.name == "correct-data-dir-permissions")].image + value: index.docker.io/sourcegraph/custom-postgres:test diff --git a/charts/sourcegraph/values.yaml b/charts/sourcegraph/values.yaml index dc02477c1..2e7d25152 100644 --- a/charts/sourcegraph/values.yaml +++ b/charts/sourcegraph/values.yaml @@ -104,22 +104,16 @@ sourcegraph: # # Toggle deployment of applications on/off. Applies to databases and third-party applications # enabled: true +# Used by the `correct-data-dir-permissions` initContainer of `pgsql` and `codeintel-db`, which runs the database's own image alpine: - # Used in init containers - image: - # -- Docker image tag for the `alpine` image - # @default -- `@sha256:`, pinned by each release - defaultTag: 8.1.2687@sha256:1e113d9625cee4d32b995bb199ca7d6b6f5db1907404acc8ce9b272d39714963 - # -- Docker image name for the `alpine` image - name: "alpine-3.14" - # -- Security context for the `alpine` initContainer, + # -- Security context for the `correct-data-dir-permissions` initContainer of `pgsql` and `codeintel-db`, # learn more from the [Kubernetes documentation](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-container) containerSecurityContext: allowPrivilegeEscalation: false runAsUser: 999 runAsGroup: 999 readOnlyRootFilesystem: true - # -- Resource requests & limits for the `alpine` initContainer, + # -- Resource requests & limits for the `correct-data-dir-permissions` initContainer of `pgsql`, `codeintel-db`, and `codeinsights-db`, # learn more from the [Kubernetes documentation](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/) resources: limits: @@ -209,7 +203,7 @@ codeInsightsDB: runAsGroup: 70 readOnlyRootFilesystem: true init: - # -- Security context for the `alpine` initContainer, + # -- Security context for the `correct-data-dir-permissions` initContainer, # learn more from the [Kubernetes documentation](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-container) containerSecurityContext: allowPrivilegeEscalation: false