Skip to content

Instantly share code, notes, and snippets.

@gionn
Last active July 31, 2023 08:09
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 gionn/f94459ca9b907d5de5f940481e4425cd to your computer and use it in GitHub Desktop.
Save gionn/f94459ca9b907d5de5f940481e4425cd to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
namespace=${1-fleet-default}
delete_before_timestamp=${2-2000-01-01T00:00:00Z}
jsonPath='{range .items[*]}{@.metadata.creationTimestamp}{"_"}{@.metadata.name}{"_"}{@.spec.clusterLabels.management\.cattle\.io/cluster-name}{"\n"}{end}'
cluster_regs=$(kubectl get clusterregistration -o=jsonpath="$jsonPath" -n $namespace | sort)
read -ra regs -d '' <<< "${cluster_regs}"
last_idx=$(( ${#regs[@]} - 1 ))
for (( i = 0; i < $last_idx; i+= 1 )); do
IFS=_ read -r creation_timestamp name cluster_name <<< "${regs[i]}"
if [[ "$creation_timestamp" < "$delete_before_timestamp" ]]; then
kubectl delete --ignore-not-found=true clusterregistration "$name" -n $namespace
else
echo "KEEP $name $creation_timestamp $cluster_name"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment