From c5a37ddad54762a1eaf80c820d1fff6d288bef4a Mon Sep 17 00:00:00 2001 From: haleyao Date: Sun, 9 Jul 2023 21:33:31 +0800 Subject: [PATCH] Remove deleted target from discovery manager Signed-off-by: haleyao --- discovery/legacymanager/manager.go | 7 ++++++- discovery/legacymanager/manager_test.go | 10 +++------- discovery/manager.go | 7 ++++++- discovery/manager_test.go | 15 ++------------- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/discovery/legacymanager/manager.go b/discovery/legacymanager/manager.go index 87823f401..e7c79a8f8 100644 --- a/discovery/legacymanager/manager.go +++ b/discovery/legacymanager/manager.go @@ -270,7 +270,12 @@ func (m *Manager) updateGroup(poolKey poolKey, tgs []*targetgroup.Group) { } for _, tg := range tgs { if tg != nil { // Some Discoverers send nil target group so need to check for it to avoid panics. - m.targets[poolKey][tg.Source] = tg + // 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 + } } } } diff --git a/discovery/legacymanager/manager_test.go b/discovery/legacymanager/manager_test.go index 13b84e6e3..bc8a419ec 100644 --- a/discovery/legacymanager/manager_test.go +++ b/discovery/legacymanager/manager_test.go @@ -824,13 +824,9 @@ func TestTargetSetRecreatesEmptyStaticConfigs(t *testing.T) { if !ok { t.Fatalf("'%v' should be present in target groups", pkey) } - group, ok := targetGroups[""] - 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)) + _, ok = targetGroups[""] + if ok { + t.Fatalf("Target groups should be empty, got %v", targetGroups) } } diff --git a/discovery/manager.go b/discovery/manager.go index 8b304a0fa..7f06b423d 100644 --- a/discovery/manager.go +++ b/discovery/manager.go @@ -387,7 +387,12 @@ func (m *Manager) updateGroup(poolKey poolKey, tgs []*targetgroup.Group) { } for _, tg := range tgs { if tg != nil { // Some Discoverers send nil target group so need to check for it to avoid panics. - m.targets[poolKey][tg.Source] = tg + // 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 + } } } } diff --git a/discovery/manager_test.go b/discovery/manager_test.go index 537160811..67ccbcac7 100644 --- a/discovery/manager_test.go +++ b/discovery/manager_test.go @@ -1044,19 +1044,8 @@ func TestTargetSetRecreatesEmptyStaticConfigs(t *testing.T) { if !ok { t.Fatalf("'%v' should be present in target groups", p) } - group, ok := targetGroups[""] - 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) - } + require.Equal(t, 0, len(targetGroups)) + require.Equal(t, 0, len(syncedTargets)) } func TestIdenticalConfigurationsAreCoalesced(t *testing.T) {