From bde64cf5a623225affbe5fe536f4f527aee52dd1 Mon Sep 17 00:00:00 2001 From: pasquier-s Date: Tue, 30 Jan 2018 16:00:33 +0100 Subject: [PATCH] Fix Kubernetes endpoints SD for empty subsets (#3660) * Fix Kubernetes endpoints SD for empty subsets When an endpoints object has no associated pods (replica scaled to zero for instance), the endpoints SD should return a target group with no targets so that the SD manager propagates this information to the scrape manager. Fixes #3659 * Don't send nil target groups from the Kubernetes SD This is to be consistent with the endpoints SD part. --- discovery/kubernetes/endpoints.go | 4 ---- discovery/kubernetes/endpoints_test.go | 29 ++++++++++++++++++++++++++ discovery/kubernetes/node.go | 3 +++ discovery/kubernetes/pod.go | 3 +++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/discovery/kubernetes/endpoints.go b/discovery/kubernetes/endpoints.go index 97729b1170..b700399ffc 100644 --- a/discovery/kubernetes/endpoints.go +++ b/discovery/kubernetes/endpoints.go @@ -185,10 +185,6 @@ const ( ) func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group { - if len(eps.Subsets) == 0 { - return nil - } - tg := &targetgroup.Group{ Source: endpointsSource(eps), } diff --git a/discovery/kubernetes/endpoints_test.go b/discovery/kubernetes/endpoints_test.go index 41f95b73e3..f7fe99f9d6 100644 --- a/discovery/kubernetes/endpoints_test.go +++ b/discovery/kubernetes/endpoints_test.go @@ -339,3 +339,32 @@ func TestEndpointsDiscoveryUpdate(t *testing.T) { }, }.Run(t) } + +func TestEndpointsDiscoveryEmptySubsets(t *testing.T) { + n, _, eps, _ := makeTestEndpointsDiscovery() + eps.GetStore().Add(makeEndpoints()) + + k8sDiscoveryTest{ + discovery: n, + afterStart: func() { + go func() { + eps.Update(&v1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "testendpoints", + Namespace: "default", + }, + Subsets: []v1.EndpointSubset{}, + }) + }() + }, + expectedRes: []*targetgroup.Group{ + { + Labels: model.LabelSet{ + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_endpoints_name": "testendpoints", + }, + Source: "endpoints/default/testendpoints", + }, + }, + }.Run(t) +} diff --git a/discovery/kubernetes/node.go b/discovery/kubernetes/node.go index 253146e6cb..f6d16aefca 100644 --- a/discovery/kubernetes/node.go +++ b/discovery/kubernetes/node.go @@ -60,6 +60,9 @@ func (n *Node) Run(ctx context.Context, ch chan<- []*targetgroup.Group) { // Send target groups for service updates. send := func(tg *targetgroup.Group) { + if tg == nil { + return + } select { case <-ctx.Done(): case ch <- []*targetgroup.Group{tg}: diff --git a/discovery/kubernetes/pod.go b/discovery/kubernetes/pod.go index 95e9f25b92..d868b1156a 100644 --- a/discovery/kubernetes/pod.go +++ b/discovery/kubernetes/pod.go @@ -68,6 +68,9 @@ func (p *Pod) Run(ctx context.Context, ch chan<- []*targetgroup.Group) { // Send target groups for pod updates. send := func(tg *targetgroup.Group) { + if tg == nil { + return + } level.Debug(p.logger).Log("msg", "pod update", "tg", fmt.Sprintf("%#v", tg)) select { case <-ctx.Done():