diff --git a/charts/matrix-stack/source/matrixAuthenticationService.json b/charts/matrix-stack/source/matrixAuthenticationService.json index bed4cf5e..f59ed757 100644 --- a/charts/matrix-stack/source/matrixAuthenticationService.json +++ b/charts/matrix-stack/source/matrixAuthenticationService.json @@ -51,6 +51,9 @@ "type": "boolean", "description": "Enable synapse to Matrix Authentication Service migration." }, + "image": { + "$ref": "file://common/image.json" + }, "migrate": { "type": "object", "properties": { diff --git a/charts/matrix-stack/source/matrixAuthenticationService.yaml.j2 b/charts/matrix-stack/source/matrixAuthenticationService.yaml.j2 index 7fcf259a..538469e5 100644 --- a/charts/matrix-stack/source/matrixAuthenticationService.yaml.j2 +++ b/charts/matrix-stack/source/matrixAuthenticationService.yaml.j2 @@ -49,6 +49,10 @@ privateKeys: ## 3. To disable the job scaling-down Synapse in future helm-upgrades, set `matrixAuthenticationService.syn2mas.enabled: false` syn2mas: enabled: false + + # Syn2Mas relies on the debug image to copy mas-cli to the matrix-tools container + {{- sub_schema_values.image(registry='ghcr.io', repository='element-hq/matrix-authentication-service', tag='0.16.0-debug') | indent(2) }} + migrate: enabled: false ## Runs the syn2mas process in dryRun mode. diff --git a/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_job.yaml b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_job.yaml new file mode 100644 index 00000000..0d50451b --- /dev/null +++ b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_job.yaml @@ -0,0 +1,291 @@ +{{- /* +Copyright 2025 New Vector Ltd + +SPDX-License-Identifier: AGPL-3.0-only +*/ -}} + +{{- with .Values.matrixAuthenticationService -}} +{{- if and .enabled .syn2mas.enabled $.Values.synapse.enabled -}} +{{- $synapseContext := (mustMergeOverwrite ($.Values.synapse | deepCopy) (dict "processType" "main" "isHook" false)) -}} +{{- $masContext := (mustMergeOverwrite ($.Values.matrixAuthenticationService | deepCopy) (dict "isHook" false)) -}} +apiVersion: batch/v1 +kind: Job +metadata: +{{- with .annotations }} + annotations: + {{- toYaml . | nindent 4 }} +{{- end }} + labels: + {{- include "element-io.matrix-authentication-service-syn2mas.labels" (dict "root" $ "context" .) | nindent 4 }} + k8s.element.io/matrix-authentication-service-config-hash: {{ include "element-io.matrix-authentication-service.configmap-data" (dict "root" $ "context" .) | sha1sum }} + k8s.element.io/matrix-authentication-service-secret-hash: {{ include "element-io.matrix-authentication-service.secret-data" (dict "root" $ "context" .) | sha1sum }} + k8s.element.io/synapse-config-hash: {{ include "element-io.synapse.configmap-data" (dict "root" $ "context" $synapseContext) | sha1sum }} + k8s.element.io/synapse-secret-hash: {{ include "element-io.synapse.secret-data" (dict "root" $ "context" $synapseContext) | sha1sum }} + {{ include "element-io.ess-library.postgres-label" (dict "root" $ "context" (dict + "essPassword" "matrixAuthenticationService" + "postgresProperty" .postgres + ) + ) }} + name: {{ $.Release.Name }}-matrix-authentication-service-syn2mas + namespace: {{ $.Release.Namespace }} +spec: + backoffLimit: 0 + completionMode: NonIndexed + completions: 1 + manualSelector: false + parallelism: 1 + podReplacementPolicy: TerminatingOrFailed + template: + metadata: + annotations: +{{- with .annotations }} + {{- toYaml . | nindent 8 }} +{{- end }} + labels: + {{- include "element-io.init-secrets.labels" (dict "root" $ "context" (dict "labels" .labels "withChartVersion" false)) | nindent 8 }} + spec: + restartPolicy: OnFailure +{{- include "element-io.ess-library.pods.commonSpec" (dict "root" $ "context" (dict "componentValues" . "instanceSuffix" "matrix-authentication-service-syn2mas" "deployment" false "usesMatrixTools" true "mountServiceAccountToken" true)) | nindent 6 }} + initContainers: + - name: copy-mas-cli +{{- with .syn2mas.image -}} +{{- if .digest }} + image: "{{ .registry }}/{{ .repository }}@{{ .digest }}" + imagePullPolicy: {{ .pullPolicy | default "IfNotPresent" }} +{{- else }} + image: "{{ .registry }}/{{ .repository }}:{{ required "matrixAuthenticationService.syn2mas.image.tag is required if no digest" .tag }}" + imagePullPolicy: {{ .pullPolicy | default "Always" }} +{{- end }} +{{- end }} + command: + - "bash" + - "-c" + - | + set -e + cp $(which mas-cli) /usr/local/bin/mas-cli +{{- with .containersSecurityContext }} + securityContext: + {{- toYaml . | nindent 10 }} +{{- end }} +{{- with .resources }} + resources: + {{- toYaml . | nindent 10 }} +{{- end }} + volumeMounts: + - mountPath: /usr/local/bin + name: usr-local-bin + - name: render-config-mas +{{- with $.Values.matrixTools.image -}} +{{- if .digest }} + image: "{{ .registry }}/{{ .repository }}@{{ .digest }}" + imagePullPolicy: {{ .pullPolicy | default "IfNotPresent" }} +{{- else }} + image: "{{ .registry }}/{{ .repository }}:{{ required "matrixTools.image.tag is required if no digest" .tag }}" + imagePullPolicy: {{ .pullPolicy | default "Always" }} +{{- end }} +{{- end }} +{{- with .containersSecurityContext }} + securityContext: + {{- toYaml . | nindent 10 }} +{{- end }} + command: + {{- include "element-io.matrix-authentication-service.render-config" (dict "root" $ "context" .) | nindent 8 }} + 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-mas-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 +{{- with $synapseContext }} + - name: render-config-syn +{{- with $.Values.matrixTools.image -}} +{{- if .digest }} + image: "{{ .registry }}/{{ .repository }}@{{ .digest }}" + imagePullPolicy: {{ .pullPolicy | default "IfNotPresent" }} +{{- else }} + image: "{{ .registry }}/{{ .repository }}:{{ required "matrixTools.image.tag is required if no digest" .tag }}" + imagePullPolicy: {{ .pullPolicy | default "Always" }} +{{- end }} +{{- end }} +{{- with .containersSecurityContext }} + securityContext: + {{- toYaml . | nindent 10 }} +{{- end }} + command: + {{- include "element-io.synapse.render-config" (dict "root" $ "context" .) | nindent 8 }} + env: + {{- include "element-io.synapse.matrixToolsEnv" (dict "root" $ "context" .) | nindent 10 }} + {{- include "element-io.synapse.env" (dict "root" $ "context" .) | nindent 10 }} +{{- with .resources }} + resources: + {{- toYaml . | nindent 10 }} +{{- end }} + volumeMounts: + - mountPath: /config-templates + name: plain-syn-config + readOnly: true +{{- range $secret := include "element-io.synapse.configSecrets" (dict "root" $ "context" .) | fromJsonArray }} + - mountPath: /secrets/{{ tpl $secret $ }} + name: "secret-{{ tpl $secret $ }}" + readOnly: true +{{- end }} + - mountPath: /conf + name: rendered-config + readOnly: false +{{- end }} + - name: db-wait-mas +{{- with $.Values.matrixTools.image -}} +{{- if .digest }} + image: "{{ .registry }}/{{ .repository }}@{{ .digest }}" + imagePullPolicy: {{ .pullPolicy | default "IfNotPresent" }} +{{- else }} + image: "{{ .registry }}/{{ .repository }}:{{ required "matrixTools.image.tag is required if no digest" .tag }}" + imagePullPolicy: {{ .pullPolicy | default "Always" }} +{{- end }} +{{- end }} +{{- with .containersSecurityContext }} + securityContext: + {{- toYaml . | nindent 10 }} +{{- end }} + command: + - "/matrix-tools" + - tcpwait + - -address + - {{ include "element-io.ess-library.postgres-host-port" (dict "root" $ "context" (dict "postgres" .postgres)) | quote }} +{{- with .resources }} + resources: + {{- toYaml . | nindent 10 }} +{{- end }} +{{- with $synapseContext }} + - name: db-wait-syn +{{- with $.Values.matrixTools.image -}} +{{- if .digest }} + image: "{{ .registry }}/{{ .repository }}@{{ .digest }}" + imagePullPolicy: {{ .pullPolicy | default "IfNotPresent" }} +{{- else }} + image: "{{ .registry }}/{{ .repository }}:{{ required "matrixTools.image.tag is required if no digest" .tag }}" + imagePullPolicy: {{ .pullPolicy | default "Always" }} +{{- end }} +{{- end }} +{{- with .containersSecurityContext }} + securityContext: + {{- toYaml . | nindent 10 }} +{{- end }} + command: + - "/matrix-tools" + - tcpwait + - -address + - {{ include "element-io.ess-library.postgres-host-port" (dict "root" $ "context" (dict "postgres" .postgres)) | quote }} +{{- with .resources }} + resources: + {{- toYaml . | nindent 10 }} +{{- end }} +{{- end }} + - name: syn2mas-check + args: ["syn2mas", "check", "--config", "/conf/config.yaml", "--synapse-config", "/conf/homeserver.yaml"] +{{- with .image -}} +{{- if .digest }} + image: "{{ .registry }}/{{ .repository }}@{{ .digest }}" + imagePullPolicy: {{ .pullPolicy | default "IfNotPresent" }} +{{- else }} + image: "{{ .registry }}/{{ .repository }}:{{ required "matrixAuthenticationService.image.tag is required if no digest" .tag }}" + imagePullPolicy: {{ .pullPolicy | default "Always" }} +{{- end }} +{{- end }} +{{- with .containersSecurityContext }} + securityContext: + {{- toYaml . | nindent 10 }} +{{- end }} + env: + {{- include "element-io.matrix-authentication-service.env" (dict "root" $ "context" .) | nindent 10 }} +{{- with .resources }} + resources: + {{- toYaml . | nindent 10 }} +{{- end }} + volumeMounts: + - mountPath: "/conf" + name: rendered-config + readOnly: true +{{- range $secret := include "element-io.matrix-authentication-service.syn2masConfigSecrets" (dict "root" $ "context" (dict "synapseContext" $synapseContext "masContext" $masContext)) | fromJsonArray }} + - mountPath: /secrets/{{ tpl $secret $ }} + name: "secret-{{ tpl $secret $ }}" + readOnly: true +{{- end }} + containers: + - name: syn2mas-migrate +{{- if .syn2mas.migrate.dryRun }} + args: ["mas-cli", "syn2mas", "migrate", "--config", "/conf/config.yaml", "--synapse-config", "/conf/homeserver.yaml", "--dry-run"], +{{- with .image -}} +{{- if .digest }} + image: "{{ .registry }}/{{ .repository }}@{{ .digest }}" + imagePullPolicy: {{ .pullPolicy | default "IfNotPresent" }} +{{- else }} + image: "{{ .registry }}/{{ .repository }}:{{ required "matrixAuthenticationService.image.tag is required if no digest" .tag }}" + imagePullPolicy: {{ .pullPolicy | default "Always" }} +{{- end }} +{{- end }} +{{- else }} + args: ["matrix-tools", "syn2mas", "--config", "/conf/config.yaml", "--synapse-config", "/conf/homeserver.yaml"] +{{- with $root.Values.matrixTools.image -}} +{{- if .digest }} + image: "{{ .registry }}/{{ .repository }}@{{ .digest }}" + imagePullPolicy: {{ .pullPolicy | default "IfNotPresent" }} +{{- else }} + image: "{{ .registry }}/{{ .repository }}:{{ required "matrixTools.image.tag is required if no digest" .tag }}" + imagePullPolicy: {{ .pullPolicy | default "Always" }} +{{- end }} +{{- end }} +{{- end }} +{{- with .containersSecurityContext }} + securityContext: + {{- toYaml . | nindent 10 }} +{{- end }} + env: + {{- include "element-io.matrix-authentication-service.env" (dict "root" $ "context" .) | nindent 10 }} +{{- with .resources }} + resources: + {{- toYaml . | nindent 10 }} +{{- end }} + volumeMounts: + - mountPath: "/conf" + name: rendered-config + readOnly: true +{{- range $secret := include "element-io.matrix-authentication-service.syn2masConfigSecrets" (dict "root" $ "context" (dict "synapseContext" $synapseContext "masContext" $masContext)) | fromJsonArray }} + - mountPath: /secrets/{{ tpl $secret $ }} + name: "secret-{{ tpl $secret $ }}" + readOnly: true +{{- end }} + volumes: + - name: plain-syn-config + configMap: + defaultMode: 420 + name: {{ include "element-io.synapse.configmap-name" (dict "root" $ "context" (dict "isHook" false)) }} + - name: plain-mas-config + configMap: + name: {{ $.Release.Name }}-matrix-authentication-service +{{- range $secret := include "element-io.matrix-authentication-service.syn2masConfigSecrets" (dict "root" $ "context" (dict "synapseContext" $synapseContext "masContext" $masContext)) | fromJsonArray }} + - secret: + secretName: {{ tpl $secret $ }} + name: secret-{{ tpl $secret $ }} +{{- end }} + - emptyDir: + medium: Memory + name: "rendered-config" + - emptyDir: + medium: Memory + name: "usr-local-bin" +{{- end }} +{{- end }} diff --git a/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_role.yaml b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_role.yaml new file mode 100644 index 00000000..00ead969 --- /dev/null +++ b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_role.yaml @@ -0,0 +1,29 @@ +{{- /* +Copyright 2024 New Vector Ltd + +SPDX-License-Identifier: AGPL-3.0-only +*/ -}} +{{- with .Values.initSecrets -}} +{{- if and .enabled .rbac.create (include "element-io.matrix-authentication-service-syn2mas.generated-secrets" (dict "root" $)) -}} +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: {{ $.Release.Name }}-matrix-authentication-service-syn2mas + namespace: {{ $.Release.Namespace }} + labels: + {{- include "element-io.matrix-authentication-service-syn2mas.labels" (dict "root" $ "context" .) | nindent 4 }} + annotations: + "helm.sh/hook": pre-install,pre-upgrade + "helm.sh/hook-weight": "-10" +rules: +{{/* + https://kubernetes.io/docs/reference/access-authn-authz/rbac/#referring-to-resources + You cannot restrict create or deletecollection requests by resourceName. + For create, this limitation is because the object name is not known at authorization time. +*/}} +- apiGroups: [""] + resources: ["statefulsets"] + resourceNames: [ "{{ $.Release.Name }}-synapse" ] + verbs: ["update"] +{{- end -}} +{{- end -}} diff --git a/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_rolebinding.yaml b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_rolebinding.yaml new file mode 100644 index 00000000..1904ac71 --- /dev/null +++ b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_rolebinding.yaml @@ -0,0 +1,28 @@ +{{- /* +Copyright 2024 New Vector Ltd + +SPDX-License-Identifier: AGPL-3.0-only +*/ -}} + +{{- with .Values.initSecrets -}} +{{- if and .enabled .rbac.create (include "element-io.matrix-authentication-service-syn2mas.generated-secrets" (dict "root" $)) -}} +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: {{ $.Release.Name }}-matrix-authentication-service-syn2mas + namespace: {{ $.Release.Namespace }} + labels: + {{- include "element-io.matrix-authentication-service-syn2mas.labels" (dict "root" $ "context" .) | nindent 4 }} + annotations: + "helm.sh/hook": pre-install,pre-upgrade + "helm.sh/hook-weight": "-10" +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: {{ $.Release.Name }}-matrix-authentication-service-syn2mas +subjects: +- kind: ServiceAccount + name: {{ include "element-io.ess-library.serviceAccountName" (dict "root" $ "context" (dict "serviceAccount" .serviceAccount "nameSuffix" "matrix-authentication-service-syn2mas")) }} + namespace: {{ $.Release.Namespace }} +{{- end -}} +{{- end -}} diff --git a/charts/matrix-stack/values.schema.json b/charts/matrix-stack/values.schema.json index c16d0113..4f64be79 100644 --- a/charts/matrix-stack/values.schema.json +++ b/charts/matrix-stack/values.schema.json @@ -3451,6 +3451,47 @@ "type": "boolean", "description": "Enable synapse to Matrix Authentication Service migration." }, + "image": { + "type": "object", + "required": [ + "repository" + ], + "properties": { + "registry": { + "type": "string" + }, + "repository": { + "type": "string" + }, + "tag": { + "type": "string" + }, + "digest": { + "type": "string" + }, + "pullPolicy": { + "type": "string", + "enum": [ + "Always", + "IfNotPresent", + "Never" + ] + }, + "pullSecrets": { + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string" + } + }, + "additionalProperties": false + } + } + }, + "additionalProperties": false + }, "migrate": { "type": "object", "properties": { diff --git a/charts/matrix-stack/values.yaml b/charts/matrix-stack/values.yaml index b1679b9c..28867d43 100644 --- a/charts/matrix-stack/values.yaml +++ b/charts/matrix-stack/values.yaml @@ -1752,6 +1752,35 @@ matrixAuthenticationService: ## 3. To disable the job scaling-down Synapse in future helm-upgrades, set `matrixAuthenticationService.syn2mas.enabled: false` syn2mas: enabled: false + + # Syn2Mas relies on the debug image to copy mas-cli to the matrix-tools container + # Details of the image to be used + image: + ## The host and (optional) port of the container image registry for this component. + ## If not specified Docker Hub is implied + registry: ghcr.io + + ## The path in the registry where the container image is located + repository: element-hq/matrix-authentication-service + + ## The tag of the container image to use. + ## Defaults to the Chart's appVersion if not set + tag: "0.16.0-debug" + + ## Container digest to use. Used to pull the image instead of the image tag / Chart appVersion if set + # digest: + + ## Whether the image should be pulled on container startup. Valid values are Always, IfNotPresent and Never + ## If this isn't provided it defaults to Always when using the image tag / Chart appVersion or + ## IfNotPresent if using a digest + # pullPolicy: + + ## A list of pull secrets to use for this image + ## e.g. + ## pullSecrets: + ## - name: dockerhub + pullSecrets: [] + migrate: enabled: false ## Runs the syn2mas process in dryRun mode. diff --git a/matrix-tools/internal/pkg/syn2mas/syn2mas.go b/matrix-tools/internal/pkg/syn2mas/syn2mas.go index 36b01666..a26703f0 100644 --- a/matrix-tools/internal/pkg/syn2mas/syn2mas.go +++ b/matrix-tools/internal/pkg/syn2mas/syn2mas.go @@ -82,7 +82,7 @@ func scaleBack(client kubernetes.Interface, namespace string, scaledSts map[stri func RunSyn2MAS(client kubernetes.Interface, namespace string, synapseConfigPath string, masConfigMap string) { originStsReplicas := scaleDownSynapse(client, namespace) // Run syn2mas cli, and in case of failure, scale back synapse up - cmd := exec.Command("mas-cli", "syn2mas", "migrate", "--config", masConfigMap, "--synapse-config", synapseConfigPath) + cmd := exec.Command("/usr/local/bin/mas-cli", "syn2mas", "migrate", "--config", masConfigMap, "--synapse-config", synapseConfigPath) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Run()