Fix errors load config (#470)

* Trim spaces in rules

Signed-off-by: Emile Vauge <emile@vauge.com>

* dont break the whole config while only one part is wrong

Signed-off-by: Emile Vauge <emile@vauge.com>
This commit is contained in:
Emile Vauge 2016-06-20 15:19:52 +02:00 committed by Vincent Demeester
parent 11874bc4ae
commit 93494c7e35
2 changed files with 40 additions and 18 deletions

View file

@ -123,11 +123,11 @@ func (r *Rules) Parse(expression string) (*mux.Route, error) {
// get function // get function
parsedFunctions := strings.FieldsFunc(rule, f) parsedFunctions := strings.FieldsFunc(rule, f)
if len(parsedFunctions) == 0 { if len(parsedFunctions) == 0 {
return nil, errors.New("Error parsing rule: " + rule) return nil, errors.New("Error parsing rule: '" + rule + "'")
} }
parsedFunction, ok := functions[parsedFunctions[0]] parsedFunction, ok := functions[strings.TrimSpace(parsedFunctions[0])]
if !ok { if !ok {
return nil, errors.New("Error parsing rule: " + rule + ". Unknown function: " + parsedFunctions[0]) return nil, errors.New("Error parsing rule: '" + rule + "'. Unknown function: '" + parsedFunctions[0] + "'")
} }
parsedFunctions = append(parsedFunctions[:0], parsedFunctions[1:]...) parsedFunctions = append(parsedFunctions[:0], parsedFunctions[1:]...)
fargs := func(c rune) bool { fargs := func(c rune) bool {
@ -136,12 +136,12 @@ func (r *Rules) Parse(expression string) (*mux.Route, error) {
// get function // get function
parsedArgs := strings.FieldsFunc(strings.Join(parsedFunctions, ":"), fargs) parsedArgs := strings.FieldsFunc(strings.Join(parsedFunctions, ":"), fargs)
if len(parsedArgs) == 0 { if len(parsedArgs) == 0 {
return nil, errors.New("Error parsing args from rule: " + rule) return nil, errors.New("Error parsing args from rule: '" + rule + "'")
} }
inputs := make([]reflect.Value, len(parsedArgs)) inputs := make([]reflect.Value, len(parsedArgs))
for i := range parsedArgs { for i := range parsedArgs {
inputs[i] = reflect.ValueOf(parsedArgs[i]) inputs[i] = reflect.ValueOf(strings.TrimSpace(parsedArgs[i]))
} }
method := reflect.ValueOf(parsedFunction) method := reflect.ValueOf(parsedFunction)
if method.IsValid() { if method.IsValid() {
@ -154,7 +154,7 @@ func (r *Rules) Parse(expression string) (*mux.Route, error) {
} }
} else { } else {
return nil, errors.New("Method not found: " + parsedFunctions[0]) return nil, errors.New("Method not found: '" + parsedFunctions[0] + "'")
} }
} }
return resultRoute, nil return resultRoute, nil

View file

@ -369,6 +369,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
backend2FrontendMap := map[string]string{} backend2FrontendMap := map[string]string{}
for _, configuration := range configurations { for _, configuration := range configurations {
frontendNames := sortedFrontendNamesForConfig(configuration) frontendNames := sortedFrontendNamesForConfig(configuration)
frontend:
for _, frontendName := range frontendNames { for _, frontendName := range frontendNames {
frontend := configuration.Frontends[frontendName] frontend := configuration.Frontends[frontendName]
@ -380,19 +381,24 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
frontend.EntryPoints = globalConfiguration.DefaultEntryPoints frontend.EntryPoints = globalConfiguration.DefaultEntryPoints
} }
if len(frontend.EntryPoints) == 0 { if len(frontend.EntryPoints) == 0 {
log.Errorf("No entrypoint defined for frontend %s, defaultEntryPoints:%s. Skipping it", frontendName, globalConfiguration.DefaultEntryPoints) log.Errorf("No entrypoint defined for frontend %s, defaultEntryPoints:%s", frontendName, globalConfiguration.DefaultEntryPoints)
continue log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
} }
for _, entryPointName := range frontend.EntryPoints { for _, entryPointName := range frontend.EntryPoints {
log.Debugf("Wiring frontend %s to entryPoint %s", frontendName, entryPointName) log.Debugf("Wiring frontend %s to entryPoint %s", frontendName, entryPointName)
if _, ok := serverEntryPoints[entryPointName]; !ok { if _, ok := serverEntryPoints[entryPointName]; !ok {
return nil, errors.New("Undefined entrypoint: " + entryPointName) log.Errorf("Undefined entrypoint '%s' for frontend %s", entryPointName, frontendName)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
} }
newServerRoute := &serverRoute{route: serverEntryPoints[entryPointName].httpRouter.GetHandler().NewRoute().Name(frontendName)} newServerRoute := &serverRoute{route: serverEntryPoints[entryPointName].httpRouter.GetHandler().NewRoute().Name(frontendName)}
for routeName, route := range frontend.Routes { for routeName, route := range frontend.Routes {
err := getRoute(newServerRoute, &route) err := getRoute(newServerRoute, &route)
if err != nil { if err != nil {
return nil, err log.Errorf("Error creating route for frontend %s: %v", frontendName, err)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
} }
log.Debugf("Creating route %s %s", routeName, route.Rule) log.Debugf("Creating route %s %s", routeName, route.Rule)
} }
@ -401,7 +407,9 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
if redirectHandlers[entryPointName] != nil { if redirectHandlers[entryPointName] != nil {
newServerRoute.route.Handler(redirectHandlers[entryPointName]) newServerRoute.route.Handler(redirectHandlers[entryPointName])
} else if handler, err := server.loadEntryPointConfig(entryPointName, entryPoint); err != nil { } else if handler, err := server.loadEntryPointConfig(entryPointName, entryPoint); err != nil {
return nil, err log.Errorf("Error loading entrypoint configuration for frontend %s: %v", frontendName, err)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
} else { } else {
newServerRoute.route.Handler(handler) newServerRoute.route.Handler(handler)
redirectHandlers[entryPointName] = handler redirectHandlers[entryPointName] = handler
@ -412,7 +420,9 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
var lb http.Handler var lb http.Handler
rr, _ := roundrobin.New(saveBackend) rr, _ := roundrobin.New(saveBackend)
if configuration.Backends[frontend.Backend] == nil { if configuration.Backends[frontend.Backend] == nil {
return nil, errors.New("Undefined backend: " + frontend.Backend) log.Errorf("Undefined backend '%s' for frontend %s", frontend.Backend, frontendName)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
} }
lbMethod, err := types.NewLoadBalancerMethod(configuration.Backends[frontend.Backend].LoadBalancer) lbMethod, err := types.NewLoadBalancerMethod(configuration.Backends[frontend.Backend].LoadBalancer)
if err != nil { if err != nil {
@ -426,12 +436,16 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
for serverName, server := range configuration.Backends[frontend.Backend].Servers { for serverName, server := range configuration.Backends[frontend.Backend].Servers {
url, err := url.Parse(server.URL) url, err := url.Parse(server.URL)
if err != nil { if err != nil {
return nil, err log.Errorf("Error parsing server URL %s: %v", server.URL, err)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
} }
backend2FrontendMap[url.String()] = frontendName backend2FrontendMap[url.String()] = frontendName
log.Debugf("Creating server %s at %s with weight %d", serverName, url.String(), server.Weight) log.Debugf("Creating server %s at %s with weight %d", serverName, url.String(), server.Weight)
if err := rebalancer.UpsertServer(url, roundrobin.Weight(server.Weight)); err != nil { if err := rebalancer.UpsertServer(url, roundrobin.Weight(server.Weight)); err != nil {
return nil, err log.Errorf("Error adding server %s to load balancer: %v", server.URL, err)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
} }
} }
case types.Wrr: case types.Wrr:
@ -440,12 +454,16 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
for serverName, server := range configuration.Backends[frontend.Backend].Servers { for serverName, server := range configuration.Backends[frontend.Backend].Servers {
url, err := url.Parse(server.URL) url, err := url.Parse(server.URL)
if err != nil { if err != nil {
return nil, err log.Errorf("Error parsing server URL %s: %v", server.URL, err)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
} }
backend2FrontendMap[url.String()] = frontendName backend2FrontendMap[url.String()] = frontendName
log.Debugf("Creating server %s at %s with weight %d", serverName, url.String(), server.Weight) 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 { if err := rr.UpsertServer(url, roundrobin.Weight(server.Weight)); err != nil {
return nil, err log.Errorf("Error adding server %s to load balancer: %v", server.URL, err)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
} }
} }
} }
@ -453,12 +471,16 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
if maxConns != nil && maxConns.Amount != 0 { if maxConns != nil && maxConns.Amount != 0 {
extractFunc, err := utils.NewExtractor(maxConns.ExtractorFunc) extractFunc, err := utils.NewExtractor(maxConns.ExtractorFunc)
if err != nil { if err != nil {
return nil, err log.Errorf("Error creating connlimit: %v", err)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
} }
log.Debugf("Creating loadd-balancer connlimit") log.Debugf("Creating loadd-balancer connlimit")
lb, err = connlimit.New(lb, extractFunc, maxConns.Amount, connlimit.Logger(oxyLogger)) lb, err = connlimit.New(lb, extractFunc, maxConns.Amount, connlimit.Logger(oxyLogger))
if err != nil { if err != nil {
return nil, err log.Errorf("Error creating connlimit: %v", err)
log.Errorf("Skipping frontend %s...", frontendName)
continue frontend
} }
} }
// retry ? // retry ?