| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | package kubernetes
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 23:13:08 +01:00
										 |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2020-03-30 11:10:41 -07:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2021-07-29 21:27:25 -06:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2019-08-23 12:54:06 -04:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2017-08-10 23:13:08 +01:00
										 |  |  | 	"testing"
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/kubernetes/object"
 | 
					
						
							| 
									
										
										
										
											2018-06-27 07:45:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:19:26 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2018-01-03 19:11:28 +08:00
										 |  |  | 	api "k8s.io/api/core/v1"
 | 
					
						
							| 
									
										
										
										
											2017-08-10 23:13:08 +01:00
										 |  |  | )
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | type APIConnTest struct{}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-06 13:55:15 -04:00
										 |  |  | func (APIConnTest) HasSynced() bool                             { return true }
 | 
					
						
							|  |  |  | func (APIConnTest) Run()                                        {}
 | 
					
						
							|  |  |  | func (APIConnTest) Stop() error                                 { return nil }
 | 
					
						
							|  |  |  | func (APIConnTest) PodIndex(string) []*object.Pod               { return nil }
 | 
					
						
							|  |  |  | func (APIConnTest) SvcIndexReverse(string) []*object.Service    { return nil }
 | 
					
						
							|  |  |  | func (APIConnTest) SvcExtIndexReverse(string) []*object.Service { return nil }
 | 
					
						
							|  |  |  | func (APIConnTest) EpIndex(string) []*object.Endpoints          { return nil }
 | 
					
						
							|  |  |  | func (APIConnTest) EndpointsList() []*object.Endpoints          { return nil }
 | 
					
						
							|  |  |  | func (APIConnTest) Modified(bool) int64                         { return 0 }
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 12:54:06 -04:00
										 |  |  | func (a APIConnTest) SvcIndex(s string) []*object.Service {
 | 
					
						
							|  |  |  | 	switch s {
 | 
					
						
							|  |  |  | 	case "dns-service.kube-system":
 | 
					
						
							|  |  |  | 		return []*object.Service{a.ServiceList()[0]}
 | 
					
						
							|  |  |  | 	case "hdls-dns-service.kube-system":
 | 
					
						
							|  |  |  | 		return []*object.Service{a.ServiceList()[1]}
 | 
					
						
							|  |  |  | 	case "dns6-service.kube-system":
 | 
					
						
							|  |  |  | 		return []*object.Service{a.ServiceList()[2]}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:18:23 -05:00
										 |  |  | var svcs = []*object.Service{
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Name:       "dns-service",
 | 
					
						
							|  |  |  | 		Namespace:  "kube-system",
 | 
					
						
							|  |  |  | 		ClusterIPs: []string{"10.0.0.111"},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Name:       "hdls-dns-service",
 | 
					
						
							|  |  |  | 		Namespace:  "kube-system",
 | 
					
						
							|  |  |  | 		ClusterIPs: []string{api.ClusterIPNone},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Name:       "dns6-service",
 | 
					
						
							|  |  |  | 		Namespace:  "kube-system",
 | 
					
						
							|  |  |  | 		ClusterIPs: []string{"10::111"},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 21:56:09 +01:00
										 |  |  | func (APIConnTest) ServiceList() []*object.Service {
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	return svcs
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 12:54:06 -04:00
										 |  |  | func (APIConnTest) EpIndexReverse(ip string) []*object.Endpoints {
 | 
					
						
							|  |  |  | 	if ip != "10.244.0.20" {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	eps := []*object.Endpoints{
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 			Name:      "dns-service-slice1",
 | 
					
						
							|  |  |  | 			Namespace: "kube-system",
 | 
					
						
							|  |  |  | 			Index:     object.EndpointsKey("dns-service", "kube-system"),
 | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 			Subsets: []object.EndpointSubset{
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 				{Addresses: []object.EndpointAddress{{IP: "10.244.0.20"}}},
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 			},
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2019-08-23 12:54:06 -04:00
										 |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 			Name:      "hdls-dns-service-slice1",
 | 
					
						
							|  |  |  | 			Namespace: "kube-system",
 | 
					
						
							|  |  |  | 			Index:     object.EndpointsKey("hdls-dns-service", "kube-system"),
 | 
					
						
							| 
									
										
										
										
											2019-08-23 12:54:06 -04:00
										 |  |  | 			Subsets: []object.EndpointSubset{
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 				{Addresses: []object.EndpointAddress{{IP: "10.244.0.20"}}},
 | 
					
						
							| 
									
										
										
										
											2019-08-23 12:54:06 -04:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 			Name:      "dns6-service-slice1",
 | 
					
						
							|  |  |  | 			Namespace: "kube-system",
 | 
					
						
							|  |  |  | 			Index:     object.EndpointsKey("dns6-service", "kube-system"),
 | 
					
						
							| 
									
										
										
										
											2019-08-23 12:54:06 -04:00
										 |  |  | 			Subsets: []object.EndpointSubset{
 | 
					
						
							| 
									
										
										
										
											2020-10-30 08:14:30 -04:00
										 |  |  | 				{Addresses: []object.EndpointAddress{{IP: "10.244.0.20"}}},
 | 
					
						
							| 
									
										
										
										
											2019-08-23 12:54:06 -04:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-10-11 21:59:50 +01:00
										 |  |  | 	return eps
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 22:48:39 +08:00
										 |  |  | func (APIConnTest) GetNodeByName(ctx context.Context, name string) (*api.Node, error) {
 | 
					
						
							|  |  |  | 	return &api.Node{}, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2021-07-29 21:27:25 -06:00
										 |  |  | func (APIConnTest) GetNamespaceByName(name string) (*object.Namespace, error) {
 | 
					
						
							|  |  |  | 	return nil, fmt.Errorf("namespace not found")
 | 
					
						
							| 
									
										
										
										
											2017-11-13 21:51:51 +00:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 12:54:06 -04:00
										 |  |  | func TestNsAddrs(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 	k := New([]string{"inter.webs.test."})
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	k.APIConn = &APIConnTest{}
 | 
					
						
							| 
									
										
										
										
											2019-09-05 09:07:55 -04:00
										 |  |  | 	k.localIPs = []net.IP{net.ParseIP("10.244.0.20")}
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-30 20:59:27 +02:00
										 |  |  | 	cdrs := k.nsAddrs(false, false, k.Zones[0])
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 12:54:06 -04:00
										 |  |  | 	if len(cdrs) != 3 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected 3 results, got %v", len(cdrs))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	cdr := cdrs[0]
 | 
					
						
							|  |  |  | 	expected := "10.0.0.111"
 | 
					
						
							|  |  |  | 	if cdr.(*dns.A).A.String() != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected 1st A to be %q, got %q", expected, cdr.(*dns.A).A.String())
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	expected = "dns-service.kube-system.svc.inter.webs.test."
 | 
					
						
							|  |  |  | 	if cdr.Header().Name != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected 1st Header Name to be %q, got %q", expected, cdr.Header().Name)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	cdr = cdrs[1]
 | 
					
						
							|  |  |  | 	expected = "10.244.0.20"
 | 
					
						
							|  |  |  | 	if cdr.(*dns.A).A.String() != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected 2nd A to be %q, got %q", expected, cdr.(*dns.A).A.String())
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	expected = "10-244-0-20.hdls-dns-service.kube-system.svc.inter.webs.test."
 | 
					
						
							|  |  |  | 	if cdr.Header().Name != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected 2nd Header Name to be %q, got %q", expected, cdr.Header().Name)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	cdr = cdrs[2]
 | 
					
						
							|  |  |  | 	expected = "10::111"
 | 
					
						
							|  |  |  | 	if cdr.(*dns.AAAA).AAAA.String() != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected AAAA to be %q, got %q", expected, cdr.(*dns.A).A.String())
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2019-08-23 12:54:06 -04:00
										 |  |  | 	expected = "dns6-service.kube-system.svc.inter.webs.test."
 | 
					
						
							|  |  |  | 	if cdr.Header().Name != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected AAAA Header Name to be %q, got %q", expected, cdr.Header().Name)
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:18:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-30 20:59:27 +02:00
										 |  |  | func TestNsAddrsExternalHeadless(t *testing.T) {
 | 
					
						
							|  |  |  | 	k := New([]string{"example.com."})
 | 
					
						
							|  |  |  | 	k.APIConn = &APIConnTest{}
 | 
					
						
							|  |  |  | 	k.localIPs = []net.IP{net.ParseIP("10.244.0.20")}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 23:06:48 +08:00
										 |  |  | 	// there are only headless services
 | 
					
						
							| 
									
										
										
										
											2022-08-30 20:59:27 +02:00
										 |  |  | 	cdrs := k.nsAddrs(true, true, k.Zones[0])
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(cdrs) != 1 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected 0 results, got %v", cdrs)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cdr := cdrs[0]
 | 
					
						
							|  |  |  | 	expected := "10.244.0.20"
 | 
					
						
							|  |  |  | 	if cdr.(*dns.A).A.String() != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected A address to be %q, got %q", expected, cdr.(*dns.A).A.String())
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	expected = "10-244-0-20.hdls-dns-service.kube-system.example.com."
 | 
					
						
							|  |  |  | 	if cdr.Header().Name != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected record name to be %q, got %q", expected, cdr.Header().Name)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:18:23 -05:00
										 |  |  | func TestNsAddrsExternal(t *testing.T) {
 | 
					
						
							|  |  |  | 	k := New([]string{"example.com."})
 | 
					
						
							|  |  |  | 	k.APIConn = &APIConnTest{}
 | 
					
						
							|  |  |  | 	k.localIPs = []net.IP{net.ParseIP("10.244.0.20")}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// initially no services have an external IP ...
 | 
					
						
							| 
									
										
										
										
											2022-08-30 20:59:27 +02:00
										 |  |  | 	cdrs := k.nsAddrs(true, false, k.Zones[0])
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:18:23 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if len(cdrs) != 0 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected 0 results, got %v", len(cdrs))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Add an external IP to one of the services ...
 | 
					
						
							|  |  |  | 	svcs[0].ExternalIPs = []string{"1.2.3.4"}
 | 
					
						
							| 
									
										
										
										
											2022-08-30 20:59:27 +02:00
										 |  |  | 	cdrs = k.nsAddrs(true, false, k.Zones[0])
 | 
					
						
							| 
									
										
										
										
											2022-03-07 12:18:23 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if len(cdrs) != 1 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected 1 results, got %v", len(cdrs))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	cdr := cdrs[0]
 | 
					
						
							|  |  |  | 	expected := "1.2.3.4"
 | 
					
						
							|  |  |  | 	if cdr.(*dns.A).A.String() != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected A address to be %q, got %q", expected, cdr.(*dns.A).A.String())
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	expected = "dns-service.kube-system.example.com."
 | 
					
						
							|  |  |  | 	if cdr.Header().Name != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected record name to be %q, got %q", expected, cdr.Header().Name)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2022-08-30 20:59:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestNsAddrsExternalWithPreexistingExternalIP(t *testing.T) {
 | 
					
						
							|  |  |  | 	k := New([]string{"example.com."})
 | 
					
						
							|  |  |  | 	k.APIConn = &APIConnTest{}
 | 
					
						
							|  |  |  | 	k.localIPs = []net.IP{net.ParseIP("10.244.0.20")}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	svcs[0].ExternalIPs = []string{"1.2.3.4"}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// initially no services have an external IP ...
 | 
					
						
							|  |  |  | 	cdrs := k.nsAddrs(true, false, k.Zones[0])
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(cdrs) != 1 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected 1 results, got %v", len(cdrs))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(cdrs) != 1 {
 | 
					
						
							|  |  |  | 		t.Fatalf("Expected 1 results, got %v", len(cdrs))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	cdr := cdrs[0]
 | 
					
						
							|  |  |  | 	expected := "1.2.3.4"
 | 
					
						
							|  |  |  | 	if cdr.(*dns.A).A.String() != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected A address to be %q, got %q", expected, cdr.(*dns.A).A.String())
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	expected = "dns-service.kube-system.example.com."
 | 
					
						
							|  |  |  | 	if cdr.Header().Name != expected {
 | 
					
						
							|  |  |  | 		t.Errorf("Expected record name to be %q, got %q", expected, cdr.Header().Name)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2022-09-05 10:29:36 +00:00
										 |  |  | }
 |