Enhance k8s-container-cleanup.sh to operate during upgrade abort

This change is to add optional argument 'force-clean' to
stop all containers during 'system kube-upgrade-abort'.
The default usage of this script with no options only performs
cleanup during systemd 'stopping' phase.

Test-plan:
Pass: During 'system kube-upgrade-abort' process verify the puppet.log
and check '/usr/sbin/k8s-container-cleanup.sh  force-clean' is
executed successfully.
Pass: Manually execute '/usr/sbin/k8s-container-cleanup.sh force-clean'
verify all the containers have exited.
Pass: Manually execute the script by passing the wrong argument
"/usr/sbin/k8s-container-cleanup.sh clean-force" and verify it
gives the appropriate error message -
"usage: /usr/sbin/k8s-container-cleanup.sh { force-clean }"

Story: 2010565
Task: 48094

Change-Id: I8029e89b600cb644bfa4a83f3208e677878f19e7
Signed-off-by: Boovan Rajendran <boovan.rajendran@windriver.com>
This commit is contained in:
Boovan Rajendran 2023-05-24 10:56:07 -04:00
parent 0aa36aeaad
commit 8edb19bdd5
1 changed files with 36 additions and 20 deletions

View File

@ -26,27 +26,43 @@ function ERROR {
logger -p daemon.error -t "${NAME}($$): " "${@}"
}
state=$(timeout 10 systemctl is-system-running)
RC=$?
LOG "System state is: ${state}, RC = ${RC}."
case ${RC} in
124)
# systemctl hung.
ERROR "systemctl timed out. System state unknown."
;;
function do_force_clean {
LOG "Stopping all containers."
# Use crictl to gracefully stop each container. If specified timeout is
# reached, it forcibly kills the container. There is no need to check
# return code since there is nothing more we can do, and crictl already
# logs to daemon.log.
crictl ps -q | xargs -n 10 -r crictl stop --timeout 5
LOG "Stopping all containers completed."
}
[01])
# 0 - running; 1 - initializing, starting, degraded, maintenance, stopping
if [ "${state}" = "stopping" ]; then
LOG "Stopping all containers."
# Use crictl to gracefully stop each container. If specified timeout is
# reached, it forcibly kills the container. There is no need to check
# return code since there is nothing more we can do, and crictl already
# logs to daemon.log.
crictl ps -q | xargs -r -I {} crictl stop --timeout 5 {}
LOG "Stopping all containers completed."
exit 0
fi
case "$1" in
"")
state=$(timeout 10 systemctl is-system-running)
RC=$?
LOG "System state is: ${state}, RC = ${RC}."
case ${RC} in
124)
# systemctl hung.
ERROR "systemctl timed out. System state unknown."
exit 0
;;
1)
# 1 - initializing, starting, degraded, maintenance, stopping
if [ "${state}" = "stopping" ]; then
do_force_clean
fi
;;
esac
;;
force-clean)
do_force_clean
;;
*)
echo "usage: $0 { force-clean }" >&2
exit 3
;;
esac