Skip to content

Instantly share code, notes, and snippets.

@Thearas
Created April 2, 2021 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thearas/e5811723026e0792538c90c442e1af13 to your computer and use it in GitHub Desktop.
Save Thearas/e5811723026e0792538c90c442e1af13 to your computer and use it in GitHub Desktop.
Cleanup dangling PVCs which were created by tidb-operator
#!/bin/bash
set -xuo pipefail
IFS='
'
pvc2tcs=( $(kubectl get pvc -l app.kubernetes.io/managed-by=tidb-operator,app.kubernetes.io/name=tidb-cluster -o jsonpath="{range .items[*]}{.metadata.name}{' '}{.metadata.labels.app\.kubernetes\.io/instance}{'\n'}{end}") )
echo "Cleanup all dangling PVCs created by tidb-operator."
for pvc2tc in "${pvc2tcs[@]}"; do
pvc=$(echo "${pvc2tc}" | awk -F ' ' '{print $1}')
tc=$(echo "${pvc2tc}" | awk -F ' ' '{print $2}')
kubectl get tc "${tc}" >/dev/null || kubectl delete pvc "${pvc}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment