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 @   cosmoswong  阅读(170)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示