@@ -11,6 +11,7 @@ IS_MICROK8S=false
1111IS_K3S=false
1212IS_RKE2_AGENT=false
1313IS_K0S_WORKER=false
14+
1415if pgrep -f snap/microk8s > /dev/null; then
1516 CONTAINERD_CONF=/var/snap/microk8s/current/args/containerd-template.toml
1617 IS_MICROK8S=true
@@ -34,11 +35,40 @@ elif pgrep -f /var/lib/k0s/bin/kubelet > /dev/null; then
3435 touch $NODE_ROOT$CONTAINERD_CONF
3536fi
3637
38+ # If SYSTEMD_CGROUP is not set, default to true except for distros that do not default to systemd
39+ # TODO: detect k3d which defaults to cgroupfs
40+ if [ -z " ${SYSTEMD_CGROUP+x} " ] && [ " $IS_MICROK8S " = " true" ]; then
41+ SYSTEMD_CGROUP=false
42+ else
43+ : " ${SYSTEMD_CGROUP:= true} "
44+ fi
45+
46+ # Install D-Bus if it's not available but systemd cgroups are requested
47+ if [ " $SYSTEMD_CGROUP " = " true" ]; then
48+ if ! nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- which dbus-daemon > /dev/null 2>&1 ; then
49+ if nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- which apt-get > /dev/null 2>&1 ; then
50+ nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- apt-get update -y
51+ nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- apt-get install -y dbus
52+ elif nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- which yum > /dev/null 2>&1 ; then
53+ nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- yum install -y dbus
54+ elif nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- which dnf > /dev/null 2>&1 ; then
55+ nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- dnf install -y dbus
56+ elif nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- which apk > /dev/null 2>&1 ; then
57+ nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- apk add dbus
58+ else
59+ echo " WARNING: Could not install D-Bus. No supported package manager found."
60+ SYSTEMD_CGROUP=false
61+ echo " SYSTEMD_CGROUP is now set to $SYSTEMD_CGROUP "
62+ fi
63+ fi
64+ fi
65+
3766mkdir -p $NODE_ROOT$KWASM_DIR /bin/
3867
3968cp /assets/containerd-shim-spin-v2 $NODE_ROOT$KWASM_DIR /bin/
4069
4170if ! grep -q spin $NODE_ROOT$CONTAINERD_CONF ; then
71+ echo " Adding Spin runtime to containerd"
4272 if $IS_K3S ; then
4373 echo '
4474[plugins."io.containerd.cri.v1.runtime".containerd.runtimes."spin"]
@@ -53,12 +83,36 @@ if ! grep -q spin $NODE_ROOT$CONTAINERD_CONF; then
5383 rm -Rf $NODE_ROOT$KWASM_DIR /active
5484fi
5585
86+ # Configure Spin runtime options if using systemd cgroups and no options are configured
87+ # TODO: this should allow for some options to already be configured and just additively
88+ # configure SystemdCgroup
89+ if ! grep -q ' runtimes.spin.options' $NODE_ROOT$CONTAINERD_CONF && [ " $SYSTEMD_CGROUP " = " true" ]; then
90+ echo " Setting SystemdCgroup to $SYSTEMD_CGROUP in Spin containerd configuration"
91+ if $IS_K3S ; then
92+ echo '
93+ [plugins."io.containerd.cri.v1.runtime".containerd.runtimes.spin.options]
94+ SystemdCgroup = ' $SYSTEMD_CGROUP '
95+ ' >> $NODE_ROOT$CONTAINERD_CONF
96+ else
97+ echo '
98+ [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.spin.options]
99+ SystemdCgroup = ' $SYSTEMD_CGROUP '
100+ ' >> $NODE_ROOT$CONTAINERD_CONF
101+ fi
102+ fi
103+
56104if $IS_K3S ; then
57105 sed -i " s|runtime_type = \" io.containerd.spin.*\" |runtime_type = \" $KWASM_DIR /bin/containerd-shim-spin-v2\" |g" $NODE_ROOT$CONTAINERD_CONF
58106fi
59107
60108if [ ! -f $NODE_ROOT$KWASM_DIR /active ]; then
61109 touch $NODE_ROOT$KWASM_DIR /active
110+
111+ # Ensure D-Bus is running before restarting services if using systemd cgroups
112+ if [ " $SYSTEMD_CGROUP " = " true" ]; then
113+ nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- systemctl restart dbus
114+ fi
115+
62116 if $IS_MICROK8S ; then
63117 nsenter -m/$NODE_ROOT /proc/1/ns/mnt -- systemctl restart snap.microk8s.daemon-containerd
64118 elif $IS_K3S ; then
0 commit comments