From 15dc4ba6d81eb32ca8713e4e2fef72b91ed9d396 Mon Sep 17 00:00:00 2001 From: Gael Goinvic Date: Fri, 16 Oct 2026 13:39:10 +0000 Subject: [PATCH] Add a helper to wait for charts used in tests --- newsfragments/1687.internal.md | 1 + tests/integration/fixtures/cluster.py | 80 ++++--------------------- tests/integration/lib/utils.py | 85 +++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 69 deletions(-) create mode 100644 newsfragments/1687.internal.md diff --git a/newsfragments/1687.internal.md b/newsfragments/1687.internal.md new file mode 100644 index 00000000..fe089169 --- /dev/null +++ b/newsfragments/1687.internal.md @@ -0,0 +1 @@ +CI: Implement a helper function to actively chart resources in order to Helm v4 flakes. diff --git a/tests/integration/fixtures/cluster.py b/tests/integration/fixtures/cluster.py index ae7adddb..7550a35d 100644 --- a/tests/integration/fixtures/cluster.py +++ b/tests/integration/fixtures/cluster.py @@ -25,13 +25,11 @@ from lightkube.config.kubeconfig import SingleConfig from lightkube.core.generic_client import GenericAsyncClient from lightkube.generic_resource import create_global_resource, create_namespaced_resource from lightkube.models.meta_v1 import ObjectMeta -from lightkube.resources.apiextensions_v1 import CustomResourceDefinition -from lightkube.resources.apps_v1 import Deployment from lightkube.resources.core_v1 import Namespace, Secret, Service from pytest_kubernetes.options import ClusterOptions from pytest_kubernetes.providers import K3dManagerBase -from ..lib.utils import async_retry_with_timeout, b64encode, chart_from_ci_cache +from ..lib.utils import b64encode, chart_from_ci_cache, install_release_and_wait_for_resources from .data import ESSData ClusterIssuer = create_global_resource( @@ -166,13 +164,12 @@ async def cert_manager(helm_client, kube_client): return chart = await chart_from_ci_cache(helm_client, "oci://quay.io/jetstack/charts/cert-manager") - # The chart ships its CRDs in templates. We don't use helm's --wait: - # in flaky runs helm 4's status watcher never sees the CRDs as ready, - # even though they are established, and hangs until the timeout. - # See https://github.com/helm/helm/issues/32615 - # We also don't use --atomic/--rollback-on-failure as it implies --wait. - # The readiness of the Deployments and CRDs is polled below instead. - await helm_client.install_or_upgrade_release( + # The chart ships its CRDs in templates. + # The webhook needs the Deployments to be ready to accept the certificate created right after this fixture. + # We create Certificates and ClusterIssuers from these CRDs right after this fixture. + await install_release_and_wait_for_resources( + helm_client, + kube_client, "cert-manager", chart, yaml.safe_load((Path(__file__).parent / "files/charts/cert-manager.yml").open()), @@ -180,42 +177,6 @@ async def cert_manager(helm_client, kube_client): create_namespace=True, ) - async def _deployments_are_ready(): - # The webhook needs the Deployments to be ready - # to accept the certificate created right after this fixture - deployments = [deployment async for deployment in kube_client.list(Deployment, namespace="cert-manager")] - assert deployments, "No cert-manager Deployments found" - for deployment in deployments: - conditions = deployment.status.conditions if deployment.status else [] - assert any(condition.type == "Available" and condition.status == "True" for condition in conditions), ( - f"Deployment {deployment.metadata.name} not ready, conditions: {conditions}" - ) - - # Pulling images on a cold runner can take a while, so we allow up to 5 minutes - await async_retry_with_timeout( - _deployments_are_ready, - max_retries=150, - delay=2, - timeout_seconds=300, - ) - - async def _crds_are_established(): - # We create Certificates and ClusterIssuers from these CRDs - # right after this fixture - for crd_name in ("certificates.cert-manager.io", "clusterissuers.cert-manager.io"): - crd = await kube_client.get(CustomResourceDefinition, name=crd_name) - conditions = crd.status.conditions if crd.status else [] - assert any(condition.type == "Established" and condition.status == "True" for condition in conditions), ( - f"{crd_name} CRD not established, conditions: {conditions}" - ) - - await async_retry_with_timeout( - _crds_are_established, - max_retries=30, - delay=2, - timeout_seconds=60, - ) - ca_folder = Path(os.environ.get("HOME")) / ".config" / "ess-helm-ca" if not ca_folder.exists(): ca_folder.mkdir() @@ -285,13 +246,10 @@ async def prometheus_operator_crds(helm_client: pyhelm3.Client, kube_client: Asy helm_client, "oci://ghcr.io/prometheus-community/charts/prometheus-operator-crds", ) - # The release only holds CRDs. We don't use helm's --wait: - # in flaky runs helm 4's status watcher never sees the CRDs as - # ready, even though they are established, and hangs until the timeout. - # The ServiceMonitor CRD readiness is polled below instead. - # See https://github.com/helm/helm/issues/32615 - # We also don't use --atomic/--rollback-on-failure as it implies --wait. - await helm_client.install_or_upgrade_release( + # The release only holds CRDs. + await install_release_and_wait_for_resources( + helm_client, + kube_client, "prometheus-operator-crds", chart, {}, @@ -299,22 +257,6 @@ async def prometheus_operator_crds(helm_client: pyhelm3.Client, kube_client: Asy create_namespace=True, ) - async def _service_monitor_crd_is_established(): - # The ESS chart only creates ServiceMonitors from these CRDs, - # so this is the only CRD we need to be ready before continuing - crd = await kube_client.get(CustomResourceDefinition, name="servicemonitors.monitoring.coreos.com") - conditions = crd.status.conditions if crd.status else [] - assert any(condition.type == "Established" and condition.status == "True" for condition in conditions), ( - f"ServiceMonitor CRD not established, conditions: {conditions}" - ) - - await async_retry_with_timeout( - _service_monitor_crd_is_established, - max_retries=30, - delay=2, - timeout_seconds=60, - ) - @pytest.fixture(scope="session") async def ess_namespace(cluster: PotentiallyExistingK3dCluster, kube_client: AsyncClient, generated_data: ESSData): diff --git a/tests/integration/lib/utils.py b/tests/integration/lib/utils.py index 18638b3e..fdc53d42 100644 --- a/tests/integration/lib/utils.py +++ b/tests/integration/lib/utils.py @@ -28,6 +28,7 @@ from lightkube.generic_resource import ( get_generic_resource, ) from lightkube.resources.apiextensions_v1 import CustomResourceDefinition +from lightkube.resources.apps_v1 import Deployment from lightkube.resources.core_v1 import Pod from pytest_kubernetes.providers import AClusterManager @@ -293,6 +294,90 @@ async def chart_from_ci_cache(helm_client: pyhelm3.Client, chart_ref: str) -> py return await helm_client.get_chart(chart_ref) +async def install_release_and_wait_for_resources( + helm_client: pyhelm3.Client, + kube_client: AsyncClient, + release_name: str, + chart: pyhelm3.Chart, + values: dict, + namespace: str, + create_namespace: bool, +): + """Install or upgrade a Helm release without --wait, then poll its resources until ready. + + We don't use helm's --wait: in flaky runs helm 4's status watcher never + sees the resources as ready, even though they are, and hangs until the timeout. + See https://github.com/helm/helm/issues/32615 + We also don't use --atomic/--rollback-on-failure as it implies --wait. + + The Deployments and CustomResourceDefinitions of the release are read from + its manifest, and their readiness is polled instead. + Note that CRDs placed in the chart's `crds/` folder are not part of the + release manifest, so they are not waited on. + """ + revision = await helm_client.install_or_upgrade_release( + release_name, + chart, + values, + namespace=namespace, + create_namespace=create_namespace, + ) + + # The manifest does not always carry the namespace. Helm then installs + # the resource in the release namespace. + deployments = [] + crd_names = [] + for resource in await revision.resources(): + # The manifest can hold empty documents between its --- separators. + # They are parsed as None, skip them. + if not resource: + continue + kind = resource.get("kind") + metadata = resource.get("metadata") or {} + name = metadata.get("name") + if not name: + continue + if kind == "Deployment": + deployments.append((name, metadata.get("namespace") or namespace)) + elif kind == "CustomResourceDefinition": + crd_names.append(name) + + if deployments: + + async def _deployments_are_ready(): + for deployment_name, deployment_namespace in deployments: + deployment = await kube_client.get(Deployment, name=deployment_name, namespace=deployment_namespace) + conditions = deployment.status.conditions if deployment.status else [] + assert any(condition.type == "Available" and condition.status == "True" for condition in conditions), ( + f"Deployment {deployment_name} not ready, conditions: {conditions}" + ) + + # Pulling images on a cold runner can take a while, so we allow up to 5 minutes + await async_retry_with_timeout( + _deployments_are_ready, + max_retries=150, + delay=2, + timeout_seconds=300, + ) + + if crd_names: + + async def _crds_are_established(): + for crd_name in crd_names: + crd = await kube_client.get(CustomResourceDefinition, name=crd_name) + conditions = crd.status.conditions if crd.status else [] + assert any( + condition.type == "Established" and condition.status == "True" for condition in conditions + ), f"{crd_name} CRD not established, conditions: {conditions}" + + await async_retry_with_timeout( + _crds_are_established, + max_retries=30, + delay=2, + timeout_seconds=60, + ) + + async def async_iterable_payload(value: AsyncIterator[bytes], file_size: int) -> aiohttp.payload.AsyncIterablePayload: # Workaround to the fact that aiohttp hardcodes sending a chuncked upload # when it receives an async iterator