diff --git a/charts/matrix-stack/templates/deployment-markers/job.yaml b/charts/matrix-stack/templates/deployment-markers/job.yaml index 4ba81ce1..f5924aa4 100644 --- a/charts/matrix-stack/templates/deployment-markers/job.yaml +++ b/charts/matrix-stack/templates/deployment-markers/job.yaml @@ -15,7 +15,7 @@ metadata: namespace: {{ $.Release.Namespace }} annotations: "helm.sh/hook": {{ $step }}-install,{{ $step }}-upgrade - "helm.sh/hook-weight": "-20" + "helm.sh/hook-weight": "{{ (eq $step "pre") | ternary -10 10 }}" {{- with .annotations }} {{- toYaml . | nindent 4 }} {{- end }} diff --git a/charts/matrix-stack/templates/deployment-markers/role.yaml b/charts/matrix-stack/templates/deployment-markers/role.yaml index 716dd754..cc03b7b3 100644 --- a/charts/matrix-stack/templates/deployment-markers/role.yaml +++ b/charts/matrix-stack/templates/deployment-markers/role.yaml @@ -16,7 +16,7 @@ metadata: {{- include "element-io.deployment-markers.labels" (dict "root" $ "context" (mustMergeOverwrite (dict "step" $step) .)) | nindent 4 }} annotations: "helm.sh/hook": {{ $step }}-install,{{ $step }}-upgrade - "helm.sh/hook-weight": "-20" + "helm.sh/hook-weight": "{{ (eq $step "pre") | ternary -11 9 }}" rules: {{/* https://kubernetes.io/docs/reference/access-authn-authz/rbac/#referring-to-resources diff --git a/charts/matrix-stack/templates/deployment-markers/rolebinding.yaml b/charts/matrix-stack/templates/deployment-markers/rolebinding.yaml index d268fcbf..b0d65167 100644 --- a/charts/matrix-stack/templates/deployment-markers/rolebinding.yaml +++ b/charts/matrix-stack/templates/deployment-markers/rolebinding.yaml @@ -17,7 +17,7 @@ metadata: {{- include "element-io.deployment-markers.labels" (dict "root" $ "context" (mustMergeOverwrite (dict "step" $step) .)) | nindent 4 }} annotations: "helm.sh/hook": {{ $step }}-install,{{ $step }}-upgrade - "helm.sh/hook-weight": "-20" + "helm.sh/hook-weight": "{{ (eq $step "pre") | ternary -11 9 }}" roleRef: apiGroup: rbac.authorization.k8s.io kind: Role diff --git a/charts/matrix-stack/templates/deployment-markers/serviceaccount.yaml b/charts/matrix-stack/templates/deployment-markers/serviceaccount.yaml index 20f9612c..f5c8b4eb 100644 --- a/charts/matrix-stack/templates/deployment-markers/serviceaccount.yaml +++ b/charts/matrix-stack/templates/deployment-markers/serviceaccount.yaml @@ -8,10 +8,10 @@ SPDX-License-Identifier: AGPL-3.0-only {{- with $.Values.deploymentMarkers -}} {{- if .enabled -}} {{- if and .enabled (include "element-io.deployment-markers.markers" (dict "root" $)) -}} -{{- include "element-io.ess-library.serviceAccount" (dict "root" $ "context" (dict "componentValues" . "nameSuffix" (printf "deployment-markers-%s" $step) "extraAnnotations" (dict "helm.sh/hook" (printf "%s-install,%s-upgrade" $step $step) "helm.sh/hook-weight" "-20"))) }} +{{- $weight := (eq $step "pre") | ternary "-11" "9" -}} +{{- include "element-io.ess-library.serviceAccount" (dict "root" $ "context" (dict "componentValues" . "nameSuffix" (printf "deployment-markers-%s" $step) "extraAnnotations" (dict "helm.sh/hook" (printf "%s-install,%s-upgrade" $step $step) "helm.sh/hook-weight" $weight))) }} --- {{- end }} {{- end }} {{- end }} {{- end }} - diff --git a/charts/matrix-stack/templates/init-secrets/role.yaml b/charts/matrix-stack/templates/init-secrets/role.yaml index 1b4816ec..52212fe8 100644 --- a/charts/matrix-stack/templates/init-secrets/role.yaml +++ b/charts/matrix-stack/templates/init-secrets/role.yaml @@ -15,7 +15,7 @@ metadata: {{- include "element-io.init-secrets.labels" (dict "root" $ "context" .) | nindent 4 }} annotations: "helm.sh/hook": pre-install,pre-upgrade - "helm.sh/hook-weight": "-10" + "helm.sh/hook-weight": "-11" rules: {{/* https://kubernetes.io/docs/reference/access-authn-authz/rbac/#referring-to-resources diff --git a/charts/matrix-stack/templates/init-secrets/rolebinding.yaml b/charts/matrix-stack/templates/init-secrets/rolebinding.yaml index 826f9575..889075b9 100644 --- a/charts/matrix-stack/templates/init-secrets/rolebinding.yaml +++ b/charts/matrix-stack/templates/init-secrets/rolebinding.yaml @@ -16,7 +16,7 @@ metadata: {{- include "element-io.init-secrets.labels" (dict "root" $ "context" .) | nindent 4 }} annotations: "helm.sh/hook": pre-install,pre-upgrade - "helm.sh/hook-weight": "-10" + "helm.sh/hook-weight": "-11" roleRef: apiGroup: rbac.authorization.k8s.io kind: Role diff --git a/charts/matrix-stack/templates/init-secrets/serviceaccount.yaml b/charts/matrix-stack/templates/init-secrets/serviceaccount.yaml index 00944335..5bbc2683 100644 --- a/charts/matrix-stack/templates/init-secrets/serviceaccount.yaml +++ b/charts/matrix-stack/templates/init-secrets/serviceaccount.yaml @@ -7,8 +7,7 @@ SPDX-License-Identifier: AGPL-3.0-only {{- with .Values.initSecrets -}} {{- if .enabled -}} {{- if and .enabled (include "element-io.init-secrets.generated-secrets" (dict "root" $)) -}} -{{- include "element-io.ess-library.serviceAccount" (dict "root" $ "context" (dict "componentValues" . "nameSuffix" "init-secrets" "extraAnnotations" (dict "helm.sh/hook" "pre-install,pre-upgrade" "helm.sh/hook-weight" "-10"))) }} +{{- include "element-io.ess-library.serviceAccount" (dict "root" $ "context" (dict "componentValues" . "nameSuffix" "init-secrets" "extraAnnotations" (dict "helm.sh/hook" "pre-install,pre-upgrade" "helm.sh/hook-weight" "-11"))) }} {{- end }} {{- end }} {{- end }} - diff --git a/charts/matrix-stack/templates/matrix-authentication-service/configmap_hook.yaml b/charts/matrix-stack/templates/matrix-authentication-service/configmap_hook.yaml index 126ad5f7..56f4fd64 100644 --- a/charts/matrix-stack/templates/matrix-authentication-service/configmap_hook.yaml +++ b/charts/matrix-stack/templates/matrix-authentication-service/configmap_hook.yaml @@ -17,7 +17,7 @@ metadata: {{- include "element-io.matrix-authentication-service.labels" (dict "root" $ "context" $masContext) | nindent 4 }} annotations: "helm.sh/hook": pre-install,pre-upgrade - "helm.sh/hook-weight": "-5" + "helm.sh/hook-weight": "-1" data: {{- include "element-io.matrix-authentication-service.configmap-data" (dict "root" $ "context" $masContext) | nindent 2 -}} {{ end -}} diff --git a/charts/matrix-stack/templates/matrix-authentication-service/secret_hook.yaml b/charts/matrix-stack/templates/matrix-authentication-service/secret_hook.yaml index 40e32254..19d5d4bc 100644 --- a/charts/matrix-stack/templates/matrix-authentication-service/secret_hook.yaml +++ b/charts/matrix-stack/templates/matrix-authentication-service/secret_hook.yaml @@ -22,7 +22,7 @@ metadata: {{- include "element-io.matrix-authentication-service.labels" (dict "root" $ "context" $masContext) | nindent 4 }} annotations: "helm.sh/hook": pre-install,pre-upgrade - "helm.sh/hook-weight": "-5" + "helm.sh/hook-weight": "-1" type: Opaque data: {{- if .syn2mas.enabled }} diff --git a/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_job.yaml b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_job.yaml index 6eceb3db..de75e5d1 100644 --- a/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_job.yaml +++ b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_job.yaml @@ -37,23 +37,11 @@ metadata: ) }} annotations: {{- if .dryRun }} -{{- /* -Hook Weights are -- -25 : The syn2mas dryRun job, running after MAS pod which initialized its DB -- 0 : The deployment marker job, we want it not to update any marker if the dryRun is not completed -*/}} "helm.sh/hook": post-install,post-upgrade - "helm.sh/hook-weight": "-25" {{- else }} "helm.sh/hook": pre-install,pre-upgrade -{{- /* -Hook Weights are -- -10 : The initSecret hook generating secrets used by the syn2mas job -- -5 : The MAS & synapse secret & configMap for the hook, so that they are created before the job -- 0 : The job itself, so that it is run after the secrets and configs are created -*/}} - "helm.sh/hook-weight": "0" {{- end }} + "helm.sh/hook-weight": "0" name: {{ $.Release.Name }}-syn2mas namespace: {{ $.Release.Namespace }} spec: diff --git a/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_role.yaml b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_role.yaml index 2a013d0d..8bcf4908 100644 --- a/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_role.yaml +++ b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_role.yaml @@ -16,13 +16,7 @@ metadata: {{- include "element-io.syn2mas.labels" (dict "root" $ "context" .) | nindent 4 }} annotations: "helm.sh/hook": pre-install,pre-upgrade -{{- /* -Hook Weights are -- -10 : The initSecret hook generating secrets used by the syn2mas job -- -5 : The MAS & synapse secret & configMap for the hook, so that they are created before the job -- 0 : The job itself, so that it is run after the secrets and configs are created -*/}} - "helm.sh/hook-weight": "0" + "helm.sh/hook-weight": "-1" rules: - apiGroups: [""] resources: ["pods"] diff --git a/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_rolebinding.yaml b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_rolebinding.yaml index 8f81250f..13680cb7 100644 --- a/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_rolebinding.yaml +++ b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_rolebinding.yaml @@ -17,13 +17,7 @@ metadata: {{- include "element-io.syn2mas.labels" (dict "root" $ "context" .) | nindent 4 }} annotations: "helm.sh/hook": pre-install,pre-upgrade -{{- /* -Hook Weights are -- -10 : The initSecret hook generating secrets used by the syn2mas job -- -5 : The MAS & synapse secret & configMap for the hook, so that they are created before the job -- 0 : The job itself, so that it is run after the secrets and configs are created -*/}} - "helm.sh/hook-weight": "0" + "helm.sh/hook-weight": "-1" roleRef: apiGroup: rbac.authorization.k8s.io kind: Role diff --git a/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_serviceaccount.yaml b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_serviceaccount.yaml index 60b7f6d7..e59a1cff 100644 --- a/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_serviceaccount.yaml +++ b/charts/matrix-stack/templates/matrix-authentication-service/syn2mas_serviceaccount.yaml @@ -7,7 +7,7 @@ SPDX-License-Identifier: AGPL-3.0-only {{- with .Values.matrixAuthenticationService -}} {{- if and .enabled .syn2mas.enabled $.Values.synapse.enabled -}} {{- with .syn2mas -}} -{{- include "element-io.ess-library.serviceAccount" (dict "root" $ "context" (dict "componentValues" . "nameSuffix" "syn2mas" "extraAnnotations" (dict "helm.sh/hook" "pre-install,pre-upgrade" "helm.sh/hook-weight" "0"))) }} +{{- include "element-io.ess-library.serviceAccount" (dict "root" $ "context" (dict "componentValues" . "nameSuffix" "syn2mas" "extraAnnotations" (dict "helm.sh/hook" "pre-install,pre-upgrade" "helm.sh/hook-weight" "-1"))) }} {{- end }} {{- end }} {{- end }} 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 93912c81..73b10d81 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 @@ -17,12 +17,6 @@ metadata: namespace: {{ $.Release.Namespace }} annotations: "helm.sh/hook": pre-install,pre-upgrade -{{- /* -Hook Weights are -- -10 : The initSecret hook generating secrets used by the check config job -- -5 : The synapse secret & configMap for the hook, so that they are created before the job -- 0 : The job itself, so that it is run after the secrets and configs are created & -*/}} "helm.sh/hook-weight": "0" {{- with .checkConfigHook.annotations }} {{- toYaml . | nindent 4 }} diff --git a/charts/matrix-stack/templates/synapse/synapse_configmap_hook.yaml b/charts/matrix-stack/templates/synapse/synapse_configmap_hook.yaml index f669f83f..32b6fc34 100644 --- a/charts/matrix-stack/templates/synapse/synapse_configmap_hook.yaml +++ b/charts/matrix-stack/templates/synapse/synapse_configmap_hook.yaml @@ -16,7 +16,7 @@ metadata: {{- include "element-io.synapse-check-config.labels" (dict "root" $ "context" .) | nindent 4 }} annotations: "helm.sh/hook": pre-install,pre-upgrade - "helm.sh/hook-weight": "-5" + "helm.sh/hook-weight": "-1" name: {{ include "element-io.synapse.configmap-name" (dict "root" $ "context" (dict "isHook" true)) }} namespace: {{ $.Release.Namespace }} data: diff --git a/charts/matrix-stack/templates/synapse/synapse_secret_hook.yaml b/charts/matrix-stack/templates/synapse/synapse_secret_hook.yaml index 164d9551..b51c817a 100644 --- a/charts/matrix-stack/templates/synapse/synapse_secret_hook.yaml +++ b/charts/matrix-stack/templates/synapse/synapse_secret_hook.yaml @@ -19,7 +19,7 @@ metadata: namespace: {{ $.Release.Namespace }} annotations: "helm.sh/hook": pre-install,pre-upgrade - "helm.sh/hook-weight": "-5" + "helm.sh/hook-weight": "-1" {{- include "element-io.synapse.secret-data" (dict "root" $ "context" .) -}} {{- end -}} {{- end -}} diff --git a/charts/matrix-stack/templates/synapse/synapse_serviceaccount_hook.yaml b/charts/matrix-stack/templates/synapse/synapse_serviceaccount_hook.yaml index 3523a2e4..4964a339 100644 --- a/charts/matrix-stack/templates/synapse/synapse_serviceaccount_hook.yaml +++ b/charts/matrix-stack/templates/synapse/synapse_serviceaccount_hook.yaml @@ -9,7 +9,7 @@ SPDX-License-Identifier: AGPL-3.0-only {{- if .enabled }} {{- with .checkConfigHook }} {{ if and .enabled -}} -{{- include "element-io.ess-library.serviceAccount" (dict "root" $ "context" (dict "componentValues" . "nameSuffix" "synapse-check-config" "extraAnnotations" (dict "helm.sh/hook" "pre-install,pre-upgrade" "helm.sh/hook-weight" "0"))) }} +{{- include "element-io.ess-library.serviceAccount" (dict "root" $ "context" (dict "componentValues" . "nameSuffix" "synapse-check-config" "extraAnnotations" (dict "helm.sh/hook" "pre-install,pre-upgrade" "helm.sh/hook-weight" "-1"))) }} {{- end -}} {{- end -}} {{- end -}} diff --git a/newsfragments/880.changed.md b/newsfragments/880.changed.md new file mode 100644 index 00000000..b32e9001 --- /dev/null +++ b/newsfragments/880.changed.md @@ -0,0 +1,3 @@ +Harmonise the hook weights and reduce the number of distinct hook weight values. + +This should speed up installs and upgrades as now there are only 2 distinct pre-install/pre-upgrade hook weights. diff --git a/tests/manifests/test_helm_hooks.py b/tests/manifests/test_helm_hooks.py index 48a30a92..789118dc 100644 --- a/tests/manifests/test_helm_hooks.py +++ b/tests/manifests/test_helm_hooks.py @@ -13,7 +13,6 @@ from .utils import template_id, template_to_deployable_details @pytest.mark.parametrize("values_file", values_files_to_test) @pytest.mark.asyncio_cooperative async def test_hook_weight_is_specified_if_manifest_is_hook(templates): - hook_weight_by_deployable_hook = {} for template in templates: annotations = template["metadata"].get("annotations", {}) @@ -22,14 +21,6 @@ async def test_hook_weight_is_specified_if_manifest_is_hook(templates): assert "helm.sh/hook-weight" in annotations, ( f"{template_id(template)} is a hook ({annotations['helm.sh/hook']}) but doesn't set a hook weight" ) - deployable_details = template_to_deployable_details(template) - hook_id = f"{deployable_details.name}-{annotations['helm.sh/hook']}" - if hook_id in hook_weight_by_deployable_hook: - assert hook_weight_by_deployable_hook[hook_id] == int(annotations["helm.sh/hook-weight"]), ( - f"{template_id(template)} has a different hook weight than other manifests for the same deployable" - ) - else: - hook_weight_by_deployable_hook[hook_id] = int(annotations["helm.sh/hook-weight"]) else: assert "helm.sh/hook-weight" not in annotations, ( f"{template_id(template)} is not a hook but has set a hook weight" @@ -53,3 +44,45 @@ async def test_sets_both_install_upgrade_hooks(templates): assert ("pre-install" in hooks and "pre-upgrade" in hooks) or ( "post-install" in hooks and "post-upgrade" in hooks ), f"{template_id(template)} didn't set both the install and upgrade hook: {','.join(hooks)}" + + +@pytest.mark.parametrize("values_file", values_files_to_test) +@pytest.mark.asyncio_cooperative +async def test_hook_weights_are_well_known(templates): + for template in templates: + annotations = template["metadata"].get("annotations", {}) + + if "helm.sh/hook-weight" not in annotations: + continue + + # Manifest install order by hook weight. + # The kind order of manifests within the same weight is not respected, despire being documented as doing so + # - https://github.com/helm/helm/issues/12414 + # - https://github.com/helm/helm-www/pull/1520 + # As such the weights for Jobs are given below, and then weight for non-Jobs are -1 + # + # PRE INSTALL/UPGRADE + # -10: init secrets, pre-deployment markers + # 0: config check, syn2mas actual migration + # + # NON-HOOKS + # + # POST INSTALL/UPGRADE + # 0: syn2mas dry-run + # 10: post-deployment markers + hook_weight = int(annotations["helm.sh/hook-weight"]) + weight_adjustment = 0 if template["kind"] == "Job" else 1 + hook_weight += weight_adjustment + assert hook_weight in [-10, 0, 10], f"{template_id(template)} had an unexpected hook weight" + + deployable_details = template_to_deployable_details(template) + template_name = template["metadata"]["name"] + if deployable_details.name == "deployment-markers": + if template_name.endswith("-pre"): + assert hook_weight == -10, f"{template_id(template)} should have a weight of {-10 - weight_adjustment}" + else: + assert hook_weight == 10, f"{template_id(template)} should have a weight of {10 - weight_adjustment}" + elif deployable_details.name == "init-secrets": + assert hook_weight == -10, f"{template_id(template)} should have a weight of {-10 - weight_adjustment}" + else: + assert hook_weight == 0, f"{template_id(template)} should have a weight of {0 - weight_adjustment}" diff --git a/tests/manifests/utils.py b/tests/manifests/utils.py index 0a6f1ea3..b876b540 100644 --- a/tests/manifests/utils.py +++ b/tests/manifests/utils.py @@ -126,7 +126,7 @@ def generated_secrets(release_name: str, values: dict[str, Any], helm_generated_ # We simulate the fact that it exists after initSecret # using the hook weight. # Actually it does not have any - # but this is necessary for tests/manifests/test_configs_and_mounts_consistency.py + # but this is necessary for tests/manifests/test_configs_consistency.py "helm.sh/hook-weight": "-9" }, }, @@ -171,7 +171,7 @@ def external_secrets(release_name, values): # We simulate the fact that it exists before the chart deployment # using the hook weight. # Actually it does not have any - # but this is necessary for tests/manifests/test_configs_and_mounts_consistency.py + # but this is necessary for tests/manifests/test_configs_consistency.py "helm.sh/hook-weight": "-100" }, },