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.
This commit is contained in:
pasquier-s 2018-01-30 16:00:33 +01:00 committed by Brian Brazil
parent 47538cf6ce
commit bde64cf5a6
4 changed files with 35 additions and 4 deletions

View File

@ -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),
}

View File

@ -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)
}

View File

@ -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}:

View File

@ -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():