Fix nginx variable service_port (nginx) (#4500)

This commit is contained in:
Manuel Alejandro de Brito Fontes 2019-08-31 11:24:01 -04:00 committed by GitHub
parent 72cb7f5e14
commit c7d2444cf4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 88 deletions

View file

@ -797,6 +797,7 @@ type ingressInformation struct {
Namespace string Namespace string
Rule string Rule string
Service string Service string
ServicePort string
Annotations map[string]string Annotations map[string]string
} }
@ -810,6 +811,9 @@ func (info *ingressInformation) Equal(other *ingressInformation) bool {
if info.Service != other.Service { if info.Service != other.Service {
return false return false
} }
if info.ServicePort != other.ServicePort {
return false
}
if !reflect.DeepEqual(info.Annotations, other.Annotations) { if !reflect.DeepEqual(info.Annotations, other.Annotations) {
return false return false
} }
@ -848,6 +852,9 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
if ing.Spec.Backend != nil { if ing.Spec.Backend != nil {
info.Service = ing.Spec.Backend.ServiceName info.Service = ing.Spec.Backend.ServiceName
if ing.Spec.Backend.ServicePort.String() != "0" {
info.ServicePort = ing.Spec.Backend.ServicePort.String()
}
} }
for _, rule := range ing.Spec.Rules { for _, rule := range ing.Spec.Rules {
@ -862,6 +869,10 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
for _, rPath := range rule.HTTP.Paths { for _, rPath := range rule.HTTP.Paths {
if path == rPath.Path { if path == rPath.Path {
info.Service = rPath.Backend.ServiceName info.Service = rPath.Backend.ServiceName
if rPath.Backend.ServicePort.String() != "0" {
info.ServicePort = rPath.Backend.ServicePort.String()
}
return info return info
} }
} }

View file

@ -29,7 +29,11 @@ import (
"testing" "testing"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
apiv1 "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1beta1" networking "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/ingress-nginx/internal/ingress" "k8s.io/ingress-nginx/internal/ingress"
"k8s.io/ingress-nginx/internal/ingress/annotations/authreq" "k8s.io/ingress-nginx/internal/ingress/annotations/authreq"
@ -899,104 +903,108 @@ func TestOpentracingPropagateContext(t *testing.T) {
} }
func TestGetIngressInformation(t *testing.T) { func TestGetIngressInformation(t *testing.T) {
validIngress := &ingress.Ingress{}
invalidIngress := "wrongtype"
host := "host1"
validPath := "/ok"
invalidPath := 10
info := getIngressInformation(invalidIngress, host, validPath) testcases := map[string]struct {
expected := &ingressInformation{} Ingress interface{}
if !info.Equal(expected) { Host string
t.Errorf("Expected %v, but got %v", expected, info) Path interface{}
} Expected *ingressInformation
}{
info = getIngressInformation(validIngress, host, invalidPath) "wrong ingress type": {
if !info.Equal(expected) { "wrongtype",
t.Errorf("Expected %v, but got %v", expected, info) "host1",
} "/ok",
&ingressInformation{},
// Setup Ingress Resource
validIngress.Namespace = "default"
validIngress.Name = "validIng"
validIngress.Annotations = map[string]string{
"ingress.annotation": "ok",
}
validIngress.Spec.Backend = &networking.IngressBackend{
ServiceName: "a-svc",
}
info = getIngressInformation(validIngress, host, validPath)
expected = &ingressInformation{
Namespace: "default",
Rule: "validIng",
Annotations: map[string]string{
"ingress.annotation": "ok",
}, },
Service: "a-svc", "wrong path type": {
} &ingress.Ingress{},
if !info.Equal(expected) { "host1",
t.Errorf("Expected %v, but got %v", expected, info) 10,
} &ingressInformation{},
},
validIngress.Spec.Backend = nil "valid ingress definition with name validIng in namespace default": {
validIngress.Spec.Rules = []networking.IngressRule{ &ingress.Ingress{
{ networking.Ingress{
Host: host, ObjectMeta: metav1.ObjectMeta{
IngressRuleValue: networking.IngressRuleValue{ Name: "validIng",
HTTP: &networking.HTTPIngressRuleValue{ Namespace: apiv1.NamespaceDefault,
Paths: []networking.HTTPIngressPath{ Annotations: map[string]string{
{ "ingress.annotation": "ok",
Path: "/ok", },
Backend: networking.IngressBackend{ },
ServiceName: "b-svc", Spec: networking.IngressSpec{
Backend: &networking.IngressBackend{
ServiceName: "a-svc",
},
},
},
nil,
},
"host1",
"",
&ingressInformation{
Namespace: "default",
Rule: "validIng",
Annotations: map[string]string{
"ingress.annotation": "ok",
},
Service: "a-svc",
},
},
"valid ingress definition with name demo in namespace something and path /ok using a service with name b-svc port 80": {
&ingress.Ingress{
networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "demo",
Namespace: "something",
Annotations: map[string]string{
"ingress.annotation": "ok",
},
},
Spec: networking.IngressSpec{
Rules: []networking.IngressRule{
{
Host: "foo.bar",
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{
{
Path: "/ok",
Backend: networking.IngressBackend{
ServiceName: "b-svc",
ServicePort: intstr.FromInt(80),
},
},
},
},
},
}, },
{},
}, },
}, },
}, },
nil,
}, },
}, "foo.bar",
{}, "/ok",
} &ingressInformation{
Namespace: "something",
info = getIngressInformation(validIngress, host, validPath) Rule: "demo",
expected = &ingressInformation{ Annotations: map[string]string{
Namespace: "default", "ingress.annotation": "ok",
Rule: "validIng",
Annotations: map[string]string{
"ingress.annotation": "ok",
},
Service: "b-svc",
}
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info)
}
validIngress.Spec.Rules = append(validIngress.Spec.Rules, networking.IngressRule{
Host: "host2",
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{
{
Path: "/ok",
Backend: networking.IngressBackend{
ServiceName: "c-svc",
},
},
}, },
Service: "b-svc",
ServicePort: "80",
}, },
}, },
})
info = getIngressInformation(validIngress, host, validPath)
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info)
} }
info = getIngressInformation(validIngress, "host2", validPath) for title, testCase := range testcases {
expected.Service = "c-svc" info := getIngressInformation(testCase.Ingress, testCase.Host, testCase.Path)
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info) if !info.Equal(testCase.Expected) {
t.Fatalf("%s: expected '%v' but returned %v", title, testCase.Expected, info)
}
} }
} }

View file

@ -347,7 +347,7 @@ func (l1 *Location) Equal(l2 *Location) bool {
} }
} }
if l1.Port.StrVal != l2.Port.StrVal { if l1.Port.String() != l2.Port.String() {
return false return false
} }
if !(&l1.BasicDigestAuth).Equal(&l2.BasicDigestAuth) { if !(&l1.BasicDigestAuth).Equal(&l2.BasicDigestAuth) {

View file

@ -955,7 +955,7 @@ stream {
set $namespace {{ $ing.Namespace | quote}}; set $namespace {{ $ing.Namespace | quote}};
set $ingress_name {{ $ing.Rule | quote }}; set $ingress_name {{ $ing.Rule | quote }};
set $service_name {{ $ing.Service | quote }}; set $service_name {{ $ing.Service | quote }};
set $service_port {{ $location.Port | quote }}; set $service_port {{ $ing.ServicePort | quote }};
set $location_path {{ $location.Path | escapeLiteralDollar | quote }}; set $location_path {{ $location.Path | escapeLiteralDollar | quote }};
{{ if $all.Cfg.EnableOpentracing }} {{ if $all.Cfg.EnableOpentracing }}