diff options
author | 2022-08-30 20:59:27 +0200 | |
---|---|---|
committer | 2022-08-30 14:59:27 -0400 | |
commit | 6782b7fb42efc979ab2649228a83c0ac20d50d5e (patch) | |
tree | 465f49c700aebcfb825227a3a45dfd38daace374 /plugin/kubernetes/external_test.go | |
parent | b218b56063a965dceeda2835b020f8672ba7e27a (diff) | |
download | coredns-6782b7fb42efc979ab2649228a83c0ac20d50d5e.tar.gz coredns-6782b7fb42efc979ab2649228a83c0ac20d50d5e.tar.zst coredns-6782b7fb42efc979ab2649228a83c0ac20d50d5e.zip |
plugin/k8s_external: Resolve headless services (#5505)
*add option for resolving headless Services without external IPs in k8s_external
Signed-off-by: Tomas Kohout <tomas.kohout1995@gmail.com>
Diffstat (limited to 'plugin/kubernetes/external_test.go')
-rw-r--r-- | plugin/kubernetes/external_test.go | 88 |
1 files changed, 78 insertions, 10 deletions
diff --git a/plugin/kubernetes/external_test.go b/plugin/kubernetes/external_test.go index b0e89e000..670d2b9b2 100644 --- a/plugin/kubernetes/external_test.go +++ b/plugin/kubernetes/external_test.go @@ -43,6 +43,22 @@ var extCases = []struct { { Qname: "svc0.svc-nons.example.com.", Rcode: dns.RcodeNameError, }, + { + Qname: "svc-headless.testns.example.com.", Rcode: dns.RcodeSuccess, + Msg: []msg.Service{ + {Host: "1.2.3.4", Port: 80, TTL: 5, Weight: 50, Key: "/c/org/example/testns/svc-headless"}, + {Host: "1.2.3.5", Port: 80, TTL: 5, Weight: 50, Key: "/c/org/example/testns/svc-headless"}, + }, + }, + { + Qname: "endpoint-0.svc-headless.testns.example.com.", Rcode: dns.RcodeSuccess, + Msg: []msg.Service{ + {Host: "1.2.3.4", Port: 80, TTL: 5, Weight: 100, Key: "/c/org/example/testns/svc-headless/endpoint-0"}, + }, + }, + { + Qname: "endpoint-1.svc-nons.testns.example.com.", Rcode: dns.RcodeNameError, + }, } func TestExternal(t *testing.T) { @@ -54,7 +70,7 @@ func TestExternal(t *testing.T) { for i, tc := range extCases { state := testRequest(tc.Qname) - svc, rcode := k.External(state) + svc, rcode := k.External(state, true) if x := tc.Rcode; x != rcode { t.Errorf("Test %d, expected rcode %d, got %d", i, x, rcode) @@ -75,15 +91,23 @@ func TestExternal(t *testing.T) { type external struct{} -func (external) HasSynced() bool { return true } -func (external) Run() {} -func (external) Stop() error { return nil } -func (external) EpIndexReverse(string) []*object.Endpoints { return nil } -func (external) SvcIndexReverse(string) []*object.Service { return nil } -func (external) SvcExtIndexReverse(string) []*object.Service { return nil } -func (external) Modified(bool) int64 { return 0 } -func (external) EpIndex(s string) []*object.Endpoints { return nil } -func (external) EndpointsList() []*object.Endpoints { return nil } +func (external) HasSynced() bool { return true } +func (external) Run() {} +func (external) Stop() error { return nil } +func (external) EpIndexReverse(string) []*object.Endpoints { return nil } +func (external) SvcIndexReverse(string) []*object.Service { return nil } +func (external) SvcExtIndexReverse(string) []*object.Service { return nil } +func (external) Modified(bool) int64 { return 0 } +func (external) EpIndex(s string) []*object.Endpoints { + return epIndexExternal[s] +} +func (external) EndpointsList() []*object.Endpoints { + var eps []*object.Endpoints + for _, ep := range epIndexExternal { + eps = append(eps, ep...) + } + return eps +} func (external) GetNodeByName(ctx context.Context, name string) (*api.Node, error) { return nil, nil } func (external) SvcIndex(s string) []*object.Service { return svcIndexExternal[s] } func (external) PodIndex(string) []*object.Pod { return nil } @@ -94,6 +118,41 @@ func (external) GetNamespaceByName(name string) (*object.Namespace, error) { }, nil } +var epIndexExternal = map[string][]*object.Endpoints{ + "svc-headless.testns": { + { + Name: "svc-headless", + Namespace: "testns", + Index: "svc-headless.testns", + Subsets: []object.EndpointSubset{ + { + Ports: []object.EndpointPort{ + { + Port: 80, + Name: "http", + Protocol: "TCP", + }, + }, + Addresses: []object.EndpointAddress{ + { + IP: "1.2.3.4", + Hostname: "endpoint-svc-0", + NodeName: "test-node", + TargetRefName: "endpoint-svc-0", + }, + { + IP: "1.2.3.5", + Hostname: "endpoint-svc-1", + NodeName: "test-node", + TargetRefName: "endpoint-svc-1", + }, + }, + }, + }, + }, + }, +} + var svcIndexExternal = map[string][]*object.Service{ "svc1.testns": { { @@ -115,6 +174,15 @@ var svcIndexExternal = map[string][]*object.Service{ Ports: []api.ServicePort{{Name: "http", Protocol: "tcp", Port: 80}}, }, }, + "svc-headless.testns": { + { + Name: "svc-headless", + Namespace: "testns", + Type: api.ServiceTypeClusterIP, + ClusterIPs: []string{api.ClusterIPNone}, + Ports: []api.ServicePort{{Name: "http", Protocol: "tcp", Port: 80}}, + }, + }, } func (external) ServiceList() []*object.Service { |