Skip to content

Commit f1c70e8

Browse files
authored
Merge pull request containerd#9470 from lengrongfu/feat/add-e2e-for-systemdcgroup
add use systemd cgroup e2e
2 parents e1006c0 + 095131a commit f1c70e8

File tree

8 files changed

+154
-8
lines changed

8 files changed

+154
-8
lines changed

.github/workflows/ci.yml

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ jobs:
219219
fail-fast: false
220220
matrix:
221221
os: [windows-2022, windows-2019]
222+
cgroup_driver: [cgroupfs]
222223

223224
defaults:
224225
run:
@@ -345,12 +346,14 @@ jobs:
345346
- name: CRI Integration Test
346347
env:
347348
TEST_IMAGE_LIST: ${{github.workspace}}/repolist.toml
349+
CGROUP_DRIVER: ${{ matrix.cgroup_driver }}
348350
run: |
349351
make cri-integration
350352
351353
- name: cri-tools critest
352354
env:
353355
CRI_TEST_IMAGES: ${{github.workspace}}/cri-test-images.yaml
356+
CGROUP_DRIVER: ${{ matrix.cgroup_driver }}
354357
shell: powershell
355358
run: |
356359
Start-Process -FilePath containerd.exe -NoNewWindow -RedirectStandardError true -PassThru
@@ -367,7 +370,7 @@ jobs:
367370
- uses: actions/upload-artifact@v4
368371
if: always()
369372
with:
370-
name: TestResults ${{ matrix.os }}
373+
name: TestResults ${{ matrix.os }} ${{ matrix.cgroup_driver }}
371374
path: |
372375
${{github.workspace}}/*-junit.xml
373376
${{github.workspace}}/*-gotest.json
@@ -388,6 +391,7 @@ jobs:
388391
os: [ubuntu-22.04, ubuntu-24.04, actuated-arm64-4cpu-16gb]
389392
exclude:
390393
- os: ${{ github.repository != 'containerd/containerd' && 'actuated-arm64-4cpu-16gb' }}
394+
cgroup_driver: [cgroupfs, systemd]
391395

392396
env:
393397
GOTEST: gotestsum --
@@ -476,7 +480,15 @@ jobs:
476480
- name: CRI Integration Test
477481
env:
478482
TEST_RUNTIME: ${{ matrix.runtime }}
483+
CGROUP_DRIVER: ${{ matrix.cgroup_driver }}
484+
RUNC_FLAVOR: ${{ matrix.runc }}
479485
run: |
486+
cat /sys/fs/cgroup/cgroup.controllers
487+
systemctl status
488+
[ "${RUNC_FLAVOR}" == "crun" ] && {
489+
export XDG_RUNTIME_DIR=/run/user/$(id -u)
490+
}
491+
runc --version
480492
CONTAINERD_RUNTIME=$TEST_RUNTIME make cri-integration
481493
482494
- name: Fix up for actuated
@@ -488,6 +500,7 @@ jobs:
488500
- name: cri-tools critest
489501
env:
490502
TEST_RUNTIME: ${{ matrix.runtime }}
503+
CGROUP_DRIVER: ${{ matrix.cgroup_driver }}
491504
run: |
492505
env
493506
sudo -E PATH=$PATH ./script/critest.sh "${{github.workspace}}/report"
@@ -510,7 +523,7 @@ jobs:
510523
- uses: actions/upload-artifact@v4
511524
if: always()
512525
with:
513-
name: TestResults ${{ matrix.runtime }} ${{matrix.runc}} ${{ matrix.os }}
526+
name: TestResults ${{ matrix.runtime }} ${{matrix.runc}} ${{ matrix.os }} ${{ matrix.cgroup_driver }}
514527
path: |
515528
*-junit.xml
516529
*-gotest.json
@@ -534,9 +547,10 @@ jobs:
534547
# https://github.com/containerd/containerd/pull/10297
535548
- almalinux/8
536549
- rockylinux/[email protected]
550+
cgroup_driver: [cgroupfs, systemd]
537551
env:
538552
BOX: ${{ matrix.box }}
539-
553+
CGROUP_DRIVER: ${{ matrix.cgroup_driver }}
540554
steps:
541555
- name: Show the host info
542556
run: |
@@ -586,9 +600,9 @@ jobs:
586600
- name: test-integration
587601
run: sudo BOX=$BOX vagrant up --provision-with=selinux,install-runc,install-gotestsum,test-integration
588602
- name: test-cri-integration
589-
run: sudo BOX=$BOX vagrant up --provision-with=selinux,install-runc,install-gotestsum,test-cri-integration
603+
run: sudo BOX=$BOX CGROUP_DRIVER=$CGROUP_DRIVER vagrant up --provision-with=selinux,install-runc,install-gotestsum,test-cri-integration
590604
- name: test-cri
591-
run: sudo BOX=$BOX vagrant up --provision-with=selinux,install-runc,install-gotestsum,test-cri
605+
run: sudo BOX=$BOX CGROUP_DRIVER=$CGROUP_DRIVER vagrant up --provision-with=selinux,install-runc,install-gotestsum,test-cri
592606

593607
tests-cri-in-userns:
594608
name: "CRI-in-UserNS"
@@ -597,6 +611,11 @@ jobs:
597611
timeout-minutes: 40
598612
needs: [project, linters, protos, man]
599613

614+
strategy:
615+
fail-fast: false
616+
matrix:
617+
cgroup_driver: [cgroupfs, systemd]
618+
600619
steps:
601620
- uses: actions/checkout@v4
602621
- name: Set up cgroup v2 delegation
@@ -608,11 +627,35 @@ jobs:
608627
EOF
609628
sudo systemctl daemon-reload
610629
- name: Build cri-in-userns image
611-
run: podman build --target cri-in-userns -t cri-in-userns -f ./contrib/Dockerfile.test .
630+
env:
631+
CGROUP_DRIVER: ${{ matrix.cgroup_driver }}
632+
run: |
633+
if [ "$CGROUP_DRIVER" = "systemd" ];then
634+
podman build --target cri-in-userns-systemd -t cri-in-userns-systemd -f ./contrib/Dockerfile.test .
635+
else
636+
podman build --target cri-in-userns -t cri-in-userns -f ./contrib/Dockerfile.test .
637+
fi
638+
612639
- name: Run cri-in-userns image
640+
env:
641+
CGROUP_DRIVER: ${{ matrix.cgroup_driver }}
613642
# Rootless Podman is used for testing CRI-in-UserNS
614643
# (We could use rootless Docker or rootless nerdctl, but we are using Podman here because it is preinstalled)
615-
run: podman run --rm --privileged cri-in-userns
644+
run: |
645+
if [ "$CGROUP_DRIVER" = "systemd" ];then
646+
set +e
647+
touch ./critest_exit_code.txt
648+
podman run --rm --privileged --group-add keep-groups -v ./critest_exit_code.txt:/tmp/critest_exit_code.txt cri-in-userns-systemd
649+
exit_code=`cat ./critest_exit_code.txt`
650+
echo "exit_code:"$exit_code
651+
if [ "$exit_code" -gt 0 ]; then
652+
exit 1
653+
else
654+
exit 0
655+
fi
656+
else
657+
podman run --rm --privileged cri-in-userns
658+
fi
616659
617660
tests-mac-os:
618661
name: MacOS unit tests

Vagrantfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ EOF
272272
'GOTESTSUM_JUNITFILE': ENV['GOTESTSUM_JUNITFILE'],
273273
'GOTESTSUM_JSONFILE': ENV['GOTESTSUM_JSONFILE'],
274274
'GITHUB_WORKSPACE': '',
275+
'CGROUP_DRIVER': ENV['CGROUP_DRIVER'],
275276
}
276277
sh.inline = <<~SHELL
277278
#!/usr/bin/env bash
@@ -299,6 +300,7 @@ EOF
299300
sh.env = {
300301
'GOTEST': ENV['GOTEST'] || "go test",
301302
'REPORT_DIR': ENV['REPORT_DIR'],
303+
'CGROUP_DRIVER': ENV['CGROUP_DRIVER'],
302304
}
303305
sh.inline = <<~SHELL
304306
#!/usr/bin/env bash

contrib/Dockerfile.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
# "cri-in-userns": for running critest with "CRI-in-UserNS" mode; needs Rootless Docker/Podman/nerdctl:
2828
# docker build -t containerd-test -f Dockerfile.test --target cri-in-userns ../
2929
# docker run --privileged containerd-test
30+
#
31+
# "cri-in-userns-systemd": for running critest with "CRI-in-UserNS-Systemd" mode; needs Rootless Docker/Podman/nerdctl:
32+
# docker build -t containerd-test -f Dockerfile.test --target cri-in-userns-systemd ../
33+
# touch ./critest_exit_code.txt
34+
# docker run --privileged --group-add keep-groups -v ./critest_exit_code.txt:/tmp/critest_exit_code.txt containerd-test
3035
# ------------------------------------------------------------------------------
3136

3237
ARG GOLANG_VERSION=1.23.1
@@ -111,6 +116,7 @@ CMD ["script/critest.sh", "/tmp"]
111116
# Requires Rootless Docker/Podman/nerdctl with cgroup v2 delegation: https://rootlesscontaine.rs/getting-started/common/cgroup2/
112117
# (Rootless Docker/Podman/nerdctl prepares the UserNS, so we do not need to create UserNS by ourselves)
113118
FROM critest AS cri-in-userns
119+
ENV IS_SYSTEMD_CGROUP=false
114120
COPY contrib/Dockerfile.test.d/cri-in-userns/etc_containerd_config.toml /etc/containerd/config.toml
115121
COPY contrib/Dockerfile.test.d/cri-in-userns/docker-entrypoint.sh /docker-entrypoint.sh
116122
ENTRYPOINT ["/docker-entrypoint.sh"]
@@ -119,6 +125,24 @@ ENTRYPOINT ["/docker-entrypoint.sh"]
119125
# Skip "should allow privilege escalation when (NoNewPrivis is) false": expected log "Effective uid: 0\n" (stream="stdout") not found in logs [{timestamp:{wall:974487519 ext:63761339984 loc:<nil>} stream:stdout log:Effective uid: 1000) }]
120126
CMD ["critest", "--ginkgo.skip=should support unsafe sysctls|should support safe sysctls|should allow privilege escalation when false"]
121127

128+
# cri-in-userns-systemd stage is for testing "CRI-in-UserNS-Systemd", which should be used in conjunction with
129+
# "Kubelet-in-UserNS": https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/2033-kubelet-in-userns-aka-rootless
130+
# This feature is mostly expected to be used for `kind` and `minikube`.
131+
#
132+
# Requires Rootless Docker/Podman/nerdctl with cgroup v2 delegation: https://rootlesscontaine.rs/getting-started/common/cgroup2/
133+
# (Rootless Docker/Podman/nerdctl prepares the UserNS, so we do not need to create UserNS by ourselves)
134+
FROM critest AS cri-in-userns-systemd
135+
ENV IS_SYSTEMD_CGROUP=true
136+
RUN apt-get update && apt-get install -y systemd sysvinit-core
137+
COPY contrib/Dockerfile.test.d/cri-in-userns/etc_containerd_config.toml /etc/containerd/config.toml
138+
COPY contrib/Dockerfile.test.d/cri-in-userns/docker-entrypoint.sh /docker-entrypoint.sh
139+
COPY contrib/Dockerfile.test.d/critest.sh /critest.sh
140+
# Skip "runtime should support unsafe sysctls": `container init caused: write sysctl key fs.mqueue.msg_max: open /proc/sys/fs/mqueue/msg_max: permission denied`
141+
# Skip "runtime should support safe sysctls": `container init caused: write sysctl key kernel.shm_rmid_forced: open /proc/sys/kernel/shm_rmid_forced: permission denied`
142+
# Skip "should allow privilege escalation when (NoNewPrivis is) false": expected log "Effective uid: 0\n" (stream="stdout") not found in logs [{timestamp:{wall:974487519 ext:63761339984 loc:<nil>} stream:stdout log:Effective uid: 1000) }]
143+
ENTRYPOINT ["/critest.sh","start"]
144+
145+
122146
# Install proto3
123147
FROM golang AS proto3
124148
ARG DESTDIR=/build

contrib/Dockerfile.test.d/cri-in-userns/docker-entrypoint.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,23 @@ xargs -rn1 < /sys/fs/cgroup/cgroup.procs > /sys/fs/cgroup/init/cgroup.procs || :
4141
sed -e 's/ / +/g' -e 's/^/+/' < /sys/fs/cgroup/cgroup.controllers \
4242
> /sys/fs/cgroup/cgroup.subtree_control
4343

44+
if [ ! -z "$IS_SYSTEMD_CGROUP" ] && [ "$IS_SYSTEMD_CGROUP" = true ];then
45+
cat >> /etc/containerd/config.toml <<EOF
46+
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
47+
SystemdCgroup = true
48+
EOF
49+
fi
50+
4451
set -x
4552
echo >&2 "Running containerd in background"
4653
containerd &
4754

4855
echo >&2 "Waiting for containerd"
4956
until ctr plugins list; do sleep 3; done
5057

51-
exec "$@"
58+
if [ ! -z "$IS_SYSTEMD_CGROUP" ] && [ "$IS_SYSTEMD_CGROUP" = true ];then
59+
critest "--ginkgo.skip=should support unsafe sysctls|should support safe sysctls|should allow privilege escalation when false"
60+
/bin/bash /critest.sh exit
61+
else
62+
exec "$@"
63+
fi
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
cat > /etc/systemd/system/critest.service << EOF
4+
[Unit]
5+
Description=critest script
6+
[Service]
7+
Type=simple
8+
Environment="IS_SYSTEMD_CGROUP=true"
9+
RemainAfterExit=yes
10+
ExecStart=/bin/bash /docker-entrypoint.sh
11+
StandardOutput=/dev/stdout
12+
StandardError=/dev/stderr
13+
[Install]
14+
WantedBy=default.target
15+
EOF
16+
17+
18+
function echo_exit_code() {
19+
sleep 30
20+
log_str=`systemctl status critest.service|grep "SUCCESS!"`
21+
if [ -z "$log_str" ]; then
22+
echo 1 > /tmp/critest_exit_code.txt
23+
/bin/systemctl poweroff
24+
fi
25+
failed_count=$(echo "$log_str" | awk '{for(i=1;i<=NF;i++) if($i=="Failed") {print $(i-1); exit}}')
26+
if [ "$failed_count" -gt 0 ]; then
27+
echo 1 > /tmp/critest_exit_code.txt
28+
else
29+
echo 0 > /tmp/critest_exit_code.txt
30+
fi
31+
/bin/systemctl poweroff
32+
}
33+
34+
function start(){
35+
systemctl enable critest.service
36+
journalctl -f &
37+
exec /lib/systemd/systemd
38+
}
39+
40+
case $1 in
41+
start)
42+
start
43+
;;
44+
exit)
45+
echo_exit_code
46+
;;
47+
esac

integration/nri_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ func TestNriLinuxCpusetAdjustmentUpdate(t *testing.T) {
401401
)
402402

403403
t.Log("Test that NRI plugins can update linux cpusets of existing containers.")
404+
t.Logf("availableCpuset values is %v", availableCpuset)
404405

405406
var (
406407
out = t.TempDir()
@@ -423,10 +424,12 @@ func TestNriLinuxCpusetAdjustmentUpdate(t *testing.T) {
423424
Type: "bind",
424425
Options: []string{"bind"},
425426
})
427+
t.Logf("ctr0 availableCpuset values is %v", availableCpuset)
426428
adjust.SetLinuxCPUSetCPUs(availableCpuset[0])
427429
} else {
428430
update = []*api.ContainerUpdate{{}}
429431
update[0].SetContainerId(ctr0)
432+
t.Logf("ctr1 availableCpuset values is %v", availableCpuset)
430433
update[0].SetLinuxCPUSetCPUs(availableCpuset[1])
431434
}
432435
return adjust, update, nil

script/critest.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ runtime_type = "${TEST_RUNTIME}"
6262
# This is safely ignored for kernel >= 5.19.
6363
slow_chown = true
6464
EOF
65+
66+
if [ ! -z "$CGROUP_DRIVER" ] && [ "$CGROUP_DRIVER" = "systemd" ];then
67+
cat >> ${BDIR}/config.toml <<EOF
68+
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
69+
SystemdCgroup = true
70+
EOF
71+
fi
72+
6573
ls /etc/cni/net.d
6674

6775
/usr/local/bin/containerd \

script/test/utils.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ version = 2
167167
EOF
168168
fi
169169

170+
if [ $IS_WINDOWS -eq 0 ] && [ ! -z "$CGROUP_DRIVER" ] && [ "$CGROUP_DRIVER" = "systemd" ];then
171+
cat >> ${CONTAINERD_CONFIG_FILE} << EOF
172+
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
173+
SystemdCgroup = true
174+
EOF
175+
fi
176+
170177
# CONTAINERD_TEST_SUFFIX is the suffix appended to the root/state directory used
171178
# by test containerd.
172179
CONTAINERD_TEST_SUFFIX=${CONTAINERD_TEST_SUFFIX:-"-test"}

0 commit comments

Comments
 (0)