Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions kuma/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os
import platform
from contextlib import contextmanager

import pytest

from datadog_checks.dev.kind import kind_run
from datadog_checks.dev.kind import ClusterConfig, kind_run
from datadog_checks.dev.subprocess import run_command

KUMA_NAMESPACE = 'kuma-system'
Expand All @@ -14,6 +16,68 @@
KUMA_METRICS_ENDPOINT = f'http://{KUMA_SERVICE}.{KUMA_NAMESPACE}.svc.cluster.local:5680/metrics'


def _kuma_images():
# Images the Kuma Helm chart pulls from Docker Hub during a fresh install
# (the control-plane deployment and the CRD-install hook job). Preloading
# them into the kind nodes avoids slow or rate-limited pulls from docker.io
# on constrained connections; the chart defaults to `imagePullPolicy:
# IfNotPresent`, so the side-loaded copies are used. The `kubectl` hook
# image is pulled from registry.k8s.io (fast) and left to pull normally.
kuma_version = os.environ.get('KUMA_VERSION', '2.10.6')
return [
f'kumahq/kuma-cp:{kuma_version}',
f'kumahq/kumactl:{kuma_version}',
]


def _platform_spec():
# `docker save --platform` only accepts a single os/arch pair; default to
# the host platform so the saved archive contains exactly the blobs that
# were pulled (and no cross-platform references that break `kind load`).
return f'{platform.system().lower()}/{platform.machine()}'


@contextmanager
def _pull_images(images):
for image in images:
run_command(['docker', 'pull', '--platform', _platform_spec(), image], check=True)
yield


class _PreloadImages:
"""Load the Kuma images into the kind cluster before `setup_kuma` runs.

Uses `docker save --platform` + `kind load image-archive` rather than
`kind load docker-image`: on Docker daemons using the containerd image
store, `docker save` of a multi-arch image emits an OCI index referencing
blobs for every platform, and `kind load docker-image` (which pipes that to
`ctr images import --all-platforms`) then fails with "content digest ...
not found" for the non-host platforms. Saving a single-platform archive
sidesteps this and keeps the original image name so the chart's image
references resolve to the side-loaded copies.
"""

def __init__(self, images):
self.images = images
self.cluster_name = None

def add_cluster_info(self, cluster_config: ClusterConfig):
self.cluster_name = cluster_config.cluster_name

def __call__(self):
if self.cluster_name is None:
raise RuntimeError('cluster_name must be set before preloading images')

spec = _platform_spec()
for image in self.images:
archive = f'/tmp/{image.replace("/", "_").replace(":", "_")}.tar'
run_command(['docker', 'save', '--platform', spec, image, '-o', archive], check=True)
run_command(
['kind', 'load', 'image-archive', archive, '--name', self.cluster_name],
check=True,
)


def setup_kuma():
kuma_version = os.environ.get('KUMA_VERSION', '2.10.6')
run_command(['kubectl', 'create', 'namespace', KUMA_NAMESPACE], check=True)
Expand Down Expand Up @@ -51,7 +115,11 @@ def setup_kuma():

@pytest.fixture(scope='session')
def dd_environment(dd_save_state):
with kind_run(conditions=[setup_kuma]) as kubeconfig:
images = _kuma_images()
with kind_run(
wrappers=[_pull_images(images)],
conditions=[_PreloadImages(images), setup_kuma],
) as kubeconfig:
instance = {'openmetrics_endpoint': KUMA_METRICS_ENDPOINT}
metadata = {
'agent_type': 'kubernetes',
Expand Down
Loading