Created
January 8, 2021 22:37
-
-
Save berttejeda/e89da293d9b3ed8183948caac2bdd09c to your computer and use it in GitHub Desktop.
Use jq to display Limits/Allocation Ratio for a given Kubernetes namespace quota
This file contains 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
[ .items[]? | | |
{ | |
namespace:.metadata.namespace, | |
quotaname:.metadata.name, | |
limitsCpu:.status.hard."limits.cpu", | |
limitsCpu_used:.status.used."limits.cpu", | |
"limitsCpu_Allocation_Ratio": ( | |
( | |
(if .status.hard."limits.cpu" | test("Gi") then | |
(.status.hard."limits.cpu" | rtrimstr("Gi") | tonumber) | |
elif .status.hard."limits.cpu" | test("m") then | |
(.status.hard."limits.cpu" | rtrimstr("m") | tonumber) / 1000 | |
else | |
(.status.hard."limits.cpu" | tonumber) | |
end | |
) as $cpuL | | |
(if .status.used."limits.cpu" | test("Gi") then | |
(.status.used."limits.cpu" | rtrimstr("Gi") | tonumber) | |
elif .status.used."limits.cpu" | test("m") then | |
(.status.used."limits.cpu" | rtrimstr("m") | tonumber) / 1000 | |
else | |
(.status.used."limits.cpu" | tonumber) | |
end | |
) as $cpuU | try ( | |
($cpuU/$cpuL * 100) | |
) catch "N/A" | |
) | |
), | |
limitsMemory:.status.hard."limits.memory", | |
limitsMemory_used:.status.used."limits.memory", | |
"limitsMemory_Allocation_Ratio": ( | |
( | |
(if .status.hard."limits.memory" | test("Gi") then | |
(.status.hard."limits.memory" | rtrimstr("Gi") | tonumber) | |
elif .status.hard."limits.memory" | test("Mi") then | |
(.status.hard."limits.memory" | rtrimstr("Mi") | tonumber) / 1024 | |
else | |
(.status.hard."limits.memory" | tonumber) | |
end | |
) as $memL | | |
(if .status.used."limits.memory" | test("Gi") then | |
(.status.used."limits.memory" | rtrimstr("Gi") | tonumber) | |
elif .status.used."limits.memory" | test("Mi") then | |
(.status.used."limits.memory" | rtrimstr("Mi") | tonumber) / 1024 | |
else | |
(.status.used."limits.memory" | tonumber) | |
end | |
) as $memU | try ( | |
($memU/$memL * 100) | |
) catch "N/A" | |
) | |
), | |
limitsPod:.status.hard."pods", | |
limitsPods_used:.status.used.pods, | |
"limitsPod_Allocation_Ratio": ( | |
( | |
(if (.status.hard.pods | length) == 0 then | |
0 | |
else | |
(.status.hard.pods | tonumber) | |
end | |
) as $podsL | | |
(if (.status.used.pods | length) == 0 then | |
0 | |
else | |
(.status.used.pods | tonumber) | |
end | |
) as $podsU | try ( | |
($podsU/$podsL * 100) | |
) catch "N/A" | |
) | |
) | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
jq
to display Limits/Allocation Ratio for a given Kubernetes namespace quota:kubectl get quota -n my-namespace -o json | jq -r "$(curl -ks https://gist.githubusercontent.com/berttejeda/e89da293d9b3ed8183948caac2bdd09c/raw/9dc2617baaa7f389ad8cbd92d3a7049169ad0cbc/k8s.quotas.jq)"
Sample output: