endsWith는 최신 jq 버전에 추가된 내장 함수라 jq 구버전(1.5 이하)이 설치된 환경에서는 endsWith/1 is not defined 컴파일 에러가 발생합니다.
endsWith 함수 대신 정규식 매칭(test)을 사용하여 jq 모든 버전에서 에러 없이 동작하도록 작성한 원라이너/스크립트입니다.
kubectl get pods --all-namespaces -o json | jq -r '
.items
| group_by(.metadata.namespace)
| map({
namespace: .[0].metadata.namespace,
cpu_request: (
map(
(.spec.containers // [])[].resources.requests.cpu // "0"
| if (type == "string" and test("m$")) then
(sub("m$"; "") | tonumber / 1000)
else
(tonumber? // 0)
end
) | add // 0
)
})
| sort_by(.cpu_request)
| reverse
| .[:10]
| .[] | "\(.namespace)\t\(.cpu_request) Cores"
' | column -t
kubectl get pods --all-namespaces -o json | jq -r '
.items
| group_by(.metadata.namespace)
| map({
namespace: .[0].metadata.namespace,
mem_bytes: (
map(
(.spec.containers // [])[].resources.requests.memory // "0"
| if (type == "string" and test("Gi$")) then (sub("Gi$"; "") | tonumber * 1073741824)
elif (type == "string" and test("Mi$")) then (sub("Mi$"; "") | tonumber * 1048576)
elif (type == "string" and test("Ki$")) then (sub("Ki$"; "") | tonumber * 1024)
elif (type == "string" and test("G$")) then (sub("G$"; "") | tonumber * 1000000000)
elif (type == "string" and test("M$")) then (sub("M$"; "") | tonumber * 1000000)
elif (type == "string" and test("K$")) then (sub("K$"; "") | tonumber * 1000)
else (tonumber? // 0) end
) | add // 0
)
})
| sort_by(.mem_bytes)
| reverse
| .[:10]
| .[] | "\(.namespace)\t\((.mem_bytes / 1073741824 * 100 | round) / 100) GiB"
' | column -t
endsWith("m") ➡️ test("m$") (정규표현식 매칭으로 변경하여 모든 jq 버전 호환성 확보)