some Discoverers send nil targetgroup so need to check for it when updating a group

This commit is contained in:
Krasi Georgiev 2018-01-04 13:57:34 +00:00
parent 7e28397a2c
commit 638818a974
1 changed files with 9 additions and 11 deletions

View File

@ -158,16 +158,18 @@ func (m *Manager) cancelDiscoverers() {
m.discoverCancel = nil m.discoverCancel = nil
} }
func (m *Manager) updateGroup(poolKey poolKey, tg []*targetgroup.Group) { func (m *Manager) updateGroup(poolKey poolKey, tgs []*targetgroup.Group) {
done := make(chan struct{}) done := make(chan struct{})
m.actionCh <- func(ctx context.Context) { m.actionCh <- func(ctx context.Context) {
if tg != nil { if tgs != nil {
for _, t := range tg { for _, tg := range tgs {
if tg != nil { // Some Discoverers send nil targetgroup so need to check for it to avoid panics.
if _, ok := m.targets[poolKey]; !ok { if _, ok := m.targets[poolKey]; !ok {
m.targets[poolKey] = make(map[string]*targetgroup.Group) m.targets[poolKey] = make(map[string]*targetgroup.Group)
} }
m.targets[poolKey][t.Source] = t m.targets[poolKey][tg.Source] = tg
}
} }
} }
close(done) close(done)
@ -191,13 +193,9 @@ func (m *Manager) allGroups() map[string][]*targetgroup.Group {
tSetsAll := map[string][]*targetgroup.Group{} tSetsAll := map[string][]*targetgroup.Group{}
for _, pk := range pKeys { for _, pk := range pKeys {
for _, tg := range m.targets[pk] { for _, tg := range m.targets[pk] {
// Don't add empty targets.
// Some Discoverers(eg. k8s) send only the updates so removed targets will be updated with an empty Source value.
if tg.Source != "" {
tSetsAll[pk.setName] = append(tSetsAll[pk.setName], tg) tSetsAll[pk.setName] = append(tSetsAll[pk.setName], tg)
} }
} }
}
tSets <- tSetsAll tSets <- tSetsAll
} }
return <-tSets return <-tSets