Last active
July 31, 2023 08:09
-
-
Save gionn/f94459ca9b907d5de5f940481e4425cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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