使用 GoLang 模板模仿默认 Kubernetes CLI 输出

oui*_*oui 3 go-templates kubernetes kubectl

我想自定义输出:

kubectl get pod . . .
Run Code Online (Sandbox Code Playgroud)

--output=go-template代替--output=yaml--output=json

get我的目的是使用一个命令获取除了默认列(例如,NAME、READY、STATUS 等..,.)之外的其他值(例如,容器端口) :

kubectl get pods --output=go-template --template=$GO_TEMPLATE

#=>

NAME           READY   STATUS    RESTARTS   AGE     CONTAINER PORTS   . . .
. . .          . . .   . . .     . . .      . . .   . . .             . . .
client-qfr4s   1/1     Running   0          14d     80,443            . . .
. . .          . . .   . . .     . . .      . . .   . . .             . . .
Run Code Online (Sandbox Code Playgroud)

什么是$GO_TEMPLATE

hoq*_*que 6

get您可以使用以下标志将自定义列添加到命令的输出--output=custom-columns

kubectl get pods \
--output=custom-columns='NAME:.metadata.name,STATUS:.status.phase,RESTARTS:.status.containerStatuses[].restartCount,CONATAINER_NAME:.spec.containers[*].name,PORT:.spec.containers[*].ports[*],READY:.status.containerStatuses[*].ready'

#=>

NAME                         STATUS    RESTARTS   CONATAINER_NAME   PORT                                            READY
nginx-6bc98f4797-7kv6m       Pending   0          busy,nginx        map[containerPort:8000 protocol:TCP]            false,true
nginx-6bc98f4797-zv4sp       Pending   0          busy,nginx        map[containerPort:8000 protocol:TCP]            false,true
php-apache-5986bb6b9-gllq8   Running   5          php-apache        map[containerPort:80 protocol:TCP]              true
Run Code Online (Sandbox Code Playgroud)

您可以在这里找到更多详细信息