如何使用kubernetes go-client获得与kubectl相同的Pod状态信息 [英] How to use the kubernetes go-client to get the same Pod status info that kubectl gives

查看:436
本文介绍了如何使用kubernetes go-client获得与kubectl相同的Pod状态信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用kubernetes go-client(k8s.io/client-go/kubernetes),我知道如何获取pod.Status并且发现pod.Status.Phase有用( ... 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) }

但是,phase有点简化,因为它只显示5个值(PendingRunningSucceededFailedUnknown).我希望获得与kubectl get pods状态列中提供的信息相同的信息,例如:

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

我尤其对Init:0/1PodInitializing状态感兴趣.使用pod.Status.Phase时,处于这些状态的Pod只是显示为待处理".

是否可以使用k8s.io/client-go/kubernetes获取状态,例如Init:0/1?还是没有捷径,我需要像kubectl一样重新计算?我猜它使用Pod状态条件容器状态以构建信息.如果需要重新计算,也许可以使用 kubectl源代码?有人知道我在哪里可以找到相关的位吗? (我的Golang体验非常有限)

解决方案

简短的答案通常是不必在客户端上计算状态",因为它是在服务器级别计算的.

说明:

您尝试使用kubectl get pods打印的标准方式,在Kubernetes代码库中称为在这里定义了TablePrinter类型.

如您所见,TablePrinter的PrintObj函数被委托 humanizable_glags.go ,其中包括k8s.io/cli-runtime/pkg/printers,您会看到它正在实例化使用https://github.com/kubernetes/kubernetes/tree/master/pkg/printers/internalversion ,以便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)

解决方案

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

这篇关于如何使用kubernetes go-client获得与kubectl相同的Pod状态信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆