Send update when pod's IP address is empty

When the pod gets evicted, its IP address becomes empty and it needs to
be removed from the targets.
This commit is contained in:
Simon Pasquier 2018-02-14 14:23:52 +01:00
parent d127a21071
commit 2072bbc824
2 changed files with 48 additions and 5 deletions

View file

@ -170,14 +170,14 @@ func podLabels(pod *apiv1.Pod) model.LabelSet {
}
func (p *Pod) buildPod(pod *apiv1.Pod) *targetgroup.Group {
// During startup the pod may not have an IP yet. This does not even allow
// for an up metric, so we skip the target.
if len(pod.Status.PodIP) == 0 {
return nil
}
tg := &targetgroup.Group{
Source: podSource(pod),
}
// PodIP can be empty when a pod is starting or has been evicted.
if len(pod.Status.PodIP) == 0 {
return tg
}
tg.Labels = podLabels(pod)
tg.Labels[namespaceLabel] = lv(pod.Namespace)

View file

@ -350,3 +350,46 @@ func TestPodDiscoveryUpdate(t *testing.T) {
},
}.Run(t)
}
func TestPodDiscoveryUpdateEmptyPodIP(t *testing.T) {
n, i := makeTestPodDiscovery()
initialPod := makePod()
updatedPod := makePod()
updatedPod.Status.PodIP = ""
i.GetStore().Add(initialPod)
k8sDiscoveryTest{
discovery: n,
afterStart: func() { go func() { i.Update(updatedPod) }() },
expectedInitial: []*targetgroup.Group{
{
Targets: []model.LabelSet{
{
"__address__": "1.2.3.4:9000",
"__meta_kubernetes_pod_container_name": "testcontainer",
"__meta_kubernetes_pod_container_port_name": "testport",
"__meta_kubernetes_pod_container_port_number": "9000",
"__meta_kubernetes_pod_container_port_protocol": "TCP",
},
},
Labels: model.LabelSet{
"__meta_kubernetes_pod_name": "testpod",
"__meta_kubernetes_namespace": "default",
"__meta_kubernetes_pod_node_name": "testnode",
"__meta_kubernetes_pod_ip": "1.2.3.4",
"__meta_kubernetes_pod_host_ip": "2.3.4.5",
"__meta_kubernetes_pod_ready": "true",
"__meta_kubernetes_pod_uid": "abc123",
},
Source: "pod/default/testpod",
},
},
expectedRes: []*targetgroup.Group{
{
Source: "pod/default/testpod",
},
},
}.Run(t)
}