如何使用kubernetes go-client获得与kubectl相同的Pod状态信息 [英] How to use the kubernetes go-client to get the same Pod status info that kubectl gives
问题描述
使用kubernetes go-client( 但是, 我尤其对 是否可以使用 简短的答案通常是不必在客户端上计算状态",因为它是在服务器级别计算的. 说明: 您尝试使用 如您所见,TablePrinter的 此外,您还会在 humanizable_glags.go ,其中包括 您还可以看到,在上述情况下,使用https://github.com/kubernetes/kubernetes/tree/master/pkg/printers/internalversion ,以便 https://github.com/kubernetes/kubernetes/tree/master/staging/src/k8s.io/cli-runtime/pkg/printers Using the kubernetes go-client ( However, the In particular, I'm interested in Is there a way to get a Status such as 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 As you can see the Also, you see that in humanreadable_glags.go it's including 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屋!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个值(Pending
,Running
,Succeeded
,Failed
,Unknown
).我希望获得与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/1
和PodInitializing
状态感兴趣.使用pod.Status.Phase
时,处于这些状态的Pod只是显示为待处理".
Init:0/1
表示该Pod具有1个Init容器,而0个容器已成功完成.在应用容器启动之前,先运行初始容器.PodInitializing
表示Pod已完成执行初始化容器.k8s.io/client-go/kubernetes
获取状态,例如Init:0/1
?还是没有捷径,我需要像kubectl一样重新计算?我猜它使用Pod状态条件和容器状态以构建信息.如果需要重新计算,也许可以使用 kubectl源代码?有人知道我在哪里可以找到相关的位吗? (我的Golang体验非常有限)kubectl get pods
打印的标准方式,在Kubernetes代码库中称为 Kubernetes TablePrinter . 在这里定义了TablePrinter类型.PrintObj
函数被委托打印机.k8s.io/cli-runtime/pkg/printers
,您会看到它正在实例化 PrintObj ,您会看到它可以处理3种情况,因为在某些情况下服务器返回了一个表,而在某些情况下则未返回(看起来是< 1.16种情况).>
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)
}
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>
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.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)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.PrintObj
function for the TablePrinter gets delegated here, but that delegation comes from configuring the HumanPrintFlags and saving the original printer.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
.