From b94570520dce6ccbcd2fd7e1455768afe6fbbd32 Mon Sep 17 00:00:00 2001 From: Waiariki Koia Date: Wed, 4 Sep 2019 13:47:38 +0100 Subject: [PATCH 1/2] Add validIngressFilter function to check if Ingress has at least one loadbalancer hostname/ip and host in the rules --- pkg/envoy/configurator.go | 2 +- pkg/envoy/ingress_translator.go | 24 ++++++++++++++++++++++ pkg/envoy/ingress_translator_test.go | 30 ++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/pkg/envoy/configurator.go b/pkg/envoy/configurator.go index b2b8e3a..23082f5 100644 --- a/pkg/envoy/configurator.go +++ b/pkg/envoy/configurator.go @@ -59,7 +59,7 @@ func (c *KubernetesConfigurator) Generate(ingresses []v1beta1.Ingress) cache.Sna c.Lock() defer c.Unlock() - config := translateIngresses(classFilter(ingresses, c.ingressClasses)) + config := translateIngresses(validIngressFilter(classFilter(ingresses, c.ingressClasses))) vmatch, cmatch := config.equals(c.previousConfig) diff --git a/pkg/envoy/ingress_translator.go b/pkg/envoy/ingress_translator.go index 073c960..276eceb 100644 --- a/pkg/envoy/ingress_translator.go +++ b/pkg/envoy/ingress_translator.go @@ -5,6 +5,7 @@ import ( "strings" "time" + "github.com/sirupsen/logrus" "k8s.io/api/extensions/v1beta1" ) @@ -147,6 +148,29 @@ func classFilter(ingresses []v1beta1.Ingress, ingressClass []string) []v1beta1.I return is } +func validIngressFilter(ingresses []v1beta1.Ingress) []v1beta1.Ingress { + vi := make([]v1beta1.Ingress, 0) + + Ingress: + for _, i := range ingresses { + for _, j := range i.Status.LoadBalancer.Ingress { + if j.Hostname != "" || j.IP != "" { + for _, k := range i.Spec.Rules { + if k.Host != "" { + vi = append(vi, i) + continue Ingress + } + } + logrus.Debugf("no host found in ingress config for: %+v in namespace: %+v", i.Name, i.Namespace) + continue Ingress + } + } + logrus.Debugf("no hostname or ip for loadbalancer found in ingress config for: %+v in namespace: %+v", i.Name, i.Namespace) + } + + return vi +} + type envoyIngress struct { vhost *virtualHost cluster *cluster diff --git a/pkg/envoy/ingress_translator_test.go b/pkg/envoy/ingress_translator_test.go index 4505212..59570f0 100644 --- a/pkg/envoy/ingress_translator_test.go +++ b/pkg/envoy/ingress_translator_test.go @@ -252,6 +252,36 @@ func TestIngressWithIP(t *testing.T) { } } +func TestIngressFilterWithValidConfig(t *testing.T) { + ingresses := []v1beta1.Ingress{ + newIngress("app.com", "foo.com"), + } + matchingIngresses := validIngressFilter(ingresses) + if len(matchingIngresses) != 1 { + t.Errorf("expected one ingress to be valid, got %d ingresses", len(matchingIngresses)) + } +} + +func TestIngressFilterWithNoHost(t *testing.T) { + ingresses := []v1beta1.Ingress{ + newIngress("", "foo.com"), + } + matchingIngresses := validIngressFilter(ingresses) + if len(matchingIngresses) != 0 { + t.Errorf("expected no ingress to be valid without a hostname or ip, got %d ingresses", len(matchingIngresses)) + } +} + +func TestIngressFilterWithNoLoadBalancerHostName(t *testing.T) { + ingresses := []v1beta1.Ingress{ + newIngress("app.com", ""), + } + matchingIngresses := validIngressFilter(ingresses) + if len(matchingIngresses) != 0 { + t.Errorf("expected no ingress to be valid without a hostname, got %d ingresses", len(matchingIngresses)) + } +} + func newIngress(specHost string, loadbalancerHost string) v1beta1.Ingress { return v1beta1.Ingress{ ObjectMeta: metav1.ObjectMeta{ From d446c9af9569331af0849f7595f656167e21e2f0 Mon Sep 17 00:00:00 2001 From: Waiariki Koia Date: Mon, 9 Sep 2019 13:15:01 +0100 Subject: [PATCH 2/2] Add test where ingress has an IP --- pkg/envoy/ingress_translator_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/envoy/ingress_translator_test.go b/pkg/envoy/ingress_translator_test.go index 59570f0..e6a4fa9 100644 --- a/pkg/envoy/ingress_translator_test.go +++ b/pkg/envoy/ingress_translator_test.go @@ -252,7 +252,7 @@ func TestIngressWithIP(t *testing.T) { } } -func TestIngressFilterWithValidConfig(t *testing.T) { +func TestIngressFilterWithValidConfigWithHostname(t *testing.T) { ingresses := []v1beta1.Ingress{ newIngress("app.com", "foo.com"), } @@ -262,6 +262,16 @@ func TestIngressFilterWithValidConfig(t *testing.T) { } } +func TestIngressFilterWithValidConfigWithIP(t *testing.T) { + ingresses := []v1beta1.Ingress{ + newIngressIP("app.com", "127.0.0.1"), + } + matchingIngresses := validIngressFilter(ingresses) + if len(matchingIngresses) != 1 { + t.Errorf("expected one ingress to be valid, got %d ingresses", len(matchingIngresses)) + } +} + func TestIngressFilterWithNoHost(t *testing.T) { ingresses := []v1beta1.Ingress{ newIngress("", "foo.com"),