|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | + |
| 14 | +from testcontainers.core.config import MAX_TRIES |
| 15 | +from testcontainers.core.container import DockerContainer |
| 16 | +from testcontainers.core.waiting_utils import wait_for_logs |
| 17 | + |
| 18 | + |
| 19 | +class K3SContainer(DockerContainer): |
| 20 | + """ |
| 21 | + K3S container. |
| 22 | +
|
| 23 | + Example: |
| 24 | +
|
| 25 | + .. doctest:: |
| 26 | +
|
| 27 | + >>> import yaml |
| 28 | + >>> from testcontainers.k3s import K3SContainer |
| 29 | + >>> from kubernetes import client, config |
| 30 | +
|
| 31 | + >>> with K3SContainer() as k3s: |
| 32 | + ... config.load_kube_config_from_dict(yaml.safe_load(k3s.config_yaml())) |
| 33 | + ... pod = client.CoreV1Api().list_pod_for_all_namespaces(limit=1) |
| 34 | + ... assert len(pod.items) > 0, "Unable to get running nodes from k3s cluster" |
| 35 | + """ |
| 36 | + |
| 37 | + KUBE_SECURE_PORT = 6443 |
| 38 | + RANCHER_WEBHOOK_PORT = 8443 |
| 39 | + |
| 40 | + def __init__(self, image="rancher/k3s:latest", **kwargs) -> None: |
| 41 | + super(K3SContainer, self).__init__(image, **kwargs) |
| 42 | + self.with_exposed_ports(self.KUBE_SECURE_PORT, self.RANCHER_WEBHOOK_PORT) |
| 43 | + self.with_env("K3S_URL", f'https://{self.get_container_host_ip()}:{self.KUBE_SECURE_PORT}') |
| 44 | + self.with_command("server --disable traefik --tls-san=" + self.get_container_host_ip()) |
| 45 | + self.with_kwargs(privileged=True, tmpfs={"/run": "", "/var/run": ""}) |
| 46 | + self.with_volume_mapping("/sys/fs/cgroup", "/sys/fs/cgroup", "rw") |
| 47 | + |
| 48 | + def _connect(self) -> None: |
| 49 | + wait_for_logs(self, predicate="Node controller sync successful", timeout=MAX_TRIES) |
| 50 | + |
| 51 | + def start(self) -> "K3SContainer": |
| 52 | + super().start() |
| 53 | + self._connect() |
| 54 | + return self |
| 55 | + |
| 56 | + def config_yaml(self) -> str: |
| 57 | + """This function returns the kubernetes config yaml which can be used |
| 58 | + to initialise k8s client |
| 59 | + """ |
| 60 | + execution = self.get_wrapped_container().exec_run(['cat', '/etc/rancher/k3s/k3s.yaml']) |
| 61 | + config_yaml = execution.output.decode('utf-8') \ |
| 62 | + .replace(f'https://127.0.0.1:{self.KUBE_SECURE_PORT}', |
| 63 | + f'https://{self.get_container_host_ip()}:' |
| 64 | + f'{self.get_exposed_port(self.KUBE_SECURE_PORT)}') |
| 65 | + return config_yaml |
0 commit comments