k8s Ingress: fix crash on rules with nil http

This commit is contained in:
Gary Kramlich 2020-01-07 09:26:08 -06:00 committed by Traefiker Bot
parent 49356cadd4
commit bd676922c3
3 changed files with 56 additions and 32 deletions

View file

@ -0,0 +1,9 @@
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: ""
namespace: testing
spec:
rules:
- host: testing.example.com

View file

@ -313,53 +313,57 @@ func (p *Provider) loadConfigurationFromIngresses(ctx context.Context, client Cl
conf.HTTP.Services["default-backend"] = service conf.HTTP.Services["default-backend"] = service
} }
} }
for _, rule := range ingress.Spec.Rules { for _, rule := range ingress.Spec.Rules {
if err := checkStringQuoteValidity(rule.Host); err != nil { if err := checkStringQuoteValidity(rule.Host); err != nil {
log.FromContext(ctx).Errorf("Invalid syntax for host: %s", rule.Host) log.FromContext(ctx).Errorf("Invalid syntax for host: %s", rule.Host)
continue continue
} }
for _, p := range rule.HTTP.Paths { if rule.HTTP != nil {
service, err := loadService(client, ingress.Namespace, p.Backend) for _, p := range rule.HTTP.Paths {
if err != nil { service, err := loadService(client, ingress.Namespace, p.Backend)
log.FromContext(ctx). if err != nil {
WithField("serviceName", p.Backend.ServiceName). log.FromContext(ctx).
WithField("servicePort", p.Backend.ServicePort.String()). WithField("serviceName", p.Backend.ServiceName).
Errorf("Cannot create service: %v", err) WithField("servicePort", p.Backend.ServicePort.String()).
continue Errorf("Cannot create service: %v", err)
} continue
}
if err = checkStringQuoteValidity(p.Path); err != nil { if err = checkStringQuoteValidity(p.Path); err != nil {
log.FromContext(ctx).Errorf("Invalid syntax for path: %s", p.Path) log.FromContext(ctx).Errorf("Invalid syntax for path: %s", p.Path)
continue continue
} }
serviceName := provider.Normalize(ingress.Namespace + "-" + p.Backend.ServiceName + "-" + p.Backend.ServicePort.String()) serviceName := provider.Normalize(ingress.Namespace + "-" + p.Backend.ServiceName + "-" + p.Backend.ServicePort.String())
var rules []string var rules []string
if len(rule.Host) > 0 { if len(rule.Host) > 0 {
rules = []string{"Host(`" + rule.Host + "`)"} rules = []string{"Host(`" + rule.Host + "`)"}
} }
if len(p.Path) > 0 { if len(p.Path) > 0 {
rules = append(rules, "PathPrefix(`"+p.Path+"`)") rules = append(rules, "PathPrefix(`"+p.Path+"`)")
} }
routerKey := strings.TrimPrefix(provider.Normalize(rule.Host+p.Path), "-") routerKey := strings.TrimPrefix(provider.Normalize(rule.Host+p.Path), "-")
conf.HTTP.Routers[routerKey] = &dynamic.Router{ conf.HTTP.Routers[routerKey] = &dynamic.Router{
Rule: strings.Join(rules, " && "),
Service: serviceName,
}
if len(ingress.Spec.TLS) > 0 {
// TLS enabled for this ingress, add TLS router
conf.HTTP.Routers[routerKey+"-tls"] = &dynamic.Router{
Rule: strings.Join(rules, " && "), Rule: strings.Join(rules, " && "),
Service: serviceName, Service: serviceName,
TLS: &dynamic.RouterTLSConfig{},
} }
if len(ingress.Spec.TLS) > 0 {
// TLS enabled for this ingress, add TLS router
conf.HTTP.Routers[routerKey+"-tls"] = &dynamic.Router{
Rule: strings.Join(rules, " && "),
Service: serviceName,
TLS: &dynamic.RouterTLSConfig{},
}
}
conf.HTTP.Services[serviceName] = service
} }
conf.HTTP.Services[serviceName] = service
} }
err := p.updateIngressStatus(ingress, client) err := p.updateIngressStatus(ingress, client)
if err != nil { if err != nil {
log.FromContext(ctx).Errorf("Error while updating ingress status: %v", err) log.FromContext(ctx).Errorf("Error while updating ingress status: %v", err)

View file

@ -38,6 +38,17 @@ func TestLoadConfigurationFromIngresses(t *testing.T) {
}, },
}, },
}, },
{
desc: "Ingress one rule host only",
expected: &dynamic.Configuration{
TCP: &dynamic.TCPConfiguration{},
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{},
Middlewares: map[string]*dynamic.Middleware{},
Services: map[string]*dynamic.Service{},
},
},
},
{ {
desc: "Ingress with a basic rule on one path", desc: "Ingress with a basic rule on one path",
expected: &dynamic.Configuration{ expected: &dynamic.Configuration{