Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(eks):privileged container (#614)
Nahuel Nucera428e6727fe93
diff --git a/ministack/services/eks.py b/ministack/services/eks.pyindex e59a8426..b557d293 100644--- a/ministack/services/eks.py+++ b/ministack/services/eks.py@@ -173,6 +173,46 @@ def _wait_for_port(host, port, timeout=30):return False+def _k3s_run_kwargs(name: str, port: int, ms_network: str | None = None) -> dict:+ """Build the docker run kwargs for a k3s server container.++ `privileged=True` is required: k3s server mode remounts `/sys/fs/cgroup`,+ which the granular `cap_add` list below cannot grant. Without it k3s+ fails on boot with "failed to evacuate root cgroup: mkdir+ /sys/fs/cgroup/init: read-only file system" (issue #611). The cap_add+ list and unconfined security_opt are kept as defence-in-depth so that+ hardened Docker setups still get the right capability set.+ """+ run_kwargs = dict(+ image=apply_image_prefix(EKS_K3S_IMAGE),+ command=["server",+ "--disable=traefik,metrics-server,servicelb",+ "--tls-san=0.0.0.0",+ "--https-listen-port=6443"],+ detach=True,+ privileged=True,+ cap_add=[+ "SYS_ADMIN", "NET_ADMIN", "NET_RAW", "NET_BIND_SERVICE",+ "SYS_PTRACE", "SYS_RESOURCE", "SYS_CHROOT",+ "DAC_OVERRIDE", "DAC_READ_SEARCH",+ "FOWNER", "FSETID", "CHOWN", "MKNOD",+ "KILL", "SETGID", "SETUID", "SETPCAP", "SETFCAP",+ "AUDIT_WRITE",+ ],+ security_opt=["seccomp=unconfined", "apparmor=unconfined"],+ devices=["/dev/fuse"],+ ports={"6443/tcp": port},+ name=f"ministack-eks-{name}",+ labels={"ministack": "eks", "cluster_name": name},+ environment={"K3S_KUBECONFIG_MODE": "644"},+ volumes={"/lib/modules": {"bind": "/lib/modules", "mode": "ro"}},+ tmpfs={"/run": "", "/var/run": "", "/tmp": ""},+ )+ if ms_network:+ run_kwargs["network"] = ms_network+ return run_kwargs++def _stop_all_k3s():"""Stop all k3s containers managed by MiniStack."""client = _get_docker()@@ -276,32 +316,7 @@ def _create_cluster(body):returntry:ms_network = _get_ministack_network(client)- run_kwargs = dict(- image=apply_image_prefix(EKS_K3S_IMAGE),- command=["server",- "--disable=traefik,metrics-server,servicelb",- "--tls-san=0.0.0.0",- "--https-listen-port=6443"],- detach=True,- cap_add=[- "SYS_ADMIN", "NET_ADMIN", "NET_RAW", "NET_BIND_SERVICE",- "SYS_PTRACE", "SYS_RESOURCE", "SYS_CHROOT",- "DAC_OVERRIDE", "DAC_READ_SEARCH",- "FOWNER", "FSETID", "CHOWN", "MKNOD",- "KILL", "SETGID", "SETUID", "SETPCAP", "SETFCAP",- "AUDIT_WRITE",- ],- security_opt=["seccomp=unconfined", "apparmor=unconfined"],- devices=["/dev/fuse"],- ports={"6443/tcp": port},- name=f"ministack-eks-{name}",- labels={"ministack": "eks", "cluster_name": name},- environment={"K3S_KUBECONFIG_MODE": "644"},- volumes={"/lib/modules": {"bind": "/lib/modules", "mode": "ro"}},- tmpfs={"/run": "", "/var/run": "", "/tmp": ""},- )- if ms_network:- run_kwargs["network"] = ms_network+ run_kwargs = _k3s_run_kwargs(name=name, port=port, ms_network=ms_network)container = client.containers.run(**run_kwargs)cluster["_docker_id"] = container.iddiff --git a/tests/test_eks.py b/tests/test_eks.pyindex 38fb17a6..89a1dec7 100644--- a/tests/test_eks.py+++ b/tests/test_eks.py@@ -251,3 +251,53 @@ def test_eks_cfn_cluster(cfn, eks):cfn.delete_stack(StackName=stack_name)time.sleep(2)+++# -- k3s container run kwargs ----------------------------------------------+#+# Issue #611: k3s requires `--privileged` to remount /sys/fs/cgroup; without+# it the container exits on boot with "failed to evacuate root cgroup". The+# kwargs builder is unit-tested in isolation so this doesn't depend on Docker+# being available in CI.+++def test_eks_k3s_run_kwargs_includes_privileged():+ """Regression for #611: k3s server mode needs privileged=True."""+ from ministack.services.eks import _k3s_run_kwargs++ kwargs = _k3s_run_kwargs(name="test-cluster", port=16443)++ assert kwargs["privileged"] is True, (+ "k3s requires privileged=True — without it the cgroup remount fails "+ "with 'failed to evacuate root cgroup' (issue #611)"+ )+++def test_eks_k3s_run_kwargs_port_mapping():+ """The 6443 port mapping must be present (the issue report flagged this+ as missing — it wasn't, but lock it in so it stays present)."""+ from ministack.services.eks import _k3s_run_kwargs++ kwargs = _k3s_run_kwargs(name="test-cluster", port=16443)+ assert kwargs["ports"] == {"6443/tcp": 16443}+++def test_eks_k3s_run_kwargs_network_optional():+ """`network` is set only when ms_network is provided."""+ from ministack.services.eks import _k3s_run_kwargs++ no_net = _k3s_run_kwargs(name="c1", port=16443)+ assert "network" not in no_net++ with_net = _k3s_run_kwargs(name="c1", port=16443, ms_network="ministack-net")+ assert with_net["network"] == "ministack-net"+++def test_eks_k3s_run_kwargs_container_name_and_labels():+ """Each cluster's k3s container is named and labelled so `_stop_all_k3s`+ can find it. Lock the shape used by that lookup."""+ from ministack.services.eks import _k3s_run_kwargs++ kwargs = _k3s_run_kwargs(name="my-cluster", port=16443)+ assert kwargs["name"] == "ministack-eks-my-cluster"+ assert kwargs["labels"] == {"ministack": "eks", "cluster_name": "my-cluster"}