Skip to content

Commit d234c10

Browse files
claudiubeluberkayoz
authored andcommitted
Cleans up containers and shims on microk8s stop and snap removal (canonical#4755)
The containerd shims and the containers are now removed in both classic and strict modes when stopping microk8s (microk8s stop) or when removing the snap. Note that ctr container delete has no ``--force`` flag. Adds additional assertion in the upgrade test, ensuring that the Pods / containers have been removed on snap removal. Adds additional test, ensuring that the Pods / containers have been removed when running microk8s stop. (cherry picked from commit 941b781)
1 parent 1b35d30 commit d234c10

File tree

10 files changed

+49
-29
lines changed

10 files changed

+49
-29
lines changed

.github/workflows/build-snap.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
set -x
4747
sudo apt-get install python3-setuptools
4848
sudo pip3 install --upgrade pip
49-
sudo pip3 install -U pytest sh
49+
sudo pip3 install -U pytest sh psutil
5050
sudo apt-get -y install open-iscsi
5151
sudo systemctl enable iscsid
5252
- name: Fetch snap
@@ -71,7 +71,7 @@ jobs:
7171
set -x
7272
sudo apt-get install python3-setuptools
7373
sudo pip3 install --upgrade pip
74-
sudo pip3 install -U pytest sh
74+
sudo pip3 install -U pytest sh psutil
7575
sudo apt-get -y install open-iscsi
7676
sudo systemctl enable iscsid
7777
- name: Fetch snap
@@ -139,7 +139,7 @@ jobs:
139139
set -x
140140
sudo apt-get install python3-setuptools
141141
sudo pip3 install --upgrade pip
142-
sudo pip3 install -U pytest sh
142+
sudo pip3 install -U pytest sh psutil
143143
sudo apt-get -y install open-iscsi
144144
sudo systemctl enable iscsid
145145
- name: Fetch snap

microk8s-resources/actions/common/utils.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ remove_all_containers() {
988988

989989
for container in $("${SNAP}/microk8s-ctr.wrapper" containers ls | $SNAP/bin/sed -n '1!p' | $SNAP/usr/bin/gawk '{print $1}')
990990
do
991-
"${SNAP}/microk8s-ctr.wrapper" container delete --force $container &>/dev/null || true
991+
"${SNAP}/microk8s-ctr.wrapper" container delete $container &>/dev/null || true
992992
done
993993
}
994994

microk8s-resources/default-hooks/remove.d/90-containers

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,5 @@
22

33
. "${SNAP}/actions/common/utils.sh"
44

5-
if is_strict
6-
then
7-
remove_all_containers
8-
else
9-
kill_all_container_shims
10-
fi
5+
remove_all_containers
6+
kill_all_container_shims

microk8s-resources/wrappers/apiservice-kicker

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,9 @@ do
6565
echo "cert change detected. Reconfiguring the kube-apiserver"
6666
rm -rf .srl
6767
snapctl stop microk8s.daemon-kubelite
68-
if is_strict
69-
then
70-
remove_all_containers
71-
snapctl restart microk8s.daemon-containerd
72-
else
73-
snapctl stop microk8s.daemon-containerd
74-
kill_all_container_shims
75-
snapctl start microk8s.daemon-containerd
76-
fi
68+
remove_all_containers
69+
kill_all_container_shims
70+
snapctl restart microk8s.daemon-containerd
7771
snapctl start microk8s.daemon-kubelite
7872
start_all_containers
7973
restart_attempt=$[$restart_attempt+1]

microk8s-resources/wrappers/microk8s-stop.wrapper

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,22 @@ while true; do
4040
esac
4141
done
4242

43-
stopcmd="run_with_sudo snap stop ${SNAP_NAME} --disable"
43+
prefix_cmd="run_with_sudo snap"
4444
if is_strict
4545
then
46-
stopcmd="snapctl stop microk8s.daemon-kubelite --disable"
46+
prefix_cmd="snapctl"
4747
fi
4848

49-
$stopcmd
49+
$prefix_cmd stop microk8s.daemon-kubelite --disable
5050
stop_status=$?
5151

5252
if ! [ $stop_status -eq 0 ]
5353
then
5454
echo 'Failed to stop microk8s services. Check snapd logs with "journalctl -u snapd.service"'
5555
exit 1
5656
else
57-
if is_strict
58-
then
59-
remove_all_containers
60-
snapctl stop microk8s --disable
61-
else
62-
kill_all_container_shims
63-
fi
57+
remove_all_containers
58+
kill_all_container_shims
59+
$prefix_cmd stop microk8s --disable
6460
run_with_sudo touch ${SNAP_DATA}/var/lock/stopped.lock
6561
fi

snap/hooks/remove

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ use_snap_env
77

88
snapctl stop ${SNAP_NAME}.daemon-kubelite 2>&1 || true
99

10+
# Temporarily start containerd so we can stop and kill all the microk8s containers.
11+
snapctl start ${SNAP_NAME}.daemon-containerd 2>&1 || true
12+
# wait for containerd to start.
13+
sleep 5
14+
15+
# Remove any lingering containers and shims.
16+
remove_all_containers
17+
kill_all_container_shims
18+
1019
# Try to symlink /var/lib/kubelet so that most kubelet device plugins work out of the box.
1120
if test -L /var/lib/kubelet; then
1221
unlink /var/lib/kubelet || true

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ pyyaml
77
sh
88
jsonschema==4.0.0
99
pdbpp
10+
psutil
1011
netifaces
1112
requests

tests/test-simple.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import requests
44
import os.path
55

6+
import utils
7+
68

79
class TestSimple(object):
810
def test_microk8s_nodes_ready(self):
@@ -132,3 +134,17 @@ def test_microk8s_services_running(self):
132134

133135
# Verify that all node services are running
134136
assert running_node_services == set(node_services), "Not all node services are running"
137+
138+
def test_microk8s_stop_start(self):
139+
coredns_procs = utils._get_process("coredns")
140+
assert len(coredns_procs) > 0, "Expected to find a coredns process running."
141+
142+
utils.run_until_success("/snap/bin/microk8s.stop", timeout_insec=180)
143+
144+
new_coredns_procs = utils._get_process("coredns")
145+
assert len(new_coredns_procs) == 0, "coredns found still running after microk8s stop."
146+
147+
utils.run_until_success("/snap/bin/microk8s.start", timeout_insec=180)
148+
149+
new_coredns_procs = utils._get_process("coredns")
150+
assert len(new_coredns_procs) > 0, "Expected to find a new coredns process running."

tests/test-upgrade.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
is_container,
2121
is_ipv6_configured,
2222
kubectl,
23+
_get_process,
2324
)
2425

2526
upgrade_from = os.environ.get("UPGRADE_MICROK8S_FROM", "beta")
@@ -162,3 +163,5 @@ def test_upgrade(self):
162163
if not is_container():
163164
# On lxc umount docker overlay is not permitted.
164165
check_call("sudo snap remove microk8s".split())
166+
coredns_procs = _get_process("coredns")
167+
assert len(coredns_procs) == 0, "Expected to have 0 coredns processes running."

tests/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import yaml
55
import platform
6+
import psutil
67
from subprocess import check_output, CalledProcessError, check_call
78

89

@@ -270,3 +271,7 @@ def is_ipv6_configured():
270271
return b"inet6" in output
271272
except CalledProcessError:
272273
return False
274+
275+
276+
def _get_process(name):
277+
return [p for p in psutil.process_iter() if name == p.name()]

0 commit comments

Comments
 (0)