如何使用带有 `custom-columns` 的 `kubectl` 来处理数组?

How can I work with arrays using `kubectl` with `custom-columns`?

reference docs 中,他们说您可以像这样打印容器的图像。

kubectl get pod test-pod -o custom-columns=CONTAINER:.spec.containers[0].name,IMAGE:.spec.containers[0].image

但是,当我尝试任何带有括号的命令时,我收到一条错误消息并且没有输出。

no matches found: custom-columns=CONTAINER:.spec.containers[0].name,IMAGE:.spec.containers[0].image

如果我只指定类似 .spec.containers 的内容,它将输出类似于这样的整个数组:

  [map[image:weaveworksplugins/scope-traffic-control:latest imagePullPolicy:Always name:weavescope-traffic-control-plugin resources:map[requests:map[cpu:100m]] securityContext:map[privileged:true procMount:Default] terminationMessagePath:/dev/termination-log terminationMessagePolicy:File volumeMounts:[map[mountPath:/var/run/docker.sock name:docker-sock] map[mountPath:/var/run/scope/plugins name:scope-plugins]
...

Kubectl 版本信息

Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T16:54:35Z", GoVersion:"go1.12.7", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.2", GitCommit:"66049e3b21efe110454d67df4fa62b08ea79a19b", GitTreeState:"clean", BuildDate:"2019-05-16T16:14:56Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}

那是 shell(可能是 ZSH?)错误消息,与 kubectl 无关。您需要引用包含 [] 的字符串,这样它们就不会被解释为 shell 元字符。

kubectl get pod test-pod \
  -o custom-columns='CONTAINER:.spec.containers[0].name,IMAGE:.spec.containers[0].image'