| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | package kubernetes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:21:24 -05:00
										 |  |  | 	"log"
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	"sync"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 15:43:27 +00:00
										 |  |  | 	"k8s.io/client-go/1.5/kubernetes"
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  | 	"k8s.io/client-go/1.5/pkg/api"
 | 
					
						
							|  |  |  | 	"k8s.io/client-go/1.5/pkg/api/v1"
 | 
					
						
							|  |  |  | 	"k8s.io/client-go/1.5/pkg/labels"
 | 
					
						
							|  |  |  | 	"k8s.io/client-go/1.5/pkg/runtime"
 | 
					
						
							|  |  |  | 	"k8s.io/client-go/1.5/pkg/watch"
 | 
					
						
							| 
									
										
										
										
											2016-11-05 15:43:27 +00:00
										 |  |  | 	"k8s.io/client-go/1.5/tools/cache"
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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-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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 15:43:27 +00:00
										 |  |  | 	svcController *cache.Controller
 | 
					
						
							|  |  |  | 	nsController  *cache.Controller
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 15:43:27 +00:00
										 |  |  | 	svcLister cache.StoreToServiceLister
 | 
					
						
							|  |  |  | 	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-11-05 07:57:08 -04:00
										 |  |  | func newdnsController(kubeClient *kubernetes.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-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-11-05 07:57:08 -04:00
										 |  |  | func serviceListFunc(c *kubernetes.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-11-07 07:43:38 +00:00
										 |  |  | 		listV1, err := c.Core().Services(ns).List(opts)
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-11-07 07:43:38 +00:00
										 |  |  | 		var listAPI api.ServiceList
 | 
					
						
							|  |  |  | 		err = v1.Convert_v1_ServiceList_To_api_ServiceList(listV1, &listAPI, nil)
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-11-07 10:33:48 +00:00
										 |  |  | 		return &listAPI, err
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | func v1ToAPIFilter(in watch.Event) (out watch.Event, keep bool) {
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:21:24 -05:00
										 |  |  | 	if in.Type == watch.Error {
 | 
					
						
							|  |  |  | 		return in, true
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch v1Obj := in.Object.(type) {
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 	case *v1.Service:
 | 
					
						
							|  |  |  | 		var apiObj api.Service
 | 
					
						
							|  |  |  | 		err := v1.Convert_v1_Service_To_api_Service(v1Obj, &apiObj, nil)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			log.Printf("[ERROR] Could not convert v1.Service: %s", err)
 | 
					
						
							|  |  |  | 			return in, true
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return watch.Event{Type: in.Type, Object: &apiObj}, true
 | 
					
						
							|  |  |  | 	case *v1.Namespace:
 | 
					
						
							|  |  |  | 		var apiObj api.Namespace
 | 
					
						
							|  |  |  | 		err := v1.Convert_v1_Namespace_To_api_Namespace(v1Obj, &apiObj, nil)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			log.Printf("[ERROR] Could not convert v1.Namespace: %s", err)
 | 
					
						
							|  |  |  | 			return in, true
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return watch.Event{Type: in.Type, Object: &apiObj}, true
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:21:24 -05:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	log.Printf("[WARN] Unhandled v1 type in event: %v", in)
 | 
					
						
							|  |  |  | 	return in, true
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  | func serviceWatchFunc(c *kubernetes.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-11-07 11:21:24 -05:00
										 |  |  | 		w, err := c.Core().Services(ns).Watch(options)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 		return watch.Filter(w, v1ToAPIFilter), nil
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  | func namespaceListFunc(c *kubernetes.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-11-07 07:43:38 +00:00
										 |  |  | 		listV1, err := c.Core().Namespaces().List(opts)
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-11-07 07:43:38 +00:00
										 |  |  | 		var listAPI api.NamespaceList
 | 
					
						
							|  |  |  | 		err = v1.Convert_v1_NamespaceList_To_api_NamespaceList(listV1, &listAPI, nil)
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-11-07 10:33:48 +00:00
										 |  |  | 		return &listAPI, err
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  | func namespaceWatchFunc(c *kubernetes.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-11-07 11:21:24 -05:00
										 |  |  | 		w, err := c.Core().Namespaces().Watch(options)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return nil, err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 		return watch.Filter(w, v1ToAPIFilter), nil
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dns *dnsController) controllersInSync() bool {
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  | 	return dns.svcController.HasSynced()
 | 
					
						
							| 
									
										
										
										
											2016-08-05 18:19:51 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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.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
										 |  |  | }
 |