Also store the PodName when fetching from k8s API server. Also map from podName to GoldpingerPod sruct

Signed-off-by: Sachin Kamboj <skamboj1@bloomberg.net>
This commit is contained in:
Sachin Kamboj
2020-04-03 23:10:36 -04:00
parent 00cd1e3886
commit 7fa3138235
+18 -6
View File
@@ -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
}