diff --git a/charts/matrix-stack/templates/ess-library/_render_config.tpl b/charts/matrix-stack/templates/ess-library/_render_config.tpl new file mode 100644 index 00000000..bed0e078 --- /dev/null +++ b/charts/matrix-stack/templates/ess-library/_render_config.tpl @@ -0,0 +1,111 @@ +{{- /* +Copyright 2025 New Vector Ltd + +SPDX-License-Identifier: AGPL-3.0-only +*/ -}} + +{{- define "element-io.ess-library.render-config-container" -}} +{{- $root := .root -}} +{{- with required "element-io.ess-library.render-config-container missing context" .context -}} +{{- $context := . -}} +{{- $nameSuffix := required "element-io.ess-library.render-config-container missing context.nameSuffix" .nameSuffix -}} +{{- $additionalPath := required "element-io.ess-library.render-config-container context missing additionalPath" .additionalPath -}} +{{- $additionalProperty := include "element-io.ess-library.value-from-values-path" (dict "root" $root "context" $additionalPath) | fromJson -}} +{{- $outputFile := required "element-io.ess-library.render-config-container missing context.outputFile" .outputFile -}} +{{- $underrides := .underrides | default list -}} +{{- $overrides := required "element-io.ess-library.render-config-container missing context.overrides" .overrides -}} +- name: render-config +{{- with $root.Values.matrixTools.image -}} +{{- if .digest }} + image: "{{ .registry }}/{{ .repository }}@{{ .digest }}" + imagePullPolicy: {{ .pullPolicy | default "IfNotPresent" }} +{{- else }} + image: "{{ .registry }}/{{ .repository }}:{{ .tag }}" + imagePullPolicy: {{ .pullPolicy | default "Always" }} +{{- end }} +{{- end }} +{{- with .containersSecurityContext }} + securityContext: + {{- toYaml . | nindent 4 }} +{{- end }} + command: + - "/matrix-tools" + - render-config + - -output + - /conf/{{ $outputFile }} + {{- range $underrides }} + - /config-templates/{{ . }} + {{- end }} + {{- range $key := ($additionalProperty | keys | uniq | sortAlpha) -}} + {{- $prop := index $additionalProperty $key }} + {{- if $prop.config }} + - /secrets/{{ include (printf "element-io.%s.secret-name" $nameSuffix) (dict "root" $root "context" $context) }}/user-{{ $key }} + {{- end }} + {{- if $prop.configSecret }} + - /secrets/{{ tpl $prop.configSecret $root }}/{{ $prop.configSecretKey }} + {{- end }} + {{- end }} + {{- range $overrides }} + - /config-templates/{{ . }} + {{- end }} + env: + {{- include (printf "element-io.%s.matrixToolsEnv" $nameSuffix) (dict "root" $root "context" .) | nindent 2 }} + {{- include (printf "element-io.%s.env" $nameSuffix) (dict "root" $root "context" .) | nindent 2 }} +{{- with .resources }} + resources: + {{- toYaml . | nindent 4 }} +{{- end }} + volumeMounts: + - mountPath: /config-templates + name: plain-config + readOnly: true +{{- range $secret := include (printf "element-io.%s.configSecrets" $nameSuffix) (dict "root" $root "context" .) | fromJsonArray }} + - mountPath: /secrets/{{ tpl $secret $root }} + name: "secret-{{ tpl $secret $root }}" + readOnly: true +{{- end }} + - mountPath: /conf + name: rendered-config + readOnly: false +{{- end }} +{{- end }} + + +{{- define "element-io.ess-library.render-config-volumes" -}} +{{- $root := .root -}} +{{- with required "element-io.ess-library.render-config-volumes missing context" .context -}} +{{- $nameSuffix := required "element-io.ess-library.render-config-volumes missing context.nameSuffix" .nameSuffix -}} +{{- $additionalPath := required "element-io.ess-library.render-config-volumes context missing additionalPath" .additionalPath -}} +{{- $additionalProperty := include "element-io.ess-library.value-from-values-path" (dict "root" $root "context" $additionalPath) | fromJson -}} +- configMap: + defaultMode: 420 + name: {{ include (printf "element-io.%s.configmap-name" $nameSuffix) (dict "root" $root "context" .) }} + name: plain-config +{{- range $secret := include (printf "element-io.%s.configSecrets" $nameSuffix) (dict "root" $root "context" .) | fromJsonArray }} +- secret: + secretName: {{ tpl $secret $root }} + name: secret-{{ tpl $secret $root }} +{{- end }} +- emptyDir: + medium: Memory + name: "rendered-config" +{{- end -}} +{{- end -}} + + +{{- define "element-io.ess-library.render-config-volume-mounts" -}} +{{- $root := .root -}} +{{- with required "element-io.ess-library.render-config-volume-mounts context" .context -}} +{{- $nameSuffix := required "element-io.ess-library.render-config-volume-mounts context.nameSuffix" .nameSuffix -}} +{{- $outputFile := required "element-io.ess-library.render-config-volume-mounts context.outputFile" .outputFile -}} +- mountPath: "/conf/{{ $outputFile }}" + name: rendered-config + subPath: {{ $outputFile }} + readOnly: true +{{- range $secret := include (printf "element-io.%s.configSecrets" $nameSuffix) (dict "root" $root "context" .) | fromJsonArray }} +- mountPath: /secrets/{{ tpl $secret $root }} + name: "secret-{{ tpl $secret $root }}" + readOnly: true +{{- end }} +{{- end -}} +{{- end -}} diff --git a/charts/matrix-stack/templates/matrix-authentication-service/_helpers.tpl b/charts/matrix-stack/templates/matrix-authentication-service/_helpers.tpl index b7e6f2cc..f898860e 100644 --- a/charts/matrix-stack/templates/matrix-authentication-service/_helpers.tpl +++ b/charts/matrix-stack/templates/matrix-authentication-service/_helpers.tpl @@ -25,7 +25,7 @@ app.kubernetes.io/version: {{ include "element-io.ess-library.labels.makeSafe" . {{- define "element-io.matrix-authentication-service.configSecrets" -}} {{- $root := .root -}} {{- with required "element-io.matrix-authentication-service.configSecrets missing context" .context -}} -{{ $configSecrets := list (printf "%s-matrix-authentication-service" $root.Release.Name) }} +{{ $configSecrets := list (include "element-io.matrix-authentication-service.secret-name" (dict "root" $root "context" .)) }} {{- if and $root.Values.initSecrets.enabled (include "element-io.init-secrets.generated-secrets" (dict "root" $root)) }} {{ $configSecrets = append $configSecrets (printf "%s-generated" $root.Release.Name) }} {{- end }} @@ -33,7 +33,7 @@ app.kubernetes.io/version: {{ include "element-io.ess-library.labels.makeSafe" . (dict "root" $root "context" (dict "essPassword" "matrixAuthenticationService" "componentPasswordPath" "matrixAuthenticationService.postgres.password" - "defaultSecretName" (printf "%s-matrix-authentication-service" $root.Release.Name) + "defaultSecretName" (include "element-io.matrix-authentication-service.secret-name" (dict "root" $root "context" .)) "isHook" false ) ) @@ -43,6 +43,7 @@ app.kubernetes.io/version: {{ include "element-io.ess-library.labels.makeSafe" . {{ $configSecrets = append $configSecrets (tpl $value.secret $root) }} {{- end -}} {{- end -}} +{{- with $root.Values.matrixAuthenticationService }} {{- with .synapseSharedSecret.secret -}} {{ $configSecrets = append $configSecrets (tpl . $root) }} {{- end -}} @@ -60,6 +61,7 @@ app.kubernetes.io/version: {{ include "element-io.ess-library.labels.makeSafe" . {{- end }} {{- end }} {{- end }} +{{- end }} {{ $configSecrets | uniq | toJson }} {{- end }} {{- end }} @@ -71,7 +73,7 @@ app.kubernetes.io/version: {{ include "element-io.ess-library.labels.makeSafe" . {{- range $envEntry := .extraEnv -}} {{- $_ := set $resultEnv $envEntry.name $envEntry.value -}} {{- end -}} -{{- $overrideEnv := dict "MAS_CONFIG" "/config.yaml" -}} +{{- $overrideEnv := dict "MAS_CONFIG" "/conf/config.yaml" -}} {{- $resultEnv := mustMergeOverwrite $resultEnv $overrideEnv -}} {{- range $key, $value := $resultEnv }} - name: {{ $key | quote }} @@ -161,6 +163,12 @@ app.kubernetes.io/version: {{ include "element-io.ess-library.labels.makeSafe" . {{- end }} +{{- define "element-io.matrix-authentication-service.configmap-name" -}} +{{- $root := .root -}} +{{- $root.Release.Name }}-matrix-authentication-service +{{- end }} + + {{- define "element-io.matrix-authentication-service.secret-name" }} {{- $root := .root }} {{- with required "element-io.matrix-authentication-service.secret-name requires context" .context }} diff --git a/charts/matrix-stack/templates/matrix-authentication-service/configmap.yaml b/charts/matrix-stack/templates/matrix-authentication-service/configmap.yaml index 7a6d4ca8..cf6bb054 100644 --- a/charts/matrix-stack/templates/matrix-authentication-service/configmap.yaml +++ b/charts/matrix-stack/templates/matrix-authentication-service/configmap.yaml @@ -9,7 +9,7 @@ SPDX-License-Identifier: AGPL-3.0-only apiVersion: v1 kind: ConfigMap metadata: - name: {{ $.Release.Name }}-matrix-authentication-service + name: {{ include "element-io.matrix-authentication-service.configmap-name" (dict "root" $) }} namespace: {{ $.Release.Namespace }} labels: {{- include "element-io.matrix-authentication-service.labels" (dict "root" $ "context" .) | nindent 4 }} diff --git a/charts/matrix-stack/templates/matrix-authentication-service/deployment.yaml b/charts/matrix-stack/templates/matrix-authentication-service/deployment.yaml index 0eae67f0..985e331c 100644 --- a/charts/matrix-stack/templates/matrix-authentication-service/deployment.yaml +++ b/charts/matrix-stack/templates/matrix-authentication-service/deployment.yaml @@ -64,54 +64,15 @@ spec: {{- end }} {{- include "element-io.ess-library.pods.commonSpec" (dict "root" $ "context" (dict "componentValues" . "instanceSuffix" "matrix-authentication-service" "deployment" true "usesMatrixTools" true)) | nindent 6 }} initContainers: - - name: render-config -{{- with $.Values.matrixTools.image -}} -{{- if .digest }} - image: "{{ .registry }}/{{ .repository }}@{{ .digest }}" - imagePullPolicy: {{ .pullPolicy | default "IfNotPresent" }} -{{- else }} - image: "{{ .registry }}/{{ .repository }}:{{ .tag }}" - imagePullPolicy: {{ .pullPolicy | default "Always" }} -{{- end }} -{{- end }} -{{- with .containersSecurityContext }} - securityContext: - {{- toYaml . | nindent 10 }} -{{- end }} - command: - - "/matrix-tools" - - render-config - - -output - - /conf/config.yaml - {{- range $key := (.additional | keys | uniq | sortAlpha) -}} - {{- $prop := index $.Values.matrixAuthenticationService.additional $key }} - {{- if $prop.config }} - - /secrets/{{ $.Release.Name }}-matrix-authentication-service/user-{{ $key }} - {{- end }} - {{- if $prop.configSecret }} - - /secrets/{{ tpl $prop.configSecret $ }}/{{ $prop.configSecretKey }} - {{- end }} - {{- end }} - - /config-templates/config.yaml - env: - {{- include "element-io.matrix-authentication-service.matrixToolsEnv" (dict "root" $ "context" .) | nindent 8 }} - {{- include "element-io.matrix-authentication-service.env" (dict "root" $ "context" .) | nindent 8 }} -{{- with .resources }} - resources: - {{- toYaml . | nindent 10 }} -{{- end }} - volumeMounts: - - mountPath: /config-templates - name: plain-config - readOnly: true -{{- range $secret := include "element-io.matrix-authentication-service.configSecrets" (dict "root" $ "context" .) | fromJsonArray }} - - mountPath: /secrets/{{ tpl $secret $ }} - name: "secret-{{ tpl $secret $ }}" - readOnly: true -{{- end }} - - mountPath: /conf - name: rendered-config - readOnly: false + {{ include "element-io.ess-library.render-config-container" (dict "root" $ "context" ( + dict "additionalPath" "matrixAuthenticationService.additional" + "nameSuffix" "matrix-authentication-service" + "overrides" (list "config.yaml") + "outputFile" "config.yaml" + "resources" .resources + "containersSecurityContext" .containersSecurityContext + "extraEnv" .extraEnv + "isHook" false)) | nindent 6 }} - name: db-wait {{- with $.Values.matrixTools.image -}} {{- if .digest }} @@ -157,15 +118,10 @@ spec: {{- toYaml . | nindent 10 }} {{- end }} volumeMounts: - - mountPath: "/config.yaml" - name: rendered-config - subPath: config.yaml - readOnly: true -{{- range $secret := include "element-io.matrix-authentication-service.configSecrets" (dict "root" $ "context" .) | fromJsonArray }} - - mountPath: /secrets/{{ tpl $secret $ }} - name: "secret-{{ tpl $secret $ }}" - readOnly: true -{{- end }} + {{- include "element-io.ess-library.render-config-volume-mounts" (dict "root" $ "context" + (dict "nameSuffix" "matrix-authentication-service" + "outputFile" "config.yaml" + "isHook" false)) | nindent 8 }} containers: - name: matrix-authentication-service args: ["server", "--no-migrate"] @@ -211,26 +167,14 @@ spec: port: internal scheme: HTTP volumeMounts: - - mountPath: "/config.yaml" - name: rendered-config - subPath: config.yaml - readOnly: true -{{- range $secret := include "element-io.matrix-authentication-service.configSecrets" (dict "root" $ "context" .) | fromJsonArray }} - - mountPath: /secrets/{{ tpl $secret $ }} - name: "secret-{{ tpl $secret $ }}" - readOnly: true -{{- end }} + {{- include "element-io.ess-library.render-config-volume-mounts" (dict "root" $ "context" + (dict "nameSuffix" "matrix-authentication-service" + "outputFile" "config.yaml" + "isHook" false)) | nindent 8 }} volumes: - - emptyDir: - medium: Memory - name: "rendered-config" - - name: plain-config - configMap: - name: {{ $.Release.Name }}-matrix-authentication-service -{{- range $secret := include "element-io.matrix-authentication-service.configSecrets" (dict "root" $ "context" .) | fromJsonArray }} - - secret: - secretName: {{ tpl $secret $ }} - name: secret-{{ tpl $secret $ }} -{{- end }} + {{- include "element-io.ess-library.render-config-volumes" (dict "root" $ "context" + (dict "additionalPath" "matrixAuthenticationService.additional" + "nameSuffix" "matrix-authentication-service" + "isHook" false)) | nindent 6 }} {{- end -}} {{- end -}} diff --git a/charts/matrix-stack/templates/synapse/_synapse_details.tpl b/charts/matrix-stack/templates/synapse/_synapse_details.tpl index 5a13fc60..6c4bf05e 100644 --- a/charts/matrix-stack/templates/synapse/_synapse_details.tpl +++ b/charts/matrix-stack/templates/synapse/_synapse_details.tpl @@ -181,6 +181,7 @@ responsibleForMedia {{- end -}} {{- end -}} {{- end -}} +{{- with $root.Values.synapse -}} {{- with .macaroon.secret -}} {{ $configSecrets = append $configSecrets (tpl . $root) }} {{- end -}} @@ -198,6 +199,7 @@ responsibleForMedia {{- end }} {{- end }} {{- end }} +{{- end }} {{ $configSecrets | uniq | toJson }} {{- end }} {{- end }} diff --git a/charts/matrix-stack/templates/synapse/_synapse_pod.tpl b/charts/matrix-stack/templates/synapse/_synapse_pod.tpl index 6be190f3..9c760ab4 100644 --- a/charts/matrix-stack/templates/synapse/_synapse_pod.tpl +++ b/charts/matrix-stack/templates/synapse/_synapse_pod.tpl @@ -60,60 +60,17 @@ We have an init container to render & merge the config for several reasons: * We could do this all in the main Synapse container but then there's potential confusion between `/config-templates`, `/conf` in the image and `/conf` the `emptyDir` */}} initContainers: - - name: render-config -{{- with $root.Values.matrixTools.image -}} -{{- if .digest }} - image: "{{ .registry }}/{{ .repository }}@{{ .digest }}" - imagePullPolicy: {{ .pullPolicy | default "IfNotPresent" }} -{{- else }} - image: "{{ .registry }}/{{ .repository }}:{{ .tag }}" - imagePullPolicy: {{ .pullPolicy | default "Always" }} -{{- end }} -{{- end }} -{{- with .containersSecurityContext }} - securityContext: - {{- toYaml . | nindent 8 }} -{{- end }} - command: - - "/matrix-tools" - - render-config - - -output - - /conf/homeserver.yaml - - /config-templates/01-homeserver-underrides.yaml - {{- range $key := (.additional | keys | uniq | sortAlpha) -}} - {{- $prop := index $root.Values.synapse.additional $key }} - {{- if $prop.config }} - - /secrets/{{ (include "element-io.synapse.secret-name" (dict "root" $root "context" (dict "isHook" $isHook))) }}/user-{{ $key }} - {{- end }} - {{- if $prop.configSecret }} - - /secrets/{{ tpl $prop.configSecret $root }}/{{ $prop.configSecretKey }} - {{- end }} - {{- end }} - - /config-templates/04-homeserver-overrides.yaml -{{- if eq $processType "check-config-hook" }} - - /config-templates/05-main.yaml -{{- else }} - - /config-templates/05-{{ $processType }}.yaml -{{- end }} - env: - {{- include "element-io.synapse.matrixToolsEnv" (dict "root" $root "context" .) | nindent 8 }} - {{- include "element-io.synapse.env" (dict "root" $root "context" .) | nindent 8 }} -{{- with .resources }} - resources: - {{- toYaml . | nindent 8 }} -{{- end }} - volumeMounts: - - mountPath: /config-templates - name: plain-config - readOnly: true -{{- range $secret := include "element-io.synapse.configSecrets" (dict "root" $root "context" .) | fromJsonArray }} - - mountPath: /secrets/{{ tpl $secret $root }} - name: "secret-{{ tpl $secret $root }}" - readOnly: true -{{- end }} - - mountPath: /conf - name: rendered-config - readOnly: false + {{- include "element-io.ess-library.render-config-container" (dict "root" $root "context" + (dict "additionalPath" "synapse.additional" + "nameSuffix" "synapse" + "underrides" (list "01-homeserver-underrides.yaml") + "overrides" (list "04-homeserver-overrides.yaml" + (eq $processType "check-config-hook" | ternary "05-main.yaml" (printf "05-%s.yaml" $processType))) + "outputFile" "homeserver.yaml" + "resources" .resources + "containersSecurityContext" .containersSecurityContext + "extraEnv" .extraEnv + "isHook" $isHook)) | nindent 4 }} {{- if ne $processType "check-config-hook" }} - name: db-wait {{- with $root.Values.matrixTools.image -}} @@ -202,11 +159,10 @@ We have an init container to render & merge the config for several reasons: {{- toYaml . | nindent 8 }} {{- end }} volumeMounts: -{{- range $secret := include "element-io.synapse.configSecrets" (dict "root" $root "context" .) | fromJsonArray }} - - mountPath: /secrets/{{ tpl $secret $root }} - name: "secret-{{ tpl $secret $root }}" - readOnly: true -{{- end }} + {{- include "element-io.ess-library.render-config-volume-mounts" (dict "root" $root "context" + (dict "nameSuffix" "synapse" + "outputFile" "homeserver.yaml" + "isHook" $isHook)) | nindent 6 }} {{- range $idx, $appservice := .appservices }} - name: as-{{ $idx }} readOnly: true @@ -223,9 +179,6 @@ We have an init container to render & merge the config for several reasons: name: plain-config subPath: log_config.yaml readOnly: false - - mountPath: /conf - name: rendered-config - readOnly: false - mountPath: /media name: media readOnly: false @@ -233,18 +186,10 @@ We have an init container to render & merge the config for several reasons: name: tmp readOnly: false volumes: - - configMap: - defaultMode: 420 - name: {{ include "element-io.synapse.configmap-name" (dict "root" $root "context" (dict "isHook" $isHook)) }} - name: plain-config -{{- range $secret := include "element-io.synapse.configSecrets" (dict "root" $root "context" .) | fromJsonArray }} - - secret: - secretName: {{ tpl $secret $root }} - name: secret-{{ tpl $secret $root }} -{{- end }} - - emptyDir: - medium: Memory - name: "rendered-config" + {{- include "element-io.ess-library.render-config-volumes" (dict "root" $root "context" + (dict "additionalPath" "synapse.additional" + "nameSuffix" "synapse" + "isHook" $isHook)) | nindent 4 }} {{- range $idx, $appservice := .appservices }} - name: as-{{ $idx }} {{- with $appservice.configMap }} diff --git a/newsfragments/488.internal.md b/newsfragments/488.internal.md new file mode 100644 index 00000000..15a11b1c --- /dev/null +++ b/newsfragments/488.internal.md @@ -0,0 +1 @@ +Refactor to use a common helper for `render-config` additional mechanism. diff --git a/tests/manifests/test_configs_and_mounts_consistency.py b/tests/manifests/test_configs_and_mounts_consistency.py index 03db3389..4ca3523e 100644 --- a/tests/manifests/test_configs_and_mounts_consistency.py +++ b/tests/manifests/test_configs_and_mounts_consistency.py @@ -282,7 +282,10 @@ async def test_secrets_consistency(templates, other_secrets, template_to_deploya # We can't verify rendered-config, it's generated at runtime uses_rendered_config = True mount_paths.append(volume_mount["mountPath"]) - key = f"{volume_mount['mountPath']}/{get_key_from_render_config(template)}" + if "subPath" in volume_mount: + key = volume_mount["mountPath"] + else: + key = f"{volume_mount['mountPath']}/{get_key_from_render_config(template)}" mounted_keys.append(key) mounted_keys_to_parents[key] = volume_mount["mountPath"] diff --git a/tests/manifests/test_matrix_authentication_service.py b/tests/manifests/test_matrix_authentication_service.py index 6a73d1e1..ecaddfea 100644 --- a/tests/manifests/test_matrix_authentication_service.py +++ b/tests/manifests/test_matrix_authentication_service.py @@ -1,4 +1,4 @@ -# Copyright 2024 New Vector Ltd +# Copyright 2024-2025 New Vector Ltd # # SPDX-License-Identifier: AGPL-3.0-only @@ -11,7 +11,7 @@ async def test_matrix_authentication_service_env_overrides(values, make_template for template in await make_templates(values): if "matrix-authentication-service" in template["metadata"]["name"] and template["kind"] == "Deployment": env = {e["name"]: e["value"] for e in template["spec"]["template"]["spec"]["containers"][0]["env"]} - assert env["MAS_CONFIG"] == "/config.yaml" + assert env["MAS_CONFIG"] == "/conf/config.yaml" break else: raise RuntimeError("Could not find Matrix Authentication Service deployment") @@ -24,7 +24,7 @@ async def test_matrix_authentication_service_env_overrides(values, make_template for template in await make_templates(values): if "matrix-authentication-service" in template["metadata"]["name"] and template["kind"] == "Deployment": env = {e["name"]: e["value"] for e in template["spec"]["template"]["spec"]["containers"][0]["env"]} - assert env["MAS_CONFIG"] == "/config.yaml" + assert env["MAS_CONFIG"] == "/conf/config.yaml" assert env["OTHER_KEY"] == "should-exists" break else: