Skip to content

Commit

Permalink
Merge pull request #47 from uswitch/only-valid-envoy-config
Browse files Browse the repository at this point in the history
Add config validation before sending to envoy
  • Loading branch information
waiariki-koia authored Sep 10, 2019
2 parents 603485b + d446c9a commit 828ea60
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/envoy/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions pkg/envoy/ingress_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"time"

"github.com/sirupsen/logrus"
"k8s.io/api/extensions/v1beta1"
)

Expand Down Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions pkg/envoy/ingress_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,46 @@ func TestIngressWithIP(t *testing.T) {
}
}

func TestIngressFilterWithValidConfigWithHostname(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 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"),
}
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{
Expand Down

0 comments on commit 828ea60

Please sign in to comment.