Revert "Remove deleted target from discovery manager"

Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
This commit is contained in:
Julien Pivotto 2023-08-14 23:28:23 +02:00
parent d6e1b1acdb
commit 009017a3fb
4 changed files with 22 additions and 17 deletions

View file

@ -270,14 +270,9 @@ func (m *Manager) updateGroup(poolKey poolKey, tgs []*targetgroup.Group) {
} }
for _, tg := range tgs { for _, tg := range tgs {
if tg != nil { // Some Discoverers send nil target group so need to check for it to avoid panics. if tg != nil { // Some Discoverers send nil target group so need to check for it to avoid panics.
// Remove the deleted target.
if len(tg.Targets) == 0 && len(tg.Labels) == 0 {
delete(m.targets[poolKey], tg.Source)
} else {
m.targets[poolKey][tg.Source] = tg m.targets[poolKey][tg.Source] = tg
} }
} }
}
} }
func (m *Manager) allGroups() map[string][]*targetgroup.Group { func (m *Manager) allGroups() map[string][]*targetgroup.Group {

View file

@ -824,9 +824,13 @@ func TestTargetSetRecreatesEmptyStaticConfigs(t *testing.T) {
if !ok { if !ok {
t.Fatalf("'%v' should be present in target groups", pkey) t.Fatalf("'%v' should be present in target groups", pkey)
} }
_, ok = targetGroups[""] group, ok := targetGroups[""]
if ok { if !ok {
t.Fatalf("Target groups should be empty, got %v", targetGroups) t.Fatalf("missing '' key in target groups %v", targetGroups)
}
if len(group.Targets) != 0 {
t.Fatalf("Invalid number of targets: expected 0, got %d", len(group.Targets))
} }
} }

View file

@ -387,14 +387,9 @@ func (m *Manager) updateGroup(poolKey poolKey, tgs []*targetgroup.Group) {
} }
for _, tg := range tgs { for _, tg := range tgs {
if tg != nil { // Some Discoverers send nil target group so need to check for it to avoid panics. if tg != nil { // Some Discoverers send nil target group so need to check for it to avoid panics.
// Remove the deleted target.
if len(tg.Targets) == 0 && len(tg.Labels) == 0 {
delete(m.targets[poolKey], tg.Source)
} else {
m.targets[poolKey][tg.Source] = tg m.targets[poolKey][tg.Source] = tg
} }
} }
}
} }
func (m *Manager) allGroups() map[string][]*targetgroup.Group { func (m *Manager) allGroups() map[string][]*targetgroup.Group {

View file

@ -1044,8 +1044,19 @@ func TestTargetSetRecreatesEmptyStaticConfigs(t *testing.T) {
if !ok { if !ok {
t.Fatalf("'%v' should be present in target groups", p) t.Fatalf("'%v' should be present in target groups", p)
} }
require.Equal(t, 0, len(targetGroups)) group, ok := targetGroups[""]
require.Equal(t, 0, len(syncedTargets)) if !ok {
t.Fatalf("missing '' key in target groups %v", targetGroups)
}
if len(group.Targets) != 0 {
t.Fatalf("Invalid number of targets: expected 0, got %d", len(group.Targets))
}
require.Equal(t, 1, len(syncedTargets))
require.Equal(t, 1, len(syncedTargets["prometheus"]))
if lbls := syncedTargets["prometheus"][0].Labels; lbls != nil {
t.Fatalf("Unexpected Group: expected nil Labels, got %v", lbls)
}
} }
func TestIdenticalConfigurationsAreCoalesced(t *testing.T) { func TestIdenticalConfigurationsAreCoalesced(t *testing.T) {