kubectl -o go-template

关于golang的text/template

 
$ cat <<EOF |kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  shareProcessNamespace: true
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  - name: shell
    image: busybox
    imagePullPolicy: IfNotPresent
    securityContext:
      capabilities:
        add:
        - SYS_PTRACE
    stdin: true
    tty: true
EOF

查看Kind:

$ kubectl get pod nginx -o go-template='{{.kind}}'
Pod

查看POD的name:

$ kubectl get pod nginx -o go-template='{{.metadata.name}}'
nginx

查看镜像名称和镜像:

简单点的:

$  kubectl get pod nginx -o go-template='{{range .spec.containers}}{{printf "name:%v,images:%v\n" .name .image}}{{end}}'
name:nginx,images:nginx
name:shell,images:busybox

复杂点的:

kubectl get pod nginx -o go-template='<<EOF
The pod has two containers:
{{range $index,$elem:=.spec.containers}}
  ===========================
  {{$index}}: name:{{$elem.name}} image:{{$elem.image}}
{{end}}'
EOF

输出结果:

[root@k8s-node1 ~]# kubectl get pod nginx -o go-template='<<EOF
> The pod has two containers:
> {{range $index,$elem:=.spec.containers}}
>   ===========================
>   {{$index}}: name:{{$elem.name}} image:{{$elem.image}}
> {{end}}'
<<EOF
The pod has two containers:

  ===========================
  0: name:nginx image:nginx

  ===========================
  1: name:shell image:busybox

参考链接:https://www.cnblogs.com/summershan/p/15672179.html

posted @ 2022-11-02 19:50  cosmoswong  阅读(145)  评论(0编辑  收藏  举报