'how to list the kubernetes pod which has CharDevice in it?

how to list the Kubernetes pod which has CharDevice in it ?

I could able to get the pod with CharDevice, but need to print the pod name only

kubectl get pod -o jsonpath='{spec.volumes.hostPath.type=="CharDevice"}'


Solution 1:[1]

You can get json first and use jq to get desired result :

kubectl get pod -o json |
jq -r '.items[]|select(any(.spec.volumes[];.hostPath.type=="CharDevice")).metadata.name'

Solution 2:[2]

I think the filter/parsing you are expecting is not possible using jsonpath supplied with Kubectl. However, you can use go-template if you want to do it with kubectl only without using any other tool:

kubectl get pod -A -o  go-template='{{range $index, $element := .items}}{{range $key, $vol := .spec.volumes}}{{range $sk ,$sv := .hostPath}}{{if (eq $sv "CharDevice") }}{{$element.metadata.name}}{{"\n"}}{{end}}{{end}}{{end}}{{end}}'

For readability:

You can use {{- to trim out the spaces on the left,-}} to trim the spaces on the right. With this info, the same command as above can be transformed to:

kubectl get pod -A -o  go-template='{{- range $index, $element := .items -}}
  {{- range $key, $vol := .spec.volumes -}}
    {{- range $sk ,$sv := .hostPath -}}
      {{- if (eq $sv "CharDevice") -}}
        {{- $element.metadata.name -}}{{"\n"}}
      {{- end -}}
    {{- end -}}
  {{- end -}}
{{- end -}}'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Philippe
Solution 2