Skip to content

Commit d7b0dfd

Browse files
committed
service source uses externalIPs in ExternalName type if available
1 parent d8f408b commit d7b0dfd

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

docs/sources/service.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,6 @@ as one of the values.
106106

107107
### ExternalName
108108

109-
Creates a target with the value of the Service's `externalName` field.
109+
1. If the Service has one or more `spec.externalIPs`, uses the values in that field.
110+
2. Otherwise, creates a target with the value of the Service's `externalName` field.
110111

source/service.go

+3
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,9 @@ func extractServiceIps(svc *v1.Service) endpoint.Targets {
555555
}
556556

557557
func extractServiceExternalName(svc *v1.Service) endpoint.Targets {
558+
if len(svc.Spec.ExternalIPs) > 0 {
559+
return svc.Spec.ExternalIPs
560+
}
558561
return endpoint.Targets{svc.Spec.ExternalName}
559562
}
560563

source/service_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -3545,6 +3545,7 @@ func TestExternalServices(t *testing.T) {
35453545
labels map[string]string
35463546
annotations map[string]string
35473547
externalName string
3548+
externalIPs []string
35483549
expected []*endpoint.Endpoint
35493550
expectError bool
35503551
}{
@@ -3562,6 +3563,7 @@ func TestExternalServices(t *testing.T) {
35623563
hostnameAnnotationKey: "service.example.org",
35633564
},
35643565
"111.111.111.111",
3566+
[]string{},
35653567
[]*endpoint.Endpoint{
35663568
{DNSName: "service.example.org", Targets: endpoint.Targets{"111.111.111.111"}, RecordType: endpoint.RecordTypeA},
35673569
},
@@ -3581,6 +3583,7 @@ func TestExternalServices(t *testing.T) {
35813583
hostnameAnnotationKey: "service.example.org",
35823584
},
35833585
"2001:db8::111",
3586+
[]string{},
35843587
[]*endpoint.Endpoint{
35853588
{DNSName: "service.example.org", Targets: endpoint.Targets{"2001:db8::111"}, RecordType: endpoint.RecordTypeAAAA},
35863589
},
@@ -3600,11 +3603,32 @@ func TestExternalServices(t *testing.T) {
36003603
hostnameAnnotationKey: "service.example.org",
36013604
},
36023605
"remote.example.com",
3606+
[]string{},
36033607
[]*endpoint.Endpoint{
36043608
{DNSName: "service.example.org", Targets: endpoint.Targets{"remote.example.com"}, RecordType: endpoint.RecordTypeCNAME},
36053609
},
36063610
false,
36073611
},
3612+
{
3613+
"annotated ExternalName service with externalIPs returns a single endpoint with multiple targets",
3614+
"",
3615+
"testing",
3616+
"foo",
3617+
v1.ServiceTypeExternalName,
3618+
"",
3619+
"",
3620+
false,
3621+
map[string]string{"component": "foo"},
3622+
map[string]string{
3623+
hostnameAnnotationKey: "service.example.org",
3624+
},
3625+
"service.example.org",
3626+
[]string{"10.2.3.4", "11.2.3.4"},
3627+
[]*endpoint.Endpoint{
3628+
{DNSName: "service.example.org", RecordType: endpoint.RecordTypeA, Targets: endpoint.Targets{"10.2.3.4", "11.2.3.4"}},
3629+
},
3630+
false,
3631+
},
36083632
} {
36093633
tc := tc
36103634
t.Run(tc.title, func(t *testing.T) {
@@ -3617,6 +3641,7 @@ func TestExternalServices(t *testing.T) {
36173641
Spec: v1.ServiceSpec{
36183642
Type: tc.svcType,
36193643
ExternalName: tc.externalName,
3644+
ExternalIPs: tc.externalIPs,
36203645
},
36213646
ObjectMeta: metav1.ObjectMeta{
36223647
Namespace: tc.svcNamespace,

0 commit comments

Comments
 (0)