Restart appservices and Synapse if a registration changes

This commit is contained in:
Ben Banfield-Zanin committed 2026-11-19 13:23:03 +00:00
1 parent 7ba043178e
commit 85283b1040
13 files changed
+198 -24

No files matched your search

@@ -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 -}}
@@ -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 -}}
@@ -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:
@@ -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 -}}
@@ -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 }}
@@ -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 }}
@@ -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
@@ -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:
+1
View File
@@ -0,0 +1 @@
Fix Synapse not restarting if Hookshot or MatrixRTC authoriser have their appservice registration changed.
+1
View File
@@ -0,0 +1 @@
Fix Hookshot and MatrixRTC authoriser not restarting if their appservice registration changes.
+1
View File
@@ -13,6 +13,7 @@ import pyhelm3
class PropertyType(Enum):
AdditionalConfig = "additional"
AppServiceRegistration = "appserviceRegistration"
Affinity = "affinity"
ContainersSecurityContext = "containersSecurityContext"
DNSPolicy = "dnsPolicy"
+106
View File
@@ -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"
+2 -1
View File
@@ -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",