Skip to content

Instantly share code, notes, and snippets.

@looselycoupled
Last active June 5, 2019 15:33
Show Gist options
  • Save looselycoupled/2d938c0906eb493239ff4fa327175876 to your computer and use it in GitHub Desktop.
Save looselycoupled/2d938c0906eb493239ff4fa327175876 to your computer and use it in GitHub Desktop.
k8s node capacity
import re
import subprocess
from tabulate import tabulate
def get_nodes():
output = subprocess.run("kubectl get nodes".split(), stdout=subprocess.PIPE)
text = output.stdout.decode("utf-8").split("\n")
return [(re.split("\s+", line)[0],re.split("\s+", line)[2]) for line in text[1:] if line]
def cluster_capacity():
data = []
for node, role in get_nodes():
text = subprocess.run(f"kubectl describe node {node}".split(), stdout=subprocess.PIPE).stdout.decode("utf-8")
table = text.split("Allocated resources:\n")[1].split("Events:")[0].split("------\n")[1].split("\n")
rows = [re.split("\s+", row) for row in table if row]
data.append({
"node": node,
"role": role,
f"{rows[0][1]} (requests)": " ".join(rows[0][2:4]),
f"{rows[0][1]} (limits)": " ".join(rows[0][-2:]),
f"{rows[1][1]} (requests)": " ".join(rows[1][2:4]),
f"{rows[1][1]} (limits)": " ".join(rows[1][-2:]),
})
print(tabulate(data, "keys"))
if __name__ == '__main__':
cluster_capacity()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment