| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | package kubernetes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 	"errors"
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"sync"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	"k8s.io/client-go/kubernetes"
 | 
					
						
							|  |  |  | 	api "k8s.io/client-go/pkg/api/v1"
 | 
					
						
							|  |  |  | 	"k8s.io/client-go/tools/cache"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	meta "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
					
						
							|  |  |  | 	"k8s.io/apimachinery/pkg/labels"
 | 
					
						
							|  |  |  | 	"k8s.io/apimachinery/pkg/runtime"
 | 
					
						
							|  |  |  | 	"k8s.io/apimachinery/pkg/watch"
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var (
 | 
					
						
							|  |  |  | 	namespace = api.NamespaceAll
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | const podIPIndex = "PodIP"
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | const svcNameNamespaceIndex = "NameNamespace"
 | 
					
						
							|  |  |  | const svcIPIndex = "ServiceIP"
 | 
					
						
							|  |  |  | const epNameNamespaceIndex = "EndpointNameNamespace"
 | 
					
						
							|  |  |  | const epIPIndex = "EndpointsIP"
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | type dnsController interface {
 | 
					
						
							|  |  |  | 	ServiceList() []*api.Service
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	SvcIndex(string) []*api.Service
 | 
					
						
							|  |  |  | 	SvcIndexReverse(string) []*api.Service
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	PodIndex(string) []*api.Pod
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	EpIndex(string) []*api.Endpoints
 | 
					
						
							|  |  |  | 	EpIndexReverse(string) []*api.Endpoints
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	EndpointsList() []*api.Endpoints
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	GetNodeByName(string) (*api.Node, error)
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	Run()
 | 
					
						
							| 
									
										
										
										
											2017-10-20 22:53:17 +01:00
										 |  |  | 	HasSynced() bool
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	Stop() error
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type dnsControl struct {
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  | 	client *kubernetes.Clientset
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	selector *labels.Selector
 | 
					
						
							| 
									
										
										
										
											2016-08-12 20:44:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	svcController cache.Controller
 | 
					
						
							|  |  |  | 	podController cache.Controller
 | 
					
						
							|  |  |  | 	epController  cache.Controller
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	svcLister cache.Indexer
 | 
					
						
							|  |  |  | 	podLister cache.Indexer
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	epLister  cache.Indexer
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// stopLock is used to enforce only a single call to Stop is active.
 | 
					
						
							|  |  |  | 	// Needed because we allow stopping through an http endpoint and
 | 
					
						
							|  |  |  | 	// allowing concurrent stoppers leads to stack traces.
 | 
					
						
							|  |  |  | 	stopLock sync.Mutex
 | 
					
						
							|  |  |  | 	shutdown bool
 | 
					
						
							|  |  |  | 	stopCh   chan struct{}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | type dnsControlOpts struct {
 | 
					
						
							|  |  |  | 	initPodCache bool
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:52:18 +01:00
										 |  |  | 	resyncPeriod time.Duration
 | 
					
						
							|  |  |  | 	// Label handling.
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	labelSelector *meta.LabelSelector
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:52:18 +01:00
										 |  |  | 	selector      *labels.Selector
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 12:46:35 +01:00
										 |  |  | // newDNSController creates a controller for CoreDNS.
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:52:18 +01:00
										 |  |  | func newdnsController(kubeClient *kubernetes.Clientset, opts dnsControlOpts) *dnsControl {
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	dns := dnsControl{
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		client:   kubeClient,
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:52:18 +01:00
										 |  |  | 		selector: opts.selector,
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		stopCh:   make(chan struct{}),
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	dns.svcLister, dns.svcController = cache.NewIndexerInformer(
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		&cache.ListWatch{
 | 
					
						
							| 
									
										
										
										
											2016-08-12 20:44:08 -07:00
										 |  |  | 			ListFunc:  serviceListFunc(dns.client, namespace, dns.selector),
 | 
					
						
							|  |  |  | 			WatchFunc: serviceWatchFunc(dns.client, namespace, dns.selector),
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 		&api.Service{},
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:52:18 +01:00
										 |  |  | 		opts.resyncPeriod,
 | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 		cache.ResourceEventHandlerFuncs{},
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 		cache.Indexers{svcNameNamespaceIndex: svcNameNamespaceIndexFunc, svcIPIndex: svcIPIndexFunc})
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | 	if opts.initPodCache {
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		dns.podLister, dns.podController = cache.NewIndexerInformer(
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 			&cache.ListWatch{
 | 
					
						
							|  |  |  | 				ListFunc:  podListFunc(dns.client, namespace, dns.selector),
 | 
					
						
							|  |  |  | 				WatchFunc: podWatchFunc(dns.client, namespace, dns.selector),
 | 
					
						
							|  |  |  | 			},
 | 
					
						
							|  |  |  | 			&api.Pod{}, // TODO replace with a lighter-weight custom struct
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:52:18 +01:00
										 |  |  | 			opts.resyncPeriod,
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 			cache.ResourceEventHandlerFuncs{},
 | 
					
						
							|  |  |  | 			cache.Indexers{podIPIndex: podIPIndexFunc})
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	dns.epLister, dns.epController = cache.NewIndexerInformer(
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 		&cache.ListWatch{
 | 
					
						
							|  |  |  | 			ListFunc:  endpointsListFunc(dns.client, namespace, dns.selector),
 | 
					
						
							|  |  |  | 			WatchFunc: endpointsWatchFunc(dns.client, namespace, dns.selector),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:52:18 +01:00
										 |  |  | 		&api.Endpoints{},
 | 
					
						
							|  |  |  | 		opts.resyncPeriod,
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 		cache.ResourceEventHandlerFuncs{},
 | 
					
						
							|  |  |  | 		cache.Indexers{epNameNamespaceIndex: epNameNamespaceIndexFunc, epIPIndex: epIPIndexFunc})
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	return &dns
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | func podIPIndexFunc(obj interface{}) ([]string, error) {
 | 
					
						
							|  |  |  | 	p, ok := obj.(*api.Pod)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		return nil, errors.New("obj was not an *api.Pod")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return []string{p.Status.PodIP}, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | func svcIPIndexFunc(obj interface{}) ([]string, error) {
 | 
					
						
							|  |  |  | 	svc, ok := obj.(*api.Service)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		return nil, errors.New("obj was not an *api.Service")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return []string{svc.Spec.ClusterIP}, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func svcNameNamespaceIndexFunc(obj interface{}) ([]string, error) {
 | 
					
						
							|  |  |  | 	s, ok := obj.(*api.Service)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		return nil, errors.New("obj was not an *api.Service")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return []string{s.ObjectMeta.Name + "." + s.ObjectMeta.Namespace}, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func epNameNamespaceIndexFunc(obj interface{}) ([]string, error) {
 | 
					
						
							|  |  |  | 	s, ok := obj.(*api.Endpoints)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		return nil, errors.New("obj was not an *api.Endpoints")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return []string{s.ObjectMeta.Name + "." + s.ObjectMeta.Namespace}, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func epIPIndexFunc(obj interface{}) ([]string, error) {
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:38:45 -05:00
										 |  |  | 	ep, ok := obj.(*api.Endpoints)
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	if !ok {
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:38:45 -05:00
										 |  |  | 		return nil, errors.New("obj was not an *api.Endpoints")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	var idx []string
 | 
					
						
							|  |  |  | 	for _, eps := range ep.Subsets {
 | 
					
						
							|  |  |  | 		for _, addr := range eps.Addresses {
 | 
					
						
							|  |  |  | 			idx = append(idx, addr.IP)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:38:45 -05:00
										 |  |  | 	return idx, nil
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | func serviceListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(meta.ListOptions) (runtime.Object, error) {
 | 
					
						
							|  |  |  | 	return func(opts meta.ListOptions) (runtime.Object, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		if s != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 			opts.LabelSelector = (*s).String()
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		listV1, err := c.Services(ns).List(opts)
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:21:24 -05:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		return listV1, err
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | func podListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(meta.ListOptions) (runtime.Object, error) {
 | 
					
						
							|  |  |  | 	return func(opts meta.ListOptions) (runtime.Object, error) {
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 		if s != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 			opts.LabelSelector = (*s).String()
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		listV1, err := c.Pods(ns).List(opts)
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		return listV1, err
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | func serviceWatchFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
 | 
					
						
							|  |  |  | 	return func(options meta.ListOptions) (watch.Interface, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		if s != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 			options.LabelSelector = (*s).String()
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		w, err := c.Services(ns).Watch(options)
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		return w, nil
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | func podWatchFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
 | 
					
						
							|  |  |  | 	return func(options meta.ListOptions) (watch.Interface, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		if s != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 			options.LabelSelector = (*s).String()
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		w, err := c.Pods(ns).Watch(options)
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:21:24 -05:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		return w, nil
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | func endpointsListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(meta.ListOptions) (runtime.Object, error) {
 | 
					
						
							|  |  |  | 	return func(opts meta.ListOptions) (runtime.Object, error) {
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 		if s != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 			opts.LabelSelector = (*s).String()
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		listV1, err := c.Endpoints(ns).List(opts)
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		return listV1, err
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | func endpointsWatchFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
 | 
					
						
							|  |  |  | 	return func(options meta.ListOptions) (watch.Interface, error) {
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 		if s != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 			options.LabelSelector = (*s).String()
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		w, err := c.Endpoints(ns).Watch(options)
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		return w, nil
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | // Stop stops the  controller.
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | func (dns *dnsControl) Stop() error {
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	dns.stopLock.Lock()
 | 
					
						
							|  |  |  | 	defer dns.stopLock.Unlock()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Only try draining the workqueue if we haven't already.
 | 
					
						
							|  |  |  | 	if !dns.shutdown {
 | 
					
						
							|  |  |  | 		close(dns.stopCh)
 | 
					
						
							|  |  |  | 		dns.shutdown = true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return fmt.Errorf("shutdown already in progress")
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Run starts the controller.
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | func (dns *dnsControl) Run() {
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	go dns.svcController.Run(dns.stopCh)
 | 
					
						
							| 
									
										
										
										
											2016-12-02 17:50:01 -05:00
										 |  |  | 	go dns.epController.Run(dns.stopCh)
 | 
					
						
							| 
									
										
										
										
											2017-01-20 02:22:11 -05:00
										 |  |  | 	if dns.podController != nil {
 | 
					
						
							|  |  |  | 		go dns.podController.Run(dns.stopCh)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	<-dns.stopCh
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-20 22:53:17 +01:00
										 |  |  | // HasSynced calls on all controllers.
 | 
					
						
							|  |  |  | func (dns *dnsControl) HasSynced() bool {
 | 
					
						
							|  |  |  | 	a := dns.svcController.HasSynced()
 | 
					
						
							|  |  |  | 	b := dns.epController.HasSynced()
 | 
					
						
							|  |  |  | 	c := true
 | 
					
						
							|  |  |  | 	if dns.podController != nil {
 | 
					
						
							|  |  |  | 		c = dns.podController.HasSynced()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return a && b && c
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | func (dns *dnsControl) ServiceList() (svcs []*api.Service) {
 | 
					
						
							|  |  |  | 	os := dns.svcLister.List()
 | 
					
						
							|  |  |  | 	for _, o := range os {
 | 
					
						
							|  |  |  | 		s, ok := o.(*api.Service)
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		svcs = append(svcs, s)
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 	return svcs
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | func (dns *dnsControl) PodIndex(ip string) (pods []*api.Pod) {
 | 
					
						
							|  |  |  | 	if dns.podLister == nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-21 13:06:35 -04:00
										 |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	os, err := dns.podLister.ByIndex(podIPIndex, ip)
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	for _, o := range os {
 | 
					
						
							|  |  |  | 		p, ok := o.(*api.Pod)
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		pods = append(pods, p)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	return pods
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | func (dns *dnsControl) SvcIndex(idx string) (svcs []*api.Service) {
 | 
					
						
							|  |  |  | 	if dns.svcLister == nil {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	os, err := dns.svcLister.ByIndex(svcNameNamespaceIndex, idx)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for _, o := range os {
 | 
					
						
							|  |  |  | 		s, ok := o.(*api.Service)
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		svcs = append(svcs, s)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return svcs
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dns *dnsControl) SvcIndexReverse(ip string) (svcs []*api.Service) {
 | 
					
						
							|  |  |  | 	if dns.svcLister == nil {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	os, err := dns.svcLister.ByIndex(svcIPIndex, ip)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, o := range os {
 | 
					
						
							|  |  |  | 		s, ok := o.(*api.Service)
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		svcs = append(svcs, s)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return svcs
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dns *dnsControl) EpIndex(idx string) (ep []*api.Endpoints) {
 | 
					
						
							|  |  |  | 	if dns.epLister == nil {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	os, err := dns.epLister.ByIndex(epNameNamespaceIndex, idx)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for _, o := range os {
 | 
					
						
							|  |  |  | 		e, ok := o.(*api.Endpoints)
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		ep = append(ep, e)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return ep
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dns *dnsControl) EpIndexReverse(ip string) (ep []*api.Endpoints) {
 | 
					
						
							|  |  |  | 	if dns.svcLister == nil {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	os, err := dns.epLister.ByIndex(epIPIndex, ip)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for _, o := range os {
 | 
					
						
							|  |  |  | 		e, ok := o.(*api.Endpoints)
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		ep = append(ep, e)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return ep
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | func (dns *dnsControl) EndpointsList() (eps []*api.Endpoints) {
 | 
					
						
							|  |  |  | 	os := dns.epLister.List()
 | 
					
						
							|  |  |  | 	for _, o := range os {
 | 
					
						
							|  |  |  | 		ep, ok := o.(*api.Endpoints)
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		eps = append(eps, ep)
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	return eps
 | 
					
						
							| 
									
										
										
										
											2017-05-22 16:05:48 -04:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | func (dns *dnsControl) GetNodeByName(name string) (*api.Node, error) {
 | 
					
						
							|  |  |  | 	v1node, err := dns.client.Nodes().Get(name, meta.GetOptions{})
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 		return &api.Node{}, err
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	return v1node, nil
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:38:00 -04:00
										 |  |  | }
 |