26Z05d7

Young-Kyoo Kim·2026년 8월 4일

endsWith는 최신 jq 버전에 추가된 내장 함수라 jq 구버전(1.5 이하)이 설치된 환경에서는 endsWith/1 is not defined 컴파일 에러가 발생합니다.

endsWith 함수 대신 정규식 매칭(test)을 사용하여 jq 모든 버전에서 에러 없이 동작하도록 작성한 원라이너/스크립트입니다.


1. CPU Request Top 10 Namespace (구버전 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

2. Memory Request Top 10 Namespace (구버전 jq 대응)

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 버전 호환성 확보)

0개의 댓글