26Z28b2

QK·2026년 8월 28일
kubectl get pods -A -o json | jq -r '
  def parse_cpu:
    if . == null then 0
    elif type == "number" then . * 1000
    elif endswith("m") then (.[0:-1] | tonumber)
    elif endswith("u") then (.[0:-1] | tonumber / 1000)
    elif endswith("n") then (.[0:-1] | tonumber / 1000000)
    else ((. | tonumber) * 1000) end;

  def parse_mem:
    if . == null then 0
    elif type == "number" then (. / 1024 / 1024)
    elif endswith("Ki") then (.[0:-2] | tonumber / 1024)
    elif endswith("Mi") then (.[0:-2] | tonumber)
    elif endswith("Gi") then (.[0:-2] | tonumber * 1024)
    elif endswith("Ti") then (.[0:-2] | tonumber * 1024 * 1024)
    elif endswith("K") or endswith("k") then (.[0:-1] | tonumber * 1000 / 1048576)
    elif endswith("M") or endswith("m") then (.[0:-1] | tonumber * 1000000 / 1048576)
    elif endswith("G") or endswith("g") then (.[0:-1] | tonumber * 1000000000 / 1048576)
    else ((. | tonumber) / 1024 / 1024) end;

  [ .items[] | select(.status.phase=="Running") | {
      ns: .metadata.namespace,
      cpu: ([ (.spec.containers // [])[].resources.requests.cpu ] | map(parse_cpu) | add // 0),
      mem: ([ (.spec.containers // [])[].resources.requests.memory ] | map(parse_mem) | add // 0)
  } ]
  | group_by(.ns)
  | map({
      Namespace: .[0].ns,
      "CPU_Req(Cores)": (((map(.cpu) | add) / 1000) | tostring),
      "Mem_Req(GiB)": (((map(.mem) | add) / 1024 | round) | tostring)
    })
'
profile
engineer

0개의 댓글