diff --git a/internal/ingress/controller/template/template.go b/internal/ingress/controller/template/template.go index 8e69da2302..9d0374f7e4 100644 --- a/internal/ingress/controller/template/template.go +++ b/internal/ingress/controller/template/template.go @@ -26,6 +26,7 @@ import ( "net/url" "os" "os/exec" + "reflect" "regexp" "strings" text_template "text/template" @@ -762,6 +763,23 @@ type ingressInformation struct { Annotations map[string]string } +func (info *ingressInformation) Equal(other *ingressInformation) bool { + if info.Namespace != other.Namespace { + return false + } + if info.Rule != other.Rule { + return false + } + if info.Service != other.Service { + return false + } + if !reflect.DeepEqual(info.Annotations, other.Annotations) { + return false + } + + return true +} + func getIngressInformation(i, p interface{}) *ingressInformation { ing, ok := i.(*ingress.Ingress) if !ok { diff --git a/internal/ingress/controller/template/template_test.go b/internal/ingress/controller/template/template_test.go index 6d343d475c..0df0f0b195 100644 --- a/internal/ingress/controller/template/template_test.go +++ b/internal/ingress/controller/template/template_test.go @@ -29,6 +29,7 @@ import ( "fmt" jsoniter "github.com/json-iterator/go" + extensions "k8s.io/api/extensions/v1beta1" "k8s.io/ingress-nginx/internal/file" "k8s.io/ingress-nginx/internal/ingress" "k8s.io/ingress-nginx/internal/ingress/annotations/authreq" @@ -843,3 +844,76 @@ func TestOpentracingPropagateContext(t *testing.T) { } } } + +func TestGetIngressInformation(t *testing.T) { + validIngress := &ingress.Ingress{} + invalidIngress := "wrongtype" + validPath := "/ok" + invalidPath := 10 + + info := getIngressInformation(invalidIngress, validPath) + expected := &ingressInformation{} + if !info.Equal(expected) { + t.Errorf("Expected %v, but got %v", expected, info) + } + + info = getIngressInformation(validIngress, invalidPath) + if !info.Equal(expected) { + t.Errorf("Expected %v, but got %v", expected, info) + } + + // Setup Ingress Resource + validIngress.Namespace = "default" + validIngress.Name = "validIng" + validIngress.Annotations = map[string]string{ + "ingress.annotation": "ok", + } + validIngress.Spec.Backend = &extensions.IngressBackend{ + ServiceName: "a-svc", + } + + info = getIngressInformation(validIngress, validPath) + expected = &ingressInformation{ + Namespace: "default", + Rule: "validIng", + Annotations: map[string]string{ + "ingress.annotation": "ok", + }, + Service: "a-svc", + } + if !info.Equal(expected) { + t.Errorf("Expected %v, but got %v", expected, info) + } + + validIngress.Spec.Backend = nil + validIngress.Spec.Rules = []extensions.IngressRule{ + { + IngressRuleValue: extensions.IngressRuleValue{ + HTTP: &extensions.HTTPIngressRuleValue{ + Paths: []extensions.HTTPIngressPath{ + { + Path: "/ok", + Backend: extensions.IngressBackend{ + ServiceName: "b-svc", + }, + }, + }, + }, + }, + }, + {}, + } + + info = getIngressInformation(validIngress, validPath) + expected = &ingressInformation{ + Namespace: "default", + 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) + } +}