Skip to content

Instantly share code, notes, and snippets.

@ghostsquad
Created September 13, 2023 17:17
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 ghostsquad/8d2b4f852f49c6f592974ca0454411f1 to your computer and use it in GitHub Desktop.
Save ghostsquad/8d2b4f852f49c6f592974ca0454411f1 to your computer and use it in GitHub Desktop.
replicaset-age.sh
#!/usr/bin/env bash
set -eou pipefail
DATE="date"
if command -v gdate &> /dev/null
then
DATE="gdate"
fi
# which "$DATE"
deployments=$(kubectl get deployments --all-namespaces --no-headers -o=wide)
current_date=$($DATE -u +%s)
data=""
while IFS= read -r deployment; do
ns=$(echo "$deployment" | awk '{ print $1 }')
name=$(echo "$deployment" | awk '{ print $2 }')
# printf "%-63s %-63s" "$ns" "$name"
data+="$ns,$name"
selectorLabels=$(kubectl get -n "$ns" deployment.apps/"$name" -o=json | jq -r '.spec.selector.matchLabels | to_entries | map("\(.key)%3D\(.value)") | join("%2C")')
selectorLabels="${selectorLabels//\\/%2F}"
# selectorLabelsJoined=$(join_by '%2C' "${selectorLabels[@]}")
kube_date=$(kubectl get --raw "/apis/apps/v1/namespaces/${ns}/replicasets?labelSelector=${selectorLabels}&limit=500" | jq -r '.items[] | .metadata.creationTimestamp' | sort -r | head -1)
data+=",${kube_date}"
kube_date_bash=$($DATE -d"$kube_date" +%s)
# echo "${kube_date_bash}"
kube_date_difference=$(($current_date-$kube_date_bash))
diff=$(printf "%02dd %02dh %02dm %02ds" $((kube_date_difference/86400)) $((kube_date_difference%86400/3600)) $((kube_date_difference%3600/60)) $((kube_date_difference%60)))
data+=",$diff"
data+=$'\n'
done <<< "$deployments"
# note: this requires "column" from util-linux on Mac
# brew install util-linux
echo "$data" | column --table --separator ","
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment