使用go-template自定义kubectl get输出
kubectl get相关资源,默认输出为kubectl内置,一般我们也可以使用-o json或者-o yaml查看其完整的资源信息。但是很多时候,我们需要关心的信息并不全面,因此我们需要自定义输出的列,那么可以使用go-template来进行实现。
go-template是golang的一种模板,可以参考template的相关说明。
比如仅仅想要查看获取的pods中的各个pod的uid,则可以使用以下命令:
[root@node root]# kubectl get pods --all-namespaces -o go-template --template='{{range .items}}{{.metadata.uid}}
{{end}}'
0313ffff-f1f4-11e7-9cda-40f2e9b98448
ee49bdcd-f1f2-11e7-9cda-40f2e9b98448
f1e0eb80-f1f2-11e7-9cda-40f2e9b98448
[root@node-106 xuxinkun]# kubectl get pods -o yaml
apiVersion: v1
items:
- apiVersion: v1
kind: Pod
metadata:
name: nginx-deployment-1751389443-26gbm
namespace: default
uid: a911e34b-f445-11e7-9cda-40f2e9b98448
...
- apiVersion: v1
kind: Pod
metadata:
name: nginx-deployment-1751389443-rsbkc
namespace: default
uid: a911d2d2-f445-11e7-9cda-40f2e9b98448
...
- apiVersion: v1
kind: Pod
metadata:
name: nginx-deployment-1751389443-sdbkx
namespace: default
uid: a911da1a-f445-11e7-9cda-40f2e9b98448
...
kind: List
metadata: {}
resourceVersion: ""
因为get pods的返回结果是List类型,获取的pods都在items这个的value中,因此需要遍历items,也就有了{{range .items}}。而后通过模板选定需要展示的内容,就是items中的每个{{.metadata.uid}}
这里特别注意,要做一个特别的处理,就是要把{{end}}前进行换行,以便在模板中插入换行符。
当然,如果觉得这样处理不优雅的话,也可以使用printf函数,在其中使用\n即可实现换行符的插入
[root@node root]# kubectl get pods --all-namespaces -o go-template --template='{{range .items}}{{printf "%s\n" .metadata.uid}}{{end}}'
其实有了printf,就可以很容易的实现对应字段的输出,且样式可以进行自己控制。比如可以这样
[root@node root]# kubectl get pods --all-namespaces -o go-template --template='{{range .items}}{{printf "|%-20s|%-50s|%-30s|\n" .metadata.namespace .metadata.name .metadata.uid}}{{end}}'
|console |console-4d2d7eab-1377218307-7lg5v |0313ffff-f1f4-11e7-9cda-40f2e9b98448|
|console |console-4d2d7eab-1377218307-q3bjd |ee49bdcd-f1f2-11e7-9cda-40f2e9b98448|
|cxftest |ipreserve-f15257ec-3788284754-nmp3x |f1e0eb80-f1f2-11e7-9cda-40f2e9b98448|
除了使用go-template之外,还可以使用go-template-file。则模板不用通过参数传进去,而是写成一个文件,然后需要指定template指向该文件即可。
[root@node root]# kubectl get pods --all-namespaces -o go-template-file --template=/home/template_file
[root@node root]# cat /home/template_file
{{range .items}}{{printf "%s\n" .metadata.uid}}{{end}}
使用go-template自定义kubectl get输出的更多相关文章
- kubectl格式化输出和调试
1.格式化输出 以特定的格式想终端输出详细信息,可以在 kubectl 命令中添加 -o 或者 -output 选项 输出格式 描述 -o=custom-columns=<spec> 使 ...
- log4j2自定义Appender(输出到文件/RPC服务中)
1.背景 虽然log4j很强大,可以将日志输出到文件.DB.ES等.但是有时候确难免完全适合自己,此时我们就需要自定义Appender,使日志输出到指定的位置上. 本文,将通过两个例子说明自定义APP ...
- 自定义 ocelot 中间件输出自定义错误信息
自定义 ocelot 中间件输出自定义错误信息 Intro ocelot 中默认的 Response 中间件在出错的时候只会设置 StatusCode 没有具体的信息,想要展示自己定义的错误信息的时候 ...
- IDEA自定义设置快捷键输出你想要的语句!
转载,侵权必删 用Eclipse时间长了, 就习惯之前的快捷键! 当然, IDEA不愧是Java开发的”利器”! 写起代码就是一个字 – “爽”! 建议大家可以去尝试一下! 当然, 在IDEA中输出S ...
- 自定义NSLog宏输出
根据编译条件,Debug时输出带行号的日志,Release时关闭日志 /* XCode LLVM XXX - Preprocessing中Debug会添加 DEBUG=1 标志 */ #ifdef D ...
- PHP内置Web Server探究(二)自定义PHP控制台输出console函数
我们在开发APP的服务器端,当和APP进行联调时通常需要实时跟踪URL请求和参数的接收情况. 但PHP并没有像Python或Java专有的控制台输出函数,Python的print()和Java的Sys ...
- ELK收集Nginx自定义日志格式输出
1.ELK收集日志的有两种常用的方式: 1.1:不修改源日志格式,简单的说就是在logstash中转通过 grok方式进行过滤处理,将原始无规则的日志转换为规则日志(Logstash自定义日志格式) ...
- IntelliJ IDEA 2017版 使用笔记(五) 模板 live template自定义设置(二) ;postfix使用;IDE快捷键使用
一.live template 活模板 就像这个单词的含义一样,live template就是一个高效的提高代码,书写速度的方式,(live template位置File-----settin ...
- 自定义SpringBoot控制台输出的图案
pringboot启动的时候,控制台输出的图案叫banner banner?啥玩意儿?相信有些人,一定是一脸懵逼... ——这个就不陌生了吧,这个是我们启动springboot的时候,控制台输出的.. ...
随机推荐
- Enable Coded UI Testing of Your Controls
http://msdn.microsoft.com/en-us/library/hh552522.aspx AccessibleObject Class http://msdn.microsoft.c ...
- 校园服务nabcd需求分析
我们的团队是敲啥都队 口号是敲啥都队敲啥都对 1.你的创意解决了用户的什么需求?(N) 我们校园服务1主要为了节省学生所浪费的没必要的时间.当你还是大一新生的时候,你是否对大学的规划一无所知,你是否迷 ...
- centos7 部署vnc
不做过多介绍了,下面直接记录下centos7系统下安装配置vncserver的操作记录 0)更改为启动桌面或命令行模式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...
- linux-rhel7配置网卡bond双网卡主备模式
参考以下文章中的 2.centos7配置bonding: https://www.cnblogs.com/huangweimin/articles/6527058.html 以下是配置过程的操作和打印 ...
- Django2.0.4 + websocket 实现实时通信,主动推送,聊天室及客服系统
webSocket是一种在单个TCP连接上进行全双工通信的协议. webSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在WebSocket API中,浏览器 ...
- 解决 warning I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
只需要加载如下代码: import os os.environ['
- match 和 lastIndex 字符串检测差异
match .replace .search 这写不能识别特殊字符 indexOf .indexof 能识别特殊字符 str.lastIndexOf('a') > -1 // 通过lastInd ...
- ES6语法 Promise Iterator
类和对象 基本定义: class Parent{ constructor(name='lmx'){ //name= 默认值 this.name=name } } let v_parent = new ...
- mvc framework ui component understand.
mvc: .htm是v, context和contex中的contextNode 是m, view controller, custom controller ,component contro ...
- window系统下搭建本地的NuGet Server
1. NuGet.Config文件所在的目录: C:\Users\xxx\AppData\Roaming\NuGet 2.将nupkg为结尾的文件放在 项目的Packages目录下.(注意是和web. ...