ouiouiouioui
ouiouiouioui

Reputation: 91

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?

Upvotes: 3

Views: 3455

Answers (2)

hoque
hoque

Reputation: 6471

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

Upvotes: 6

Fauzan
Fauzan

Reputation: 755

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
. . .

Upvotes: 2

Related Questions