-
Notifications
You must be signed in to change notification settings - Fork 312
Adds support to run lightweight kubernetes testcontainer using k3s #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7f9bc29
Adds support to run lightweight kubernetes testcontainer using k3s
ash1425 87c5c34
adds document i toctree and updates github workflow to run k3s tests
ash1425 ea446b8
incorporates review feedback and updates tests to connect to k8s clus…
ash1425 639932a
fixes failing doctest
ash1425 8e280a7
using get_container_host_ip instead of hardcoded localhost
ash1425 54722a6
Merge branch 'master' into k3s
ash1425 1089831
Merge remote-tracking branch 'upstream/main' into k3s
ash1425 a343bd0
Fixing build failures after merge
ash1425 1cb8dd8
updating requirements file from generated artifacts
ash1425 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.. autoclass:: testcontainers.k3s.K3SContainer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from setuptools import setup, find_namespace_packages | ||
|
||
description = "K3S component of testcontainers-python." | ||
|
||
setup( | ||
name="testcontainers-k3s", | ||
version="0.0.1rc1", | ||
packages=find_namespace_packages(), | ||
description=description, | ||
long_description=description, | ||
long_description_content_type="text/x-rst", | ||
url="https://github.com/testcontainers/testcontainers-python", | ||
install_requires=[ | ||
"testcontainers-core", | ||
"kubernetes", | ||
"pyyaml" | ||
], | ||
python_requires=">=3.7", | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from testcontainers.core.config import MAX_TRIES | ||
from testcontainers.core.container import DockerContainer | ||
from testcontainers.core.waiting_utils import wait_for_logs | ||
|
||
|
||
class K3SContainer(DockerContainer): | ||
""" | ||
K3S container. | ||
|
||
Example: | ||
|
||
.. doctest:: | ||
|
||
>>> import yaml | ||
>>> from testcontainers.k3s import K3SContainer | ||
>>> from kubernetes import client, config | ||
|
||
>>> with K3SContainer() as k3s: | ||
... config.load_kube_config_from_dict(yaml.safe_load(k3s.config_yaml())) | ||
... pod = client.CoreV1Api().list_pod_for_all_namespaces(limit=1) | ||
... assert len(pod.items) > 0, "Unable to get running nodes from k3s cluster" | ||
""" | ||
|
||
KUBE_SECURE_PORT = 6443 | ||
RANCHER_WEBHOOK_PORT = 8443 | ||
|
||
def __init__(self, image="rancher/k3s:latest", **kwargs) -> None: | ||
super(K3SContainer, self).__init__(image, **kwargs) | ||
self.with_exposed_ports(self.KUBE_SECURE_PORT, self.RANCHER_WEBHOOK_PORT) | ||
self.with_env("K3S_URL", f'https://{self.get_container_host_ip()}:{self.KUBE_SECURE_PORT}') | ||
self.with_command("server --disable traefik --tls-san=" + self.get_container_host_ip()) | ||
self.with_kwargs(privileged=True, tmpfs={"/run": "", "/var/run": ""}) | ||
self.with_volume_mapping("/sys/fs/cgroup", "/sys/fs/cgroup", "rw") | ||
|
||
def _connect(self) -> None: | ||
wait_for_logs(self, predicate="Node controller sync successful", timeout=MAX_TRIES) | ||
|
||
def start(self) -> "K3SContainer": | ||
super().start() | ||
self._connect() | ||
return self | ||
|
||
def config_yaml(self) -> str: | ||
"""This function returns the kubernetes config yaml which can be used | ||
to initialise k8s client | ||
""" | ||
execution = self.get_wrapped_container().exec_run(['cat', '/etc/rancher/k3s/k3s.yaml']) | ||
config_yaml = execution.output.decode('utf-8') \ | ||
.replace(f'https://127.0.0.1:{self.KUBE_SECURE_PORT}', | ||
f'https://{self.get_container_host_ip()}:' | ||
f'{self.get_exposed_port(self.KUBE_SECURE_PORT)}') | ||
return config_yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# The versions below were the current supported versions at time of writing (2022-08-11) | ||
import yaml | ||
from kubernetes import client, config | ||
|
||
from testcontainers.k3s import K3SContainer | ||
|
||
|
||
def test_docker_run_k3s(): | ||
with K3SContainer() as k3s: | ||
config.load_kube_config_from_dict(yaml.safe_load(k3s.config_yaml())) | ||
pod = client.CoreV1Api().list_pod_for_all_namespaces(limit=1) | ||
assert len(pod.items) > 0, "Unable to get running nodes from k3s cluster" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.