'Mimicking Default Kubernetes CLI Output with GoLang Templates
I want to customize the output of:
kubectl get pod . . .
with --output=go-template instead of --output=yaml or --output=json.
My purpose is to get additional values (e.g., container ports) in addition to the default columns (e.g., NAME, READY, STATUS, etc..,.) with one get command:
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 . . .
. . . . . . . . . . . . . . . . . . . . .
What is $GO_TEMPLATE?
Solution 1:[1]
You can add custom columns to the output of the get command with the --output=custom-columns flag:
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
You can find more details here
Solution 2:[2]
Instead of using get, you can use describe to get the complete status:
kubectl describe $K8S_POD_NAME
#=>
Name: $K8S_POD_NAME
. . .
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
. . .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mike |
| Solution 2 | Mike |
