1
0
Fork 0

extract lb configuration steps into method

This commit is contained in:
Marco Jantke 2017-07-08 10:33:17 +02:00 committed by SALLEYRON Julien
parent a2d68ed881
commit 58ffea6627

View file

@ -735,24 +735,14 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
rebalancer, _ = roundrobin.NewRebalancer(rr, roundrobin.RebalancerLogger(oxyLogger), roundrobin.RebalancerStickySession(sticky)) rebalancer, _ = roundrobin.NewRebalancer(rr, roundrobin.RebalancerLogger(oxyLogger), roundrobin.RebalancerStickySession(sticky))
} }
lb = rebalancer lb = rebalancer
for serverName, server := range configuration.Backends[frontend.Backend].Servers { if err := configureLBServers(rebalancer, configuration, frontend); err != nil {
url, err := url.Parse(server.URL) log.Errorf("Skipping frontend %s...", frontendName)
if err != nil { continue frontend
log.Errorf("Error parsing server URL %s: %v", server.URL, err) }
log.Errorf("Skipping frontend %s...", frontendName) hcOpts := parseHealthCheckOptions(rebalancer, frontend.Backend, configuration.Backends[frontend.Backend].HealthCheck, globalConfiguration.HealthCheck)
continue frontend if hcOpts != nil {
} log.Debugf("Setting up backend health check %s", *hcOpts)
log.Debugf("Creating server %s at %s with weight %d", serverName, url.String(), server.Weight) backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(*hcOpts)
if err := rebalancer.UpsertServer(url, roundrobin.Weight(server.Weight)); err != nil {
log.Errorf("Error adding server %s to load balancer: %v", server.URL, err)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
}
hcOpts := parseHealthCheckOptions(rebalancer, frontend.Backend, configuration.Backends[frontend.Backend].HealthCheck, globalConfiguration.HealthCheck)
if hcOpts != nil {
log.Debugf("Setting up backend health check %s", *hcOpts)
backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(*hcOpts)
}
} }
case types.Wrr: case types.Wrr:
log.Debugf("Creating load-balancer wrr") log.Debugf("Creating load-balancer wrr")
@ -765,19 +755,9 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
} }
} }
lb = rr lb = rr
for serverName, server := range configuration.Backends[frontend.Backend].Servers { if err := configureLBServers(rr, configuration, frontend); err != nil {
url, err := url.Parse(server.URL) log.Errorf("Skipping frontend %s...", frontendName)
if err != nil { continue frontend
log.Errorf("Error parsing server URL %s: %v", server.URL, err)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
}
log.Debugf("Creating server %s at %s with weight %d", serverName, url.String(), server.Weight)
if err := rr.UpsertServer(url, roundrobin.Weight(server.Weight)); err != nil {
log.Errorf("Error adding server %s to load balancer: %v", server.URL, err)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
}
} }
hcOpts := parseHealthCheckOptions(rr, frontend.Backend, configuration.Backends[frontend.Backend].HealthCheck, globalConfiguration.HealthCheck) hcOpts := parseHealthCheckOptions(rr, frontend.Backend, configuration.Backends[frontend.Backend].HealthCheck, globalConfiguration.HealthCheck)
if hcOpts != nil { if hcOpts != nil {
@ -901,6 +881,22 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
return serverEntryPoints, nil return serverEntryPoints, nil
} }
func configureLBServers(lb healthcheck.LoadBalancer, config *types.Configuration, frontend *types.Frontend) error {
for serverName, server := range config.Backends[frontend.Backend].Servers {
u, err := url.Parse(server.URL)
if err != nil {
log.Errorf("Error parsing server URL %s: %v", server.URL, err)
return err
}
log.Debugf("Creating server %s at %s with weight %d", serverName, u, server.Weight)
if err := lb.UpsertServer(u, roundrobin.Weight(server.Weight)); err != nil {
log.Errorf("Error adding server %s to load balancer: %v", server.URL, err)
return err
}
}
return nil
}
func configureIPWhitelistMiddleware(whitelistSourceRanges []string) (negroni.Handler, error) { func configureIPWhitelistMiddleware(whitelistSourceRanges []string) (negroni.Handler, error) {
if len(whitelistSourceRanges) > 0 { if len(whitelistSourceRanges) > 0 {
ipSourceRanges := whitelistSourceRanges ipSourceRanges := whitelistSourceRanges