Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cleanup old Configmaps created by helm.. faster edition
#!/usr/bin/env bash
# Courtesy: https://github.com/helm/helm/issues/2332#issuecomment-336565784
# Enhanced for performance
TARGET_NUM_REVISIONS=10
TARGET_NUM_REVISIONS=$(($TARGET_NUM_REVISIONS+0))
RELEASES=$(kubectl --namespace=kube-system get cm -l OWNER=TILLER -o go-template --template='{{range .items}}{{ .metadata.labels.NAME }}{{"\n"}}{{ end }}' | sort -u)
# create the directory to store backups
mkdir configmaps
for RELEASE in $RELEASES; do
# get the revisions of this release
REVISIONS=$(kubectl --namespace=kube-system get cm -l OWNER=TILLER -l NAME=$RELEASE | awk '{if(NR>1)print $1}' | sed 's/.*\.v//' | sort -n)
NUM_REVISIONS=$(echo $REVISIONS | tr " " "\n" | wc -l)
NUM_REVISIONS=$(($NUM_REVISIONS+0))
echo "Release $RELEASE has $NUM_REVISIONS revisions. Target is $TARGET_NUM_REVISIONS."
if [[ $NUM_REVISIONS -gt $TARGET_NUM_REVISIONS ]]; then
NUM_TO_DELETE=$(($NUM_REVISIONS-$TARGET_NUM_REVISIONS))
echo "Will delete $NUM_TO_DELETE revisions"
TO_DELETE=$(echo $REVISIONS | tr " " "\n" | head -n $NUM_TO_DELETE)
CM_NAMES=()
for DELETE_REVISION in $TO_DELETE; do
CMNAME=$RELEASE.v$DELETE_REVISION
CM_NAMES+=(${CMNAME})
done
kubectl -n kube-system get cm ${CM_NAMES[@]} -o yaml > configmaps/${RELEASE}.yaml
kubectl -n kube-system delete cm ${CM_NAMES[@]}
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment