Skip to content

Instantly share code, notes, and snippets.

@andromedarabbit
Last active July 29, 2018 14:58
Show Gist options
  • Save andromedarabbit/0089f0e0049c588c2e06dfbbfb2cc664 to your computer and use it in GitHub Desktop.
Save andromedarabbit/0089f0e0049c588c2e06dfbbfb2cc664 to your computer and use it in GitHub Desktop.
Get a report on Kuberentes application deployment across multiple AZs
#!/bin/bash -e
type csvjson || brew install csvkit
type jq || brew install jq
RED='\033[0;31m'
NC='\033[0m' # No Color
OUTPUT_PODS=pods.csv
truncate -s 0 "${OUTPUT_PODS}"
echo "Namespace,App,Node" > "${OUTPUT_PODS}"
kubeall get pod -o json | jq -r '.items[] | [ .metadata .namespace, if (.metadata .labels .app?) then (.metadata .labels .app) elif (.metadata .labels ."k8s-app"?) then (.metadata .labels ."k8s-app") else "" end, .spec .nodeName ] | @csv' >> "${OUTPUT_PODS}"
OUTPUT_NODES=nodes.csv
truncate -s 0 "${OUTPUT_NODES}"
echo "Region,AZ,Node" > "${OUTPUT_NODES}"
kubeall get node -o json | jq -r '.items[] | [ .metadata .labels ."failure-domain.beta.kubernetes.io/region", .metadata .labels ."failure-domain.beta.kubernetes.io/zone", .metadata .name ] | @csv' >> "${OUTPUT_NODES}"
TABLE_NAME=tmp
TMP_FILE=${TABLE_NAME}.csv
truncate -s 0 "${TMP_FILE}"
OUTPUT_FILE=output.csv
truncate -s 0 "${OUTPUT_FILE}"
csvjoin -c Node "${OUTPUT_PODS}" "${OUTPUT_NODES}" | csvcut -c Namespace,App,Node,AZ > "${TMP_FILE}"
csvsql --query "select Namespace, App, AZ, count(*) as cnt from '${TABLE_NAME}' group by Namespace, App, AZ order by Namespace asc, App asc, AZ asc" "${TMP_FILE}" > "${OUTPUT_FILE}"
echo -e "The following are the first 3 x 9 records of the result:"
echo -e "\n"
head -n 3 "${OUTPUT_FILE}" | cut -d$'\t' -f1-9
echo -e "\n"
echo -e "Open ${OUTPUT_FILE} to read all the records you wanted"
echo -e "\n "
echo "done!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment