![](/img/trans.png)
[英]How to use the kubernetes go-client to get the same Pod status info that kubectl gives
[英]Kubernetes go-client to list out pod details similar to `kubectl get pods`
我正在尝试使用Kube.netes client-go访问集群中的 pod 详细信息。
我想用它来获取在一个特定命名空间中运行的 pod 的详细信息,类似于kubectl get pods -n <my namespace>
。
我想要的详细信息是 pod 的name
、 status
、 ready
、 restarts
和age
。
我怎样才能得到这些数据?
所以,我写了一个 function 接受一个 Kubernetes 客户端(有关制作一个的详细信息,请参阅 client-go)和一个命名空间并返回所有可用的 pod-
func GetPods(client *meshkitkube.Client, namespace string) (*v1core.PodList, error) {
// Create a pod interface for the given namespace
podInterface := client.KubeClient.CoreV1().Pods(namespace)
// List the pods in the given namespace
podList, err := podInterface.List(context.TODO(), v1.ListOptions{})
if err != nil {
return nil, err
}
return podList, nil
}
获取所有 pod 后,我使用循环遍历每个 pod 中的所有 pod 和容器,并手动获取我需要的所有数据 -
// List all the pods similar to kubectl get pods -n <my namespace>
for _, pod := range podList.Items {
// Calculate the age of the pod
podCreationTime := pod.GetCreationTimestamp()
age := time.Since(podCreationTime.Time).Round(time.Second)
// Get the status of each of the pods
podStatus := pod.Status
var containerRestarts int32
var containerReady int
var totalContainers int
// If a pod has multiple containers, get the status from all
for container := range pod.Spec.Containers {
containerRestarts += podStatus.ContainerStatuses[container].RestartCount
if podStatus.ContainerStatuses[container].Ready {
containerReady++
}
totalContainers++
}
// Get the values from the pod status
name := pod.GetName()
ready := fmt.Sprintf("%v/%v", containerReady, totalContainers)
status := fmt.Sprintf("%v", podStatus.Phase)
restarts := fmt.Sprintf("%v", containerRestarts)
ageS := age.String()
// Append this to data to be printed in a table
data = append(data, []string{name, ready, status, restarts, ageS})
}
这将产生与运行kubectl get pods -n <my namespace>
时完全相同的数据。
添加到 Navendu 的优秀答案,有时当您运行kubectl get pods
与podStatus.Phase
时,“状态”列中存在差异。
如此处所述: How to use the kube.netes go-client to get the same Pod status info that kubectl gives 。 有时您想要了解 Pod 中特定容器未启动或正在初始化的实际原因。
为了得到原因,将 Navendu 的代码更新为:
// List all the pods similar to kubectl get pods -n <my namespace>
for _, pod := range podList.Items {
// Calculate the age of the pod
podCreationTime := pod.GetCreationTimestamp()
age := time.Since(podCreationTime.Time).Round(time.Second)
// Get the status of each of the pods
podStatus := pod.Status
var containerRestarts int32
var containerReady int
var totalContainers int
var containerReasonNotReady string
// If a pod has multiple containers, get the status from all
for container := range pod.Spec.Containers {
// Updated code to grab actual reason instead of showing just the podStatus
if !podStatus.ContainerStatuses[container].Ready {
if ok := podStatus.ContainerStatuses[container].State.Waiting; ok != nil {
containerReasonNotReady += podStatus.ContainerStatuses[container].State.Waiting.Reason
}
if ok := podStatus.ContainerStatuses[container].State.Terminated; ok != nil {
containerReasonNotReady += podStatus.ContainerStatuses[container].State.Terminated.Reason
}
}
containerRestarts += podStatus.ContainerStatuses[container].RestartCount
if podStatus.ContainerStatuses[container].Ready {
containerReady++
}
totalContainers++
}
// Get the values from the pod status
name := pod.GetName()
ready := fmt.Sprintf("%v/%v", containerReady, totalContainers)
// Updated code to grab actualStatus
var actualStatus string
if len(containerReasonNotReady) > 0 {
actualStatus = containerReasonNotReady
} else {
actualStatus = fmt.Sprintf("%v", podStatus.Phase)
}
restarts := fmt.Sprintf("%v", containerRestarts)
ageS := age.String()
// Append this to data to be printed in a table
data = append(data, []string{name, ready, actualStatus, restarts, ageS})
}
请注意,这是与源代码做同样事情的更直接的方法: https://github.com/kube.netes/kubectl/blob/81cffe129f76292f24be1d05435c8d35e975f765/pkg/describe/describe.go#L2013
看起来 kubectl 正在将最终结果抽象给编写器,并且编写器似乎也在封装有关终止代码、时间等的数据。
您可以将代码更新为大致相同,但 Waiting.Reason/Terminating.Reason 对我来说已经足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.