How to use the kubernetes go-client to get the same Pod status info that kubectl gives
Asked Answered
P

4

15

Using the kubernetes go-client (k8s.io/client-go/kubernetes), I know how to get pod.Status and I find the pod.Status.Phase useful (docs). For example, I can output the Pod Status Phase of all Pods using this:

    ...
    api := clientset.CoreV1()
    pods, err := api.Pods("").List(metav1.ListOptions{})
    for i, pod := range pods.Items {
        podstatusPhase := string(pod.Status.Phase)
        podCreationTime := pod.GetCreationTimestamp()
        age := time.Since(podCreationTime.Time).Round(time.Second)

        podInfo := fmt.Sprintf("[%d] Pod: %s, Phase: %s , Created: %s, Age: %s", i, pod.GetName(), podstatusPhase, podCreationTime, age.String())
        fmt.Println(podInfo)
    }

However, the phase is a little simplistic in that it only ever shows 5 values (Pending, Running, Succeeded, Failed, Unknown). I'd rather get the same info that kubectl get pods gives in the Status column, for example:

$ kubectl get pods

NAME                                        READY   STATUS              RESTARTS   AGE     IP             NODE                           NOMINATED NODE   READINESS GATES
moby-dick-cron-scheduler-1564578660-bg4sb   0/2     ContainerCreating   0          178m    <none>         ip-10-30-13-151.ec2.internal   <none>           <none>
notifications-missed-calls-1564564740-js762 0/2     Init:0/1            0          6h49m   <none>         ip-10-30-13-6.ec2.internal     <none>           <none>
antivirus-scanner-cron-1564576740-sd6hh     0/2     Completed           0          3h30m   10.30.13.169   ip-10-30-13-151.ec2.internal   <none>           <none>

In particular, I'm interested in Init:0/1 and PodInitializing statuses. The Pods in these statuses just show as "Pending" when using pod.Status.Phase.

  • Init:0/1 means the Pod has 1 Init containers and 0 have completed successfully so far. init containers run before app containers are started.
  • PodInitializing means the Pod has already finished executing Init Containers.

Is there a way to get a Status such as Init:0/1 using k8s.io/client-go/kubernetes? or is there no short-cut, and I'd need to re-calculate it the same way kubectl does? I guess it uses Pod Status Conditions and container statuses to build the info. If I need to re-calculate it, maybe I can use the kubectl sourcecode? Does anyone know where I can find the relevant bit? (I have very limited golang experience)

Piccalilli answered 31/7, 2019 at 19:37 Comment(0)
S
1

I was able to show the exact same information as kubectl get pods. Please see the answer here: https://mcmap.net/q/825183/-kubernetes-go-client-to-list-out-pod-details-similar-to-kubectl-get-pods

Snowblink answered 7/12, 2022 at 21:4 Comment(0)
R
10

The short answer is typically you don't have to calculate the 'Status' on the client since it's calculated at the server level.

To illustrate:

The standard way that you are trying to print with kubectl get pods, in the Kubernetes code base is called Human Readable. This method uses ServerPrint, which defaults to the Kubernetes TablePrinter. The TablePrinter type is defined here.

As you can see the PrintObj function for the TablePrinter gets delegated here. It delegates to the appropriate Kubernetes resource PrintObj. Also, that delegation comes together with the configured HumanPrintFlags and saving the original printer.

Also, you see that in humanreadable_glags.go it's including k8s.io/cli-runtime/pkg/printers, and you see that it's instantiating a printers.NewTablePrinter which is defined in k8s.io/kubernetes/pkg/printers.

The actual function to print that gets called is this PrintObj and you can see that it handles 3 cases since in some cases the server returns a table and some not (looks like < 1.16 cases).

You also see that in the above case none of the code in https://github.com/kubernetes/kubernetes/tree/4477bf02f211093b32cf58f64aa42aff77daea61/pkg/printers/internalversion is used, so that calculation happens behind the kube-apiserver side.

Keep in mind that this is the Human Readable printer and there other types of printers defined here (depending on the options): https://github.com/kubernetes/kubernetes/tree/master/staging/src/k8s.io/cli-runtime/pkg/printers

Retrogressive answered 1/8, 2019 at 23:54 Comment(6)
Most of the links you defined are broken.Heliport
Thanks, I'll take a look. Stuff changes.Retrogressive
Updated the links... seems like the moved the code.Retrogressive
How can we simply get a notification if all the containers in a Pod finish and Pod life-cycle becomes COMPLETED? Any example would be appreciated.Heliport
@Retrogressive protip: if you press a y shortcut (just a single keypress) while on any code or directory listing github page - it will give you a canonical url to that very commit: github.com/kubernetes/kubernetes/tree/master/pkg/printers/… -> y -> github.com/kubernetes/kubernetes/tree/…Wong
Thanks @zerkms. Fixed the answer and added more clarity. This part is of the K8s code is a bit confusing but I supposed it's meant to handle all the use cases from all the K8s resources.Retrogressive
K
2

I think you need to re-calculate it. See this

Kingdom answered 1/8, 2019 at 1:34 Comment(0)
T
1

you should use restClient to get raw table output Receiving resources as Tables

the calculation is more complicated than you think,like Age field,in kubernetes,the code: func HumanDuration(d time.Duration) string {...}

Thigmotaxis answered 15/11, 2021 at 16:32 Comment(0)
S
1

I was able to show the exact same information as kubectl get pods. Please see the answer here: https://mcmap.net/q/825183/-kubernetes-go-client-to-list-out-pod-details-similar-to-kubectl-get-pods

Snowblink answered 7/12, 2022 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.