parent
c8fb66f8cc
commit
6ed88fab74
13 changed files with 380 additions and 291 deletions
|
@ -20,19 +20,21 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
podIPIndex = "PodIP"
|
||||
svcIPIndex = "ServiceIP"
|
||||
epIPIndex = "EndpointsIP"
|
||||
podIPIndex = "PodIP"
|
||||
svcNameNamespaceIndex = "NameNamespace"
|
||||
svcIPIndex = "ServiceIP"
|
||||
epNameNamespaceIndex = "EndpointNameNamespace"
|
||||
epIPIndex = "EndpointsIP"
|
||||
)
|
||||
|
||||
type dnsController interface {
|
||||
ServiceList() []*object.Service
|
||||
EndpointsList() []*object.Endpoints
|
||||
SvcIndex(string) *object.Service
|
||||
SvcIndexReverse(string) *object.Service
|
||||
SvcIndex(string) []*object.Service
|
||||
SvcIndexReverse(string) []*object.Service
|
||||
PodIndex(string) []*object.Pod
|
||||
EpIndex(string) *object.Endpoints
|
||||
EpIndexReverse(string) *object.Endpoints
|
||||
EpIndex(string) []*object.Endpoints
|
||||
EpIndexReverse(string) []*object.Endpoints
|
||||
|
||||
GetNodeByName(string) (*api.Node, error)
|
||||
GetNamespaceByName(string) (*api.Namespace, error)
|
||||
|
@ -116,7 +118,7 @@ func newdnsController(kubeClient kubernetes.Interface, opts dnsControlOpts) *dns
|
|||
&object.Service{},
|
||||
opts.resyncPeriod,
|
||||
cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete},
|
||||
cache.Indexers{svcIPIndex: svcIPIndexFunc},
|
||||
cache.Indexers{svcNameNamespaceIndex: svcNameNamespaceIndexFunc, svcIPIndex: svcIPIndexFunc},
|
||||
object.ToService,
|
||||
)
|
||||
|
||||
|
@ -143,7 +145,7 @@ func newdnsController(kubeClient kubernetes.Interface, opts dnsControlOpts) *dns
|
|||
&api.Endpoints{},
|
||||
opts.resyncPeriod,
|
||||
cache.ResourceEventHandlerFuncs{AddFunc: dns.Add, UpdateFunc: dns.Update, DeleteFunc: dns.Delete},
|
||||
cache.Indexers{epIPIndex: epIPIndexFunc},
|
||||
cache.Indexers{epNameNamespaceIndex: epNameNamespaceIndexFunc, epIPIndex: epIPIndexFunc},
|
||||
object.ToEndpoints)
|
||||
}
|
||||
|
||||
|
@ -173,6 +175,22 @@ func svcIPIndexFunc(obj interface{}) ([]string, error) {
|
|||
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) {
|
||||
ep, ok := obj.(*object.Endpoints)
|
||||
if !ok {
|
||||
|
@ -341,9 +359,6 @@ func (dns *dnsControl) EndpointsList() (eps []*object.Endpoints) {
|
|||
}
|
||||
|
||||
func (dns *dnsControl) PodIndex(ip string) (pods []*object.Pod) {
|
||||
if dns.podLister == nil {
|
||||
return nil
|
||||
}
|
||||
os, err := dns.podLister.ByIndex(podIPIndex, ip)
|
||||
if err != nil {
|
||||
return nil
|
||||
|
@ -353,24 +368,27 @@ func (dns *dnsControl) PodIndex(ip string) (pods []*object.Pod) {
|
|||
if !ok {
|
||||
continue
|
||||
}
|
||||
return []*object.Pod{p}
|
||||
pods = append(pods, p)
|
||||
}
|
||||
return nil
|
||||
return pods
|
||||
}
|
||||
|
||||
func (dns *dnsControl) SvcIndex(key string) *object.Service {
|
||||
o, _, err := dns.svcLister.GetByKey(key)
|
||||
func (dns *dnsControl) SvcIndex(idx string) (svcs []*object.Service) {
|
||||
os, err := dns.svcLister.ByIndex(svcNameNamespaceIndex, idx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
s, ok := o.(*object.Service)
|
||||
if !ok {
|
||||
return nil
|
||||
for _, o := range os {
|
||||
s, ok := o.(*object.Service)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
svcs = append(svcs, s)
|
||||
}
|
||||
return s
|
||||
return svcs
|
||||
}
|
||||
|
||||
func (dns *dnsControl) SvcIndexReverse(ip string) *object.Service {
|
||||
func (dns *dnsControl) SvcIndexReverse(ip string) (svcs []*object.Service) {
|
||||
os, err := dns.svcLister.ByIndex(svcIPIndex, ip)
|
||||
if err != nil {
|
||||
return nil
|
||||
|
@ -381,27 +399,27 @@ func (dns *dnsControl) SvcIndexReverse(ip string) *object.Service {
|
|||
if !ok {
|
||||
continue
|
||||
}
|
||||
return s
|
||||
svcs = append(svcs, s)
|
||||
}
|
||||
return nil
|
||||
return svcs
|
||||
}
|
||||
|
||||
func (dns *dnsControl) EpIndex(key string) (ep *object.Endpoints) {
|
||||
o, _, err := dns.epLister.GetByKey(key)
|
||||
func (dns *dnsControl) EpIndex(idx string) (ep []*object.Endpoints) {
|
||||
os, err := dns.epLister.ByIndex(epNameNamespaceIndex, idx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
e, ok := o.(*object.Endpoints)
|
||||
if !ok {
|
||||
return nil
|
||||
for _, o := range os {
|
||||
e, ok := o.(*object.Endpoints)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
ep = append(ep, e)
|
||||
}
|
||||
return e
|
||||
return ep
|
||||
}
|
||||
|
||||
func (dns *dnsControl) EpIndexReverse(ip string) (ep *object.Endpoints) {
|
||||
if dns.epLister == nil {
|
||||
return nil
|
||||
}
|
||||
func (dns *dnsControl) EpIndexReverse(ip string) (ep []*object.Endpoints) {
|
||||
os, err := dns.epLister.ByIndex(epIPIndex, ip)
|
||||
if err != nil {
|
||||
return nil
|
||||
|
@ -411,9 +429,9 @@ func (dns *dnsControl) EpIndexReverse(ip string) (ep *object.Endpoints) {
|
|||
if !ok {
|
||||
continue
|
||||
}
|
||||
return e
|
||||
ep = append(ep, e)
|
||||
}
|
||||
return nil
|
||||
return ep
|
||||
}
|
||||
|
||||
// GetNodeByName return the node by name. If nothing is found an error is
|
||||
|
@ -432,7 +450,7 @@ func (dns *dnsControl) GetNamespaceByName(name string) (*api.Namespace, error) {
|
|||
if !ok {
|
||||
continue
|
||||
}
|
||||
if name == ns.GetName() {
|
||||
if name == ns.ObjectMeta.Name {
|
||||
return ns, nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue