eak3
eak3

Reputation: 3

jsonpath for nested arrays in kubectl get

I am trying to get the resource limits & requests for Kubernetes pods. I am attempting to output to a comma delimited row that lists the namespace, pod name, container name and then the mem & CPU limits/requests for each container. Running into issues when there's multiple containers per pod.

The closest I've been able to get is this which will print out a single row for each pod. If there are multiple containers, they are listed in separate "columns" in the same row.

kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{@.metadata.namespace}{","}{@.metadata.name}{","}{range .spec.containers[*]}{.name}{","}{@.resources.requests.cpu}{","}{@.resources.requests.memory}{","}{@.resources.limits.cpu}{","}{@.resources.limits.memory}{","}{end}{"\n"}{end}'

The output looks like this:

kube-system,metrics-server-5f8d84558d-g926z,metrics-server-vpa,5m,30Mi,100m,300Mi,metrics-server,46m,63Mi,46m,63Mi,

What I would like to see is something like this:

kube-system,metrics-server-5f8d84558d-g926z,metrics-server-vpa,5m,30Mi,100m,300Mi,
kube-system,metrics-server-5f8d84558d-g926z,metrics-server,46m,63Mi,46m,63Mi,


Appreciate any assistance. Thanks.

Upvotes: 0

Views: 1848

Answers (2)

P....
P....

Reputation: 18411

Here is one way to obtain the output natively using kubectl via the go-template output format. jsonpath is not the right tool(maybe doable) for this requirement; perhaps piping to jq or go-template is the appropriate solution.

kubectl get pod -o go-template='{{- range $index, $element := .items -}}
                                  {{- range $container, $status := $element.spec.containers -}}
                                    {{- printf "%s,%s,%s,%s,%s,%s,%s\n" $element.metadata.namespace  $element.metadata.name  $status.name (or $status.resources.requests.cpu "" ) (or $status.resources.requests.memory "") (or $status.resources.limits.memory "") (or $status.resources.limits.cpu "") -}}
                                    {{- end -}}
                                {{- end -}}'

Upvotes: 3

DazWilkin
DazWilkin

Reputation: 40416

I think (don't know that) you can't using only kubectl's (limited) JSONPath.

There's a UNIX principle that each tool should do one thing well:

  • kubectl does Kubernetes stuff well and can output JSON
  • jq does JSON processing well.

If you're willing to use another tool:

FILTER='
  .items[]
  |.metadata as $meta
  |.spec.containers[]
  |.name as $name
  |.resources.requests as $requests
  |.resources.limits as $limits
  |[ 
     $meta.namespace,
     $meta.name,$name,
     $requests.cpu,
     $requests.memory,
     $limits.cpu,
     $limits.memory
   ]
  |@csv
'

kubectl get pods \
--all-namespaces \
--output=json \
| jq -r "${FILTER}"

Explanation:

  1. For each items (i.e. each Pod)
  2. Set the variable meta to the (Pod's) metadata content
  3. For each containers (i.e. each Container)
  4. Set the variable name as the (Container's) name
  5. Set the variable requests as the (Container's Resources') requests
  6. Set the variable limits as the (Container's Resources') limits
  7. Create an array ([...]) by reassembling the relevant pieces
  8. Output the arrays as comma-delimited

On a cluster:

"monitoring","prometheus-adapter-59df95d9f5-kg4hc","prometheus-adapter",,,,
"monitoring","prometheus-adapter-59df95d9f5-j6rbx","prometheus-adapter",,,,
"monitoring","prometheus-operator-7775c66ccf-45z2f","prometheus-operator","100m","100Mi","200m","200Mi"
"monitoring","prometheus-operator-7775c66ccf-45z2f","kube-rbac-proxy","10m","20Mi","20m","40Mi"
"monitoring","node-exporter-7cf4m","node-exporter","102m","180Mi","250m","180Mi"
"monitoring","node-exporter-7cf4m","kube-rbac-proxy","10m","20Mi","20m","40Mi"
"monitoring","kube-state-metrics-76f6cb7996-hdxcb","kube-state-metrics","10m","190Mi","100m","250Mi"
"monitoring","kube-state-metrics-76f6cb7996-hdxcb","kube-rbac-proxy-main","20m","20Mi","40m","40Mi"
"monitoring","kube-state-metrics-76f6cb7996-hdxcb","kube-rbac-proxy-self","10m","20Mi","20m","40Mi"
"monitoring","blackbox-exporter-55c457d5fb-x6hwj","blackbox-exporter","10m","20Mi","20m","40Mi"
"monitoring","blackbox-exporter-55c457d5fb-x6hwj","module-configmap-reloader","10m","20Mi","20m","40Mi"
"monitoring","blackbox-exporter-55c457d5fb-x6hwj","kube-rbac-proxy","10m","20Mi","20m","40Mi"
"monitoring","grafana-6dd5b5f65-6jwq8","grafana","100m","100Mi","200m","200Mi"
"monitoring","alertmanager-main-0","alertmanager","4m","100Mi","100m","100Mi"
"monitoring","alertmanager-main-0","config-reloader","100m","50Mi","100m","50Mi"
"kube-system","coredns-7f9c69c78c-2zx4h","coredns","100m","70Mi",,"170Mi"
"monitoring","prometheus-k8s-0","prometheus",,"400Mi",,
"monitoring","prometheus-k8s-0","config-reloader","100m","50Mi","100m","50Mi"
"kube-system","calico-kube-controllers-5f7575cc96-6tf8x","calico-kube-controllers",,,,
"kube-system","calico-node-m78xm","calico-node","250m",,,

Upvotes: 2

Related Questions