| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | package kubernetes
 | 
					
						
							| 
									
										
										
										
											2018-06-27 07:45:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	"context"
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2018-06-27 07:45:32 -07:00
										 |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2018-06-27 07:45:32 -07:00
										 |  |  | 	"testing"
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2018-06-27 07:45:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | 	"github.com/coredns/coredns/plugin/kubernetes/object"
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2018-06-27 07:45:32 -07:00
										 |  |  | 	api "k8s.io/api/core/v1"
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 	discovery "k8s.io/api/discovery/v1"
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	meta "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
					
						
							|  |  |  | 	"k8s.io/client-go/kubernetes"
 | 
					
						
							|  |  |  | 	"k8s.io/client-go/kubernetes/fake"
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	mcs "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
 | 
					
						
							|  |  |  | 	mcsClientsetFake "sigs.k8s.io/mcs-api/pkg/client/clientset/versioned/fake"
 | 
					
						
							|  |  |  | 	mcsClientset "sigs.k8s.io/mcs-api/pkg/client/clientset/versioned/typed/apis/v1alpha1"
 | 
					
						
							| 
									
										
										
										
											2018-06-27 07:45:32 -07:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | func inc(ip net.IP) {
 | 
					
						
							|  |  |  | 	for j := len(ip) - 1; j >= 0; j-- {
 | 
					
						
							|  |  |  | 		ip[j]++
 | 
					
						
							|  |  |  | 		if ip[j] > 0 {
 | 
					
						
							|  |  |  | 			break
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | func kubernetesWithFakeClient(ctx context.Context, cidr string, initEndpointsCache bool, svcType string) *Kubernetes {
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	client := fake.NewSimpleClientset()
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	mcsClient := mcsClientsetFake.NewSimpleClientset()
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	dco := dnsControlOpts{
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 		zones:              []string{"cluster.local.", "clusterset.local."},
 | 
					
						
							|  |  |  | 		multiclusterZones:  []string{"clusterset.local."},
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 		initEndpointsCache: initEndpointsCache,
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	controller := newdnsController(ctx, client, mcsClient.MulticlusterV1alpha1(), dco)
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Add resources
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 	_, err := client.CoreV1().Namespaces().Create(ctx, &api.Namespace{ObjectMeta: meta.ObjectMeta{Name: "testns"}}, meta.CreateOptions{})
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		log.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	generateSvcs(cidr, svcType, client, mcsClient.MulticlusterV1alpha1())
 | 
					
						
							|  |  |  | 	generateEndpointSlices(cidr, svcType, client)
 | 
					
						
							|  |  |  | 	k := New([]string{"cluster.local.", "clusterset.local."})
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	k.APIConn = controller
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	k.opts.multiclusterZones = []string{"clusterset.local."}
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 	return k
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkController(b *testing.B) {
 | 
					
						
							|  |  |  | 	ctx := context.Background()
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	k := kubernetesWithFakeClient(ctx, "10.0.0.0/24", true, "all")
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	go k.APIConn.Run()
 | 
					
						
							|  |  |  | 	defer k.APIConn.Stop()
 | 
					
						
							|  |  |  | 	for !k.APIConn.HasSynced() {
 | 
					
						
							|  |  |  | 		time.Sleep(time.Millisecond)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	rw := &test.ResponseWriter{}
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetQuestion("svc1.testns.svc.cluster.local.", dns.TypeA)
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	b.ResetTimer()
 | 
					
						
							| 
									
										
										
										
											2025-05-29 03:50:55 +03:00
										 |  |  | 	for range b.N {
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 		k.ServeDNS(ctx, rw, m)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | func TestEndpointsDisabled(t *testing.T) {
 | 
					
						
							|  |  |  | 	ctx := context.Background()
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	k := kubernetesWithFakeClient(ctx, "10.0.0.0/30", false, "headless")
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 	k.opts.initEndpointsCache = false
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go k.APIConn.Run()
 | 
					
						
							|  |  |  | 	defer k.APIConn.Stop()
 | 
					
						
							|  |  |  | 	for !k.APIConn.HasSynced() {
 | 
					
						
							|  |  |  | 		time.Sleep(time.Millisecond)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rw := &dnstest.Recorder{ResponseWriter: &test.ResponseWriter{}}
 | 
					
						
							|  |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetQuestion("svc2.testns.svc.cluster.local.", dns.TypeA)
 | 
					
						
							|  |  |  | 	k.ServeDNS(ctx, rw, m)
 | 
					
						
							|  |  |  | 	if rw.Msg.Rcode != dns.RcodeNameError {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected NXDOMAIN, got %v", dns.RcodeToString[rw.Msg.Rcode])
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestEndpointsEnabled(t *testing.T) {
 | 
					
						
							|  |  |  | 	ctx := context.Background()
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	k := kubernetesWithFakeClient(ctx, "10.0.0.0/30", true, "headless")
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 	k.opts.initEndpointsCache = true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go k.APIConn.Run()
 | 
					
						
							|  |  |  | 	defer k.APIConn.Stop()
 | 
					
						
							|  |  |  | 	for !k.APIConn.HasSynced() {
 | 
					
						
							|  |  |  | 		time.Sleep(time.Millisecond)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rw := &dnstest.Recorder{ResponseWriter: &test.ResponseWriter{}}
 | 
					
						
							|  |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetQuestion("svc2.testns.svc.cluster.local.", dns.TypeA)
 | 
					
						
							|  |  |  | 	k.ServeDNS(ctx, rw, m)
 | 
					
						
							|  |  |  | 	if rw.Msg.Rcode != dns.RcodeSuccess {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected SUCCESS, got %v", dns.RcodeToString[rw.Msg.Rcode])
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | func TestMultiClusterHeadless(t *testing.T) {
 | 
					
						
							|  |  |  | 	ctx := context.Background()
 | 
					
						
							|  |  |  | 	k := kubernetesWithFakeClient(ctx, "10.0.0.0/30", true, "mcs-headless")
 | 
					
						
							|  |  |  | 	k.opts.initEndpointsCache = true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go k.APIConn.Run()
 | 
					
						
							|  |  |  | 	defer k.APIConn.Stop()
 | 
					
						
							|  |  |  | 	for !k.APIConn.HasSynced() {
 | 
					
						
							|  |  |  | 		time.Sleep(time.Millisecond)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rw := &dnstest.Recorder{ResponseWriter: &test.ResponseWriter{}}
 | 
					
						
							|  |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetQuestion("svc2.testns.svc.clusterset.local.", dns.TypeA)
 | 
					
						
							|  |  |  | 	k.ServeDNS(ctx, rw, m)
 | 
					
						
							|  |  |  | 	if rw.Msg.Rcode != dns.RcodeSuccess {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected SUCCESS, got %v", dns.RcodeToString[rw.Msg.Rcode])
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func generateEndpointSlices(cidr string, svcType string, client kubernetes.Interface) {
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	// https://groups.google.com/d/msg/golang-nuts/zlcYA4qk-94/TWRFHeXJCcYJ
 | 
					
						
							|  |  |  | 	ip, ipnet, err := net.ParseCIDR(cidr)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		log.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	count := 1
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 	port := int32(80)
 | 
					
						
							|  |  |  | 	protocol := api.Protocol("tcp")
 | 
					
						
							|  |  |  | 	name := "http"
 | 
					
						
							|  |  |  | 	eps := &discovery.EndpointSlice{
 | 
					
						
							|  |  |  | 		Ports: []discovery.EndpointPort{
 | 
					
						
							|  |  |  | 			{
 | 
					
						
							|  |  |  | 				Port:     &port,
 | 
					
						
							|  |  |  | 				Protocol: &protocol,
 | 
					
						
							|  |  |  | 				Name:     &name,
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 			},
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 		ObjectMeta: meta.ObjectMeta{
 | 
					
						
							|  |  |  | 			Namespace: "testns",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 	ctx := context.TODO()
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 		hostname := "foo" + strconv.Itoa(count)
 | 
					
						
							|  |  |  | 		eps.Endpoints = []discovery.Endpoint{
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 			{
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 				Addresses: []string{ip.String()},
 | 
					
						
							|  |  |  | 				Hostname:  &hostname,
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2025-04-04 20:27:39 +02:00
										 |  |  | 		eps.Name = "svc" + strconv.Itoa(count)
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 		if !strings.Contains(svcType, "mcs") {
 | 
					
						
							|  |  |  | 			eps.Labels = map[string]string{discovery.LabelServiceName: eps.Name}
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			eps.Labels = map[string]string{mcs.LabelServiceName: eps.Name}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 		_, err := client.DiscoveryV1().EndpointSlices("testns").Create(ctx, eps, meta.CreateOptions{})
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			log.Fatal(err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 		count++
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | func generateSvcs(cidr string, svcType string, client kubernetes.Interface, mcsClient mcsClientset.MulticlusterV1alpha1Interface) {
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	ip, ipnet, err := net.ParseCIDR(cidr)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		log.Fatal(err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	count := 1
 | 
					
						
							|  |  |  | 	switch svcType {
 | 
					
						
							|  |  |  | 	case "clusterip":
 | 
					
						
							|  |  |  | 		for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
 | 
					
						
							|  |  |  | 			createClusterIPSvc(count, client, ip)
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			count++
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	case "headless":
 | 
					
						
							|  |  |  | 		for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
 | 
					
						
							|  |  |  | 			createHeadlessSvc(count, client, ip)
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			count++
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	case "external":
 | 
					
						
							|  |  |  | 		for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
 | 
					
						
							|  |  |  | 			createExternalSvc(count, client, ip)
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			count++
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	case "mcs-headless":
 | 
					
						
							|  |  |  | 		for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
 | 
					
						
							|  |  |  | 			createMultiClusterHeadlessSvc(count, mcsClient, ip)
 | 
					
						
							|  |  |  | 			count++
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 	default:
 | 
					
						
							|  |  |  | 		for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
 | 
					
						
							| 
									
										
										
										
											2025-04-04 20:27:39 +02:00
										 |  |  | 			switch count % 3 {
 | 
					
						
							|  |  |  | 			case 0:
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 				createClusterIPSvc(count, client, ip)
 | 
					
						
							| 
									
										
										
										
											2025-04-04 20:27:39 +02:00
										 |  |  | 			case 1:
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 				createHeadlessSvc(count, client, ip)
 | 
					
						
							| 
									
										
										
										
											2025-04-04 20:27:39 +02:00
										 |  |  | 			case 2:
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 				createExternalSvc(count, client, ip)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			count++
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func createClusterIPSvc(suffix int, client kubernetes.Interface, ip net.IP) {
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 	client.CoreV1().Services("testns").Create(ctx, &api.Service{
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 		ObjectMeta: meta.ObjectMeta{
 | 
					
						
							|  |  |  | 			Name:      "svc" + strconv.Itoa(suffix),
 | 
					
						
							|  |  |  | 			Namespace: "testns",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		Spec: api.ServiceSpec{
 | 
					
						
							|  |  |  | 			ClusterIP: ip.String(),
 | 
					
						
							|  |  |  | 			Ports: []api.ServicePort{{
 | 
					
						
							|  |  |  | 				Name:     "http",
 | 
					
						
							|  |  |  | 				Protocol: "tcp",
 | 
					
						
							|  |  |  | 				Port:     80,
 | 
					
						
							|  |  |  | 			}},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 	}, meta.CreateOptions{})
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func createHeadlessSvc(suffix int, client kubernetes.Interface, ip net.IP) {
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 	client.CoreV1().Services("testns").Create(ctx, &api.Service{
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 		ObjectMeta: meta.ObjectMeta{
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 			Name:      "svc" + strconv.Itoa(suffix),
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 			Namespace: "testns",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		Spec: api.ServiceSpec{
 | 
					
						
							|  |  |  | 			ClusterIP: api.ClusterIPNone,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 	}, meta.CreateOptions{})
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func createExternalSvc(suffix int, client kubernetes.Interface, ip net.IP) {
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 	client.CoreV1().Services("testns").Create(ctx, &api.Service{
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 		ObjectMeta: meta.ObjectMeta{
 | 
					
						
							| 
									
										
										
										
											2023-06-07 16:22:28 -04:00
										 |  |  | 			Name:      "svc" + strconv.Itoa(suffix),
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | 			Namespace: "testns",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		Spec: api.ServiceSpec{
 | 
					
						
							|  |  |  | 			ExternalName: "coredns" + strconv.Itoa(suffix) + ".io",
 | 
					
						
							|  |  |  | 			Ports: []api.ServicePort{{
 | 
					
						
							|  |  |  | 				Name:     "http",
 | 
					
						
							|  |  |  | 				Protocol: "tcp",
 | 
					
						
							|  |  |  | 				Port:     80,
 | 
					
						
							|  |  |  | 			}},
 | 
					
						
							|  |  |  | 			Type: api.ServiceTypeExternalName,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 	}, meta.CreateOptions{})
 | 
					
						
							| 
									
										
										
										
											2018-09-29 10:43:09 -05:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | func createMultiClusterHeadlessSvc(suffix int, mcsClient mcsClientset.MulticlusterV1alpha1Interface, ip net.IP) {
 | 
					
						
							|  |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 	mcsClient.ServiceImports("testns").Create(ctx, &mcs.ServiceImport{
 | 
					
						
							|  |  |  | 		ObjectMeta: meta.ObjectMeta{
 | 
					
						
							|  |  |  | 			Name:      "svc" + strconv.Itoa(suffix),
 | 
					
						
							|  |  |  | 			Namespace: "testns",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		Spec: mcs.ServiceImportSpec{
 | 
					
						
							|  |  |  | 			Ports: []mcs.ServicePort{{
 | 
					
						
							|  |  |  | 				Name:     "http",
 | 
					
						
							|  |  |  | 				Protocol: "tcp",
 | 
					
						
							|  |  |  | 				Port:     80,
 | 
					
						
							|  |  |  | 			}},
 | 
					
						
							|  |  |  | 			Type: mcs.Headless,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	}, meta.CreateOptions{})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | func TestServiceModified(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2025-05-19 07:58:16 +02:00
										 |  |  | 	tests := []struct {
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:16:24 -05:00
										 |  |  | 		oldSvc   interface{}
 | 
					
						
							|  |  |  | 		newSvc   interface{}
 | 
					
						
							|  |  |  | 		ichanged bool
 | 
					
						
							|  |  |  | 		echanged bool
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			oldSvc:   nil,
 | 
					
						
							|  |  |  | 			newSvc:   &object.Service{},
 | 
					
						
							|  |  |  | 			ichanged: true,
 | 
					
						
							|  |  |  | 			echanged: false,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			oldSvc:   &object.Service{},
 | 
					
						
							|  |  |  | 			newSvc:   nil,
 | 
					
						
							|  |  |  | 			ichanged: true,
 | 
					
						
							|  |  |  | 			echanged: false,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			oldSvc:   nil,
 | 
					
						
							|  |  |  | 			newSvc:   &object.Service{ExternalIPs: []string{"10.0.0.1"}},
 | 
					
						
							|  |  |  | 			ichanged: true,
 | 
					
						
							|  |  |  | 			echanged: true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			oldSvc:   &object.Service{ExternalIPs: []string{"10.0.0.1"}},
 | 
					
						
							|  |  |  | 			newSvc:   nil,
 | 
					
						
							|  |  |  | 			ichanged: true,
 | 
					
						
							|  |  |  | 			echanged: true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			oldSvc:   &object.Service{ExternalIPs: []string{"10.0.0.1"}},
 | 
					
						
							|  |  |  | 			newSvc:   &object.Service{ExternalIPs: []string{"10.0.0.2"}},
 | 
					
						
							|  |  |  | 			ichanged: false,
 | 
					
						
							|  |  |  | 			echanged: true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			oldSvc:   &object.Service{ExternalName: "10.0.0.1"},
 | 
					
						
							|  |  |  | 			newSvc:   &object.Service{ExternalName: "10.0.0.2"},
 | 
					
						
							|  |  |  | 			ichanged: true,
 | 
					
						
							|  |  |  | 			echanged: false,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			oldSvc:   &object.Service{Ports: []api.ServicePort{{Name: "test1"}}},
 | 
					
						
							|  |  |  | 			newSvc:   &object.Service{Ports: []api.ServicePort{{Name: "test2"}}},
 | 
					
						
							|  |  |  | 			ichanged: true,
 | 
					
						
							|  |  |  | 			echanged: true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			oldSvc:   &object.Service{Ports: []api.ServicePort{{Name: "test1"}}},
 | 
					
						
							|  |  |  | 			newSvc:   &object.Service{Ports: []api.ServicePort{{Name: "test2"}, {Name: "test3"}}},
 | 
					
						
							|  |  |  | 			ichanged: true,
 | 
					
						
							|  |  |  | 			echanged: true,
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range tests {
 | 
					
						
							|  |  |  | 		ichanged, echanged := serviceModified(test.oldSvc, test.newSvc)
 | 
					
						
							|  |  |  | 		if test.ichanged != ichanged || test.echanged != echanged {
 | 
					
						
							|  |  |  | 			t.Errorf("Expected %v, %v for test %v. Got %v, %v", test.ichanged, test.echanged, i, ichanged, echanged)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |