Fix wildcard hostname issue

This commit is contained in:
Avdhoot Dendge 2021-01-05 16:56:04 +05:30 committed by GitHub
parent c4fa96c41e
commit d4f0a9ff62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 193 additions and 1 deletions

View file

@ -2,6 +2,7 @@ package ingress
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"math"
@ -253,6 +254,8 @@ func (p *Provider) loadConfigurationFromIngresses(ctx context.Context, client Cl
conf.HTTP.Services["default-backend"] = service
}
routers := map[string][]*dynamic.Router{}
for _, rule := range ingress.Spec.Rules {
if err := p.updateIngressStatus(ingress, client); err != nil {
log.FromContext(ctx).Errorf("Error while updating ingress status: %v", err)
@ -276,8 +279,26 @@ func (p *Provider) loadConfigurationFromIngresses(ctx context.Context, client Cl
conf.HTTP.Services[serviceName] = service
routerKey := strings.TrimPrefix(provider.Normalize(ingress.Name+"-"+ingress.Namespace+"-"+rule.Host+pa.Path), "-")
routers[routerKey] = append(routers[routerKey], loadRouter(rule, pa, rtConfig, serviceName))
}
}
conf.HTTP.Routers[routerKey] = loadRouter(rule, pa, rtConfig, serviceName)
for routerKey, conflictingRouters := range routers {
if len(conflictingRouters) == 1 {
conf.HTTP.Routers[routerKey] = conflictingRouters[0]
continue
}
log.FromContext(ctx).Debugf("Multiple routers are defined with the same key %q, generating hashes to avoid conflicts", routerKey)
for _, router := range conflictingRouters {
key, err := makeRouterKeyWithHash(routerKey, router.Rule)
if err != nil {
log.FromContext(ctx).Error(err)
continue
}
conf.HTTP.Routers[key] = router
}
}
}
@ -543,6 +564,17 @@ func getProtocol(portSpec corev1.ServicePort, portName string, svcConfig *Servic
return protocol
}
func makeRouterKeyWithHash(key, rule string) (string, error) {
h := sha256.New()
if _, err := h.Write([]byte(rule)); err != nil {
return "", err
}
dupKey := fmt.Sprintf("%s-%.10x", key, h.Sum(nil))
return dupKey, nil
}
func loadRouter(rule networkingv1beta1.IngressRule, pa networkingv1beta1.HTTPIngressPath, rtConfig *RouterConfig, serviceName string) *dynamic.Router {
var rules []string
if len(rule.Host) > 0 {