Skip to content

Commit 7607275

Browse files
fix: prevent DinD containers escaping the pod cgroup on cgroup v2 (#98)
* fix: add cgroup wrapper * Revert "fix: add cgroup wrapper" This reverts commit f0147f2. * fix: run all containers in given cgroup * hardcode parent cgroup * fix: fix cgroups * fix: add dockerd_params * fix(cgroup): set `memory.oom.group=0` for dind container * change v2 detection condition * buld: revert me * Revert "buld: revert me" This reverts commit 922c0e1. * fix: create custom cgroup * revert cgroup parent * refine logs * refine logs * Revert "revert cgroup parent" This reverts commit 9ba581a. * fix: move containerd process to init cgroup * fix: specify correct path for cgroup * docs: add explanational comments * docs: improve comments --------- Co-authored-by: Vadim Gusev <vadim.gusev@codefresh.io> Co-authored-by: Vadim Gusev <vadim.gusev@octopus.com>
1 parent 0e500d0 commit 7607275

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

run.sh

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,34 @@ do
187187
fi
188188

189189
echo "Starting dockerd"
190-
dockerd ${DOCKERD_PARAMS} <&- &
190+
if [ ! -f /sys/fs/cgroup/cgroup.controllers ]; then
191+
echo "Using cgroup v1"
192+
dockerd ${DOCKERD_PARAMS} <&- &
193+
else
194+
echo "Using cgroup v2"
195+
CURRENT_CGROUP=$(cat /proc/self/cgroup | sed 's/0:://')
196+
CURRENT_CGROUP_PATH="/sys/fs/cgroup/${CURRENT_CGROUP}"
197+
echo "Current cgroup: ${CURRENT_CGROUP}"
198+
199+
# Move the processes from the current group to the `./init` group,
200+
# otherwise the current group will become of type "domain threaded",
201+
# and it will not be possible to enable required controllers for DinD group.
202+
# Ref: https://github.com/moby/moby/blob/38805f20f9bcc5e87869d6c79d432b166e1c88b4/hack/dind#L28-L38
203+
mkdir -p ${CURRENT_CGROUP_PATH}/init
204+
xargs -rn1 < ${CURRENT_CGROUP_PATH}/cgroup.procs > ${CURRENT_CGROUP_PATH}/init/cgroup.procs || :
205+
206+
# Set `memory.oom.group=0` to disable killing all processes in cgroup at once on OOM.
207+
# if all processes are killed at once, the system will not be able to detect this event;
208+
# instead, we expect separate pipeline steps to be killed if total consumptions exceed limits.
209+
MEMORY_OOM_GROUP="${CURRENT_CGROUP_PATH}/memory.oom.group"
210+
echo "Ensuring memory.oom.group is set to 0 to disable killing all processes in cgroup at once on OOM"
211+
echo "0" > "${MEMORY_OOM_GROUP}"
212+
echo "Current memory.oom.group value: $(cat "${MEMORY_OOM_GROUP}")"
213+
214+
# Explicitly set --cgroup-parent to prevent DinD containers escaping the pod cgroup on cgroup v2.
215+
dockerd --cgroup-parent "${CURRENT_CGROUP}/codefresh-dind" ${DOCKERD_PARAMS} <&- &
216+
fi
217+
191218
echo "Waiting at most 20s for docker pid"
192219
CNT=0
193220
while ! test -f "${DOCKERD_PID_FILE}" || test -z "$(cat ${DOCKERD_PID_FILE})"

service.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version: 3.0.2
1+
version: 3.0.3

0 commit comments

Comments
 (0)