Test that we can configure pre-stopp values accordingly

This commit is contained in:
Gael Goinvic committed 2026-10-15 13:20:12 +00:00
1 parent deedc52746
commit a16237aeca
3 files changed
+102 -13

No files matched your search

+7
View File
@@ -29,6 +29,7 @@ class PropertyType(Enum):
LivenessProbe = "livenessProbe"
NodeSelector = "nodeSelector"
PodSecurityContext = "podSecurityContext"
PreStopSleepSeconds = "preStopSleepSeconds"
Postgres = "postgres"
PriorityClassName = "priorityClassName"
RuntimeClassName = "runtimeClassName"
@@ -266,6 +267,7 @@ class SidecarDetails(DeployableDetails):
PropertyType.InitContainers: ValuesFilePath.not_supported(),
PropertyType.NodeSelector: ValuesFilePath.not_supported(),
PropertyType.PodSecurityContext: ValuesFilePath.not_supported(),
PropertyType.PreStopSleepSeconds: ValuesFilePath.not_supported(),
PropertyType.PriorityClassName: ValuesFilePath.not_supported(),
PropertyType.RuntimeClassName: ValuesFilePath.not_supported(),
PropertyType.SchedulerName: ValuesFilePath.not_supported(),
@@ -399,6 +401,7 @@ def make_synapse_worker_sub_component(worker_name: str, worker_type: str) -> Sub
PropertyType.Image: ValuesFilePath.read_elsewhere("synapse", "image"),
PropertyType.InitContainers: ValuesFilePath.read_elsewhere("synapse", "extraInitContainers"),
PropertyType.Labels: ValuesFilePath.read_elsewhere("synapse", "labels"),
PropertyType.PreStopSleepSeconds: ValuesFilePath.read_write("synapse", "preStopSleepSeconds"),
PropertyType.Affinity: ValuesFilePath.read_elsewhere("synapse", "affinity"),
PropertyType.NodeSelector: ValuesFilePath.read_elsewhere("synapse", "nodeSelector"),
PropertyType.ContainersSecurityContext: ValuesFilePath.read_elsewhere("synapse", "containersSecurityContext"),
@@ -515,6 +518,10 @@ all_components_details = [
),
ComponentDetails(
name="haproxy",
values_file_path_overrides={
# HAProxy handles graceful shutdown itself with grace and hard-stop-after
PropertyType.PreStopSleepSeconds: ValuesFilePath.not_supported(),
},
has_additional_config=False,
has_credentials=False,
has_ingress=False,
+34 -1
View File
@@ -1,5 +1,5 @@
# Copyright 2025 New Vector Ltd
# Copyright 2025 Element Creations Ltd
# Copyright 2025-2026 Element Creations Ltd
#
# SPDX-License-Identifier: AGPL-3.0-only
@@ -79,3 +79,36 @@ async def test_haproxy_config_ends_in_2_newlines(templates):
assert haproxy_configmap["data"]["haproxy.cfg"].endswith("\n\n"), (
f"{template_id(haproxy_configmap)}/haproxy.cfg should end with at least 2 \\n"
)
@pytest.mark.parametrize("values_file", values_files_to_test)
@pytest.mark.asyncio_cooperative
async def test_haproxy_config_respects_grace_and_hard_stop_after(values, make_templates):
# HAProxy keeps its listeners open for `grace` when it starts stopping, then closes
# every remaining connection at the latest `hard-stop-after` after it starts stopping.
# This is how HAProxy drains its connections, so it must not also sleep before stopping.
haproxy_values = values.setdefault("haproxy", {})
haproxy_values["grace"] = "11s"
haproxy_values["hardStopAfter"] = "42s"
for template in await make_templates(values):
if template_to_deployable_details(template).name != "haproxy":
continue
if template["kind"] == "ConfigMap":
haproxy_configmap = template
break
else:
# HAProxy isn't deployed with this values file
return
assert "haproxy.cfg" in haproxy_configmap["data"], f"{template_id(haproxy_configmap)} didn't contain haproxy.cfg"
haproxy_cfg = haproxy_configmap["data"]["haproxy.cfg"]
assert " grace 11s" in haproxy_cfg, (
f"{template_id(haproxy_configmap)}/haproxy.cfg doesn't keep the listeners open for 11s when stopping"
)
assert " hard-stop-after 42s" in haproxy_cfg, (
f"{template_id(haproxy_configmap)}/haproxy.cfg doesn't close the remaining connections "
"42s after it starts stopping at the latest"
)
+61 -12
View File
@@ -4,8 +4,13 @@
import pytest
from . import values_files_to_test
from .utils import PERSISTENT_WORKLOAD_KINDS, iterate_pod_template
from . import PropertyType, values_files_to_test
from .utils import (
PERSISTENT_WORKLOAD_KINDS,
PodTemplateDetails,
iterate_deployables_parts,
iterate_pod_template,
)
# When a non-singleton workload is rolled out, its old pods are killed while new pods
# serve traffic. The Service's iptables rules must be updated before a pod is killed.
@@ -14,24 +19,68 @@ from .utils import PERSISTENT_WORKLOAD_KINDS, iterate_pod_template
# "handles-graceful-shutdown-natively" pod annotation and must not have the preStop sleep.
def skip_workloads_that_do_not_need_prestop_sleep(pod_template_details: PodTemplateDetails) -> bool:
pod_annotations = pod_template_details.pod_template["metadata"].get("annotations", {})
if pod_annotations.get("handles-graceful-shutdown-natively") == "yes":
return True
# Singleton workloads are not killed during a rolling rollout, so they
# do not need to wait before stopping.
return pod_template_details.deployable_details().is_singleton
@pytest.mark.parametrize("values_file", values_files_to_test)
@pytest.mark.asyncio_cooperative
async def test_non_singleton_workloads_have_prestop_sleep(templates):
async def test_non_singleton_workloads_have_prestop_sleep_by_default(values, templates):
for pod_template_details in iterate_pod_template(templates, kinds=PERSISTENT_WORKLOAD_KINDS):
pod_annotations = pod_template_details.pod_template["metadata"].get("annotations", {})
if pod_annotations.get("handles-graceful-shutdown-natively") == "yes":
continue
# Singleton workloads are not killed during a rolling rollout, so they
# do not need to wait before stopping.
if pod_template_details.deployable_details().is_singleton:
if skip_workloads_that_do_not_need_prestop_sleep(pod_template_details):
continue
for container in pod_template_details.pod_template["spec"]["containers"]:
id = f"{pod_template_details.manifest_id}/{container['name']}"
deployable_details = pod_template_details.deployable_details()
expected_sleep = deployable_details.get_helm_values(values, PropertyType.PreStopSleepSeconds, 3)
prestop_sleep = container.get("lifecycle", {}).get("preStop", {}).get("sleep", {}).get("seconds")
assert prestop_sleep == 3, (
f"{id} should sleep 3 seconds before stopping. "
assert prestop_sleep == expected_sleep, (
f"{id} should sleep {expected_sleep} seconds before stopping. "
"This gives the Service time to update its iptables rules before the pod is killed"
)
@pytest.mark.parametrize("values_file", values_files_to_test)
@pytest.mark.asyncio_cooperative
async def test_non_singleton_workloads_respect_prestop_sleep(values, make_templates):
# We have a counter that increments for each preStopSleepSeconds field for each deployable details
# That way we can assert a) the correct value is going into the correct field and
# b) that the correct part of the values file is being used
counter = 100
def set_prestop_sleep_details(deployable_details):
nonlocal counter
counter += 1
deployable_details.set_helm_values(values, PropertyType.PreStopSleepSeconds, counter)
iterate_deployables_parts(
set_prestop_sleep_details,
lambda deployable_details: (
deployable_details.has_workloads
and not deployable_details.is_singleton
and not deployable_details.is_hook
and deployable_details.get_values_file_path(PropertyType.PreStopSleepSeconds).write_path is not None
),
)
for pod_template_details in iterate_pod_template(await make_templates(values), kinds=PERSISTENT_WORKLOAD_KINDS):
if skip_workloads_that_do_not_need_prestop_sleep(pod_template_details):
continue
for container in pod_template_details.pod_template["spec"]["containers"]:
id = f"{pod_template_details.manifest_id}/{container['name']}"
deployable_details = pod_template_details.deployable_details()
expected_sleep = deployable_details.get_helm_values(values, PropertyType.PreStopSleepSeconds)
prestop_sleep = container.get("lifecycle", {}).get("preStop", {}).get("sleep", {}).get("seconds")
assert prestop_sleep == expected_sleep, (
f"{id} should sleep {expected_sleep} seconds before stopping. "
"This gives the Service time to update its iptables rules before the pod is killed"
)