Skip to content

Commit

Permalink
Merge pull request #3864 from alpeb/multihosts_servicename
Browse files Browse the repository at this point in the history
ing.Service with multiple hosts fix
  • Loading branch information
k8s-ci-robot authored Mar 9, 2019
2 parents 6525ea8 + 7ea245e commit 99888d6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
12 changes: 11 additions & 1 deletion internal/ingress/controller/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,13 +731,19 @@ func (info *ingressInformation) Equal(other *ingressInformation) bool {
return true
}

func getIngressInformation(i, p interface{}) *ingressInformation {
func getIngressInformation(i, h, p interface{}) *ingressInformation {
ing, ok := i.(*ingress.Ingress)
if !ok {
klog.Errorf("expected an '*ingress.Ingress' type but %T was returned", i)
return &ingressInformation{}
}

hostname, ok := h.(string)
if !ok {
klog.Errorf("expected a 'string' type but %T was returned", h)
return &ingressInformation{}
}

path, ok := p.(string)
if !ok {
klog.Errorf("expected a 'string' type but %T was returned", p)
Expand All @@ -763,6 +769,10 @@ func getIngressInformation(i, p interface{}) *ingressInformation {
continue
}

if hostname != "" && hostname != rule.Host {
continue
}

for _, rPath := range rule.HTTP.Paths {
if path == rPath.Path {
info.Service = rPath.Backend.ServiceName
Expand Down
37 changes: 33 additions & 4 deletions internal/ingress/controller/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,16 +832,17 @@ func TestOpentracingPropagateContext(t *testing.T) {
func TestGetIngressInformation(t *testing.T) {
validIngress := &ingress.Ingress{}
invalidIngress := "wrongtype"
host := "host1"
validPath := "/ok"
invalidPath := 10

info := getIngressInformation(invalidIngress, validPath)
info := getIngressInformation(invalidIngress, host, validPath)
expected := &ingressInformation{}
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info)
}

info = getIngressInformation(validIngress, invalidPath)
info = getIngressInformation(validIngress, host, invalidPath)
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info)
}
Expand All @@ -856,7 +857,7 @@ func TestGetIngressInformation(t *testing.T) {
ServiceName: "a-svc",
}

info = getIngressInformation(validIngress, validPath)
info = getIngressInformation(validIngress, host, validPath)
expected = &ingressInformation{
Namespace: "default",
Rule: "validIng",
Expand All @@ -872,6 +873,7 @@ func TestGetIngressInformation(t *testing.T) {
validIngress.Spec.Backend = nil
validIngress.Spec.Rules = []extensions.IngressRule{
{
Host: host,
IngressRuleValue: extensions.IngressRuleValue{
HTTP: &extensions.HTTPIngressRuleValue{
Paths: []extensions.HTTPIngressPath{
Expand All @@ -888,7 +890,7 @@ func TestGetIngressInformation(t *testing.T) {
{},
}

info = getIngressInformation(validIngress, validPath)
info = getIngressInformation(validIngress, host, validPath)
expected = &ingressInformation{
Namespace: "default",
Rule: "validIng",
Expand All @@ -900,6 +902,33 @@ func TestGetIngressInformation(t *testing.T) {
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info)
}

validIngress.Spec.Rules = append(validIngress.Spec.Rules, extensions.IngressRule{
Host: "host2",
IngressRuleValue: extensions.IngressRuleValue{
HTTP: &extensions.HTTPIngressRuleValue{
Paths: []extensions.HTTPIngressPath{
{
Path: "/ok",
Backend: extensions.IngressBackend{
ServiceName: "c-svc",
},
},
},
},
},
})

info = getIngressInformation(validIngress, host, validPath)
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info)
}

info = getIngressInformation(validIngress, "host2", validPath)
expected.Service = "c-svc"
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info)
}
}

func TestBuildCustomErrorLocationsPerServer(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ stream {
{{ end }}

location {{ $path }} {
{{ $ing := (getIngressInformation $location.Ingress $location.Path) }}
{{ $ing := (getIngressInformation $location.Ingress $server.Hostname $location.Path) }}
set $namespace "{{ $ing.Namespace }}";
set $ingress_name "{{ $ing.Rule }}";
set $service_name "{{ $ing.Service }}";
Expand Down

0 comments on commit 99888d6

Please sign in to comment.