| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | package kubernetes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"sync"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"k8s.io/kubernetes/pkg/api"
 | 
					
						
							|  |  |  | 	"k8s.io/kubernetes/pkg/client/cache"
 | 
					
						
							| 
									
										
										
										
											2016-10-30 11:58:33 -03:00
										 |  |  | 	clientset_generated "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_4"
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	"k8s.io/kubernetes/pkg/labels"
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	"k8s.io/kubernetes/pkg/runtime"
 | 
					
						
							|  |  |  | 	"k8s.io/kubernetes/pkg/watch"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var (
 | 
					
						
							|  |  |  | 	namespace = api.NamespaceAll
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:48:11 -03:00
										 |  |  | // storeToNamespaceLister makes a Store that lists Namespaces.
 | 
					
						
							|  |  |  | type storeToNamespaceLister struct {
 | 
					
						
							|  |  |  | 	cache.Store
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // List lists all Namespaces in the store.
 | 
					
						
							|  |  |  | func (s *storeToNamespaceLister) List() (ns api.NamespaceList, err error) {
 | 
					
						
							|  |  |  | 	for _, m := range s.Store.List() {
 | 
					
						
							|  |  |  | 		ns.Items = append(ns.Items, *(m.(*api.Namespace)))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return ns, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | type dnsController struct {
 | 
					
						
							| 
									
										
										
										
											2016-10-30 11:58:33 -03:00
										 |  |  | 	client *clientset_generated.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 08:31:23 -07:00
										 |  |  | 	endpController *cache.Controller
 | 
					
						
							|  |  |  | 	svcController  *cache.Controller
 | 
					
						
							|  |  |  | 	nsController   *cache.Controller
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	svcLister  cache.StoreToServiceLister
 | 
					
						
							|  |  |  | 	endpLister cache.StoreToEndpointsLister
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:48:11 -03:00
										 |  |  | 	nsLister   storeToNamespaceLister
 | 
					
						
							| 
									
										
										
										
											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{}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 12:46:35 +01:00
										 |  |  | // newDNSController creates a controller for CoreDNS.
 | 
					
						
							| 
									
										
										
										
											2016-10-30 11:58:33 -03:00
										 |  |  | func newdnsController(kubeClient *clientset_generated.Clientset, resyncPeriod time.Duration, lselector *labels.Selector) *dnsController {
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	dns := dnsController{
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		client:   kubeClient,
 | 
					
						
							|  |  |  | 		selector: lselector,
 | 
					
						
							|  |  |  | 		stopCh:   make(chan struct{}),
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 08:31:23 -07:00
										 |  |  | 	dns.endpLister.Store, dns.endpController = cache.NewInformer(
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		&cache.ListWatch{
 | 
					
						
							| 
									
										
										
										
											2016-08-12 20:44:08 -07:00
										 |  |  | 			ListFunc:  endpointsListFunc(dns.client, namespace, dns.selector),
 | 
					
						
							|  |  |  | 			WatchFunc: endpointsWatchFunc(dns.client, namespace, dns.selector),
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2016-09-16 08:31:23 -07:00
										 |  |  | 		&api.Endpoints{}, resyncPeriod, cache.ResourceEventHandlerFuncs{})
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 	dns.svcLister.Indexer, 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{},
 | 
					
						
							|  |  |  | 		resyncPeriod,
 | 
					
						
							|  |  |  | 		cache.ResourceEventHandlerFuncs{},
 | 
					
						
							|  |  |  | 		cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 08:31:23 -07:00
										 |  |  | 	dns.nsLister.Store, dns.nsController = cache.NewInformer(
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		&cache.ListWatch{
 | 
					
						
							| 
									
										
										
										
											2016-08-12 20:44:08 -07:00
										 |  |  | 			ListFunc:  namespaceListFunc(dns.client, dns.selector),
 | 
					
						
							|  |  |  | 			WatchFunc: namespaceWatchFunc(dns.client, dns.selector),
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2016-09-16 08:31:23 -07:00
										 |  |  | 		&api.Namespace{}, resyncPeriod, cache.ResourceEventHandlerFuncs{})
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return &dns
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 11:58:33 -03:00
										 |  |  | func serviceListFunc(c *clientset_generated.Clientset, ns string, s *labels.Selector) func(api.ListOptions) (runtime.Object, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	return func(opts api.ListOptions) (runtime.Object, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		if s != nil {
 | 
					
						
							|  |  |  | 			opts.LabelSelector = *s
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		return c.Services(ns).List(opts)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 11:58:33 -03:00
										 |  |  | func serviceWatchFunc(c *clientset_generated.Clientset, ns string, s *labels.Selector) func(options api.ListOptions) (watch.Interface, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	return func(options api.ListOptions) (watch.Interface, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		if s != nil {
 | 
					
						
							|  |  |  | 			options.LabelSelector = *s
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		return c.Services(ns).Watch(options)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 11:58:33 -03:00
										 |  |  | func endpointsListFunc(c *clientset_generated.Clientset, ns string, s *labels.Selector) func(api.ListOptions) (runtime.Object, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	return func(opts api.ListOptions) (runtime.Object, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		if s != nil {
 | 
					
						
							|  |  |  | 			opts.LabelSelector = *s
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		return c.Endpoints(ns).List(opts)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 11:58:33 -03:00
										 |  |  | func endpointsWatchFunc(c *clientset_generated.Clientset, ns string, s *labels.Selector) func(options api.ListOptions) (watch.Interface, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	return func(options api.ListOptions) (watch.Interface, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		if s != nil {
 | 
					
						
							|  |  |  | 			options.LabelSelector = *s
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		return c.Endpoints(ns).Watch(options)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 11:58:33 -03:00
										 |  |  | func namespaceListFunc(c *clientset_generated.Clientset, s *labels.Selector) func(api.ListOptions) (runtime.Object, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	return func(opts api.ListOptions) (runtime.Object, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		if s != nil {
 | 
					
						
							|  |  |  | 			opts.LabelSelector = *s
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		return c.Namespaces().List(opts)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-30 11:58:33 -03:00
										 |  |  | func namespaceWatchFunc(c *clientset_generated.Clientset, s *labels.Selector) func(options api.ListOptions) (watch.Interface, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	return func(options api.ListOptions) (watch.Interface, error) {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		if s != nil {
 | 
					
						
							|  |  |  | 			options.LabelSelector = *s
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		return c.Namespaces().Watch(options)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dns *dnsController) controllersInSync() bool {
 | 
					
						
							|  |  |  | 	return dns.svcController.HasSynced() && dns.endpController.HasSynced()
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Stop stops the  controller.
 | 
					
						
							|  |  |  | func (dns *dnsController) Stop() error {
 | 
					
						
							|  |  |  | 	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.
 | 
					
						
							|  |  |  | func (dns *dnsController) Run() {
 | 
					
						
							|  |  |  | 	go dns.endpController.Run(dns.stopCh)
 | 
					
						
							|  |  |  | 	go dns.svcController.Run(dns.stopCh)
 | 
					
						
							|  |  |  | 	go dns.nsController.Run(dns.stopCh)
 | 
					
						
							|  |  |  | 	<-dns.stopCh
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 12:46:35 +01:00
										 |  |  | func (dns *dnsController) NamespaceList() *api.NamespaceList {
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	nsList, err := dns.nsLister.List()
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return &api.NamespaceList{}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &nsList
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 12:46:35 +01:00
										 |  |  | func (dns *dnsController) ServiceList() []*api.Service {
 | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 	svcs, err := dns.svcLister.List(labels.Everything())
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 		return []*api.Service{}
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-09-22 08:29:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 	return svcs
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 12:46:35 +01:00
										 |  |  | // ServicesByNamespace returns a map of:
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | // namespacename :: [ kubernetesService ]
 | 
					
						
							| 
									
										
										
										
											2016-10-12 12:46:35 +01:00
										 |  |  | func (dns *dnsController) ServicesByNamespace() map[string][]api.Service {
 | 
					
						
							|  |  |  | 	k8sServiceList := dns.ServiceList()
 | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 	items := make(map[string][]api.Service, len(k8sServiceList))
 | 
					
						
							|  |  |  | 	for _, i := range k8sServiceList {
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		namespace := i.Namespace
 | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 		items[namespace] = append(items[namespace], *i)
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-09-22 08:29:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	return items
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 12:46:35 +01:00
										 |  |  | // ServiceInNamespace returns the Service that matches servicename in the namespace
 | 
					
						
							|  |  |  | func (dns *dnsController) ServiceInNamespace(namespace, servicename string) *api.Service {
 | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 	svcObj, err := dns.svcLister.Services(namespace).Get(servicename)
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		// TODO(...): should return err here
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-09-23 10:13:02 -03:00
										 |  |  | 	return svcObj
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | }
 |