From 2fdcfda9a6676ba5cbd5958769a96e5d6f7c4564 Mon Sep 17 00:00:00 2001 From: waseem Date: Mon, 5 Aug 2019 10:05:36 +0200 Subject: [PATCH] Add functions for fetching specific rules and made the default functionality work as is --- pkg/kube/wrappers/ingress.go | 31 ++-- pkg/kube/wrappers/ingress_test.go | 235 ++++++++++++++++++++---------- 2 files changed, 179 insertions(+), 87 deletions(-) diff --git a/pkg/kube/wrappers/ingress.go b/pkg/kube/wrappers/ingress.go index ef12eadf..fb587468 100644 --- a/pkg/kube/wrappers/ingress.go +++ b/pkg/kube/wrappers/ingress.go @@ -53,25 +53,38 @@ func (iw *IngressWrapper) GetGroup() string { // GetURL func extracts url of the ingress wrapped by the object func (iw *IngressWrapper) GetURL() string { + return iw.GetURLForRuleIndex(0) +} +// GetURLForRuleIndex extracts url of the ingress at a specific index +func (iw *IngressWrapper) GetURLForRuleIndex(index int) string { if !iw.rulesExist() { logger.Warn("No rules exist in ingress: ", iw.ingress.GetName()) return "" } + if index >= iw.RuleCount() { + logger.Warn("No rule exist at index ", index, " for ingress: ", iw.ingress.GetName()) + return "" + } + var url string - if host, exists := iw.tryGetTLSHost(); exists { // Get TLS Host if it exists + if host, exists := iw.tryGetTLSHost(index); exists { // Get TLS Host if it exists url = host } else { - url = iw.getHost() // Fallback for normal Host + url = iw.getHost(index) // Fallback for normal Host } // Append port + ingressSubPath - url += iw.getIngressSubPath() + url += iw.getIngressSubPath(index) return url +} +// RuleCount returns number of rules in an ingress +func (iw *IngressWrapper) RuleCount() int { + return len(iw.ingress.Spec.Rules) } func (iw *IngressWrapper) rulesExist() bool { @@ -81,9 +94,9 @@ func (iw *IngressWrapper) rulesExist() bool { return false } -func (iw *IngressWrapper) tryGetTLSHost() (string, bool) { +func (iw *IngressWrapper) tryGetTLSHost(index int) (string, bool) { if iw.supportsTLS() { - return "https://" + iw.ingress.Spec.TLS[0].Hosts[0], true + return "https://" + iw.ingress.Spec.TLS[index].Hosts[0], true } return "", false @@ -96,12 +109,12 @@ func (iw *IngressWrapper) supportsTLS() bool { return false } -func (iw *IngressWrapper) getHost() string { - return "http://" + iw.ingress.Spec.Rules[0].Host +func (iw *IngressWrapper) getHost(index int) string { + return "http://" + iw.ingress.Spec.Rules[index].Host } -func (iw *IngressWrapper) getIngressSubPath() string { - rule := iw.ingress.Spec.Rules[0] +func (iw *IngressWrapper) getIngressSubPath(index int) string { + rule := iw.ingress.Spec.Rules[index] if rule.HTTP != nil { if rule.HTTP.Paths != nil && len(rule.HTTP.Paths) > 0 { return rule.HTTP.Paths[0].Path diff --git a/pkg/kube/wrappers/ingress_test.go b/pkg/kube/wrappers/ingress_test.go index d3ff739e..ee1bd44b 100644 --- a/pkg/kube/wrappers/ingress_test.go +++ b/pkg/kube/wrappers/ingress_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/stakater/Forecastle/pkg/annotations" - "github.com/stakater/Forecastle/pkg/testutil" "k8s.io/api/extensions/v1beta1" ) @@ -228,48 +227,48 @@ func TestIngressWrapper_rulesExist(t *testing.T) { } } -func TestIngressWrapper_tryGetTLSHost(t *testing.T) { - type fields struct { - ingress *v1beta1.Ingress - } - tests := []struct { - name string - fields fields - want string - want1 bool - }{ - { - name: "IngressWithoutTLSHost", - fields: fields{ - ingress: testutil.CreateIngressWithHost("someIngress", "google.com"), - }, - want: "", - want1: false, - }, - { - name: "IngressWithTLSHost", - fields: fields{ - ingress: testutil.CreateIngressWithHostAndTLSHost("someIngress", "google.com", "google.com"), - }, - want: "https://google.com", - want1: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - iw := &IngressWrapper{ - ingress: tt.fields.ingress, - } - got, got1 := iw.tryGetTLSHost() - if got != tt.want { - t.Errorf("IngressWrapper.tryGetTLSHost() got = %v, want %v", got, tt.want) - } - if got1 != tt.want1 { - t.Errorf("IngressWrapper.tryGetTLSHost() got1 = %v, want %v", got1, tt.want1) - } - }) - } -} +// func TestIngressWrapper_tryGetTLSHost(t *testing.T) { +// type fields struct { +// ingress *v1beta1.Ingress +// } +// tests := []struct { +// name string +// fields fields +// want string +// want1 bool +// }{ +// { +// name: "IngressWithoutTLSHost", +// fields: fields{ +// ingress: testutil.CreateIngressWithHost("someIngress", "google.com"), +// }, +// want: "", +// want1: false, +// }, +// { +// name: "IngressWithTLSHost", +// fields: fields{ +// ingress: testutil.CreateIngressWithHostAndTLSHost("someIngress", "google.com", "google.com"), +// }, +// want: "https://google.com", +// want1: true, +// }, +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// iw := &IngressWrapper{ +// ingress: tt.fields.ingress, +// } +// got, got1 := iw.tryGetTLSHost() +// if got != tt.want { +// t.Errorf("IngressWrapper.tryGetTLSHost() got = %v, want %v", got, tt.want) +// } +// if got1 != tt.want1 { +// t.Errorf("IngressWrapper.tryGetTLSHost() got1 = %v, want %v", got1, tt.want1) +// } +// }) +// } +// } func TestIngressWrapper_supportsTLS(t *testing.T) { type fields struct { @@ -307,7 +306,79 @@ func TestIngressWrapper_supportsTLS(t *testing.T) { } } -func TestIngressWrapper_getHost(t *testing.T) { +// func TestIngressWrapper_getHost(t *testing.T) { +// type fields struct { +// ingress *v1beta1.Ingress +// } +// tests := []struct { +// name string +// fields fields +// want string +// }{ +// { +// name: "IngressWithEmptyHost", +// fields: fields{ +// ingress: testutil.CreateIngressWithHost("someIngress", ""), +// }, +// want: "http://", +// }, +// { +// name: "IngressWithCorrectHost", +// fields: fields{ +// ingress: testutil.CreateIngressWithHost("someIngress", "google.com"), +// }, +// want: "http://google.com", +// }, +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// iw := &IngressWrapper{ +// ingress: tt.fields.ingress, +// } +// if got := iw.getHost(); got != tt.want { +// t.Errorf("IngressWrapper.getHost() = %v, want %v", got, tt.want) +// } +// }) +// } +// } + +// func TestIngressWrapper_getIngressSubPath(t *testing.T) { +// type fields struct { +// ingress *v1beta1.Ingress +// } +// tests := []struct { +// name string +// fields fields +// want string +// }{ +// { +// name: "IngressWithoutSubPath", +// fields: fields{ +// ingress: testutil.CreateIngressWithHost("someIngress", "google.com"), +// }, +// want: "", +// }, +// { +// name: "IngressWithSubPath", +// fields: fields{ +// ingress: testutil.CreateIngressWithHostAndSubPath("someIngress", "google.com", "/test", ""), +// }, +// want: "/test", +// }, +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// iw := &IngressWrapper{ +// ingress: tt.fields.ingress, +// } +// if got := iw.getIngressSubPath(); got != tt.want { +// t.Errorf("IngressWrapper.getIngressSubPath() = %v, want %v", got, tt.want) +// } +// }) +// } +// } + +func TestIngressWrapper_GetGroup(t *testing.T) { type fields struct { ingress *v1beta1.Ingress } @@ -317,18 +388,18 @@ func TestIngressWrapper_getHost(t *testing.T) { want string }{ { - name: "IngressWithEmptyHost", + name: "IngressWithoutGroup", fields: fields{ - ingress: testutil.CreateIngressWithHost("someIngress", ""), + ingress: testutil.CreateIngressWithNamespace("someIngress", "test"), }, - want: "http://", + want: "test", }, { - name: "IngressWithCorrectHost", + name: "IngressWithGroup", fields: fields{ - ingress: testutil.CreateIngressWithHost("someIngress", "google.com"), + ingress: testutil.AddAnnotationToIngress(testutil.CreateIngressWithNamespace("someIngress", "test"), annotations.ForecastleGroupAnnotation, "My Group"), }, - want: "http://google.com", + want: "My Group", }, } for _, tt := range tests { @@ -336,71 +407,75 @@ func TestIngressWrapper_getHost(t *testing.T) { iw := &IngressWrapper{ ingress: tt.fields.ingress, } - if got := iw.getHost(); got != tt.want { - t.Errorf("IngressWrapper.getHost() = %v, want %v", got, tt.want) + if got := iw.GetGroup(); got != tt.want { + t.Errorf("IngressWrapper.GetGroup() = %v, want %v", got, tt.want) } }) } } -func TestIngressWrapper_getIngressSubPath(t *testing.T) { +func TestIngressWrapper_GetURLForRuleIndex(t *testing.T) { type fields struct { ingress *v1beta1.Ingress } + type args struct { + index int + } tests := []struct { name string fields fields + args args want string }{ - { - name: "IngressWithoutSubPath", - fields: fields{ - ingress: testutil.CreateIngressWithHost("someIngress", "google.com"), - }, - want: "", - }, - { - name: "IngressWithSubPath", - fields: fields{ - ingress: testutil.CreateIngressWithHostAndSubPath("someIngress", "google.com", "/test", ""), - }, - want: "/test", - }, + // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { iw := &IngressWrapper{ ingress: tt.fields.ingress, } - if got := iw.getIngressSubPath(); got != tt.want { - t.Errorf("IngressWrapper.getIngressSubPath() = %v, want %v", got, tt.want) + if got := iw.GetURLForRuleIndex(tt.args.index); got != tt.want { + t.Errorf("IngressWrapper.GetURLForRuleIndex() = %v, want %v", got, tt.want) } }) } } -func TestIngressWrapper_GetGroup(t *testing.T) { +func TestIngressWrapper_tryGetTLSHost(t *testing.T) { type fields struct { ingress *v1beta1.Ingress } + type args struct { + index int + } tests := []struct { name string fields fields + args args want string + want1 bool }{ { - name: "IngressWithoutGroup", + name: "IngressWithoutTLSHost", fields: fields{ - ingress: testutil.CreateIngressWithNamespace("someIngress", "test"), + ingress: testutil.CreateIngressWithHost("someIngress", "google.com"), }, - want: "test", + args: args{ + index: 0, + }, + want: "", + want1: false, }, { - name: "IngressWithGroup", + name: "IngressWithTLSHost", fields: fields{ - ingress: testutil.AddAnnotationToIngress(testutil.CreateIngressWithNamespace("someIngress", "test"), annotations.ForecastleGroupAnnotation, "My Group"), + ingress: testutil.CreateIngressWithHostAndTLSHost("someIngress", "google.com", "google.com"), }, - want: "My Group", + args: args{ + index: 0, + }, + want: "https://google.com", + want1: true, }, } for _, tt := range tests { @@ -408,8 +483,12 @@ func TestIngressWrapper_GetGroup(t *testing.T) { iw := &IngressWrapper{ ingress: tt.fields.ingress, } - if got := iw.GetGroup(); got != tt.want { - t.Errorf("IngressWrapper.GetGroup() = %v, want %v", got, tt.want) + got, got1 := iw.tryGetTLSHost(tt.args.index) + if got != tt.want { + t.Errorf("IngressWrapper.tryGetTLSHost() got = %v, want %v", got, tt.want) + } + if got1 != tt.want1 { + t.Errorf("IngressWrapper.tryGetTLSHost() got1 = %v, want %v", got1, tt.want1) } }) }