golang獲取pods
阿新 • • 發佈:2020-12-29
for { pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{}) //deployments, err := clientset.AppsV1beta1().Deployments("").List(context.TODO(), metav1.ListOptions{}) if err != nil { panic(err.Error()) } fmt.Printf("There are %d pods in the cluster\n", len(pods.Items)) // Examples for error handling: // - Use helper functions like e.g. errors.IsNotFound() // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message namespace := "default" pod := "example-xxxxx" _, err = clientset.CoreV1().Pods(namespace).Get(context.TODO(), pod, metav1.GetOptions{}) if errors.IsNotFound(err) { fmt.Printf("Pod %s in namespace %s not found\n", pod, namespace) } else if statusError, isStatus := err.(*errors.StatusError); isStatus { fmt.Printf("Error getting pod %s in namespace %s: %v\n", pod, namespace, statusError.ErrStatus.Message) } else if err != nil { panic(err.Error()) } else { fmt.Printf("Found pod %s in namespace %s\n", pod, namespace) } time.Sleep(5 * time.Second) }
fmt.Println(pods.Items[1].Name) fmt.Println(pods.Items[1].CreationTimestamp) fmt.Println(pods.Items[1].Labels) fmt.Println(pods.Items[1].Namespace) fmt.Println(pods.Items[1].Status.HostIP) fmt.Println(pods.Items[1].Status.PodIP) fmt.Println(pods.Items[1].Status.StartTime) fmt.Println(pods.Items[1].Status.Phase) fmt.Println(pods.Items[1].Status.ContainerStatuses[0].RestartCount) //重啟次數 fmt.Println(pods.Items[1].Status.ContainerStatuses[0].Image) //獲取重啟時間 //獲取NODE fmt.Println("##################") nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{}) fmt.Println(nodes.Items[0].Name) fmt.Println(nodes.Items[0].CreationTimestamp) //加入叢集時間 fmt.Println(nodes.Items[0].Status.NodeInfo) fmt.Println(nodes.Items[0].Status.Conditions[len(nodes.Items[0].Status.Conditions)-1].Type) fmt.Println(nodes.Items[0].Status.Allocatable.Memory().String())