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

时间:2019-08-18 22:06:04

标签: kubectl

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"}

1 个答案:

答案 0 :(得分:2)

这是一个shell(可能是ZSH?)错误消息,与kubectl不相关。您需要引用包含[]的字符串,以便它们不会被解释为外壳元字符。

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