Log what backends are added/changed/removed on reload.

This commit is contained in:
Joachim Bauch 2020-10-06 16:26:19 +02:00
parent cd4d930835
commit 5797111033
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02

View file

@ -120,13 +120,17 @@ func NewBackendConfiguration(config *goconf.ConfigFile) (*BackendConfiguration,
}, nil }, nil
} }
func (b *BackendConfiguration) RemoveBackend(host string) { func (b *BackendConfiguration) RemoveBackendsForHost(host string) {
if oldBackends := b.backends[host]; len(oldBackends) > 0 {
for _, backend := range oldBackends {
log.Printf("Backend %s removed for %s", backend.id, backend.url)
}
}
delete(b.backends, host) delete(b.backends, host)
} }
func (b *BackendConfiguration) UpsertHost(host string, backends []*Backend) { func (b *BackendConfiguration) UpsertHost(host string, backends []*Backend) {
existingIndex := 0 for existingIndex, existingBackend := range b.backends[host] {
for _, existingBackend := range b.backends[host] {
found := false found := false
index := 0 index := 0
for _, newBackend := range backends { for _, newBackend := range backends {
@ -134,16 +138,26 @@ func (b *BackendConfiguration) UpsertHost(host string, backends []*Backend) {
found = true found = true
backends = append(backends[:index], backends[index+1:]...) backends = append(backends[:index], backends[index+1:]...)
break break
} else if newBackend.id == existingBackend.id {
found = true
b.backends[host][existingIndex] = newBackend
backends = append(backends[:index], backends[index+1:]...)
log.Printf("Backend %s updated for %s", newBackend.id, newBackend.url)
break
} }
index++ index++
} }
if !found { if !found {
removed := b.backends[host][existingIndex]
log.Printf("Backend %s removed for %s", removed.id, removed.url)
b.backends[host] = append(b.backends[host][:existingIndex], b.backends[host][existingIndex+1:]...) b.backends[host] = append(b.backends[host][:existingIndex], b.backends[host][existingIndex+1:]...)
} }
existingIndex++
} }
b.backends[host] = append(b.backends[host], backends...) b.backends[host] = append(b.backends[host], backends...)
for _, added := range backends {
log.Printf("Backend %s added for %s", added.id, added.url)
}
} }
func getConfiguredBackendIDs(backendIds string) (ids []string) { func getConfiguredBackendIDs(backendIds string) (ids []string) {
@ -211,7 +225,7 @@ func (b *BackendConfiguration) Reload(config *goconf.ConfigFile) {
// remove backends that are no longer configured // remove backends that are no longer configured
for hostname := range b.backends { for hostname := range b.backends {
if _, ok := configuredHosts[hostname]; !ok { if _, ok := configuredHosts[hostname]; !ok {
b.RemoveBackend(hostname) b.RemoveBackendsForHost(hostname)
} }
} }