Skip to content

Commit

Permalink
Add test for LoadBalancer and ExternalIPs
Browse files Browse the repository at this point in the history
  • Loading branch information
burningalchemist committed May 4, 2020
1 parent 7123782 commit e53aeef
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
7 changes: 3 additions & 4 deletions source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ func extractLoadBalancerTargets(svc *v1.Service) endpoint.Targets {
externalIPs endpoint.Targets
)


// Create a corresponding endpoint for each configured external entrypoint.
for _, lb := range svc.Status.LoadBalancer.Ingress {
if lb.IP != "" {
Expand All @@ -501,14 +500,14 @@ func extractLoadBalancerTargets(svc *v1.Service) endpoint.Targets {
}

if svc.Spec.ExternalIPs != nil {
for _, ext := range svc.Spec.ExternalIPs {
externalIPs = append(externalIPs, ext)
for _, ext := range svc.Spec.ExternalIPs {
externalIPs = append(externalIPs, ext)
}
}

if len(externalIPs) > 0 {
return externalIPs
}
}

return targets
}
Expand Down
71 changes: 68 additions & 3 deletions source/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func testServiceSourceEndpoints(t *testing.T) {
labels map[string]string
annotations map[string]string
clusterIP string
externalIPs []string
lbs []string
serviceTypesFilter []string
expected []*endpoint.Endpoint
Expand All @@ -193,13 +194,14 @@ func testServiceSourceEndpoints(t *testing.T) {
map[string]string{},
map[string]string{},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{},
false,
},
{
"no annotated services return no endpoints when ignoreing annotations",
"no annotated services return no endpoints when ignoring annotations",
"",
"",
"testing",
Expand All @@ -212,6 +214,7 @@ func testServiceSourceEndpoints(t *testing.T) {
map[string]string{},
map[string]string{},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{},
Expand All @@ -233,6 +236,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -256,6 +260,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{},
Expand All @@ -279,6 +284,7 @@ func testServiceSourceEndpoints(t *testing.T) {
"1.2.3.4",
[]string{},
[]string{},
[]string{},
[]*endpoint.Endpoint{},
false,
},
Expand All @@ -296,6 +302,7 @@ func testServiceSourceEndpoints(t *testing.T) {
map[string]string{},
map[string]string{},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -318,6 +325,7 @@ func testServiceSourceEndpoints(t *testing.T) {
map[string]string{},
map[string]string{},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -342,6 +350,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org., bar.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -368,6 +377,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org., bar.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -392,6 +402,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org., bar.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -416,6 +427,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org, bar.example.org",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -440,6 +452,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"lb.example.com"}, // Kubernetes omits the trailing dot
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -463,6 +476,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org", // Trailing dot is omitted
},
"",
[]string{},
[]string{"1.2.3.4", "lb.example.com"}, // Kubernetes omits the trailing dot
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -488,6 +502,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -512,6 +527,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{},
Expand All @@ -533,6 +549,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -556,6 +573,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{},
Expand All @@ -577,6 +595,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -601,6 +620,7 @@ func testServiceSourceEndpoints(t *testing.T) {
"service.beta.kubernetes.io/external-traffic": "OnlyLocal",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -625,6 +645,7 @@ func testServiceSourceEndpoints(t *testing.T) {
"service.beta.kubernetes.io/external-traffic": "SomethingElse",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{},
Expand All @@ -647,6 +668,7 @@ func testServiceSourceEndpoints(t *testing.T) {
"service.beta.kubernetes.io/external-traffic": "OnlyLocal",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{},
Expand All @@ -669,6 +691,7 @@ func testServiceSourceEndpoints(t *testing.T) {
"service.beta.kubernetes.io/external-traffic": "Global",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -693,6 +716,7 @@ func testServiceSourceEndpoints(t *testing.T) {
"service.beta.kubernetes.io/external-traffic": "OnlyLocal",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{},
Expand All @@ -716,9 +740,34 @@ func testServiceSourceEndpoints(t *testing.T) {
"",
[]string{},
[]string{},
[]string{},
[]*endpoint.Endpoint{},
false,
},
{
"annotated service with externalIPs returns a single endpoint with multiple targets",
"",
"",
"testing",
"foo",
v1.ServiceTypeLoadBalancer,
"",
"",
false,
false,
map[string]string{},
map[string]string{
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{"10.2.3.4", "11.2.3.4"},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
{DNSName: "foo.example.org", Targets: endpoint.Targets{"10.2.3.4", "11.2.3.4"}},
},
false,
},
{
"multiple external entrypoints return a single endpoint with multiple targets",
"",
Expand All @@ -735,6 +784,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4", "8.8.8.8"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -758,6 +808,7 @@ func testServiceSourceEndpoints(t *testing.T) {
"zalando.org/dnsname": "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{},
Expand All @@ -779,6 +830,7 @@ func testServiceSourceEndpoints(t *testing.T) {
"zalando.org/dnsname": "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -804,6 +856,7 @@ func testServiceSourceEndpoints(t *testing.T) {
"domainName": "foo.example.org., bar.example.org",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -826,6 +879,7 @@ func testServiceSourceEndpoints(t *testing.T) {
map[string]string{},
map[string]string{},
"",
[]string{},
[]string{"1.2.3.4", "elb.com"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -850,6 +904,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4", "elb.com"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -874,6 +929,7 @@ func testServiceSourceEndpoints(t *testing.T) {
"zalando.org/dnsname": "mate.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -895,6 +951,7 @@ func testServiceSourceEndpoints(t *testing.T) {
map[string]string{},
map[string]string{},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{},
Expand All @@ -916,6 +973,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -940,6 +998,7 @@ func testServiceSourceEndpoints(t *testing.T) {
ttlAnnotationKey: "foo",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -964,6 +1023,7 @@ func testServiceSourceEndpoints(t *testing.T) {
ttlAnnotationKey: "10",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -988,6 +1048,7 @@ func testServiceSourceEndpoints(t *testing.T) {
ttlAnnotationKey: "1m",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -1012,6 +1073,7 @@ func testServiceSourceEndpoints(t *testing.T) {
ttlAnnotationKey: "-10",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{},
[]*endpoint.Endpoint{
Expand All @@ -1035,6 +1097,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{string(v1.ServiceTypeLoadBalancer)},
[]*endpoint.Endpoint{
Expand All @@ -1058,6 +1121,7 @@ func testServiceSourceEndpoints(t *testing.T) {
hostnameAnnotationKey: "foo.example.org.",
},
"",
[]string{},
[]string{"1.2.3.4"},
[]string{string(v1.ServiceTypeLoadBalancer)},
[]*endpoint.Endpoint{},
Expand All @@ -1080,8 +1144,9 @@ func testServiceSourceEndpoints(t *testing.T) {

service := &v1.Service{
Spec: v1.ServiceSpec{
Type: tc.svcType,
ClusterIP: tc.clusterIP,
Type: tc.svcType,
ClusterIP: tc.clusterIP,
ExternalIPs: tc.externalIPs,
},
ObjectMeta: metav1.ObjectMeta{
Namespace: tc.svcNamespace,
Expand Down

0 comments on commit e53aeef

Please sign in to comment.