Home > Net >  How to omit terminating pods from output with go templates
How to omit terminating pods from output with go templates

Time:06-21

On a project I’m working on at my current job, I’ve come across a problem where I needed to get container versions from all pods with kubectl get pods -o go-template. Then I found bug in code that is related to terminating pods - during short period of time when new deployment is done, there are two different pod version and I need to filter them out:

kubectl get pods -o go-template --template '{{ range .items }}{{"\n"}}{{ .metadata.name }}{{" - "}}{{ range .spec.containers }}{{ .image }}{{" "}}{{ end }}{{ end }}

I have found some information about how to omit terminating pods, but in most cases it was simple grep -v, which is unsuitable when using go-template output.

How can I omit pods that are terminating specifically with go-template?

CodePudding user response:

The main problem is that Kubernetes does not support Terminating pod phase per se. It calculates it (see code) - specifically if the field DeletionTimestamp is set, then the pod is usually in terminating state. Therefore this does the trick:

kubectl get pods -o go-template --template '{{ range .items }}{{if not .metadata.deletionTimestamp }}{{"\n"}}{{ .metadata.name }}{{":: "}}{{ range .spec.containers }}{{ .image }}{{" "}}{{ end }}{{ end }}{{ end }}
  • Related