Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(helm): give the pre-upgrade migration its own copy of what it reads

Petr Bauch38640974cfa7
diff --git a/deploy/remote/helm/switch/templates/_helpers.tpl b/deploy/remote/helm/switch/templates/_helpers.tpl
index bdbef56fc..02311811e 100644
--- a/deploy/remote/helm/switch/templates/_helpers.tpl
+++ b/deploy/remote/helm/switch/templates/_helpers.tpl
@@ -31,13 +31,36 @@ the External Secrets Operator / sealed-secrets). When set, the chart renders no
Secret of its own.
*/}}
{{- define "switch.secretName" -}}
-{{- if .Values.secrets.existingSecret -}}
+{{- if .secretNameOverride -}}
+{{- .secretNameOverride -}}
+{{- else if .Values.secrets.existingSecret -}}
{{- .Values.secrets.existingSecret -}}
{{- else -}}
{{- include "switch.fullname" . }}-secrets
{{- end -}}
{{- end }}
+{{/*
+The Secret's data block, so the pre-upgrade copy the migration reads is
+rendered from this one source and cannot drift from the release's own.
+*/}}
+{{- define "switch.secretData" -}}
+{{- if not .Values.postgresql.existingSecret }}
+POSTGRES_PASSWORD: {{ required "secrets.postgresPassword is required (unless postgresql.existingSecret is set)" .Values.secrets.postgresPassword | b64enc | quote }}
+{{- end }}
+AGENT_REGISTRATION_TOKEN: {{ required "secrets.agentRegistrationToken is required" .Values.secrets.agentRegistrationToken | b64enc | quote }}
+JWT_SECRET_KEY: {{ required "secrets.jwtSecretKey is required" .Values.secrets.jwtSecretKey | b64enc | quote }}
+GATEWAY_ADMIN_EMAIL: {{ required "secrets.gatewayAdminEmail is required" .Values.secrets.gatewayAdminEmail | b64enc | quote }}
+GATEWAY_ADMIN_PASSWORD: {{ required "secrets.gatewayAdminPassword is required" .Values.secrets.gatewayAdminPassword | b64enc | quote }}
+{{- if .Values.mattermost.enabled }}
+MATTERMOST_ADMIN_PASSWORD: {{ .Values.secrets.mattermostAdminPassword | default .Values.secrets.postgresPassword | b64enc | quote }}
+MATTERMOST_USER_PASSWORD: {{ .Values.secrets.mattermostUserPassword | default .Values.secrets.postgresPassword | b64enc | quote }}
+{{- end }}
+{{- if .Values.switchCore.oidc.enabled }}
+GATEWAY_OIDC_CLIENT_SECRET: {{ required "secrets.gatewayOidcClientSecret is required when switchCore.oidc.enabled" .Values.secrets.gatewayOidcClientSecret | b64enc | quote }}
+{{- end }}
+{{- end }}
+
{{/*
PostgreSQL connection details.
@@ -208,13 +231,24 @@ bundle is configured, which is what every caller tests to decide whether to
mount anything.
*/}}
{{- define "switch.dbCaBundleConfigMap" -}}
-{{- if .Values.postgresql.caBundle.existingConfigMap -}}
+{{- if .dbCaConfigMapOverride -}}
+{{- .dbCaConfigMapOverride -}}
+{{- else if .Values.postgresql.caBundle.existingConfigMap -}}
{{- .Values.postgresql.caBundle.existingConfigMap -}}
{{- else if .Values.postgresql.caBundle.contents -}}
{{- include "switch.fullname" . }}-db-ca
{{- end -}}
{{- end }}
+{{/*
+The CA ConfigMap's data block, shared with the pre-upgrade copy for the same
+reason as the Secret's.
+*/}}
+{{- define "switch.dbCaBundleData" -}}
+{{ .Values.postgresql.caBundle.key }}: |
+{{ .Values.postgresql.caBundle.contents | indent 2 }}
+{{- end }}
+
{{/*
Path the CA bundle is mounted at inside every container that connects to the
database.
diff --git a/deploy/remote/helm/switch/templates/common/db-ca-configmap.yaml b/deploy/remote/helm/switch/templates/common/db-ca-configmap.yaml
index 7ba785d93..ea3a501c1 100644
--- a/deploy/remote/helm/switch/templates/common/db-ca-configmap.yaml
+++ b/deploy/remote/helm/switch/templates/common/db-ca-configmap.yaml
@@ -1,24 +1,11 @@
{{- if and .Values.postgresql.caBundle.contents (not .Values.postgresql.caBundle.existingConfigMap) }}
# Certificate authorities, not a secret: a CA bundle is public by construction.
-#
-# A hook, because the pre-upgrade migration Job mounts it, and an ordinary
-# resource is not created until after the pre-upgrade hooks have run — the Job
-# would wait for a ConfigMap that only appears once the Job it is blocking has
-# finished. A negative weight puts it before the migration; before-hook-creation
-# without hook-succeeded leaves it in place afterwards, which it has to be,
-# since switch-core, Mattermost and the backup release mount it for the life of
-# the release.
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "switch.fullname" . }}-db-ca
labels:
{{- include "switch.labels" . | nindent 4 }}
- annotations:
- "helm.sh/hook": pre-install,pre-upgrade
- "helm.sh/hook-weight": "-5"
- "helm.sh/hook-delete-policy": before-hook-creation
data:
- {{ .Values.postgresql.caBundle.key }}: |
-{{ .Values.postgresql.caBundle.contents | indent 4 }}
+ {{- include "switch.dbCaBundleData" . | nindent 2 }}
{{- end }}
diff --git a/deploy/remote/helm/switch/templates/common/secret.yaml b/deploy/remote/helm/switch/templates/common/secret.yaml
index 65c3558c5..1ad4a2552 100644
--- a/deploy/remote/helm/switch/templates/common/secret.yaml
+++ b/deploy/remote/helm/switch/templates/common/secret.yaml
@@ -1,35 +1,11 @@
{{- if not .Values.secrets.existingSecret }}
-# A hook, for the same reason as the database CA bundle: the pre-upgrade
-# migration Job reads POSTGRES_PASSWORD from here, and an ordinary resource is
-# not applied until the pre-upgrade hooks have already run. The migration would
-# authenticate with the password from the previous release — so an upgrade that
-# repoints the deployment at a different server could never succeed, because
-# the credentials for that server only land once the migration it blocks has
-# passed. It is left in place after the hook, not deleted with it.
apiVersion: v1
kind: Secret
metadata:
name: {{ include "switch.secretName" . }}
labels:
{{- include "switch.labels" . | nindent 4 }}
- annotations:
- "helm.sh/hook": pre-install,pre-upgrade
- "helm.sh/hook-weight": "-5"
- "helm.sh/hook-delete-policy": before-hook-creation
type: Opaque
data:
- {{- if not .Values.postgresql.existingSecret }}
- POSTGRES_PASSWORD: {{ required "secrets.postgresPassword is required (unless postgresql.existingSecret is set)" .Values.secrets.postgresPassword | b64enc | quote }}
- {{- end }}
- AGENT_REGISTRATION_TOKEN: {{ required "secrets.agentRegistrationToken is required" .Values.secrets.agentRegistrationToken | b64enc | quote }}
- JWT_SECRET_KEY: {{ required "secrets.jwtSecretKey is required" .Values.secrets.jwtSecretKey | b64enc | quote }}
- GATEWAY_ADMIN_EMAIL: {{ required "secrets.gatewayAdminEmail is required" .Values.secrets.gatewayAdminEmail | b64enc | quote }}
- GATEWAY_ADMIN_PASSWORD: {{ required "secrets.gatewayAdminPassword is required" .Values.secrets.gatewayAdminPassword | b64enc | quote }}
- {{- if .Values.mattermost.enabled }}
- MATTERMOST_ADMIN_PASSWORD: {{ .Values.secrets.mattermostAdminPassword | default .Values.secrets.postgresPassword | b64enc | quote }}
- MATTERMOST_USER_PASSWORD: {{ .Values.secrets.mattermostUserPassword | default .Values.secrets.postgresPassword | b64enc | quote }}
- {{- end }}
- {{- if .Values.switchCore.oidc.enabled }}
- GATEWAY_OIDC_CLIENT_SECRET: {{ required "secrets.gatewayOidcClientSecret is required when switchCore.oidc.enabled" .Values.secrets.gatewayOidcClientSecret | b64enc | quote }}
- {{- end }}
+ {{- include "switch.secretData" . | nindent 2 }}
{{- end }}
diff --git a/deploy/remote/helm/switch/templates/switch-core/migrate-job.yaml b/deploy/remote/helm/switch/templates/switch-core/migrate-job.yaml
index 83ec25c36..839a72ec0 100644
--- a/deploy/remote/helm/switch/templates/switch-core/migrate-job.yaml
+++ b/deploy/remote/helm/switch/templates/switch-core/migrate-job.yaml
@@ -1,4 +1,13 @@
{{- if .Values.switchCore.migrations.preUpgradeJob }}
+{{- $secretOverride := "" }}
+{{- if not .Values.secrets.existingSecret }}
+{{- $secretOverride = printf "%s-migrate" (include "switch.secretName" .) }}
+{{- end }}
+{{- $caOverride := "" }}
+{{- if and .Values.postgresql.caBundle.contents (not .Values.postgresql.caBundle.existingConfigMap) }}
+{{- $caOverride = printf "%s-db-ca-migrate" (include "switch.fullname" .) }}
+{{- end }}
+{{- $ctx := merge (dict "secretNameOverride" $secretOverride "dbCaConfigMapOverride" $caOverride) . }}
# Runs `alembic upgrade head` before an upgrade rolls the new switch-core pods,
# so a failed migration blocks the release instead of crash-looping the new
# pods. pre-upgrade only: on a fresh install the managed Postgres does not exist
@@ -35,13 +44,13 @@ spec:
imagePullPolicy: {{ .Values.switchCore.imagePullPolicy }}
command: ["alembic", "upgrade", "head"]
env:
- {{- include "switch.coreEnv" . | nindent 12 }}
- {{- if include "switch.dbCaBundleConfigMap" . }}
+ {{- include "switch.coreEnv" $ctx | nindent 12 }}
+ {{- if include "switch.dbCaBundleConfigMap" $ctx }}
volumeMounts:
- {{- include "switch.dbCaBundleVolumeMount" . | nindent 12 }}
+ {{- include "switch.dbCaBundleVolumeMount" $ctx | nindent 12 }}
{{- end }}
- {{- if include "switch.dbCaBundleConfigMap" . }}
+ {{- if include "switch.dbCaBundleConfigMap" $ctx }}
volumes:
- {{- include "switch.dbCaBundleVolume" . | nindent 8 }}
+ {{- include "switch.dbCaBundleVolume" $ctx | nindent 8 }}
{{- end }}
{{- end }}
diff --git a/deploy/remote/helm/switch/templates/switch-core/migrate-prereqs.yaml b/deploy/remote/helm/switch/templates/switch-core/migrate-prereqs.yaml
new file mode 100644
index 000000000..1fc55ea29
--- /dev/null
+++ b/deploy/remote/helm/switch/templates/switch-core/migrate-prereqs.yaml
@@ -0,0 +1,48 @@
+{{- if .Values.switchCore.migrations.preUpgradeJob }}
+# The migration Job runs as a pre-upgrade hook, so it starts before helm applies
+# the release's ordinary resources. Reading the release's own Secret and CA
+# ConfigMap therefore gets the *previous* release's values — which makes an
+# upgrade that repoints the deployment at a different database server
+# impossible: the credentials for that server only land once the migration
+# they are needed for has already passed.
+#
+# So the migration gets copies of its own, rendered from the same source and
+# created at a lower hook weight than the Job. They are deleted when the hook
+# succeeds, leaving nothing behind. Annotating the real Secret and ConfigMap as
+# hooks instead does not work: a resource that moves out of the manifest is
+# deleted during the apply that follows, taking the running release's
+# credentials with it.
+#
+# Not needed when the values come from outside the chart — an existing Secret or
+# ConfigMap is already there before the hook runs.
+{{- if not .Values.secrets.existingSecret }}
+apiVersion: v1
+kind: Secret
+metadata:
+ name: {{ include "switch.secretName" . }}-migrate
+ labels:
+ {{- include "switch.labels" . | nindent 4 }}
+ annotations:
+ "helm.sh/hook": pre-upgrade
+ "helm.sh/hook-weight": "-5"
+ "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
+type: Opaque
+data:
+ {{- include "switch.secretData" . | nindent 2 }}
+{{- end }}
+{{- if and .Values.postgresql.caBundle.contents (not .Values.postgresql.caBundle.existingConfigMap) }}
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+ name: {{ include "switch.fullname" . }}-db-ca-migrate
+ labels:
+ {{- include "switch.labels" . | nindent 4 }}
+ annotations:
+ "helm.sh/hook": pre-upgrade
+ "helm.sh/hook-weight": "-5"
+ "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
+data:
+ {{- include "switch.dbCaBundleData" . | nindent 2 }}
+{{- end }}
+{{- end }}