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

时光毁灭记忆、已成空白 提交于 2021-02-07 05:43:05

问题


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)


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/57297817/how-to-use-the-kubernetes-go-client-to-get-the-same-pod-status-info-that-kubectl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!