片刻后我找到了答案,但完全忘了在这里发布!
请注意,如果您不在Kubernetes环境中,它将崩溃
import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "os" "os/signal" "time")//// Pod infos//func GetPodDetails() (IP string, name string) { // creates the in-cluster config config, err := rest.InClusterConfig() if err != nil { panic(err.Error()) } // creates the clientset clientset, err := kubernetes.NewForConfig(config) if err != nil { panic(err.Error()) } IP = "" for { if IP != "" { break } else { log.Printf("No IP for now.n") } pods, err := clientset.CoreV1().Pods("default").List(metav1.ListOptions{}) if err != nil { panic(err.Error()) } for _, pod := range pods.Items { pod, _ := clientset.CoreV1().Pods("default").Get(pod.Name, metav1.GetOptions{}) if pod.Name == os.Getenv("HOSTNAME") { IP = pod.Status.PodIP } } log.Printf("Waits...n") time.Sleep(1 * time.Second) } name = os.Getenv("HOSTNAME") log.Printf("Trying os.Getenv("HOSTNAME/IP"): [%s][%s]n", name, IP) return IP, name}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)