Skip to content

Instantly share code, notes, and snippets.

@thekoma
Last active January 20, 2021 18:28
Show Gist options
  • Save thekoma/8068a82d6b4ada63021638d9b12adfe2 to your computer and use it in GitHub Desktop.
Save thekoma/8068a82d6b4ada63021638d9b12adfe2 to your computer and use it in GitHub Desktop.
openshift_orphaned_pod_cleaner.sh
#!/bin/bash
# This script has to run as root directly IN coreOS.
# Probably you shouldn't use it if you don't fully understand what it will do.
# Optimized for OCP from : https://github.com/kubernetes/kubernetes/issues/60987
KUBELET_HOME=/var/lib
PODS=$(journalctl --since "6 hour ago" --grep "orphaned pod" |awk -F"orphaned pod" '{print $2}'|awk '{print $1}' |sort -u |sed 's/"//g')
for podid in $PODS;
do
if [ ! -d ${KUBELET_HOME}/kubelet/pods/$podid ]; then
echo $podid already cleaned
continue
fi
if [ -d ${KUBELET_HOME}/kubelet/pods/$podid/volume-subpaths/ ]; then
mountpath=$(mount | grep ${KUBELET_HOME}/kubelet/pods/$podid/volume-subpaths/ | awk '{print $3}')
for mntPath in $mountpath;
do
umount $mntPath
done
rm -rf ${KUBELET_HOME}/kubelet/pods/$podid/volume-subpaths
fi
csiMounts=$(mount | grep "${KUBELET_HOME}/kubelet/pods/$podid/volumes/kubernetes.io~csi")
if [ "$csiMounts" != "" ]; then
echo "csi is mounted at: $csiMounts"
exit 1
else
rm -rf ${KUBELET_HOME}/kubelet/pods/$podid/volumes/kubernetes.io~csi
fi
volumeTypes=$(ls ${KUBELET_HOME}/kubelet/pods/$podid/volumes/)
for volumeType in $volumeTypes;
do
subVolumes=$(ls -A ${KUBELET_HOME}/kubelet/pods/$podid/volumes/$volumeType)
if [ "$subVolumes" != "" ]; then
echo "${KUBELET_HOME}/kubelet/pods/$podid/volumes/$volumeType contents volume: $subVolumes"
exit 1
else
rmdir ${KUBELET_HOME}/kubelet/pods/$podid/volumes/$volumeType
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment