Kubernetes go-client 列出类似于'kubectl get pods'的 pod

我正在尝试使用 Kubernetes client-go 来访问集群中的 pod 详细信息。

我想使用它来获取在一个特定命名空间中运行的 Pod 的详细信息,类似于 .kubectl get pods -n <my namespace>

我想要的细节是 pod 的 、 、 和 。namestatusreadyrestartsage

如何获取这些数据?


HUWWW
浏览 215回答 1
1回答

米琪卡哇伊

因此,我编写了一个函数,该函数采用Kubernetes客户端(有关制作客户端的详细信息,请参阅condue-go)和一个命名空间,并返回所有可用的 pod -func GetPods(client *meshkitkube.Client, namespace string) (*v1core.PodList, error) {&nbsp; &nbsp; // Create a pod interface for the given namespace&nbsp; &nbsp; podInterface := client.KubeClient.CoreV1().Pods(namespace)&nbsp; &nbsp; // List the pods in the given namespace&nbsp; &nbsp; podList, err := podInterface.List(context.TODO(), v1.ListOptions{})&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; return podList, nil}在获得所有 Pod 后,我使用一个循环来运行每个 pod 中的所有 pod 和容器,并手动获取我需要的所有数据 -// List all the pods similar to kubectl get pods -n <my namespace>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for _, pod := range podList.Items {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Calculate the age of the pod&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; podCreationTime := pod.GetCreationTimestamp()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; age := time.Since(podCreationTime.Time).Round(time.Second)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get the status of each of the pods&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; podStatus := pod.Status&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var containerRestarts int32&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var containerReady int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var totalContainers int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If a pod has multiple containers, get the status from all&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for container := range pod.Spec.Containers {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; containerRestarts += podStatus.ContainerStatuses[container].RestartCount&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if podStatus.ContainerStatuses[container].Ready {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; containerReady++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; totalContainers++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get the values from the pod status&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name := pod.GetName()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ready := fmt.Sprintf("%v/%v", containerReady, totalContainers)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; status := fmt.Sprintf("%v", podStatus.Phase)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; restarts := fmt.Sprintf("%v", containerRestarts)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ageS := age.String()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Append this to data to be printed in a table&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = append(data, []string{name, ready, status, restarts, ageS})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }这将导致与运行 时获得的数据完全相同。kubectl get pods -n <my namespace>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go