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>
This commit is contained in:
TomasKohout 2022-08-30 20:59:27 +02:00 committed by GitHub
parent b218b56063
commit 6782b7fb42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 491 additions and 81 deletions

View file

@ -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 {