diff --git a/charts/matrix-stack/templates/ess-library/_labels.tpl b/charts/matrix-stack/templates/ess-library/_labels.tpl index 717bb99d..574f6d53 100644 --- a/charts/matrix-stack/templates/ess-library/_labels.tpl +++ b/charts/matrix-stack/templates/ess-library/_labels.tpl @@ -49,7 +49,7 @@ k8s.element.io/postgres-password-{{ ($essPassword | lower) | trunc 24 }}-hash: { {{- else -}} {{- (printf "%s-%s" (tpl $postgresProperty.password.secret $root) $postgresProperty.password.secretKey) | sha1sum -}} {{- end -}} -{{- else if (index $root.Values.postgres.essPasswords $essPassword) }} +{{- else if (index $root.Values.postgres.essPasswords $essPassword) -}} {{- if (index $root.Values.postgres.essPasswords $essPassword).value -}} {{- (index $root.Values.postgres.essPasswords $essPassword).value | sha1sum -}} {{- else -}} @@ -57,10 +57,40 @@ k8s.element.io/postgres-password-{{ ($essPassword | lower) | trunc 24 }}-hash: { {{- end -}} {{- else -}} {{- if $root.Values.initSecrets.enabled -}} - {{- (printf "%s-generated" $root.Release.Name) | sha1sum -}} + {{- (printf "%s-generated" $root.Release.Name) | sha1sum -}} {{- else -}} {{- fail "No postgres password set and initSecrets not set" -}} {{- end -}} {{- end -}} {{- end -}} {{- end -}} + +{{- define "element-io.ess-library.appservice-hash-label" -}} +{{- $root := .root -}} +{{- with required "element-io.ess-library.appservice-hash-label requires context" .context -}} + {{- $componentValues := required "element-io.ess-library.appservice-hash-label context missing componentValues" .componentValues -}} + {{- $templatePath := required "element-io.ess-library.appservice-hash-label context missing templatePath" .templatePath -}} + {{- $label := "" -}} + {{- $value := "" -}} + {{- if eq (required "element-io.ess-library.appservice-hash-label context missing destination" .destination) "synapse" -}} + {{- $label = printf "k8s.element.io/as-registration-%s-hash" (required "element-io.ess-library.appservice-hash-label context missing componentName" .componentName) -}} + {{- else -}} + {{- $label = "k8s.element.io/as-registration-self-hash" -}} + {{- end -}} + {{- with $componentValues -}} + {{- if .enabled -}} + {{- with .appserviceRegistration -}} + {{- with .value -}} + {{- $value = tpl . $root -}} + {{- else -}} + {{- /* This is best effort as lookup might not work (return nothing) or will return more than just the key in question */ -}} + {{- $value = (lookup "v1" "Secret" $root.Release.Namespace (tpl .secret $root)) | toJson -}} + {{- end -}} + {{- else -}} + {{- $value = tpl ($root.Files.Get $templatePath) (dict "root" $root) -}} + {{- end -}} + {{- $label }}: {{ $value | sha1sum -}} + {{- end -}} + {{- end -}} +{{- end -}} +{{- end -}} diff --git a/charts/matrix-stack/templates/hookshot/_helpers.tpl b/charts/matrix-stack/templates/hookshot/_helpers.tpl index 804c2486..7392d647 100644 --- a/charts/matrix-stack/templates/hookshot/_helpers.tpl +++ b/charts/matrix-stack/templates/hookshot/_helpers.tpl @@ -41,6 +41,19 @@ app.kubernetes.io/version: {{ include "element-io.ess-library.labels.makeSafe" . {{- end }} +{{- define "element-io.hookshot.appservice-hash-label" -}} +{{- $root := .root -}} +{{- with required "element-io.hookshot.appservice-hash-label requires context" .context -}} +{{- $destination := required "element-io.hookshot.appservice-hash-label context missing destination" .destination -}} +{{- include "element-io.ess-library.appservice-hash-label" + (dict "root" $root "context" (dict "componentValues" $root.Values.hookshot + "componentName" "hookshot" + "templatePath" "configs/hookshot/registration.yaml.tpl" + "destination" $destination)) -}} +{{- end -}} +{{- end -}} + + {{- define "element-io.hookshot.configmap-name" -}} {{- $root := .root -}} {{- with required "element-io.hookshot.configmap-name requires context" .context -}} diff --git a/charts/matrix-stack/templates/hookshot/hookshot_statefulset.yaml b/charts/matrix-stack/templates/hookshot/hookshot_statefulset.yaml index c725d0ff..905bdf06 100644 --- a/charts/matrix-stack/templates/hookshot/hookshot_statefulset.yaml +++ b/charts/matrix-stack/templates/hookshot/hookshot_statefulset.yaml @@ -17,7 +17,8 @@ metadata: labels: k8s.element.io/hookshot-config-hash: "{{ include "element-io.hookshot.configmap-data" (dict "root" $ "context" .) | sha1sum }}" k8s.element.io/hookshot-secret-hash: "{{ include "element-io.hookshot.secret-data" (dict "root" $ "context" .) | sha1sum }}" - {{ include "element-io.hookshot.labels" (dict "root" $ "context" (dict "image" .image "labels" .labels)) | nindent 4 }} + {{- include "element-io.hookshot.appservice-hash-label" (dict "root" $ "context" (dict "destination" "self")) | nindent 4 }} + {{- include "element-io.hookshot.labels" (dict "root" $ "context" (dict "image" .image "labels" .labels)) | nindent 4 }} name: {{ $.Release.Name }}-hookshot namespace: {{ $.Release.Namespace }} spec: @@ -27,6 +28,7 @@ spec: labels: k8s.element.io/hookshot-secret-hash: "{{ include "element-io.hookshot.secret-data" (dict "root" $ "context" .) | sha1sum }}" k8s.element.io/hookshot-config-hash: "{{ include "element-io.hookshot.configmap-data" (dict "root" $ "context" .) | sha1sum }}" + {{- include "element-io.hookshot.appservice-hash-label" (dict "root" $ "context" (dict "destination" "self")) | nindent 8 }} {{- include "element-io.hookshot.labels" (dict "root" $ "context" (dict "image" .image "labels" .labels "withChartVersion" false)) | nindent 8 }} {{- with .annotations }} annotations: diff --git a/charts/matrix-stack/templates/matrix-rtc/_helpers.tpl b/charts/matrix-stack/templates/matrix-rtc/_helpers.tpl index e64e20f8..b82de9dd 100644 --- a/charts/matrix-stack/templates/matrix-rtc/_helpers.tpl +++ b/charts/matrix-stack/templates/matrix-rtc/_helpers.tpl @@ -62,6 +62,20 @@ app.kubernetes.io/version: {{ include "element-io.ess-library.labels.makeSafe" . {{- end }} {{- end }} + +{{- define "element-io.matrix-rtc-authorisation.appservice-hash-label" -}} +{{- $root := .root -}} +{{- with required "element-io.matrix-rtc-authorisation.appservice-hash-label requires context" .context -}} +{{- $destination := required "element-io.matrix-rtc-authorisation.appservice-hash-labell context missing destination" .destination -}} +{{- include "element-io.ess-library.appservice-hash-label" + (dict "root" $root "context" (dict "componentValues" $root.Values.matrixRTC + "componentName" "matrix-rtc" + "templatePath" "configs/matrix-rtc/registration.yaml.tpl" + "destination" $destination)) -}} +{{- end }} +{{- end }} + + {{- define "element-io.matrix-rtc-authorisation.secret-name" -}} {{- $root := .root -}} {{- with required "element-io.matrix-rtc-authorisation.secret-name requires context" .context -}} diff --git a/charts/matrix-stack/templates/matrix-rtc/authorisation_deployment.yaml b/charts/matrix-stack/templates/matrix-rtc/authorisation_deployment.yaml index 9251c39c..b8f33e01 100644 --- a/charts/matrix-stack/templates/matrix-rtc/authorisation_deployment.yaml +++ b/charts/matrix-stack/templates/matrix-rtc/authorisation_deployment.yaml @@ -15,8 +15,9 @@ metadata: {{- toYaml . | nindent 4 }} {{- end }} labels: - {{- include "element-io.matrix-rtc-authorisation.labels" (dict "root" $ "context" .) | nindent 4 }} k8s.element.io/matrix-rtc-authorisation-service-secret-hash: {{ include "element-io.matrix-rtc-authorisation.secret-data" (dict "root" $ "context" .) | sha1sum }} + {{- include "element-io.matrix-rtc-authorisation.appservice-hash-label" (dict "root" $ "context" (dict "destination" "self")) | nindent 4 }} + {{- include "element-io.matrix-rtc-authorisation.labels" (dict "root" $ "context" .) | nindent 4 }} name: {{ $.Release.Name }}-matrix-rtc-authorisation namespace: {{ $.Release.Namespace }} spec: @@ -24,8 +25,9 @@ spec: template: metadata: labels: - {{- include "element-io.matrix-rtc-authorisation.labels" (dict "root" $ "context" (dict "image" .image "labels" .labels "withChartVersion" false)) | nindent 8 }} k8s.element.io/matrix-rtc-authorisation-service-secret-hash: {{ include "element-io.matrix-rtc-authorisation.secret-data" (dict "root" $ "context" .) | sha1sum }} + {{- include "element-io.matrix-rtc-authorisation.appservice-hash-label" (dict "root" $ "context" (dict "destination" "self")) | nindent 8 }} + {{- include "element-io.matrix-rtc-authorisation.labels" (dict "root" $ "context" (dict "image" .image "labels" .labels "withChartVersion" false)) | nindent 8 }} {{- with .annotations }} annotations: {{- toYaml . | nindent 8 }} diff --git a/charts/matrix-stack/templates/synapse/_synapse_pod.tpl b/charts/matrix-stack/templates/synapse/_synapse_pod.tpl index 2024c840..0ef8b6c8 100644 --- a/charts/matrix-stack/templates/synapse/_synapse_pod.tpl +++ b/charts/matrix-stack/templates/synapse/_synapse_pod.tpl @@ -37,11 +37,12 @@ template: k8s.element.io/as-registration-{{ $index }}-hash: "{{ (lookup "v1" "Secret" $root.Release.Namespace (tpl $appservice.secret $root)) | toJson | sha1sum }}" {{- end }} {{- end }} - {{ include "element-io.ess-library.postgres-label" (dict "root" $root "context" (dict - "essPassword" "synapse" - "postgresProperty" .postgres - ) - ) -}} + {{- with (include "element-io.hookshot.appservice-hash-label" (dict "root" $root "context" (dict "destination" "synapse"))) }}{{. | nindent 6 }}{{- end }} + {{- with (include "element-io.matrix-rtc-authorisation.appservice-hash-label" (dict "root" $root "context" (dict "destination" "synapse"))) }}{{. | nindent 6 }}{{- end }} + {{- include "element-io.ess-library.postgres-label" (dict "root" $root "context" (dict + "essPassword" "synapse" + "postgresProperty" .postgres + )) | nindent 6 }} {{- with .annotations }} annotations: {{- toYaml . | nindent 6 }} diff --git a/charts/matrix-stack/templates/synapse/synapse_check_config_job_hook.yaml b/charts/matrix-stack/templates/synapse/synapse_check_config_job_hook.yaml index 73b10d81..f6db0306 100644 --- a/charts/matrix-stack/templates/synapse/synapse_check_config_job_hook.yaml +++ b/charts/matrix-stack/templates/synapse/synapse_check_config_job_hook.yaml @@ -1,6 +1,6 @@ {{- /* Copyright 2025 New Vector Ltd -Copyright 2025 Element Creations Ltd +Copyright 2025-2026 Element Creations Ltd SPDX-License-Identifier: AGPL-3.0-only */ -}} @@ -31,12 +31,13 @@ metadata: {{- else }} k8s.element.io/as-registration-{{ $index }}-hash: "{{ (lookup "v1" "Secret" $.Release.Namespace (tpl $appservice.secret $)) | toJson | sha1sum }}" {{- end }} -{{- end }} - {{ include "element-io.ess-library.postgres-label" (dict "root" $ "context" (dict - "essPassword" "synapse" - "postgresProperty" .postgres - ) - ) }} +{{- end -}} + {{- with (include "element-io.hookshot.appservice-hash-label" (dict "root" $ "context" (dict "destination" "synapse"))) }}{{. | nindent 4 }}{{- end }} + {{- with (include "element-io.matrix-rtc-authorisation.appservice-hash-label" (dict "root" $ "context" (dict "destination" "synapse"))) }}{{. | nindent 4 }}{{- end }} + {{- include "element-io.ess-library.postgres-label" (dict "root" $ "context" (dict + "essPassword" "synapse" + "postgresProperty" .postgres + )) | nindent 4 }} spec: backoffLimit: 0 completionMode: NonIndexed diff --git a/charts/matrix-stack/templates/synapse/synapse_statefulset.yaml b/charts/matrix-stack/templates/synapse/synapse_statefulset.yaml index 5fc0fcbe..1a22c8fe 100644 --- a/charts/matrix-stack/templates/synapse/synapse_statefulset.yaml +++ b/charts/matrix-stack/templates/synapse/synapse_statefulset.yaml @@ -28,12 +28,13 @@ metadata: {{- else }} k8s.element.io/as-registration-{{ $index }}-hash: "{{ (lookup "v1" "Secret" $.Release.Namespace (tpl $appservice.secret $)) | toJson | sha1sum }}" {{- end }} -{{- end }} - {{ include "element-io.ess-library.postgres-label" (dict "root" $ "context" (dict - "essPassword" "synapse" - "postgresProperty" .postgres - ) - ) }} +{{- end -}} + {{- with (include "element-io.hookshot.appservice-hash-label" (dict "root" $ "context" (dict "destination" "synapse"))) }}{{ . | nindent 4 }}{{- end }} + {{- with (include "element-io.matrix-rtc-authorisation.appservice-hash-label" (dict "root" $ "context" (dict "destination" "synapse"))) }}{{ . | nindent 4 }}{{- end }} + {{- include "element-io.ess-library.postgres-label" (dict "root" $ "context" (dict + "essPassword" "synapse" + "postgresProperty" .postgres + )) | nindent 4 }} name: {{ $.Release.Name }}-synapse-{{ $workerTypeName }} namespace: {{ $.Release.Namespace }} spec: diff --git a/newsfragments/1825.fixed.1.md b/newsfragments/1825.fixed.1.md new file mode 100644 index 00000000..4ac03dff --- /dev/null +++ b/newsfragments/1825.fixed.1.md @@ -0,0 +1 @@ +Fix Synapse not restarting if Hookshot or MatrixRTC authoriser have their appservice registration changed. diff --git a/newsfragments/1825.fixed.md b/newsfragments/1825.fixed.md new file mode 100644 index 00000000..3e73435a --- /dev/null +++ b/newsfragments/1825.fixed.md @@ -0,0 +1 @@ +Fix Hookshot and MatrixRTC authoriser not restarting if their appservice registration changes. diff --git a/tests/manifests/__init__.py b/tests/manifests/__init__.py index 785ce540..d337a6a9 100644 --- a/tests/manifests/__init__.py +++ b/tests/manifests/__init__.py @@ -13,6 +13,7 @@ import pyhelm3 class PropertyType(Enum): AdditionalConfig = "additional" + AppServiceRegistration = "appserviceRegistration" Affinity = "affinity" ContainersSecurityContext = "containersSecurityContext" DNSPolicy = "dnsPolicy" diff --git a/tests/manifests/test_appservices.py b/tests/manifests/test_appservices.py new file mode 100644 index 00000000..ab18cd68 --- /dev/null +++ b/tests/manifests/test_appservices.py @@ -0,0 +1,106 @@ +# Copyright 2026 Element Creations Ltd +# +# SPDX-License-Identifier: AGPL-3.0-only + +import random +import re +import string + +import pytest + +from manifests.utils import iterate_deployables_parts, iterate_pod_template + +from . import DeployableDetails, PropertyType, values_files_to_test + + +@pytest.mark.parametrize("values_file", values_files_to_test) +@pytest.mark.asyncio_cooperative +async def test_appservices_set_consistent_labels_on_self_and_synapse(templates): + seen_synapse = False + seen_appservice_hashes = {} + seen_appservice_hashes_on_synapse = {} + for pod_template_details in iterate_pod_template(templates): + deployable_details = pod_template_details.deployable_details() + if not deployable_details.is_appservice and not deployable_details.is_synapse_process: + continue + + if deployable_details.is_appservice: + controller_labels = pod_template_details.manifest["metadata"]["labels"] + assert "k8s.element.io/as-registration-self-hash" in controller_labels, ( + f"{pod_template_details.manifest_id} did not have its own registration hash on the parent controller" + ) + + pod_labels = pod_template_details.pod_template["metadata"]["labels"] + assert "k8s.element.io/as-registration-self-hash" in pod_labels, ( + f"{pod_template_details.manifest_id} did not have its own registration hash on the Pod" + ) + + # We don't need to assert the controller label matches the pod label + # as test_pod_spec_labels_are_consistent_with_parent_labels does this + seen_appservice_hashes[deployable_details.name] = controller_labels[ + "k8s.element.io/as-registration-self-hash" + ] + else: + controller_labels = pod_template_details.manifest["metadata"]["labels"] + appservice_hashes_on_synapse = { + key: value + for key, value in controller_labels.items() + if re.match(r"^k8s.element.io/as-registration-[a-z-]+-hash$", key) + } + if not seen_synapse: + seen_synapse = True + seen_appservice_hashes_on_synapse = appservice_hashes_on_synapse + else: + assert seen_appservice_hashes_on_synapse == appservice_hashes_on_synapse, ( + "Synapse has inconsistent Appservice hashes between Pods" + ) + + # As not all values files with appservices will also deploy Synapse + if seen_synapse: + assert len(seen_appservice_hashes) == len(seen_appservice_hashes_on_synapse), ( + "There are differing numbers of AppServices vs labels seen on Synapse (" + f"Seen AppServices={seen_appservice_hashes.keys()} vs " + f"AppService labels on Synapse={seen_appservice_hashes_on_synapse.keys()})" + ) + for appservice, hash in seen_appservice_hashes.items(): + synapse_appservice_label = f"k8s.element.io/as-registration-{appservice}-hash" + assert synapse_appservice_label in seen_appservice_hashes_on_synapse, ( + f"{appservice} didn't have a registration hash on Synapse" + ) + assert hash == seen_appservice_hashes_on_synapse[synapse_appservice_label], ( + f"{appservice} had an inconsistent registration hash vs on Synapse" + ) + + +@pytest.mark.parametrize("values_file", values_files_to_test) +@pytest.mark.asyncio_cooperative +async def test_appservices_hash_changes_when_registration_changes(values, make_templates): + original_hashes = {} + for pod_template_details in iterate_pod_template(await make_templates(values)): + deployable_details = pod_template_details.deployable_details() + if not deployable_details.is_appservice: + continue + + controller_labels = pod_template_details.manifest["metadata"]["labels"] + original_hashes[deployable_details.name] = controller_labels["k8s.element.io/as-registration-self-hash"] + + def set_registration(deployable_details: DeployableDetails): + # We can't sensibly test the built-in template changing or the contents of an external Secret + # So always set an inline value + deployable_details.set_helm_values( + values, + PropertyType.AppServiceRegistration, + {"value": "".join(random.choices(string.ascii_lowercase, k=20))}, + ) + + iterate_deployables_parts(set_registration, lambda deployable_details: deployable_details.is_appservice) + for pod_template_details in iterate_pod_template(await make_templates(values)): + deployable_details = pod_template_details.deployable_details() + if not deployable_details.is_appservice: + continue + + controller_labels = pod_template_details.manifest["metadata"]["labels"] + assert deployable_details.name in original_hashes, f"AppService {deployable_details.name} has suddenly appeared" + assert ( + original_hashes[deployable_details.name] != controller_labels["k8s.element.io/as-registration-self-hash"] + ), f"AppService {deployable_details.name} hasn't had its registration hash changed" diff --git a/tests/manifests/test_labels.py b/tests/manifests/test_labels.py index 3e01fa5f..f08753c6 100644 --- a/tests/manifests/test_labels.py +++ b/tests/manifests/test_labels.py @@ -112,7 +112,8 @@ async def test_pod_spec_labels_are_consistent_with_parent_labels(templates): @pytest.mark.asyncio_cooperative async def test_our_labels_are_named_consistently(templates): acceptable_matches = [ - "k8s.element.io/as-registration-[0-9]+-hash", + "k8s.element.io/as-registration-[0-9]+-hash", # external ASes + "k8s.element.io/as-registration-[a-z-]+-hash", # chart managed ASes "k8s.element.io/([a-z0-9-]+)-(config|secret)-hash", "k8s.element.io/hook-for", "k8s.element.io/postgres-password-([a-z]+)-hash",