Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 626
Tom Avatar asked Jul 31 '19 19:07

Tom


People also ask

How do I get POD status with kubectl?

Using kubectl describe pods to check kube-system If the output from a specific pod is desired, run the command kubectl describe pod pod_name --namespace kube-system . The Status field should be "Running" - any other status will indicate issues with the environment.

Which of the kubectl command is used to get the information about the pod?

kubectl get − This command is capable of fetching data on the cluster about the Kubernetes resources. kubectl logs − They are used to get the logs of the container in a pod. Printing the logs can be defining the container name in the pod.

Which command can be used to display the events for a pod?

Using kubectl describe pod <podname> for example will show events at the end of the output for the pod.

Which command in Kubernetes is used to fetch list of all pods?

In this exercise you will use kubectl to fetch all of the Pods running in a cluster, and format the output to pull out the list of Containers for each.


1 Answers

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 it's 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, but that delegation comes from configuring the 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/master/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

like image 99
Rico Avatar answered Sep 29 '22 22:09

Rico