From 7fa3138235bfb1104c031d661d2f0e2bf900b201 Mon Sep 17 00:00:00 2001 From: Sachin Kamboj Date: Fri, 3 Apr 2020 23:10:36 -0400 Subject: [PATCH] Also store the PodName when fetching from k8s API server. Also map from podName to GoldpingerPod sruct Signed-off-by: Sachin Kamboj --- pkg/goldpinger/k8s.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/pkg/goldpinger/k8s.go b/pkg/goldpinger/k8s.go index c3015c2..ded0b1d 100644 --- a/pkg/goldpinger/k8s.go +++ b/pkg/goldpinger/k8s.go @@ -15,11 +15,19 @@ package goldpinger import ( - "log" "io/ioutil" + "log" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +// GoldpingerPod contains just the basic info needed to ping and keep track of a given goldpinger pod +type GoldpingerPod struct { + Name string // Name is the name of the pod + PodIP string // PodIP is the IP address of the pod + HostIP string // HostIP is the IP address of the host where the pod lives +} + func getNamespace() string { b, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") if err != nil { @@ -30,8 +38,8 @@ func getNamespace() string { return namespace } -// GetAllPods returns a map of Pod IP to Host IP based on a label selector defined in config -func GetAllPods() map[string]string { +// GetAllPods returns a mapping from a pod name to a pointer to a GoldpingerPod(s) +func GetAllPods() map[string]*GoldpingerPod { timer := GetLabeledKubernetesCallsTimer() pods, err := GoldpingerConfig.KubernetesClient.CoreV1().Pods(getNamespace()).List(metav1.ListOptions{LabelSelector: GoldpingerConfig.LabelSelector}) if err != nil { @@ -41,9 +49,13 @@ func GetAllPods() map[string]string { timer.ObserveDuration() } - var podsreturn = make(map[string]string) + var podMap = make(map[string]*GoldpingerPod) for _, pod := range pods.Items { - podsreturn[pod.Status.PodIP] = pod.Status.HostIP + podMap[pod.Name] = &GoldpingerPod{ + Name: pod.Name, + PodIP: pod.Status.PodIP, + HostIP: pod.Status.HostIP, + } } - return podsreturn + return podMap }