From d7b0dfd781def4f8e934ac75c59a8de4a6f8b25b Mon Sep 17 00:00:00 2001 From: Nandor Galambosi Date: Sat, 14 Oct 2023 15:31:46 +0200 Subject: [PATCH 1/2] service source uses externalIPs in ExternalName type if available --- docs/sources/service.md | 3 ++- source/service.go | 3 +++ source/service_test.go | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/sources/service.md b/docs/sources/service.md index 9a1871c7b..c112408d0 100644 --- a/docs/sources/service.md +++ b/docs/sources/service.md @@ -106,5 +106,6 @@ as one of the values. ### ExternalName -Creates a target with the value of the Service's `externalName` field. +1. If the Service has one or more `spec.externalIPs`, uses the values in that field. +2. Otherwise, creates a target with the value of the Service's `externalName` field. diff --git a/source/service.go b/source/service.go index db7247902..df9b3a886 100644 --- a/source/service.go +++ b/source/service.go @@ -555,6 +555,9 @@ func extractServiceIps(svc *v1.Service) endpoint.Targets { } func extractServiceExternalName(svc *v1.Service) endpoint.Targets { + if len(svc.Spec.ExternalIPs) > 0 { + return svc.Spec.ExternalIPs + } return endpoint.Targets{svc.Spec.ExternalName} } diff --git a/source/service_test.go b/source/service_test.go index 13c029d59..724bcd4c6 100644 --- a/source/service_test.go +++ b/source/service_test.go @@ -3545,6 +3545,7 @@ func TestExternalServices(t *testing.T) { labels map[string]string annotations map[string]string externalName string + externalIPs []string expected []*endpoint.Endpoint expectError bool }{ @@ -3562,6 +3563,7 @@ func TestExternalServices(t *testing.T) { hostnameAnnotationKey: "service.example.org", }, "111.111.111.111", + []string{}, []*endpoint.Endpoint{ {DNSName: "service.example.org", Targets: endpoint.Targets{"111.111.111.111"}, RecordType: endpoint.RecordTypeA}, }, @@ -3581,6 +3583,7 @@ func TestExternalServices(t *testing.T) { hostnameAnnotationKey: "service.example.org", }, "2001:db8::111", + []string{}, []*endpoint.Endpoint{ {DNSName: "service.example.org", Targets: endpoint.Targets{"2001:db8::111"}, RecordType: endpoint.RecordTypeAAAA}, }, @@ -3600,11 +3603,32 @@ func TestExternalServices(t *testing.T) { hostnameAnnotationKey: "service.example.org", }, "remote.example.com", + []string{}, []*endpoint.Endpoint{ {DNSName: "service.example.org", Targets: endpoint.Targets{"remote.example.com"}, RecordType: endpoint.RecordTypeCNAME}, }, false, }, + { + "annotated ExternalName service with externalIPs returns a single endpoint with multiple targets", + "", + "testing", + "foo", + v1.ServiceTypeExternalName, + "", + "", + false, + map[string]string{"component": "foo"}, + map[string]string{ + hostnameAnnotationKey: "service.example.org", + }, + "service.example.org", + []string{"10.2.3.4", "11.2.3.4"}, + []*endpoint.Endpoint{ + {DNSName: "service.example.org", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"10.2.3.4", "11.2.3.4"}}, + }, + false, + }, } { tc := tc t.Run(tc.title, func(t *testing.T) { @@ -3617,6 +3641,7 @@ func TestExternalServices(t *testing.T) { Spec: v1.ServiceSpec{ Type: tc.svcType, ExternalName: tc.externalName, + ExternalIPs: tc.externalIPs, }, ObjectMeta: metav1.ObjectMeta{ Namespace: tc.svcNamespace, From 04e92c55564ec074b5d78fd47e86eb76206b9f49 Mon Sep 17 00:00:00 2001 From: Nandor Galambosi Date: Thu, 9 Nov 2023 21:48:36 +0100 Subject: [PATCH 2/2] Testcase for dualstack externalIPs added --- source/service_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/source/service_test.go b/source/service_test.go index 724bcd4c6..18c49a21f 100644 --- a/source/service_test.go +++ b/source/service_test.go @@ -3629,6 +3629,27 @@ func TestExternalServices(t *testing.T) { }, false, }, + { + "annotated ExternalName service with externalIPs of dualstack addresses returns 2 endpoints with multiple targets", + "", + "testing", + "foo", + v1.ServiceTypeExternalName, + "", + "", + false, + map[string]string{"component": "foo"}, + map[string]string{ + hostnameAnnotationKey: "service.example.org", + }, + "service.example.org", + []string{"10.2.3.4", "11.2.3.4", "2001:db8::1", "2001:db8::2"}, + []*endpoint.Endpoint{ + {DNSName: "service.example.org", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"10.2.3.4", "11.2.3.4"}}, + {DNSName: "service.example.org", RecordType: endpoint.RecordTypeAAAA, Targets: endpoint.Targets{"2001:db8::1", "2001:db8::2"}}, + }, + false, + }, } { tc := tc t.Run(tc.title, func(t *testing.T) {