add default path if nothing present

This commit is contained in:
Manuel Zapf 2018-10-25 09:50:03 +02:00 committed by Traefiker Bot
parent df55c24cb5
commit 22ee8700ca
2 changed files with 45 additions and 1 deletions

View file

@ -43,6 +43,7 @@ const (
traefikDefaultIngressClass = "traefik" traefikDefaultIngressClass = "traefik"
defaultBackendName = "global-default-backend" defaultBackendName = "global-default-backend"
defaultFrontendName = "global-default-frontend" defaultFrontendName = "global-default-frontend"
defaultFrontendRule = "PathPrefix:/"
allowedProtocolHTTPS = "https" allowedProtocolHTTPS = "https"
allowedProtocolH2C = "h2c" allowedProtocolH2C = "h2c"
) )
@ -238,6 +239,11 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
} }
baseName := r.Host + pa.Path baseName := r.Host + pa.Path
if len(baseName) == 0 {
baseName = pa.Backend.ServiceName
}
if priority > 0 { if priority > 0 {
baseName = strconv.Itoa(priority) + "-" + baseName baseName = strconv.Itoa(priority) + "-" + baseName
} }
@ -319,6 +325,12 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error)
} }
} }
if len(frontend.Routes) == 0 {
frontend.Routes["/"] = types.Route{
Rule: defaultFrontendRule,
}
}
templateObjects.Frontends[baseName] = frontend templateObjects.Frontends[baseName] = frontend
templateObjects.Backends[baseName].CircuitBreaker = getCircuitBreaker(service) templateObjects.Backends[baseName].CircuitBreaker = getCircuitBreaker(service)
templateObjects.Backends[baseName].LoadBalancer = getLoadBalancer(service) templateObjects.Backends[baseName].LoadBalancer = getLoadBalancer(service)
@ -539,7 +551,7 @@ func (p *Provider) addGlobalBackend(cl Client, i *extensionsv1beta1.Ingress, tem
} }
templateObjects.Frontends[defaultFrontendName].Routes["/"] = types.Route{ templateObjects.Frontends[defaultFrontendName].Routes["/"] = types.Route{
Rule: "PathPrefix:/", Rule: defaultFrontendRule,
} }
return nil return nil
@ -578,6 +590,7 @@ func getRuleForPath(pa extensionsv1beta1.HTTPIngressPath, i *extensionsv1beta1.I
rules = append(rules, rule) rules = append(rules, rule)
} }
return strings.Join(rules, ";"), nil return strings.Join(rules, ";"), nil
} }

View file

@ -50,6 +50,11 @@ func TestLoadIngresses(t *testing.T) {
onePath(iBackend("service7", intstr.FromInt(80))), onePath(iBackend("service7", intstr.FromInt(80))),
), ),
), ),
iRule(iHost(""),
iPaths(
onePath(iBackend("service8", intstr.FromInt(80))),
),
),
), ),
), ),
} }
@ -118,6 +123,14 @@ func TestLoadIngresses(t *testing.T) {
clusterIP("10.0.0.7"), clusterIP("10.0.0.7"),
sPorts(sPort(80, ""))), sPorts(sPort(80, ""))),
), ),
buildService(
sName("service8"),
sNamespace("testing"),
sUID("8"),
sSpec(
clusterIP("10.0.0.8"),
sPorts(sPort(80, ""))),
),
} }
endpoints := []*corev1.Endpoints{ endpoints := []*corev1.Endpoints{
@ -165,6 +178,14 @@ func TestLoadIngresses(t *testing.T) {
eAddresses(eAddress("10.10.0.7")), eAddresses(eAddress("10.10.0.7")),
ePorts(ePort(80, ""))), ePorts(ePort(80, ""))),
), ),
buildEndpoint(
eNamespace("testing"),
eName("service8"),
eUID("8"),
subset(
eAddresses(eAddress("10.10.0.8")),
ePorts(ePort(80, ""))),
),
} }
watchChan := make(chan interface{}) watchChan := make(chan interface{})
@ -218,6 +239,12 @@ func TestLoadIngresses(t *testing.T) {
server("http://10.10.0.7:80", weight(1)), server("http://10.10.0.7:80", weight(1)),
), ),
), ),
backend("service8",
lbMethod("wrr"),
servers(
server("http://10.10.0.8:80", weight(1)),
),
),
), ),
frontends( frontends(
frontend("foo/bar", frontend("foo/bar",
@ -248,6 +275,10 @@ func TestLoadIngresses(t *testing.T) {
passHostHeader(), passHostHeader(),
routes(route("*.service7", "HostRegexp:{subdomain:[A-Za-z0-9-_]+}.service7")), routes(route("*.service7", "HostRegexp:{subdomain:[A-Za-z0-9-_]+}.service7")),
), ),
frontend("service8",
passHostHeader(),
routes(route("/", "PathPrefix:/")),
),
), ),
) )
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)