jq: error: ... 스타일의 컴파일 에러는 보통 Shell(Bash)에서 특수문자(//, *, $)를 해석할 때 백슬래시 escapes 문제가 발생하거나, jq의 내장 수식 처리 파이프라인(if-then-else) 작성 방식 때문에 발생합니다.
아래는 문법을 보완하여 에러 없이 안정적으로 동작하도록 개작한 명령어입니다.
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 endsWith("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 endsWith("Gi") then (sub("Gi$"; "") | tonumber * 1073741824)
elif type == "string" and endsWith("Mi") then (sub("Mi$"; "") | tonumber * 1048576)
elif type == "string" and endsWith("Ki") then (sub("Ki$"; "") | tonumber * 1024)
elif type == "string" and endsWith("G") then (sub("G$"; "") | tonumber * 1000000000)
elif type == "string" and endsWith("M") then (sub("M$"; "") | tonumber * 1000000)
elif type == "string" and endsWith("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
trimstr 대신 표준 sub 함수(sub("Gi$"; ""))를 사용하여 버전 호환성을 향상시켰습니다.initContainers나 Request 미설정 컨테이너로 인해 발생할 수 있는 null 처리 예외(tonumber? // 0)를 보강했습니다.