use keys (#2167)
This commit is contained in:
parent
8432f14207
commit
974ed086f2
13 changed files with 291 additions and 380 deletions
|
@ -16,22 +16,24 @@ type APIConnFederationTest struct {
|
||||||
func (APIConnFederationTest) HasSynced() bool { return true }
|
func (APIConnFederationTest) HasSynced() bool { return true }
|
||||||
func (APIConnFederationTest) Run() { return }
|
func (APIConnFederationTest) Run() { return }
|
||||||
func (APIConnFederationTest) Stop() error { return nil }
|
func (APIConnFederationTest) Stop() error { return nil }
|
||||||
func (APIConnFederationTest) SvcIndexReverse(string) []*object.Service { return nil }
|
func (APIConnFederationTest) SvcIndexReverse(string) *object.Service { return nil }
|
||||||
func (APIConnFederationTest) EpIndexReverse(string) []*object.Endpoints { return nil }
|
func (APIConnFederationTest) EpIndexReverse(string) *object.Endpoints { return nil }
|
||||||
func (APIConnFederationTest) Modified() int64 { return 0 }
|
func (APIConnFederationTest) Modified() int64 { return 0 }
|
||||||
func (APIConnFederationTest) SetWatchChan(watch.Chan) {}
|
func (APIConnFederationTest) SetWatchChan(watch.Chan) {}
|
||||||
func (APIConnFederationTest) Watch(string) error { return nil }
|
func (APIConnFederationTest) Watch(string) error { return nil }
|
||||||
func (APIConnFederationTest) StopWatching(string) {}
|
func (APIConnFederationTest) StopWatching(string) {}
|
||||||
|
|
||||||
|
|
||||||
func (APIConnFederationTest) PodIndex(string) []*object.Pod {
|
func (APIConnFederationTest) PodIndex(string) []*object.Pod {
|
||||||
return []*object.Pod{
|
return []*object.Pod{
|
||||||
{Namespace: "podns", PodIP: "10.240.0.1"}, // Remote IP set in test.ResponseWriter
|
{Namespace: "podns", PodIP: "10.240.0.1"}, // Remote IP set in test.ResponseWriter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnFederationTest) SvcIndex(string) []*object.Service {
|
|
||||||
svcs := []*object.Service{
|
func (APIConnFederationTest) SvcIndex(key string) *object.Service {
|
||||||
{
|
svcs := map[string]*object.Service{
|
||||||
|
"testns/svc1": {
|
||||||
Name: "svc1",
|
Name: "svc1",
|
||||||
Namespace: "testns",
|
Namespace: "testns",
|
||||||
ClusterIP: "10.0.0.1",
|
ClusterIP: "10.0.0.1",
|
||||||
|
@ -39,12 +41,12 @@ func (APIConnFederationTest) SvcIndex(string) []*object.Service {
|
||||||
{Name: "http", Protocol: "tcp", Port: 80},
|
{Name: "http", Protocol: "tcp", Port: 80},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
"testns/hdls1": {
|
||||||
Name: "hdls1",
|
Name: "hdls1",
|
||||||
Namespace: "testns",
|
Namespace: "testns",
|
||||||
ClusterIP: api.ClusterIPNone,
|
ClusterIP: api.ClusterIPNone,
|
||||||
},
|
},
|
||||||
{
|
"testns/external": {
|
||||||
Name: "external",
|
Name: "external",
|
||||||
Namespace: "testns",
|
Namespace: "testns",
|
||||||
ExternalName: "ext.interwebs.test",
|
ExternalName: "ext.interwebs.test",
|
||||||
|
@ -53,9 +55,10 @@ func (APIConnFederationTest) SvcIndex(string) []*object.Service {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return svcs
|
return svcs[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (APIConnFederationTest) ServiceList() []*object.Service {
|
func (APIConnFederationTest) ServiceList() []*object.Service {
|
||||||
svcs := []*object.Service{
|
svcs := []*object.Service{
|
||||||
{
|
{
|
||||||
|
@ -83,9 +86,9 @@ func (APIConnFederationTest) ServiceList() []*object.Service {
|
||||||
return svcs
|
return svcs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnFederationTest) EpIndex(string) []*object.Endpoints {
|
func (APIConnFederationTest) EpIndex(key string) *object.Endpoints {
|
||||||
eps := []*object.Endpoints{
|
eps := map[string]*object.Endpoints{
|
||||||
{
|
"testns/svc1": {
|
||||||
Subsets: []object.EndpointSubset{
|
Subsets: []object.EndpointSubset{
|
||||||
{
|
{
|
||||||
Addresses: []object.EndpointAddress{
|
Addresses: []object.EndpointAddress{
|
||||||
|
@ -100,7 +103,7 @@ func (APIConnFederationTest) EpIndex(string) []*object.Endpoints {
|
||||||
Namespace: "testns",
|
Namespace: "testns",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return eps
|
return eps[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnFederationTest) EndpointsList() []*object.Endpoints {
|
func (APIConnFederationTest) EndpointsList() []*object.Endpoints {
|
||||||
|
|
|
@ -20,21 +20,19 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
podIPIndex = "PodIP"
|
podIPIndex = "PodIP"
|
||||||
svcNameNamespaceIndex = "NameNamespace"
|
svcIPIndex = "ServiceIP"
|
||||||
svcIPIndex = "ServiceIP"
|
epIPIndex = "EndpointsIP"
|
||||||
epNameNamespaceIndex = "EndpointNameNamespace"
|
|
||||||
epIPIndex = "EndpointsIP"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type dnsController interface {
|
type dnsController interface {
|
||||||
ServiceList() []*object.Service
|
ServiceList() []*object.Service
|
||||||
EndpointsList() []*object.Endpoints
|
EndpointsList() []*object.Endpoints
|
||||||
SvcIndex(string) []*object.Service
|
SvcIndex(string) *object.Service
|
||||||
SvcIndexReverse(string) []*object.Service
|
SvcIndexReverse(string) *object.Service
|
||||||
PodIndex(string) []*object.Pod
|
PodIndex(string) []*object.Pod
|
||||||
EpIndex(string) []*object.Endpoints
|
EpIndex(string) *object.Endpoints
|
||||||
EpIndexReverse(string) []*object.Endpoints
|
EpIndexReverse(string) *object.Endpoints
|
||||||
|
|
||||||
GetNodeByName(string) (*api.Node, error)
|
GetNodeByName(string) (*api.Node, error)
|
||||||
GetNamespaceByName(string) (*api.Namespace, error)
|
GetNamespaceByName(string) (*api.Namespace, error)
|
||||||
|
@ -118,7 +116,7 @@ func newdnsController(kubeClient kubernetes.Interface, opts dnsControlOpts) *dns
|
||||||
&object.Service{},
|
&object.Service{},
|
||||||
opts.resyncPeriod,
|
opts.resyncPeriod,
|
||||||
cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete},
|
cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete},
|
||||||
cache.Indexers{svcNameNamespaceIndex: svcNameNamespaceIndexFunc, svcIPIndex: svcIPIndexFunc},
|
cache.Indexers{svcIPIndex: svcIPIndexFunc},
|
||||||
object.ToService,
|
object.ToService,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -145,7 +143,7 @@ func newdnsController(kubeClient kubernetes.Interface, opts dnsControlOpts) *dns
|
||||||
&api.Endpoints{},
|
&api.Endpoints{},
|
||||||
opts.resyncPeriod,
|
opts.resyncPeriod,
|
||||||
cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete},
|
cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete},
|
||||||
cache.Indexers{epNameNamespaceIndex: epNameNamespaceIndexFunc, epIPIndex: epIPIndexFunc},
|
cache.Indexers{epIPIndex: epIPIndexFunc},
|
||||||
object.ToEndpoints)
|
object.ToEndpoints)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,22 +173,6 @@ func svcIPIndexFunc(obj interface{}) ([]string, error) {
|
||||||
return []string{svc.ClusterIP}, nil
|
return []string{svc.ClusterIP}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func svcNameNamespaceIndexFunc(obj interface{}) ([]string, error) {
|
|
||||||
s, ok := obj.(*object.Service)
|
|
||||||
if !ok {
|
|
||||||
return nil, errObj
|
|
||||||
}
|
|
||||||
return []string{s.Index}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func epNameNamespaceIndexFunc(obj interface{}) ([]string, error) {
|
|
||||||
s, ok := obj.(*object.Endpoints)
|
|
||||||
if !ok {
|
|
||||||
return nil, errObj
|
|
||||||
}
|
|
||||||
return []string{s.Index}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func epIPIndexFunc(obj interface{}) ([]string, error) {
|
func epIPIndexFunc(obj interface{}) ([]string, error) {
|
||||||
ep, ok := obj.(*object.Endpoints)
|
ep, ok := obj.(*object.Endpoints)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -359,6 +341,9 @@ func (dns *dnsControl) EndpointsList() (eps []*object.Endpoints) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dns *dnsControl) PodIndex(ip string) (pods []*object.Pod) {
|
func (dns *dnsControl) PodIndex(ip string) (pods []*object.Pod) {
|
||||||
|
if dns.podLister == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
os, err := dns.podLister.ByIndex(podIPIndex, ip)
|
os, err := dns.podLister.ByIndex(podIPIndex, ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -368,27 +353,24 @@ func (dns *dnsControl) PodIndex(ip string) (pods []*object.Pod) {
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
pods = append(pods, p)
|
return []*object.Pod{p}
|
||||||
}
|
}
|
||||||
return pods
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dns *dnsControl) SvcIndex(idx string) (svcs []*object.Service) {
|
func (dns *dnsControl) SvcIndex(key string) *object.Service {
|
||||||
os, err := dns.svcLister.ByIndex(svcNameNamespaceIndex, idx)
|
o, _, err := dns.svcLister.GetByKey(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for _, o := range os {
|
s, ok := o.(*object.Service)
|
||||||
s, ok := o.(*object.Service)
|
if !ok {
|
||||||
if !ok {
|
return nil
|
||||||
continue
|
|
||||||
}
|
|
||||||
svcs = append(svcs, s)
|
|
||||||
}
|
}
|
||||||
return svcs
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dns *dnsControl) SvcIndexReverse(ip string) (svcs []*object.Service) {
|
func (dns *dnsControl) SvcIndexReverse(ip string) *object.Service {
|
||||||
os, err := dns.svcLister.ByIndex(svcIPIndex, ip)
|
os, err := dns.svcLister.ByIndex(svcIPIndex, ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -399,27 +381,27 @@ func (dns *dnsControl) SvcIndexReverse(ip string) (svcs []*object.Service) {
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
svcs = append(svcs, s)
|
return s
|
||||||
}
|
}
|
||||||
return svcs
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dns *dnsControl) EpIndex(idx string) (ep []*object.Endpoints) {
|
func (dns *dnsControl) EpIndex(key string) (ep *object.Endpoints) {
|
||||||
os, err := dns.epLister.ByIndex(epNameNamespaceIndex, idx)
|
o, _, err := dns.epLister.GetByKey(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for _, o := range os {
|
e, ok := o.(*object.Endpoints)
|
||||||
e, ok := o.(*object.Endpoints)
|
if !ok {
|
||||||
if !ok {
|
return nil
|
||||||
continue
|
|
||||||
}
|
|
||||||
ep = append(ep, e)
|
|
||||||
}
|
}
|
||||||
return ep
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dns *dnsControl) EpIndexReverse(ip string) (ep []*object.Endpoints) {
|
func (dns *dnsControl) EpIndexReverse(ip string) (ep *object.Endpoints) {
|
||||||
|
if dns.epLister == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
os, err := dns.epLister.ByIndex(epIPIndex, ip)
|
os, err := dns.epLister.ByIndex(epIPIndex, ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -429,9 +411,9 @@ func (dns *dnsControl) EpIndexReverse(ip string) (ep []*object.Endpoints) {
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ep = append(ep, e)
|
return e
|
||||||
}
|
}
|
||||||
return ep
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNodeByName return the node by name. If nothing is found an error is
|
// GetNodeByName return the node by name. If nothing is found an error is
|
||||||
|
@ -450,7 +432,7 @@ func (dns *dnsControl) GetNamespaceByName(name string) (*api.Namespace, error) {
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if name == ns.ObjectMeta.Name {
|
if name == ns.GetName() {
|
||||||
return ns, nil
|
return ns, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -379,15 +379,15 @@ func TestServeDNS(t *testing.T) {
|
||||||
|
|
||||||
type APIConnServeTest struct{}
|
type APIConnServeTest struct{}
|
||||||
|
|
||||||
func (APIConnServeTest) HasSynced() bool { return true }
|
func (APIConnServeTest) HasSynced() bool { return true }
|
||||||
func (APIConnServeTest) Run() { return }
|
func (APIConnServeTest) Run() { return }
|
||||||
func (APIConnServeTest) Stop() error { return nil }
|
func (APIConnServeTest) Stop() error { return nil }
|
||||||
func (APIConnServeTest) EpIndexReverse(string) []*object.Endpoints { return nil }
|
func (APIConnServeTest) EpIndexReverse(string) *object.Endpoints { return nil }
|
||||||
func (APIConnServeTest) SvcIndexReverse(string) []*object.Service { return nil }
|
func (APIConnServeTest) SvcIndexReverse(string) *object.Service { return nil }
|
||||||
func (APIConnServeTest) Modified() int64 { return time.Now().Unix() }
|
func (APIConnServeTest) Modified() int64 { return time.Now().Unix() }
|
||||||
func (APIConnServeTest) SetWatchChan(watch.Chan) {}
|
func (APIConnServeTest) SetWatchChan(watch.Chan) {}
|
||||||
func (APIConnServeTest) Watch(string) error { return nil }
|
func (APIConnServeTest) Watch(string) error { return nil }
|
||||||
func (APIConnServeTest) StopWatching(string) {}
|
func (APIConnServeTest) StopWatching(string) {}
|
||||||
|
|
||||||
func (APIConnServeTest) PodIndex(string) []*object.Pod {
|
func (APIConnServeTest) PodIndex(string) []*object.Pod {
|
||||||
a := []*object.Pod{
|
a := []*object.Pod{
|
||||||
|
@ -396,103 +396,90 @@ func (APIConnServeTest) PodIndex(string) []*object.Pod {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
var svcIndex = map[string][]*object.Service{
|
var svcIndex = map[string]*object.Service{
|
||||||
"svc1.testns": {
|
"testns/svc1": {
|
||||||
{
|
Name: "svc1",
|
||||||
Name: "svc1",
|
Namespace: "testns",
|
||||||
Namespace: "testns",
|
Type: api.ServiceTypeClusterIP,
|
||||||
Type: api.ServiceTypeClusterIP,
|
ClusterIP: "10.0.0.1",
|
||||||
ClusterIP: "10.0.0.1",
|
Ports: []api.ServicePort{
|
||||||
Ports: []api.ServicePort{
|
{Name: "http", Protocol: "tcp", Port: 80},
|
||||||
{Name: "http", Protocol: "tcp", Port: 80},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"svcempty.testns": {
|
"testns/svcempty": {
|
||||||
{
|
Name: "svcempty",
|
||||||
Name: "svcempty",
|
Namespace: "testns",
|
||||||
Namespace: "testns",
|
Type: api.ServiceTypeClusterIP,
|
||||||
Type: api.ServiceTypeClusterIP,
|
ClusterIP: "10.0.0.1",
|
||||||
ClusterIP: "10.0.0.1",
|
Ports: []api.ServicePort{
|
||||||
Ports: []api.ServicePort{
|
{Name: "http", Protocol: "tcp", Port: 80},
|
||||||
{Name: "http", Protocol: "tcp", Port: 80},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"svc6.testns": {
|
"testns/svc6": {
|
||||||
{
|
Name: "svc6",
|
||||||
Name: "svc6",
|
Namespace: "testns",
|
||||||
Namespace: "testns",
|
Type: api.ServiceTypeClusterIP,
|
||||||
Type: api.ServiceTypeClusterIP,
|
ClusterIP: "1234:abcd::1",
|
||||||
ClusterIP: "1234:abcd::1",
|
Ports: []api.ServicePort{
|
||||||
Ports: []api.ServicePort{
|
{Name: "http", Protocol: "tcp", Port: 80},
|
||||||
{Name: "http", Protocol: "tcp", Port: 80},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"hdls1.testns": {
|
"testns/hdls1": {
|
||||||
{
|
Name: "hdls1",
|
||||||
Name: "hdls1",
|
Namespace: "testns",
|
||||||
Namespace: "testns",
|
Type: api.ServiceTypeClusterIP,
|
||||||
Type: api.ServiceTypeClusterIP,
|
ClusterIP: api.ClusterIPNone,
|
||||||
ClusterIP: api.ClusterIPNone,
|
},
|
||||||
|
"testns/external": {
|
||||||
|
|
||||||
|
Name: "external",
|
||||||
|
Namespace: "testns",
|
||||||
|
ExternalName: "ext.interwebs.test",
|
||||||
|
Type: api.ServiceTypeExternalName,
|
||||||
|
Ports: []api.ServicePort{
|
||||||
|
{Name: "http", Protocol: "tcp", Port: 80},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"external.testns": {
|
"testns/external-to-service": {
|
||||||
{
|
Name: "external-to-service",
|
||||||
Name: "external",
|
Namespace: "testns",
|
||||||
Namespace: "testns",
|
ExternalName: "svc1.testns.svc.cluster.local.",
|
||||||
ExternalName: "ext.interwebs.test",
|
Type: api.ServiceTypeExternalName,
|
||||||
Type: api.ServiceTypeExternalName,
|
Ports: []api.ServicePort{
|
||||||
Ports: []api.ServicePort{
|
{Name: "http", Protocol: "tcp", Port: 80},
|
||||||
{Name: "http", Protocol: "tcp", Port: 80},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"external-to-service.testns": {
|
"testns/hdlsprtls": {
|
||||||
{
|
Name: "hdlsprtls",
|
||||||
Name: "external-to-service",
|
Namespace: "testns",
|
||||||
Namespace: "testns",
|
Type: api.ServiceTypeClusterIP,
|
||||||
ExternalName: "svc1.testns.svc.cluster.local.",
|
ClusterIP: api.ClusterIPNone,
|
||||||
Type: api.ServiceTypeExternalName,
|
|
||||||
Ports: []api.ServicePort{
|
|
||||||
{Name: "http", Protocol: "tcp", Port: 80},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
"hdlsprtls.testns": {
|
"unexposedns/svc1": {
|
||||||
{
|
Name: "svc1",
|
||||||
Name: "hdlsprtls",
|
Namespace: "unexposedns",
|
||||||
Namespace: "testns",
|
Type: api.ServiceTypeClusterIP,
|
||||||
Type: api.ServiceTypeClusterIP,
|
ClusterIP: "10.0.0.2",
|
||||||
ClusterIP: api.ClusterIPNone,
|
Ports: []api.ServicePort{
|
||||||
},
|
{Name: "http", Protocol: "tcp", Port: 80},
|
||||||
},
|
|
||||||
"svc1.unexposedns": {
|
|
||||||
{
|
|
||||||
Name: "svc1",
|
|
||||||
Namespace: "unexposedns",
|
|
||||||
Type: api.ServiceTypeClusterIP,
|
|
||||||
ClusterIP: "10.0.0.2",
|
|
||||||
Ports: []api.ServicePort{
|
|
||||||
{Name: "http", Protocol: "tcp", Port: 80},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnServeTest) SvcIndex(s string) []*object.Service { return svcIndex[s] }
|
func (APIConnServeTest) SvcIndex(s string) *object.Service {
|
||||||
|
return svcIndex[s]
|
||||||
|
}
|
||||||
|
|
||||||
func (APIConnServeTest) ServiceList() []*object.Service {
|
func (APIConnServeTest) ServiceList() []*object.Service {
|
||||||
var svcs []*object.Service
|
var svcs []*object.Service
|
||||||
for _, svc := range svcIndex {
|
for _, svc := range svcIndex {
|
||||||
svcs = append(svcs, svc...)
|
svcs = append(svcs, svc)
|
||||||
}
|
}
|
||||||
return svcs
|
return svcs
|
||||||
}
|
}
|
||||||
|
|
||||||
var epsIndex = map[string][]*object.Endpoints{
|
var epsIndex = map[string]*object.Endpoints{
|
||||||
"svc1.testns": {{
|
"testns/svc1": {
|
||||||
Subsets: []object.EndpointSubset{
|
Subsets: []object.EndpointSubset{
|
||||||
{
|
{
|
||||||
Addresses: []object.EndpointAddress{
|
Addresses: []object.EndpointAddress{
|
||||||
|
@ -505,8 +492,8 @@ var epsIndex = map[string][]*object.Endpoints{
|
||||||
},
|
},
|
||||||
Name: "svc1",
|
Name: "svc1",
|
||||||
Namespace: "testns",
|
Namespace: "testns",
|
||||||
}},
|
},
|
||||||
"svcempty.testns": {{
|
"testns/svcempty": {
|
||||||
Subsets: []object.EndpointSubset{
|
Subsets: []object.EndpointSubset{
|
||||||
{
|
{
|
||||||
Addresses: nil,
|
Addresses: nil,
|
||||||
|
@ -517,8 +504,8 @@ var epsIndex = map[string][]*object.Endpoints{
|
||||||
},
|
},
|
||||||
Name: "svcempty",
|
Name: "svcempty",
|
||||||
Namespace: "testns",
|
Namespace: "testns",
|
||||||
}},
|
},
|
||||||
"hdls1.testns": {{
|
"testns/hdls1": {
|
||||||
Subsets: []object.EndpointSubset{
|
Subsets: []object.EndpointSubset{
|
||||||
{
|
{
|
||||||
Addresses: []object.EndpointAddress{
|
Addresses: []object.EndpointAddress{
|
||||||
|
@ -536,8 +523,8 @@ var epsIndex = map[string][]*object.Endpoints{
|
||||||
},
|
},
|
||||||
Name: "hdls1",
|
Name: "hdls1",
|
||||||
Namespace: "testns",
|
Namespace: "testns",
|
||||||
}},
|
},
|
||||||
"hdlsprtls.testns": {{
|
"testns/hdlsprtls": {
|
||||||
Subsets: []object.EndpointSubset{
|
Subsets: []object.EndpointSubset{
|
||||||
{
|
{
|
||||||
Addresses: []object.EndpointAddress{
|
Addresses: []object.EndpointAddress{
|
||||||
|
@ -548,17 +535,17 @@ var epsIndex = map[string][]*object.Endpoints{
|
||||||
},
|
},
|
||||||
Name: "hdlsprtls",
|
Name: "hdlsprtls",
|
||||||
Namespace: "testns",
|
Namespace: "testns",
|
||||||
}},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnServeTest) EpIndex(s string) []*object.Endpoints {
|
func (APIConnServeTest) EpIndex(s string) *object.Endpoints {
|
||||||
return epsIndex[s]
|
return epsIndex[s]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnServeTest) EndpointsList() []*object.Endpoints {
|
func (APIConnServeTest) EndpointsList() []*object.Endpoints {
|
||||||
var eps []*object.Endpoints
|
var eps []*object.Endpoints
|
||||||
for _, ep := range epsIndex {
|
for _, ep := range epsIndex {
|
||||||
eps = append(eps, ep...)
|
eps = append(eps, ep)
|
||||||
}
|
}
|
||||||
return eps
|
return eps
|
||||||
}
|
}
|
||||||
|
|
|
@ -444,9 +444,12 @@ func (k *Kubernetes) findServices(r recordRequest, zone string) (services []msg.
|
||||||
serviceList = k.APIConn.ServiceList()
|
serviceList = k.APIConn.ServiceList()
|
||||||
endpointsListFunc = func() []*object.Endpoints { return k.APIConn.EndpointsList() }
|
endpointsListFunc = func() []*object.Endpoints { return k.APIConn.EndpointsList() }
|
||||||
} else {
|
} else {
|
||||||
idx := object.ServiceKey(r.service, r.namespace)
|
key := object.ServiceKey(r.namespace, r.service)
|
||||||
serviceList = k.APIConn.SvcIndex(idx)
|
s := k.APIConn.SvcIndex(key)
|
||||||
endpointsListFunc = func() []*object.Endpoints { return k.APIConn.EpIndex(idx) }
|
if s != nil {
|
||||||
|
serviceList = append(serviceList, s)
|
||||||
|
}
|
||||||
|
endpointsListFunc = func() []*object.Endpoints { return []*object.Endpoints{k.APIConn.EpIndex(key)} }
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, svc := range serviceList {
|
for _, svc := range serviceList {
|
||||||
|
|
|
@ -59,43 +59,24 @@ func TestEndpointHostname(t *testing.T) {
|
||||||
|
|
||||||
type APIConnServiceTest struct{}
|
type APIConnServiceTest struct{}
|
||||||
|
|
||||||
func (APIConnServiceTest) HasSynced() bool { return true }
|
func (APIConnServiceTest) HasSynced() bool { return true }
|
||||||
func (APIConnServiceTest) Run() { return }
|
func (APIConnServiceTest) Run() { return }
|
||||||
func (APIConnServiceTest) Stop() error { return nil }
|
func (APIConnServiceTest) Stop() error { return nil }
|
||||||
func (APIConnServiceTest) PodIndex(string) []*object.Pod { return nil }
|
func (APIConnServiceTest) PodIndex(string) []*object.Pod { return nil }
|
||||||
func (APIConnServiceTest) SvcIndexReverse(string) []*object.Service { return nil }
|
func (APIConnServiceTest) SvcIndexReverse(string) *object.Service { return nil }
|
||||||
func (APIConnServiceTest) EpIndexReverse(string) []*object.Endpoints { return nil }
|
func (APIConnServiceTest) EpIndexReverse(string) *object.Endpoints { return nil }
|
||||||
func (APIConnServiceTest) Modified() int64 { return 0 }
|
func (APIConnServiceTest) Modified() int64 { return 0 }
|
||||||
func (APIConnServiceTest) SetWatchChan(watch.Chan) {}
|
func (APIConnServiceTest) SetWatchChan(watch.Chan) {}
|
||||||
func (APIConnServiceTest) Watch(string) error { return nil }
|
func (APIConnServiceTest) Watch(string) error { return nil }
|
||||||
func (APIConnServiceTest) StopWatching(string) {}
|
func (APIConnServiceTest) StopWatching(string) {}
|
||||||
|
|
||||||
func (APIConnServiceTest) SvcIndex(string) []*object.Service {
|
func (a APIConnServiceTest) SvcIndex(key string) *object.Service {
|
||||||
svcs := []*object.Service{
|
for _, s := range a.ServiceList() {
|
||||||
{
|
if object.ServiceKey(s.Namespace, s.Name) == key {
|
||||||
Name: "svc1",
|
return s
|
||||||
Namespace: "testns",
|
}
|
||||||
ClusterIP: "10.0.0.1",
|
|
||||||
Ports: []api.ServicePort{
|
|
||||||
{Name: "http", Protocol: "tcp", Port: 80},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "hdls1",
|
|
||||||
Namespace: "testns",
|
|
||||||
ClusterIP: api.ClusterIPNone,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "external",
|
|
||||||
Namespace: "testns",
|
|
||||||
ExternalName: "coredns.io",
|
|
||||||
Type: api.ServiceTypeExternalName,
|
|
||||||
Ports: []api.ServicePort{
|
|
||||||
{Name: "http", Protocol: "tcp", Port: 80},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
return svcs
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnServiceTest) ServiceList() []*object.Service {
|
func (APIConnServiceTest) ServiceList() []*object.Service {
|
||||||
|
@ -126,61 +107,13 @@ func (APIConnServiceTest) ServiceList() []*object.Service {
|
||||||
return svcs
|
return svcs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnServiceTest) EpIndex(string) []*object.Endpoints {
|
func (a APIConnServiceTest) EpIndex(key string) *object.Endpoints {
|
||||||
eps := []*object.Endpoints{
|
for _, e := range a.EndpointsList() {
|
||||||
{
|
if object.EndpointsKey(e.Namespace, e.Name) == key {
|
||||||
Subsets: []object.EndpointSubset{
|
return e
|
||||||
{
|
}
|
||||||
Addresses: []object.EndpointAddress{
|
|
||||||
{IP: "172.0.0.1", Hostname: "ep1a"},
|
|
||||||
},
|
|
||||||
Ports: []object.EndpointPort{
|
|
||||||
{Port: 80, Protocol: "tcp", Name: "http"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Name: "svc1",
|
|
||||||
Namespace: "testns",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Subsets: []object.EndpointSubset{
|
|
||||||
{
|
|
||||||
Addresses: []object.EndpointAddress{
|
|
||||||
{IP: "172.0.0.2"},
|
|
||||||
},
|
|
||||||
Ports: []object.EndpointPort{
|
|
||||||
{Port: 80, Protocol: "tcp", Name: "http"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Name: "hdls1",
|
|
||||||
Namespace: "testns",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Subsets: []object.EndpointSubset{
|
|
||||||
{
|
|
||||||
Addresses: []object.EndpointAddress{
|
|
||||||
{IP: "172.0.0.3"},
|
|
||||||
},
|
|
||||||
Ports: []object.EndpointPort{
|
|
||||||
{Port: 80, Protocol: "tcp", Name: "http"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Name: "hdls1",
|
|
||||||
Namespace: "testns",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Subsets: []object.EndpointSubset{
|
|
||||||
{
|
|
||||||
Addresses: []object.EndpointAddress{
|
|
||||||
{IP: "10.9.8.7", NodeName: "test.node.foo.bar"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
return eps
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnServiceTest) EndpointsList() []*object.Endpoints {
|
func (APIConnServiceTest) EndpointsList() []*object.Endpoints {
|
||||||
|
@ -224,7 +157,7 @@ func (APIConnServiceTest) EndpointsList() []*object.Endpoints {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Name: "hdls1",
|
Name: "hdls2",
|
||||||
Namespace: "testns",
|
Namespace: "testns",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -235,6 +168,8 @@ func (APIConnServiceTest) EndpointsList() []*object.Endpoints {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Name: "testsvc",
|
||||||
|
Namespace: "testns",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return eps
|
return eps
|
||||||
|
|
|
@ -28,14 +28,18 @@ func (k *Kubernetes) localNodeName() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find endpoint matching localIP
|
// Find endpoint matching localIP
|
||||||
for _, ep := range k.APIConn.EpIndexReverse(localIP.String()) {
|
ep := k.APIConn.EpIndexReverse(localIP.String())
|
||||||
for _, eps := range ep.Subsets {
|
if ep == nil {
|
||||||
for _, addr := range eps.Addresses {
|
return ""
|
||||||
if localIP.Equal(net.ParseIP(addr.IP)) {
|
}
|
||||||
return addr.NodeName
|
|
||||||
}
|
for _, eps := range ep.Subsets {
|
||||||
|
for _, addr := range eps.Addresses {
|
||||||
|
if localIP.Equal(net.ParseIP(addr.IP)) {
|
||||||
|
return addr.NodeName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/coredns/coredns/plugin/kubernetes/object"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
api "k8s.io/api/core/v1"
|
api "k8s.io/api/core/v1"
|
||||||
)
|
)
|
||||||
|
@ -22,8 +23,9 @@ func (k *Kubernetes) nsAddr() *dns.A {
|
||||||
localIP := k.interfaceAddrsFunc()
|
localIP := k.interfaceAddrsFunc()
|
||||||
rr.A = localIP
|
rr.A = localIP
|
||||||
|
|
||||||
FindEndpoint:
|
ep := k.APIConn.EpIndexReverse(localIP.String())
|
||||||
for _, ep := range k.APIConn.EpIndexReverse(localIP.String()) {
|
if ep != nil {
|
||||||
|
FindEndpoint:
|
||||||
for _, eps := range ep.Subsets {
|
for _, eps := range ep.Subsets {
|
||||||
for _, addr := range eps.Addresses {
|
for _, addr := range eps.Addresses {
|
||||||
if localIP.Equal(net.ParseIP(addr.IP)) {
|
if localIP.Equal(net.ParseIP(addr.IP)) {
|
||||||
|
@ -41,15 +43,12 @@ FindEndpoint:
|
||||||
return rr
|
return rr
|
||||||
}
|
}
|
||||||
|
|
||||||
FindService:
|
svc := k.APIConn.SvcIndex(object.ServiceKey(svcNamespace, svcName))
|
||||||
for _, svc := range k.APIConn.ServiceList() {
|
if svc != nil {
|
||||||
if svcName == svc.Name && svcNamespace == svc.Namespace {
|
if svc.ClusterIP == api.ClusterIPNone {
|
||||||
if svc.ClusterIP == api.ClusterIPNone {
|
rr.A = localIP
|
||||||
rr.A = localIP
|
} else {
|
||||||
} else {
|
rr.A = net.ParseIP(svc.ClusterIP)
|
||||||
rr.A = net.ParseIP(svc.ClusterIP)
|
|
||||||
}
|
|
||||||
break FindService
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,18 +11,26 @@ import (
|
||||||
|
|
||||||
type APIConnTest struct{}
|
type APIConnTest struct{}
|
||||||
|
|
||||||
func (APIConnTest) HasSynced() bool { return true }
|
func (APIConnTest) HasSynced() bool { return true }
|
||||||
func (APIConnTest) Run() { return }
|
func (APIConnTest) Run() { return }
|
||||||
func (APIConnTest) Stop() error { return nil }
|
func (APIConnTest) Stop() error { return nil }
|
||||||
func (APIConnTest) PodIndex(string) []*object.Pod { return nil }
|
func (APIConnTest) PodIndex(string) []*object.Pod { return nil }
|
||||||
func (APIConnTest) SvcIndex(string) []*object.Service { return nil }
|
func (APIConnTest) SvcIndexReverse(string) *object.Service { return nil }
|
||||||
func (APIConnTest) SvcIndexReverse(string) []*object.Service { return nil }
|
func (APIConnTest) EpIndex(string) *object.Endpoints { return nil }
|
||||||
func (APIConnTest) EpIndex(string) []*object.Endpoints { return nil }
|
func (APIConnTest) EndpointsList() []*object.Endpoints { return nil }
|
||||||
func (APIConnTest) EndpointsList() []*object.Endpoints { return nil }
|
func (APIConnTest) Modified() int64 { return 0 }
|
||||||
func (APIConnTest) Modified() int64 { return 0 }
|
func (APIConnTest) SetWatchChan(watch.Chan) {}
|
||||||
func (APIConnTest) SetWatchChan(watch.Chan) {}
|
func (APIConnTest) Watch(string) error { return nil }
|
||||||
func (APIConnTest) Watch(string) error { return nil }
|
func (APIConnTest) StopWatching(string) {}
|
||||||
func (APIConnTest) StopWatching(string) {}
|
|
||||||
|
func (a APIConnTest) SvcIndex(key string) *object.Service {
|
||||||
|
for _, s := range a.ServiceList() {
|
||||||
|
if object.ServiceKey(s.Namespace, s.Name) == key {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (APIConnTest) ServiceList() []*object.Service {
|
func (APIConnTest) ServiceList() []*object.Service {
|
||||||
svcs := []*object.Service{
|
svcs := []*object.Service{
|
||||||
|
@ -35,23 +43,21 @@ func (APIConnTest) ServiceList() []*object.Service {
|
||||||
return svcs
|
return svcs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnTest) EpIndexReverse(string) []*object.Endpoints {
|
func (APIConnTest) EpIndexReverse(string) *object.Endpoints {
|
||||||
eps := []*object.Endpoints{
|
eps := object.Endpoints{
|
||||||
{
|
Subsets: []object.EndpointSubset{
|
||||||
Subsets: []object.EndpointSubset{
|
{
|
||||||
{
|
Addresses: []object.EndpointAddress{
|
||||||
Addresses: []object.EndpointAddress{
|
{
|
||||||
{
|
IP: "127.0.0.1",
|
||||||
IP: "127.0.0.1",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Name: "dns-service",
|
|
||||||
Namespace: "kube-system",
|
|
||||||
},
|
},
|
||||||
|
Name: "dns-service",
|
||||||
|
Namespace: "kube-system",
|
||||||
}
|
}
|
||||||
return eps
|
return &eps
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnTest) GetNodeByName(name string) (*api.Node, error) { return &api.Node{}, nil }
|
func (APIConnTest) GetNodeByName(name string) (*api.Node, error) { return &api.Node{}, nil }
|
||||||
|
|
|
@ -40,7 +40,7 @@ type EndpointPort struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// EndpointsKey return a string using for the index.
|
// EndpointsKey return a string using for the index.
|
||||||
func EndpointsKey(name, namespace string) string { return name + "." + namespace }
|
func EndpointsKey(namespace, name string) string { return namespace + "/" + name }
|
||||||
|
|
||||||
// ToEndpoints converts an api.Service to a *Service.
|
// ToEndpoints converts an api.Service to a *Service.
|
||||||
func ToEndpoints(obj interface{}) interface{} {
|
func ToEndpoints(obj interface{}) interface{} {
|
||||||
|
@ -61,7 +61,7 @@ func ToEndpoints(obj interface{}) interface{} {
|
||||||
Addresses: make([]EndpointAddress, len(eps.Addresses)),
|
Addresses: make([]EndpointAddress, len(eps.Addresses)),
|
||||||
}
|
}
|
||||||
if len(eps.Ports) == 0 {
|
if len(eps.Ports) == 0 {
|
||||||
// Add sentinal if there are no ports.
|
// Add sentinel if there are no ports.
|
||||||
sub.Ports = []EndpointPort{{Port: -1}}
|
sub.Ports = []EndpointPort{{Port: -1}}
|
||||||
} else {
|
} else {
|
||||||
sub.Ports = make([]EndpointPort, len(eps.Ports))
|
sub.Ports = make([]EndpointPort, len(eps.Ports))
|
||||||
|
|
|
@ -20,7 +20,7 @@ type Service struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceKey return a string using for the index.
|
// ServiceKey return a string using for the index.
|
||||||
func ServiceKey(name, namespace string) string { return name + "." + namespace }
|
func ServiceKey(namespace, name string) string { return namespace + "/" + name }
|
||||||
|
|
||||||
// ToService converts an api.Service to a *Service.
|
// ToService converts an api.Service to a *Service.
|
||||||
func ToService(obj interface{}) interface{} {
|
func ToService(obj interface{}) interface{} {
|
||||||
|
|
|
@ -18,35 +18,35 @@ func (k *Kubernetes) Reverse(state request.Request, exact bool, opt plugin.Optio
|
||||||
return nil, e
|
return nil, e
|
||||||
}
|
}
|
||||||
|
|
||||||
records := k.serviceRecordForIP(ip, state.Name())
|
record := k.serviceRecordForIP(ip, state.Name())
|
||||||
if len(records) == 0 {
|
if record == nil {
|
||||||
return records, errNoItems
|
return nil, errNoItems
|
||||||
}
|
}
|
||||||
return records, nil
|
return []msg.Service{*record}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// serviceRecordForIP gets a service record with a cluster ip matching the ip argument
|
// serviceRecordForIP gets a service record with a cluster ip matching the ip argument
|
||||||
// If a service cluster ip does not match, it checks all endpoints
|
// If a service cluster ip does not match, it checks all endpoints
|
||||||
func (k *Kubernetes) serviceRecordForIP(ip, name string) []msg.Service {
|
func (k *Kubernetes) serviceRecordForIP(ip, name string) *msg.Service {
|
||||||
// First check services with cluster ips
|
// First check services with cluster ips
|
||||||
for _, service := range k.APIConn.SvcIndexReverse(ip) {
|
service := k.APIConn.SvcIndexReverse(ip)
|
||||||
|
if service != nil {
|
||||||
if len(k.Namespaces) > 0 && !k.namespaceExposed(service.Namespace) {
|
if len(k.Namespaces) > 0 && !k.namespaceExposed(service.Namespace) {
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
domain := strings.Join([]string{service.Name, service.Namespace, Svc, k.primaryZone()}, ".")
|
domain := strings.Join([]string{service.Name, service.Namespace, Svc, k.primaryZone()}, ".")
|
||||||
return []msg.Service{{Host: domain, TTL: k.ttl}}
|
return &msg.Service{Host: domain, TTL: k.ttl}
|
||||||
}
|
}
|
||||||
// If no cluster ips match, search endpoints
|
// If no cluster ips match, search endpoints
|
||||||
for _, ep := range k.APIConn.EpIndexReverse(ip) {
|
ep := k.APIConn.EpIndexReverse(ip)
|
||||||
if len(k.Namespaces) > 0 && !k.namespaceExposed(ep.Namespace) {
|
if ep == nil || len(k.Namespaces) > 0 && !k.namespaceExposed(ep.Namespace) {
|
||||||
continue
|
return nil
|
||||||
}
|
}
|
||||||
for _, eps := range ep.Subsets {
|
for _, eps := range ep.Subsets {
|
||||||
for _, addr := range eps.Addresses {
|
for _, addr := range eps.Addresses {
|
||||||
if addr.IP == ip {
|
if addr.IP == ip {
|
||||||
domain := strings.Join([]string{endpointHostname(addr, k.endpointNameMode), ep.Name, ep.Namespace, Svc, k.primaryZone()}, ".")
|
domain := strings.Join([]string{endpointHostname(addr, k.endpointNameMode), ep.Name, ep.Namespace, Svc, k.primaryZone()}, ".")
|
||||||
return []msg.Service{{Host: domain, TTL: k.ttl}}
|
return &msg.Service{Host: domain, TTL: k.ttl}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ func (APIConnReverseTest) HasSynced() bool { return true }
|
||||||
func (APIConnReverseTest) Run() { return }
|
func (APIConnReverseTest) Run() { return }
|
||||||
func (APIConnReverseTest) Stop() error { return nil }
|
func (APIConnReverseTest) Stop() error { return nil }
|
||||||
func (APIConnReverseTest) PodIndex(string) []*object.Pod { return nil }
|
func (APIConnReverseTest) PodIndex(string) []*object.Pod { return nil }
|
||||||
func (APIConnReverseTest) EpIndex(string) []*object.Endpoints { return nil }
|
func (APIConnReverseTest) EpIndex(string) *object.Endpoints { return nil }
|
||||||
func (APIConnReverseTest) EndpointsList() []*object.Endpoints { return nil }
|
func (APIConnReverseTest) EndpointsList() []*object.Endpoints { return nil }
|
||||||
func (APIConnReverseTest) ServiceList() []*object.Service { return nil }
|
func (APIConnReverseTest) ServiceList() []*object.Service { return nil }
|
||||||
func (APIConnReverseTest) Modified() int64 { return 0 }
|
func (APIConnReverseTest) Modified() int64 { return 0 }
|
||||||
|
@ -28,38 +28,34 @@ func (APIConnReverseTest) SetWatchChan(watch.Chan) {}
|
||||||
func (APIConnReverseTest) Watch(string) error { return nil }
|
func (APIConnReverseTest) Watch(string) error { return nil }
|
||||||
func (APIConnReverseTest) StopWatching(string) {}
|
func (APIConnReverseTest) StopWatching(string) {}
|
||||||
|
|
||||||
func (APIConnReverseTest) SvcIndex(svc string) []*object.Service {
|
func (APIConnReverseTest) SvcIndex(key string) *object.Service {
|
||||||
if svc != "svc1.testns" {
|
if key != "testns/svc1" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
svcs := []*object.Service{
|
svc := object.Service{
|
||||||
{
|
Name: "svc1",
|
||||||
Name: "svc1",
|
Namespace: "testns",
|
||||||
Namespace: "testns",
|
ClusterIP: "192.168.1.100",
|
||||||
ClusterIP: "192.168.1.100",
|
Ports: []api.ServicePort{{Name: "http", Protocol: "tcp", Port: 80}},
|
||||||
Ports: []api.ServicePort{{Name: "http", Protocol: "tcp", Port: 80}},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
return svcs
|
return &svc
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnReverseTest) SvcIndexReverse(ip string) []*object.Service {
|
func (APIConnReverseTest) SvcIndexReverse(ip string) *object.Service {
|
||||||
if ip != "192.168.1.100" {
|
if ip != "192.168.1.100" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
svcs := []*object.Service{
|
svc := object.Service{
|
||||||
{
|
Name: "svc1",
|
||||||
Name: "svc1",
|
Namespace: "testns",
|
||||||
Namespace: "testns",
|
ClusterIP: "192.168.1.100",
|
||||||
ClusterIP: "192.168.1.100",
|
Ports: []api.ServicePort{{Name: "http", Protocol: "tcp", Port: 80}},
|
||||||
Ports: []api.ServicePort{{Name: "http", Protocol: "tcp", Port: 80}},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
return svcs
|
return &svc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnReverseTest) EpIndexReverse(ip string) []*object.Endpoints {
|
func (APIConnReverseTest) EpIndexReverse(ip string) *object.Endpoints {
|
||||||
switch ip {
|
switch ip {
|
||||||
case "10.0.0.100":
|
case "10.0.0.100":
|
||||||
case "1234:abcd::1":
|
case "1234:abcd::1":
|
||||||
|
@ -68,26 +64,24 @@ func (APIConnReverseTest) EpIndexReverse(ip string) []*object.Endpoints {
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
eps := []*object.Endpoints{
|
ep := object.Endpoints{
|
||||||
{
|
Subsets: []object.EndpointSubset{
|
||||||
Subsets: []object.EndpointSubset{
|
{
|
||||||
{
|
Addresses: []object.EndpointAddress{
|
||||||
Addresses: []object.EndpointAddress{
|
{IP: "10.0.0.100", Hostname: "ep1a"},
|
||||||
{IP: "10.0.0.100", Hostname: "ep1a"},
|
{IP: "1234:abcd::1", Hostname: "ep1b"},
|
||||||
{IP: "1234:abcd::1", Hostname: "ep1b"},
|
{IP: "fd00:77:30::a", Hostname: "ip6svc1ex"},
|
||||||
{IP: "fd00:77:30::a", Hostname: "ip6svc1ex"},
|
{IP: "fd00:77:30::2:9ba6", Hostname: "ip6svc1in"},
|
||||||
{IP: "fd00:77:30::2:9ba6", Hostname: "ip6svc1in"},
|
},
|
||||||
},
|
Ports: []object.EndpointPort{
|
||||||
Ports: []object.EndpointPort{
|
{Port: 80, Protocol: "tcp", Name: "http"},
|
||||||
{Port: 80, Protocol: "tcp", Name: "http"},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Name: "svc1",
|
|
||||||
Namespace: "testns",
|
|
||||||
},
|
},
|
||||||
|
Name: "svc1",
|
||||||
|
Namespace: "testns",
|
||||||
}
|
}
|
||||||
return eps
|
return &ep
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnReverseTest) GetNodeByName(name string) (*api.Node, error) {
|
func (APIConnReverseTest) GetNodeByName(name string) (*api.Node, error) {
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/coredns/coredns/plugin"
|
"github.com/coredns/coredns/plugin"
|
||||||
"github.com/coredns/coredns/plugin/etcd/msg"
|
"github.com/coredns/coredns/plugin/etcd/msg"
|
||||||
"github.com/coredns/coredns/request"
|
"github.com/coredns/coredns/request"
|
||||||
|
"k8s.io/client-go/tools/cache"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
api "k8s.io/api/core/v1"
|
api "k8s.io/api/core/v1"
|
||||||
|
@ -114,39 +115,36 @@ func (k *Kubernetes) transfer(c chan dns.RR, zone string) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
endpointsList := k.APIConn.EpIndex(svc.Name + "." + svc.Namespace)
|
key, err := cache.MetaNamespaceKeyFunc(svc)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ep := k.APIConn.EpIndex(key)
|
||||||
|
for _, eps := range ep.Subsets {
|
||||||
|
srvWeight := calcSRVWeight(len(eps.Addresses))
|
||||||
|
for _, addr := range eps.Addresses {
|
||||||
|
s := msg.Service{Host: addr.IP, TTL: k.ttl}
|
||||||
|
s.Key = strings.Join(svcBase, "/")
|
||||||
|
// We don't need to change the msg.Service host from IP to Name yet
|
||||||
|
// so disregard the return value here
|
||||||
|
emitAddressRecord(c, s)
|
||||||
|
|
||||||
for _, ep := range endpointsList {
|
s.Key = strings.Join(append(svcBase, endpointHostname(addr, k.endpointNameMode)), "/")
|
||||||
if ep.Name != svc.Name || ep.Namespace != svc.Namespace {
|
// Change host from IP to Name for SRV records
|
||||||
continue
|
host := emitAddressRecord(c, s)
|
||||||
}
|
s.Host = host
|
||||||
|
|
||||||
for _, eps := range ep.Subsets {
|
for _, p := range eps.Ports {
|
||||||
srvWeight := calcSRVWeight(len(eps.Addresses))
|
// As per spec unnamed ports do not have a srv record
|
||||||
for _, addr := range eps.Addresses {
|
// https://github.com/kubernetes/dns/blob/master/docs/specification.md#232---srv-records
|
||||||
s := msg.Service{Host: addr.IP, TTL: k.ttl}
|
if p.Name == "" {
|
||||||
s.Key = strings.Join(svcBase, "/")
|
continue
|
||||||
// We don't need to change the msg.Service host from IP to Name yet
|
|
||||||
// so disregard the return value here
|
|
||||||
emitAddressRecord(c, s)
|
|
||||||
|
|
||||||
s.Key = strings.Join(append(svcBase, endpointHostname(addr, k.endpointNameMode)), "/")
|
|
||||||
// Change host from IP to Name for SRV records
|
|
||||||
host := emitAddressRecord(c, s)
|
|
||||||
s.Host = host
|
|
||||||
|
|
||||||
for _, p := range eps.Ports {
|
|
||||||
// As per spec unnamed ports do not have a srv record
|
|
||||||
// https://github.com/kubernetes/dns/blob/master/docs/specification.md#232---srv-records
|
|
||||||
if p.Name == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
s.Port = int(p.Port)
|
|
||||||
|
|
||||||
s.Key = strings.Join(append(svcBase, strings.ToLower("_"+string(p.Protocol)), strings.ToLower("_"+string(p.Name))), "/")
|
|
||||||
c <- s.NewSRV(msg.Domain(s.Key), srvWeight)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.Port = int(p.Port)
|
||||||
|
|
||||||
|
s.Key = strings.Join(append(svcBase, strings.ToLower("_"+string(p.Protocol)), strings.ToLower("_"+string(p.Name))), "/")
|
||||||
|
c <- s.NewSRV(msg.Domain(s.Key), srvWeight)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue