Skip to content

Instantly share code, notes, and snippets.

@grzs
Last active April 4, 2023 08:29
Show Gist options
  • Save grzs/5555400279f63b99fac175cb7257d261 to your computer and use it in GitHub Desktop.
Save grzs/5555400279f63b99fac175cb7257d261 to your computer and use it in GitHub Desktop.
jq script for creating kubernetes pods CSV report
# kubectl get pods -A -o json | jq -f k8s-pods-csv.jq -r > k8s-pods.csv
def sumarrays($arrays):
reduce $arrays[] as $item (
[]; . as $sum
| [range($item | length)]
| map($sum[.] + $item[.])
)
;
# unit conversions
def cpu2num($val):
if $val[-1:] == "m" then
$val[:-1] | tonumber / 1000 else
$val // 0 | tonumber
end
;
def mem2num($val):
if $val[-2:] == "Mi" then
$val[:-2] | tonumber / 1024 else
$val[:-2] // 0 | tonumber
end
;
# header
["namespace", "pod", "container",
"cpu_request(cores)", "memory_request(Gi)",
"cpu_limit(cores)", "memory_limit(Gi)"
] as $header
# rows
| .items
| map(
.metadata as {name: $podName, namespace: $namespace}
| .spec.containers
| map(
[$namespace, $podName, .name,
cpu2num(.resources.requests.cpu),
mem2num(.resources.requests.memory),
cpu2num(.resources.limits.cpu),
mem2num(.resources.limits.memory)
]
)
)
| flatten(1) | sort
# adding header and sum
| [$header] + . + [["Sum", "", ""] + sumarrays(.)[3:]]
| map(@csv) | join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment