@@ -31,13 +31,14 @@ import (
3131func TestServiceSourceFqdnTemplatingExamples (t * testing.T ) {
3232
3333 for _ , tt := range []struct {
34- title string
35- services []* v1.Service
36- endpointSlices []* discoveryv1.EndpointSlice
37- fqdnTemplate string
38- combineFQDN bool
39- publishHostIp bool
40- expected []* endpoint.Endpoint
34+ title string
35+ services []* v1.Service
36+ endpointSlices []* discoveryv1.EndpointSlice
37+ fqdnTemplate string
38+ combineFQDN bool
39+ publishHostIp bool
40+ serviceTypesFilter []string
41+ expected []* endpoint.Endpoint
4142 }{
4243 {
4344 title : "templating with multiple services" ,
@@ -182,6 +183,129 @@ func TestServiceSourceFqdnTemplatingExamples(t *testing.T) {
182183 {DNSName : "www.service-two.website.example.tld" , RecordType : endpoint .RecordTypeCNAME , Targets : endpoint.Targets {"www.bucket-name.amazonaws.com" }},
183184 },
184185 },
186+ {
187+ title : "fqdn with endpoint-type annotation and loose service type filtering" ,
188+ serviceTypesFilter : []string {},
189+ services : []* v1.Service {
190+ {
191+ ObjectMeta : metav1.ObjectMeta {
192+ Namespace : "svc-ns" ,
193+ Name : "svc-one" ,
194+ Annotations : map [string ]string {
195+ annotations .EndpointsTypeKey : EndpointsTypeNodeExternalIP ,
196+ },
197+ },
198+ Spec : v1.ServiceSpec {
199+ Type : v1 .ServiceTypeClusterIP ,
200+ ClusterIP : v1 .ClusterIPNone ,
201+ ClusterIPs : []string {v1 .ClusterIPNone },
202+ },
203+ Status : v1.ServiceStatus {
204+ LoadBalancer : v1.LoadBalancerStatus {},
205+ },
206+ },
207+ },
208+ endpointSlices : []* discoveryv1.EndpointSlice {
209+ {
210+ ObjectMeta : metav1.ObjectMeta {
211+ Name : "svc-one-xxxxx" ,
212+ Namespace : "svc-ns" ,
213+ Labels : map [string ]string {
214+ discoveryv1 .LabelServiceName : "svc-one" ,
215+ v1 .IsHeadlessService : "" ,
216+ },
217+ },
218+ AddressType : discoveryv1 .AddressTypeIPv4 ,
219+ Endpoints : []discoveryv1.Endpoint {
220+ {
221+ Addresses : []string {"100.66.2.246" },
222+ Hostname : testutils .ToPtr ("ip-10-1-164-158.internal" ),
223+ NodeName : testutils .ToPtr ("test-node" ),
224+ TargetRef : & v1.ObjectReference {
225+ Kind : "Pod" ,
226+ Name : "pod-1" ,
227+ Namespace : "svc-ns" ,
228+ },
229+ },
230+ {
231+ Addresses : []string {"100.66.2.247" },
232+ Hostname : testutils .ToPtr ("ip-10-1-164-158.internal" ),
233+ NodeName : testutils .ToPtr ("test-node" ),
234+ TargetRef : & v1.ObjectReference {
235+ Kind : "Pod" ,
236+ Name : "pod-2" ,
237+ Namespace : "svc-ns" ,
238+ },
239+ },
240+ },
241+ },
242+ },
243+ fqdnTemplate : "{{.Name}}.{{.Namespace}}.cluster.com" ,
244+ expected : []* endpoint.Endpoint {
245+ {DNSName : "ip-10-1-164-158.internal.svc-one.svc-ns.cluster.com" , RecordType : endpoint .RecordTypeA , Targets : endpoint.Targets {"203.0.113.10" }},
246+ {DNSName : "svc-one.svc-ns.cluster.com" , RecordType : endpoint .RecordTypeA , Targets : endpoint.Targets {"203.0.113.10" }},
247+ },
248+ },
249+ {
250+ title : "fqdn with endpoint-type annotation and service type filtering does not include required type" ,
251+ serviceTypesFilter : []string {string (v1 .ServiceTypeClusterIP )},
252+ services : []* v1.Service {
253+ {
254+ ObjectMeta : metav1.ObjectMeta {
255+ Namespace : "svc-ns" ,
256+ Name : "svc-one" ,
257+ Annotations : map [string ]string {
258+ annotations .EndpointsTypeKey : EndpointsTypeNodeExternalIP ,
259+ },
260+ },
261+ Spec : v1.ServiceSpec {
262+ Type : v1 .ServiceTypeClusterIP ,
263+ ClusterIP : v1 .ClusterIPNone ,
264+ ClusterIPs : []string {v1 .ClusterIPNone },
265+ },
266+ Status : v1.ServiceStatus {
267+ LoadBalancer : v1.LoadBalancerStatus {},
268+ },
269+ },
270+ },
271+ endpointSlices : []* discoveryv1.EndpointSlice {
272+ {
273+ ObjectMeta : metav1.ObjectMeta {
274+ Name : "svc-one-xxxxx" ,
275+ Namespace : "svc-ns" ,
276+ Labels : map [string ]string {
277+ discoveryv1 .LabelServiceName : "svc-one" ,
278+ v1 .IsHeadlessService : "" ,
279+ },
280+ },
281+ AddressType : discoveryv1 .AddressTypeIPv4 ,
282+ Endpoints : []discoveryv1.Endpoint {
283+ {
284+ Addresses : []string {"100.66.2.246" },
285+ Hostname : testutils .ToPtr ("ip-10-1-164-158.internal" ),
286+ NodeName : testutils .ToPtr ("test-node" ),
287+ TargetRef : & v1.ObjectReference {
288+ Kind : "Pod" ,
289+ Name : "pod-1" ,
290+ Namespace : "svc-ns" ,
291+ },
292+ },
293+ {
294+ Addresses : []string {"100.66.2.247" },
295+ Hostname : testutils .ToPtr ("ip-10-1-164-158.internal" ),
296+ NodeName : testutils .ToPtr ("test-node" ),
297+ TargetRef : & v1.ObjectReference {
298+ Kind : "Pod" ,
299+ Name : "pod-2" ,
300+ Namespace : "svc-ns" ,
301+ },
302+ },
303+ },
304+ },
305+ },
306+ fqdnTemplate : "{{.Name}}.{{.Namespace}}.cluster.com" ,
307+ expected : []* endpoint.Endpoint {},
308+ },
185309 {
186310 title : "templating resolve service with zone PreferSameTrafficDistribution and topology.kubernetes.io/zone annotation" ,
187311 services : []* v1.Service {
@@ -571,6 +695,17 @@ func TestServiceSourceFqdnTemplatingExamples(t *testing.T) {
571695 require .NoError (t , err )
572696 }
573697
698+ _ , err := kubeClient .CoreV1 ().Nodes ().Create (t .Context (), & v1.Node {
699+ ObjectMeta : metav1.ObjectMeta {Name : "test-node" },
700+ Status : v1.NodeStatus {
701+ Addresses : []v1.NodeAddress {
702+ {Type : v1 .NodeExternalIP , Address : "203.0.113.10" },
703+ {Type : v1 .NodeInternalIP , Address : "10.0.0.10" },
704+ },
705+ },
706+ }, metav1.CreateOptions {})
707+ require .NoError (t , err )
708+
574709 // Create endpoints and pods for the services
575710 for _ , el := range tt .endpointSlices {
576711 _ , err := kubeClient .DiscoveryV1 ().EndpointSlices (el .Namespace ).Create (t .Context (), el , metav1.CreateOptions {})
@@ -583,6 +718,7 @@ func TestServiceSourceFqdnTemplatingExamples(t *testing.T) {
583718 },
584719 Spec : v1.PodSpec {
585720 Hostname : * ep .Hostname ,
721+ NodeName : "test-node" ,
586722 },
587723 Status : v1.PodStatus {
588724 HostIP : fmt .Sprintf ("10.1.20.4%d" , i ),
@@ -603,7 +739,7 @@ func TestServiceSourceFqdnTemplatingExamples(t *testing.T) {
603739 true ,
604740 tt .publishHostIp ,
605741 true ,
606- [] string {} ,
742+ tt . serviceTypesFilter ,
607743 false ,
608744 labels .Everything (),
609745 false ,
0 commit comments