diff options
Diffstat (limited to 'plugin')
-rw-r--r-- | plugin/federation/kubernetes_api_test.go | 66 | ||||
-rw-r--r-- | plugin/kubernetes/autopath.go | 16 | ||||
-rw-r--r-- | plugin/kubernetes/controller.go | 290 | ||||
-rw-r--r-- | plugin/kubernetes/handler_test.go | 150 | ||||
-rw-r--r-- | plugin/kubernetes/kubernetes.go | 41 | ||||
-rw-r--r-- | plugin/kubernetes/kubernetes_apex_test.go | 2 | ||||
-rw-r--r-- | plugin/kubernetes/kubernetes_test.go | 146 | ||||
-rw-r--r-- | plugin/kubernetes/local.go | 7 | ||||
-rw-r--r-- | plugin/kubernetes/ns.go | 10 | ||||
-rw-r--r-- | plugin/kubernetes/ns_test.go | 58 | ||||
-rw-r--r-- | plugin/kubernetes/reverse.go | 7 | ||||
-rw-r--r-- | plugin/kubernetes/reverse_test.go | 58 | ||||
-rw-r--r-- | plugin/kubernetes/setup.go | 6 | ||||
-rw-r--r-- | plugin/kubernetes/setup_test.go | 4 |
14 files changed, 360 insertions, 501 deletions
diff --git a/plugin/federation/kubernetes_api_test.go b/plugin/federation/kubernetes_api_test.go index 48a03666e..3900ae0b8 100644 --- a/plugin/federation/kubernetes_api_test.go +++ b/plugin/federation/kubernetes_api_test.go @@ -3,7 +3,8 @@ package federation import ( "github.com/coredns/coredns/plugin/kubernetes" - "k8s.io/client-go/1.5/pkg/api" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" + api "k8s.io/client-go/pkg/api/v1" ) type APIConnFederationTest struct{} @@ -11,23 +12,22 @@ type APIConnFederationTest struct{} func (APIConnFederationTest) Run() { return } func (APIConnFederationTest) Stop() error { return nil } -func (APIConnFederationTest) PodIndex(string) []interface{} { - a := make([]interface{}, 1) - a[0] = &api.Pod{ - ObjectMeta: api.ObjectMeta{ +func (APIConnFederationTest) PodIndex(string) []*api.Pod { + a := []*api.Pod{{ + ObjectMeta: meta.ObjectMeta{ Namespace: "podns", }, Status: api.PodStatus{ PodIP: "10.240.0.1", // Remote IP set in test.ResponseWriter }, - } + }} return a } func (APIConnFederationTest) ServiceList() []*api.Service { svcs := []*api.Service{ { - ObjectMeta: api.ObjectMeta{ + ObjectMeta: meta.ObjectMeta{ Name: "svc1", Namespace: "testns", }, @@ -41,7 +41,7 @@ func (APIConnFederationTest) ServiceList() []*api.Service { }, }, { - ObjectMeta: api.ObjectMeta{ + ObjectMeta: meta.ObjectMeta{ Name: "hdls1", Namespace: "testns", }, @@ -50,7 +50,7 @@ func (APIConnFederationTest) ServiceList() []*api.Service { }, }, { - ObjectMeta: api.ObjectMeta{ + ObjectMeta: meta.ObjectMeta{ Name: "external", Namespace: "testns", }, @@ -65,42 +65,40 @@ func (APIConnFederationTest) ServiceList() []*api.Service { }, } return svcs - } -func (APIConnFederationTest) EndpointsList() api.EndpointsList { - return api.EndpointsList{ - Items: []api.Endpoints{ - { - Subsets: []api.EndpointSubset{ - { - Addresses: []api.EndpointAddress{ - { - IP: "172.0.0.1", - Hostname: "ep1a", - }, +func (APIConnFederationTest) EndpointsList() []*api.Endpoints { + eps := []*api.Endpoints{ + { + Subsets: []api.EndpointSubset{ + { + Addresses: []api.EndpointAddress{ + { + IP: "172.0.0.1", + Hostname: "ep1a", }, - Ports: []api.EndpointPort{ - { - Port: 80, - Protocol: "tcp", - Name: "http", - }, + }, + Ports: []api.EndpointPort{ + { + Port: 80, + Protocol: "tcp", + Name: "http", }, }, }, - ObjectMeta: api.ObjectMeta{ - Name: "svc1", - Namespace: "testns", - }, + }, + ObjectMeta: meta.ObjectMeta{ + Name: "svc1", + Namespace: "testns", }, }, } + return eps } -func (APIConnFederationTest) GetNodeByName(name string) (api.Node, error) { - return api.Node{ - ObjectMeta: api.ObjectMeta{ +func (APIConnFederationTest) GetNodeByName(name string) (*api.Node, error) { + return &api.Node{ + ObjectMeta: meta.ObjectMeta{ Name: "test.node.foo.bar", Labels: map[string]string{ kubernetes.LabelRegion: "fd-r", diff --git a/plugin/kubernetes/autopath.go b/plugin/kubernetes/autopath.go index f758869f1..feab430a9 100644 --- a/plugin/kubernetes/autopath.go +++ b/plugin/kubernetes/autopath.go @@ -4,7 +4,7 @@ import ( "github.com/coredns/coredns/plugin" "github.com/coredns/coredns/request" - "k8s.io/client-go/1.5/pkg/api" + api "k8s.io/client-go/pkg/api/v1" ) // AutoPath implements the AutoPathFunc call from the autopath plugin. @@ -40,14 +40,10 @@ func (k *Kubernetes) AutoPath(state request.Request) []string { } // podWithIP return the api.Pod for source IP ip. It returns nil if nothing can be found. -func (k *Kubernetes) podWithIP(ip string) (p *api.Pod) { - objList := k.APIConn.PodIndex(ip) - for _, o := range objList { - p, ok := o.(*api.Pod) - if !ok { - return nil - } - return p +func (k *Kubernetes) podWithIP(ip string) *api.Pod { + ps := k.APIConn.PodIndex(ip) + if len(ps) == 0 { + return nil } - return nil + return ps[0] } diff --git a/plugin/kubernetes/controller.go b/plugin/kubernetes/controller.go index fa2e749bb..a5b697c88 100644 --- a/plugin/kubernetes/controller.go +++ b/plugin/kubernetes/controller.go @@ -3,45 +3,31 @@ package kubernetes import ( "errors" "fmt" - "log" "sync" "time" - "k8s.io/client-go/1.5/kubernetes" - "k8s.io/client-go/1.5/pkg/api" - unversionedapi "k8s.io/client-go/1.5/pkg/api/unversioned" - "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" - "k8s.io/client-go/1.5/tools/cache" + "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" ) var ( namespace = api.NamespaceAll ) -// storeToNamespaceLister makes a Store that lists Namespaces. -type storeToNamespaceLister struct { - cache.Store -} - const podIPIndex = "PodIP" -// 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 -} - type dnsController interface { ServiceList() []*api.Service - PodIndex(string) []interface{} - EndpointsList() api.EndpointsList + PodIndex(string) []*api.Pod + EndpointsList() []*api.Endpoints - GetNodeByName(string) (api.Node, error) + GetNodeByName(string) (*api.Node, error) Run() Stop() error @@ -52,15 +38,13 @@ type dnsControl struct { selector *labels.Selector - svcController *cache.Controller - podController *cache.Controller - nsController *cache.Controller - epController *cache.Controller + svcController cache.Controller + podController cache.Controller + epController cache.Controller - svcLister cache.StoreToServiceLister - podLister cache.StoreToPodLister - nsLister storeToNamespaceLister - epLister cache.StoreToEndpointsLister + svcLister cache.Indexer + podLister cache.Indexer + epLister cache.Store // stopLock is used to enforce only a single call to Stop is active. // Needed because we allow stopping through an http endpoint and @@ -74,7 +58,7 @@ type dnsControlOpts struct { initPodCache bool resyncPeriod time.Duration // Label handling. - labelSelector *unversionedapi.LabelSelector + labelSelector *meta.LabelSelector selector *labels.Selector } @@ -85,8 +69,7 @@ func newdnsController(kubeClient *kubernetes.Clientset, opts dnsControlOpts) *dn selector: opts.selector, stopCh: make(chan struct{}), } - - dns.svcLister.Indexer, dns.svcController = cache.NewIndexerInformer( + dns.svcLister, dns.svcController = cache.NewIndexerInformer( &cache.ListWatch{ ListFunc: serviceListFunc(dns.client, namespace, dns.selector), WatchFunc: serviceWatchFunc(dns.client, namespace, dns.selector), @@ -97,7 +80,7 @@ func newdnsController(kubeClient *kubernetes.Clientset, opts dnsControlOpts) *dn cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) if opts.initPodCache { - dns.podLister.Indexer, dns.podController = cache.NewIndexerInformer( + dns.podLister, dns.podController = cache.NewIndexerInformer( &cache.ListWatch{ ListFunc: podListFunc(dns.client, namespace, dns.selector), WatchFunc: podWatchFunc(dns.client, namespace, dns.selector), @@ -107,17 +90,7 @@ func newdnsController(kubeClient *kubernetes.Clientset, opts dnsControlOpts) *dn cache.ResourceEventHandlerFuncs{}, cache.Indexers{podIPIndex: podIPIndexFunc}) } - - dns.nsLister.Store, dns.nsController = cache.NewInformer( - &cache.ListWatch{ - ListFunc: namespaceListFunc(dns.client, dns.selector), - WatchFunc: namespaceWatchFunc(dns.client, dns.selector), - }, - &api.Namespace{}, - opts.resyncPeriod, - cache.ResourceEventHandlerFuncs{}) - - dns.epLister.Store, dns.epController = cache.NewInformer( + dns.epLister, dns.epController = cache.NewInformer( &cache.ListWatch{ ListFunc: endpointsListFunc(dns.client, namespace, dns.selector), WatchFunc: endpointsWatchFunc(dns.client, namespace, dns.selector), @@ -137,182 +110,86 @@ func podIPIndexFunc(obj interface{}) ([]string, error) { return []string{p.Status.PodIP}, nil } -func serviceListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(api.ListOptions) (runtime.Object, error) { - return func(opts api.ListOptions) (runtime.Object, error) { - if s != nil { - opts.LabelSelector = *s - } - listV1, err := c.Core().Services(ns).List(opts) - - if err != nil { - return nil, err - } - var listAPI api.ServiceList - err = v1.Convert_v1_ServiceList_To_api_ServiceList(listV1, &listAPI, nil) - if err != nil { - return nil, err - } - return &listAPI, err - } -} - -func podListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(api.ListOptions) (runtime.Object, error) { - return func(opts api.ListOptions) (runtime.Object, error) { - if s != nil { - opts.LabelSelector = *s - } - listV1, err := c.Core().Pods(ns).List(opts) - - if err != nil { - return nil, err - } - var listAPI api.PodList - err = v1.Convert_v1_PodList_To_api_PodList(listV1, &listAPI, nil) - if err != nil { - return nil, err - } - - return &listAPI, err - } -} - -func v1ToAPIFilter(in watch.Event) (out watch.Event, keep bool) { - if in.Type == watch.Error { - return in, true - } - - switch v1Obj := in.Object.(type) { - 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.Pod: - var apiObj api.Pod - err := v1.Convert_v1_Pod_To_api_Pod(v1Obj, &apiObj, nil) - if err != nil { - log.Printf("[ERROR] Could not convert v1.Pod: %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 - case *v1.Endpoints: - var apiObj api.Endpoints - err := v1.Convert_v1_Endpoints_To_api_Endpoints(v1Obj, &apiObj, nil) - if err != nil { - log.Printf("[ERROR] Could not convert v1.Endpoint: %s", err) - return in, true - } - return watch.Event{Type: in.Type, Object: &apiObj}, true - } - - log.Printf("[WARN] Unhandled v1 type in event: %v", in) - return in, true -} - -func serviceWatchFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(options api.ListOptions) (watch.Interface, error) { - return func(options api.ListOptions) (watch.Interface, error) { +func serviceListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(meta.ListOptions) (runtime.Object, error) { + return func(opts meta.ListOptions) (runtime.Object, error) { if s != nil { - options.LabelSelector = *s + opts.LabelSelector = (*s).String() } - w, err := c.Core().Services(ns).Watch(options) + listV1, err := c.Services(ns).List(opts) if err != nil { return nil, err } - return watch.Filter(w, v1ToAPIFilter), nil + return listV1, err } } -func podWatchFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(options api.ListOptions) (watch.Interface, error) { - return func(options api.ListOptions) (watch.Interface, error) { +func podListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(meta.ListOptions) (runtime.Object, error) { + return func(opts meta.ListOptions) (runtime.Object, error) { if s != nil { - options.LabelSelector = *s + opts.LabelSelector = (*s).String() } - w, err := c.Core().Pods(ns).Watch(options) - + listV1, err := c.Pods(ns).List(opts) if err != nil { return nil, err } - return watch.Filter(w, v1ToAPIFilter), nil + return listV1, err } } -func namespaceListFunc(c *kubernetes.Clientset, s *labels.Selector) func(api.ListOptions) (runtime.Object, error) { - return func(opts api.ListOptions) (runtime.Object, error) { +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) { if s != nil { - opts.LabelSelector = *s + options.LabelSelector = (*s).String() } - listV1, err := c.Core().Namespaces().List(opts) + w, err := c.Services(ns).Watch(options) if err != nil { return nil, err } - var listAPI api.NamespaceList - err = v1.Convert_v1_NamespaceList_To_api_NamespaceList(listV1, &listAPI, nil) - if err != nil { - return nil, err - } - return &listAPI, err + return w, nil } } -func namespaceWatchFunc(c *kubernetes.Clientset, s *labels.Selector) func(options api.ListOptions) (watch.Interface, error) { - return func(options api.ListOptions) (watch.Interface, error) { +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) { if s != nil { - options.LabelSelector = *s + options.LabelSelector = (*s).String() } - w, err := c.Core().Namespaces().Watch(options) + w, err := c.Pods(ns).Watch(options) if err != nil { return nil, err } - return watch.Filter(w, v1ToAPIFilter), nil + return w, nil } } -func endpointsListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(api.ListOptions) (runtime.Object, error) { - return func(opts api.ListOptions) (runtime.Object, error) { +func endpointsListFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(meta.ListOptions) (runtime.Object, error) { + return func(opts meta.ListOptions) (runtime.Object, error) { if s != nil { - opts.LabelSelector = *s - } - listV1, err := c.Core().Endpoints(ns).List(opts) - - if err != nil { - return nil, err + opts.LabelSelector = (*s).String() } - var listAPI api.EndpointsList - err = v1.Convert_v1_EndpointsList_To_api_EndpointsList(listV1, &listAPI, nil) + listV1, err := c.Endpoints(ns).List(opts) if err != nil { return nil, err } - return &listAPI, err + return listV1, err } } -func endpointsWatchFunc(c *kubernetes.Clientset, ns string, s *labels.Selector) func(options api.ListOptions) (watch.Interface, error) { - return func(options api.ListOptions) (watch.Interface, error) { +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) { if s != nil { - options.LabelSelector = *s + options.LabelSelector = (*s).String() } - w, err := c.Core().Endpoints(ns).Watch(options) + w, err := c.Endpoints(ns).Watch(options) if err != nil { return nil, err } - return watch.Filter(w, v1ToAPIFilter), nil + return w, nil } } func (dns *dnsControl) controllersInSync() bool { hs := dns.svcController.HasSynced() && - dns.nsController.HasSynced() && dns.epController.HasSynced() if dns.podController != nil { @@ -341,7 +218,6 @@ func (dns *dnsControl) Stop() error { // Run starts the controller. func (dns *dnsControl) Run() { go dns.svcController.Run(dns.stopCh) - go dns.nsController.Run(dns.stopCh) go dns.epController.Run(dns.stopCh) if dns.podController != nil { go dns.podController.Run(dns.stopCh) @@ -349,54 +225,52 @@ func (dns *dnsControl) Run() { <-dns.stopCh } -func (dns *dnsControl) NamespaceList() *api.NamespaceList { - nsList, err := dns.nsLister.List() - if err != nil { - return &api.NamespaceList{} - } - - return &nsList -} - -func (dns *dnsControl) ServiceList() []*api.Service { - svcs, err := dns.svcLister.List(labels.Everything()) - if err != nil { - return []*api.Service{} +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) } - return svcs } -func (dns *dnsControl) PodIndex(ip string) []interface{} { - if dns.podLister.Indexer == nil { +func (dns *dnsControl) PodIndex(ip string) (pods []*api.Pod) { + if dns.podLister == nil { return nil } - pods, err := dns.podLister.Indexer.ByIndex(podIPIndex, ip) + os, err := dns.podLister.ByIndex(podIPIndex, ip) if err != nil { return nil } - + for _, o := range os { + p, ok := o.(*api.Pod) + if !ok { + continue + } + pods = append(pods, p) + } return pods } -func (dns *dnsControl) EndpointsList() api.EndpointsList { - epl, err := dns.epLister.List() - if err != nil { - return api.EndpointsList{} +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) } - - return epl + return eps } -func (dns *dnsControl) GetNodeByName(name string) (api.Node, error) { - v1node, err := dns.client.Core().Nodes().Get(name) - if err != nil { - return api.Node{}, err - } - var apinode api.Node - err = v1.Convert_v1_Node_To_api_Node(v1node, &apinode, nil) +func (dns *dnsControl) GetNodeByName(name string) (*api.Node, error) { + v1node, err := dns.client.Nodes().Get(name, meta.GetOptions{}) if err != nil { - return api.Node{}, err + return &api.Node{}, err } - return apinode, nil + return v1node, nil } diff --git a/plugin/kubernetes/handler_test.go b/plugin/kubernetes/handler_test.go index 1dd7ea621..388428e3a 100644 --- a/plugin/kubernetes/handler_test.go +++ b/plugin/kubernetes/handler_test.go @@ -8,7 +8,8 @@ import ( "github.com/miekg/dns" "golang.org/x/net/context" - "k8s.io/client-go/1.5/pkg/api" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" + api "k8s.io/client-go/pkg/api/v1" ) var dnsTestCases = []test.Case{ @@ -193,23 +194,22 @@ type APIConnServeTest struct{} func (APIConnServeTest) Run() { return } func (APIConnServeTest) Stop() error { return nil } -func (APIConnServeTest) PodIndex(string) []interface{} { - a := make([]interface{}, 1) - a[0] = &api.Pod{ - ObjectMeta: api.ObjectMeta{ +func (APIConnServeTest) PodIndex(string) []*api.Pod { + a := []*api.Pod{{ + ObjectMeta: meta.ObjectMeta{ Namespace: "podns", }, Status: api.PodStatus{ PodIP: "10.240.0.1", // Remote IP set in test.ResponseWriter }, - } + }} return a } func (APIConnServeTest) ServiceList() []*api.Service { svcs := []*api.Service{ { - ObjectMeta: api.ObjectMeta{ + ObjectMeta: meta.ObjectMeta{ Name: "svc1", Namespace: "testns", }, @@ -223,7 +223,7 @@ func (APIConnServeTest) ServiceList() []*api.Service { }, }, { - ObjectMeta: api.ObjectMeta{ + ObjectMeta: meta.ObjectMeta{ Name: "hdls1", Namespace: "testns", }, @@ -232,7 +232,7 @@ func (APIConnServeTest) ServiceList() []*api.Service { }, }, { - ObjectMeta: api.ObjectMeta{ + ObjectMeta: meta.ObjectMeta{ Name: "external", Namespace: "testns", }, @@ -247,100 +247,98 @@ func (APIConnServeTest) ServiceList() []*api.Service { }, } return svcs - } -func (APIConnServeTest) EndpointsList() api.EndpointsList { +func (APIConnServeTest) EndpointsList() []*api.Endpoints { n := "test.node.foo.bar" - return api.EndpointsList{ - Items: []api.Endpoints{ - { - Subsets: []api.EndpointSubset{ - { - Addresses: []api.EndpointAddress{ - { - IP: "172.0.0.1", - Hostname: "ep1a", - }, + eps := []*api.Endpoints{ + { + Subsets: []api.EndpointSubset{ + { + Addresses: []api.EndpointAddress{ + { + IP: "172.0.0.1", + Hostname: "ep1a", }, - Ports: []api.EndpointPort{ - { - Port: 80, - Protocol: "tcp", - Name: "http", - }, + }, + Ports: []api.EndpointPort{ + { + Port: 80, + Protocol: "tcp", + Name: "http", }, }, }, - ObjectMeta: api.ObjectMeta{ - Name: "svc1", - Namespace: "testns", - }, }, - { - Subsets: []api.EndpointSubset{ - { - Addresses: []api.EndpointAddress{ - { - IP: "172.0.0.2", - }, + ObjectMeta: meta.ObjectMeta{ + Name: "svc1", + Namespace: "testns", + }, + }, + { + Subsets: []api.EndpointSubset{ + { + Addresses: []api.EndpointAddress{ + { + IP: "172.0.0.2", }, - Ports: []api.EndpointPort{ - { - Port: 80, - Protocol: "tcp", - Name: "http", - }, + }, + Ports: []api.EndpointPort{ + { + Port: 80, + Protocol: "tcp", + Name: "http", }, }, }, - ObjectMeta: api.ObjectMeta{ - Name: "hdls1", - Namespace: "testns", - }, }, - { - Subsets: []api.EndpointSubset{ - { - Addresses: []api.EndpointAddress{ - { - IP: "172.0.0.3", - }, + ObjectMeta: meta.ObjectMeta{ + Name: "hdls1", + Namespace: "testns", + }, + }, + { + Subsets: []api.EndpointSubset{ + { + Addresses: []api.EndpointAddress{ + { + IP: "172.0.0.3", }, - Ports: []api.EndpointPort{ - { - Port: 80, - Protocol: "tcp", - Name: "http", - }, + }, + Ports: []api.EndpointPort{ + { + Port: 80, + Protocol: "tcp", + Name: "http", }, }, }, - ObjectMeta: api.ObjectMeta{ - Name: "hdls1", - Namespace: "testns", - }, }, - { - Subsets: []api.EndpointSubset{ - { - Addresses: []api.EndpointAddress{ - { - IP: "10.9.8.7", - NodeName: &n, - }, + ObjectMeta: meta.ObjectMeta{ + Name: "hdls1", + Namespace: "testns", + }, + }, + { + Subsets: []api.EndpointSubset{ + { + Addresses: []api.EndpointAddress{ + { + IP: "10.9.8.7", + NodeName: &n, }, }, }, }, }, } + return eps } -func (APIConnServeTest) GetNodeByName(name string) (api.Node, error) { - return api.Node{ - ObjectMeta: api.ObjectMeta{ +func (APIConnServeTest) GetNodeByName(name string) (*api.Node, error) { + return &api.Node{ + ObjectMeta: meta.ObjectMeta{ Name: "test.node.foo.bar", }, }, nil diff --git a/plugin/kubernetes/kubernetes.go b/plugin/kubernetes/kubernetes.go index afc48d0e0..211d2573c 100644 --- a/plugin/kubernetes/kubernetes.go +++ b/plugin/kubernetes/kubernetes.go @@ -17,13 +17,13 @@ import ( "github.com/coredns/coredns/request" "github.com/miekg/dns" - "k8s.io/client-go/1.5/kubernetes" - "k8s.io/client-go/1.5/pkg/api" - unversionedapi "k8s.io/client-go/1.5/pkg/api/unversioned" - "k8s.io/client-go/1.5/pkg/labels" - "k8s.io/client-go/1.5/rest" - "k8s.io/client-go/1.5/tools/clientcmd" - clientcmdapi "k8s.io/client-go/1.5/tools/clientcmd/api" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/client-go/kubernetes" + api "k8s.io/client-go/pkg/api/v1" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" ) // Kubernetes implements a plugin that connects to a Kubernetes cluster. @@ -76,7 +76,6 @@ var ( errNoItems = errors.New("no items found") errNsNotExposed = errors.New("namespace is not exposed") errInvalidRequest = errors.New("invalid query name") - errAPIBadPodType = errors.New("expected type *api.Pod") errPodsDisabled = errors.New("pod records disabled") ) @@ -152,14 +151,17 @@ func (k *Kubernetes) getClientConfig() (*rest.Config, error) { clusterinfo := clientcmdapi.Cluster{} authinfo := clientcmdapi.AuthInfo{} + // Connect to API from in cluster if len(k.APIServerList) == 0 { cc, err := rest.InClusterConfig() if err != nil { return nil, err } + cc.ContentType = "application/vnd.kubernetes.protobuf" return cc, err } + // Connect to API from out of cluster endpoint := k.APIServerList[0] if len(k.APIServerList) > 1 { // Use a random port for api proxy, will get the value later through listener.Addr() @@ -230,7 +232,10 @@ func (k *Kubernetes) getClientConfig() (*rest.Config, error) { overrides.AuthInfo = authinfo clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides) - return clientConfig.ClientConfig() + cc, err := clientConfig.ClientConfig() + cc.ContentType = "application/vnd.kubernetes.protobuf" + return cc, err + } // initKubeCache initializes a new Kubernetes cache. @@ -248,7 +253,7 @@ func (k *Kubernetes) initKubeCache(opts dnsControlOpts) (err error) { if opts.labelSelector != nil { var selector labels.Selector - selector, err = unversionedapi.LabelSelectorAsSelector(opts.labelSelector) + selector, err = meta.LabelSelectorAsSelector(opts.labelSelector) if err != nil { return fmt.Errorf("unable to create Selector for LabelSelector '%s': %q", opts.labelSelector, err) } @@ -317,13 +322,7 @@ func (k *Kubernetes) findPods(r recordRequest, zone string) (pods []msg.Service, } // PodModeVerified - objList := k.APIConn.PodIndex(ip) - - for _, o := range objList { - p, ok := o.(*api.Pod) - if !ok { - return nil, errAPIBadPodType - } + for _, p := range k.APIConn.PodIndex(ip) { // If namespace has a wildcard, filter results against Corefile namespace list. if wildcard(namespace) && !k.namespaceExposed(p.Namespace) { continue @@ -341,11 +340,11 @@ func (k *Kubernetes) findPods(r recordRequest, zone string) (pods []msg.Service, // findServices returns the services matching r from the cache. func (k *Kubernetes) findServices(r recordRequest, zone string) (services []msg.Service, err error) { - serviceList := k.APIConn.ServiceList() zonePath := msg.Path(zone, "coredns") err = errNoItems // Set to errNoItems to signal really nothing found, gets reset when name is matched. - for _, svc := range serviceList { + for _, svc := range k.APIConn.ServiceList() { + if !(match(r.namespace, svc.Namespace) && match(r.service, svc.Name)) { continue } @@ -358,8 +357,8 @@ func (k *Kubernetes) findServices(r recordRequest, zone string) (services []msg. // Endpoint query or headless service if svc.Spec.ClusterIP == api.ClusterIPNone || r.endpoint != "" { - endpointsList := k.APIConn.EndpointsList() - for _, ep := range endpointsList.Items { + + for _, ep := range k.APIConn.EndpointsList() { if ep.ObjectMeta.Name != svc.Name || ep.ObjectMeta.Namespace != svc.Namespace { continue } diff --git a/plugin/kubernetes/kubernetes_apex_test.go b/plugin/kubernetes/kubernetes_apex_test.go index ce9cf59d4..b292b90d5 100644 --- a/plugin/kubernetes/kubernetes_apex_test.go +++ b/plugin/kubernetes/kubernetes_apex_test.go @@ -10,7 +10,7 @@ import ( "golang.org/x/net/context" ) -var kubeApexCases = [](test.Case){ +var kubeApexCases = []test.Case{ { Qname: "cluster.local.", Qtype: dns.TypeSOA, Rcode: dns.RcodeSuccess, diff --git a/plugin/kubernetes/kubernetes_test.go b/plugin/kubernetes/kubernetes_test.go index f347f10fc..4a3bb3d07 100644 --- a/plugin/kubernetes/kubernetes_test.go +++ b/plugin/kubernetes/kubernetes_test.go @@ -7,7 +7,8 @@ import ( "github.com/coredns/coredns/request" "github.com/miekg/dns" - "k8s.io/client-go/1.5/pkg/api" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" + api "k8s.io/client-go/pkg/api/v1" ) func TestWildcard(t *testing.T) { @@ -50,14 +51,14 @@ func TestEndpointHostname(t *testing.T) { type APIConnServiceTest struct{} -func (APIConnServiceTest) Run() { return } -func (APIConnServiceTest) Stop() error { return nil } -func (APIConnServiceTest) PodIndex(string) []interface{} { return nil } +func (APIConnServiceTest) Run() { return } +func (APIConnServiceTest) Stop() error { return nil } +func (APIConnServiceTest) PodIndex(string) []*api.Pod { return nil } func (APIConnServiceTest) ServiceList() []*api.Service { svcs := []*api.Service{ { - ObjectMeta: api.ObjectMeta{ + ObjectMeta: meta.ObjectMeta{ Name: "svc1", Namespace: "testns", }, @@ -71,7 +72,7 @@ func (APIConnServiceTest) ServiceList() []*api.Service { }, }, { - ObjectMeta: api.ObjectMeta{ + ObjectMeta: meta.ObjectMeta{ Name: "hdls1", Namespace: "testns", }, @@ -80,7 +81,7 @@ func (APIConnServiceTest) ServiceList() []*api.Service { }, }, { - ObjectMeta: api.ObjectMeta{ + ObjectMeta: meta.ObjectMeta{ Name: "external", Namespace: "testns", }, @@ -97,97 +98,96 @@ func (APIConnServiceTest) ServiceList() []*api.Service { return svcs } -func (APIConnServiceTest) EndpointsList() api.EndpointsList { +func (APIConnServiceTest) EndpointsList() []*api.Endpoints { n := "test.node.foo.bar" - return api.EndpointsList{ - Items: []api.Endpoints{ - { - Subsets: []api.EndpointSubset{ - { - Addresses: []api.EndpointAddress{ - { - IP: "172.0.0.1", - Hostname: "ep1a", - }, + eps := []*api.Endpoints{ + { + Subsets: []api.EndpointSubset{ + { + Addresses: []api.EndpointAddress{ + { + IP: "172.0.0.1", + Hostname: "ep1a", }, - Ports: []api.EndpointPort{ - { - Port: 80, - Protocol: "tcp", - Name: "http", - }, + }, + Ports: []api.EndpointPort{ + { + Port: 80, + Protocol: "tcp", + Name: "http", }, }, }, - ObjectMeta: api.ObjectMeta{ - Name: "svc1", - Namespace: "testns", - }, }, - { - Subsets: []api.EndpointSubset{ - { - Addresses: []api.EndpointAddress{ - { - IP: "172.0.0.2", - }, + ObjectMeta: meta.ObjectMeta{ + Name: "svc1", + Namespace: "testns", + }, + }, + { + Subsets: []api.EndpointSubset{ + { + Addresses: []api.EndpointAddress{ + { + IP: "172.0.0.2", }, - Ports: []api.EndpointPort{ - { - Port: 80, - Protocol: "tcp", - Name: "http", - }, + }, + Ports: []api.EndpointPort{ + { + Port: 80, + Protocol: "tcp", + Name: "http", }, }, }, - ObjectMeta: api.ObjectMeta{ - Name: "hdls1", - Namespace: "testns", - }, }, - { - Subsets: []api.EndpointSubset{ - { - Addresses: []api.EndpointAddress{ - { - IP: "172.0.0.3", - }, + ObjectMeta: meta.ObjectMeta{ + Name: "hdls1", + Namespace: "testns", + }, + }, + { + Subsets: []api.EndpointSubset{ + { + Addresses: []api.EndpointAddress{ + { + IP: "172.0.0.3", }, - Ports: []api.EndpointPort{ - { - Port: 80, - Protocol: "tcp", - Name: "http", - }, + }, + Ports: []api.EndpointPort{ + { + Port: 80, + Protocol: "tcp", + Name: "http", }, }, }, - ObjectMeta: api.ObjectMeta{ - Name: "hdls1", - Namespace: "testns", - }, }, - { - Subsets: []api.EndpointSubset{ - { - Addresses: []api.EndpointAddress{ - { - IP: "10.9.8.7", - NodeName: &n, - }, + ObjectMeta: meta.ObjectMeta{ + Name: "hdls1", + Namespace: "testns", + }, + }, + { + Subsets: []api.EndpointSubset{ + { + Addresses: []api.EndpointAddress{ + { + IP: "10.9.8.7", + NodeName: &n, }, }, }, }, }, } + return eps } -func (APIConnServiceTest) GetNodeByName(name string) (api.Node, error) { - return api.Node{ - ObjectMeta: api.ObjectMeta{ +func (APIConnServiceTest) GetNodeByName(name string) (*api.Node, error) { + return &api.Node{ + ObjectMeta: meta.ObjectMeta{ Name: "test.node.foo.bar", }, }, nil diff --git a/plugin/kubernetes/local.go b/plugin/kubernetes/local.go index e5b7f1e0f..ef3651a91 100644 --- a/plugin/kubernetes/local.go +++ b/plugin/kubernetes/local.go @@ -1,6 +1,8 @@ package kubernetes -import "net" +import ( + "net" +) func localPodIP() net.IP { addrs, err := net.InterfaceAddrs() @@ -26,8 +28,7 @@ func (k *Kubernetes) localNodeName() string { } // Find endpoint matching localIP - endpointsList := k.APIConn.EndpointsList() - for _, ep := range endpointsList.Items { + for _, ep := range k.APIConn.EndpointsList() { for _, eps := range ep.Subsets { for _, addr := range eps.Addresses { if localIP.Equal(net.ParseIP(addr.IP)) { diff --git a/plugin/kubernetes/ns.go b/plugin/kubernetes/ns.go index 4cacc382f..c4b05d5b5 100644 --- a/plugin/kubernetes/ns.go +++ b/plugin/kubernetes/ns.go @@ -5,7 +5,7 @@ import ( "strings" "github.com/miekg/dns" - "k8s.io/client-go/1.5/pkg/api" + api "k8s.io/client-go/pkg/api/v1" ) func isDefaultNS(name, zone string) bool { @@ -20,12 +20,10 @@ func (k *Kubernetes) nsAddr() *dns.A { rr := new(dns.A) localIP := k.interfaceAddrsFunc() - endpointsList := k.APIConn.EndpointsList() - rr.A = localIP FindEndpoint: - for _, ep := range endpointsList.Items { + for _, ep := range k.APIConn.EndpointsList() { for _, eps := range ep.Subsets { for _, addr := range eps.Addresses { if localIP.Equal(net.ParseIP(addr.IP)) { @@ -42,11 +40,9 @@ FindEndpoint: rr.A = localIP return rr } - // Find service to get ClusterIP - serviceList := k.APIConn.ServiceList() FindService: - for _, svc := range serviceList { + for _, svc := range k.APIConn.ServiceList() { if svcName == svc.Name && svcNamespace == svc.Namespace { if svc.Spec.ClusterIP == api.ClusterIPNone { rr.A = localIP diff --git a/plugin/kubernetes/ns_test.go b/plugin/kubernetes/ns_test.go index 8e9e80c71..20dfef1ec 100644 --- a/plugin/kubernetes/ns_test.go +++ b/plugin/kubernetes/ns_test.go @@ -3,53 +3,53 @@ package kubernetes import ( "testing" - "k8s.io/client-go/1.5/pkg/api" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" + api "k8s.io/client-go/pkg/api/v1" ) type APIConnTest struct{} -func (APIConnTest) Run() { return } -func (APIConnTest) Stop() error { return nil } -func (APIConnTest) PodIndex(string) []interface{} { return nil } +func (APIConnTest) Run() { return } +func (APIConnTest) Stop() error { return nil } +func (APIConnTest) PodIndex(string) []*api.Pod { return nil } func (APIConnTest) ServiceList() []*api.Service { - svc := api.Service{ - ObjectMeta: api.ObjectMeta{ - Name: "dns-service", - Namespace: "kube-system", - }, - Spec: api.ServiceSpec{ - ClusterIP: "10.0.0.111", + svcs := []*api.Service{ + { + ObjectMeta: meta.ObjectMeta{ + Name: "dns-service", + Namespace: "kube-system", + }, + Spec: api.ServiceSpec{ + ClusterIP: "10.0.0.111", + }, }, } - - return []*api.Service{&svc} - + return svcs } -func (APIConnTest) EndpointsList() api.EndpointsList { - return api.EndpointsList{ - Items: []api.Endpoints{ - { - Subsets: []api.EndpointSubset{ - { - Addresses: []api.EndpointAddress{ - { - IP: "127.0.0.1", - }, +func (APIConnTest) EndpointsList() []*api.Endpoints { + eps := []*api.Endpoints{ + { + Subsets: []api.EndpointSubset{ + { + Addresses: []api.EndpointAddress{ + { + IP: "127.0.0.1", }, }, }, - ObjectMeta: api.ObjectMeta{ - Name: "dns-service", - Namespace: "kube-system", - }, + }, + ObjectMeta: meta.ObjectMeta{ + Name: "dns-service", + Namespace: "kube-system", }, }, } + 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 } func TestNsAddr(t *testing.T) { diff --git a/plugin/kubernetes/reverse.go b/plugin/kubernetes/reverse.go index 0143b721a..84baae964 100644 --- a/plugin/kubernetes/reverse.go +++ b/plugin/kubernetes/reverse.go @@ -25,9 +25,7 @@ func (k *Kubernetes) Reverse(state request.Request, exact bool, opt plugin.Optio // If a service cluster ip does not match, it checks all endpoints func (k *Kubernetes) serviceRecordForIP(ip, name string) []msg.Service { // First check services with cluster ips - svcList := k.APIConn.ServiceList() - - for _, service := range svcList { + for _, service := range k.APIConn.ServiceList() { if (len(k.Namespaces) > 0) && !k.namespaceExposed(service.Namespace) { continue } @@ -37,8 +35,7 @@ func (k *Kubernetes) serviceRecordForIP(ip, name string) []msg.Service { } } // If no cluster ips match, search endpoints - epList := k.APIConn.EndpointsList() - for _, ep := range epList.Items { + for _, ep := range k.APIConn.EndpointsList() { if (len(k.Namespaces) > 0) && !k.namespaceExposed(ep.ObjectMeta.Namespace) { continue } diff --git a/plugin/kubernetes/reverse_test.go b/plugin/kubernetes/reverse_test.go index d5b673be7..ca3d5a8b9 100644 --- a/plugin/kubernetes/reverse_test.go +++ b/plugin/kubernetes/reverse_test.go @@ -8,19 +8,20 @@ import ( "github.com/miekg/dns" "golang.org/x/net/context" - "k8s.io/client-go/1.5/pkg/api" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" + api "k8s.io/client-go/pkg/api/v1" ) type APIConnReverseTest struct{} -func (APIConnReverseTest) Run() { return } -func (APIConnReverseTest) Stop() error { return nil } -func (APIConnReverseTest) PodIndex(string) []interface{} { return nil } +func (APIConnReverseTest) Run() { return } +func (APIConnReverseTest) Stop() error { return nil } +func (APIConnReverseTest) PodIndex(string) []*api.Pod { return nil } func (APIConnReverseTest) ServiceList() []*api.Service { svcs := []*api.Service{ { - ObjectMeta: api.ObjectMeta{ + ObjectMeta: meta.ObjectMeta{ Name: "svc1", Namespace: "testns", }, @@ -37,39 +38,38 @@ func (APIConnReverseTest) ServiceList() []*api.Service { return svcs } -func (APIConnReverseTest) EndpointsList() api.EndpointsList { - return api.EndpointsList{ - Items: []api.Endpoints{ - { - Subsets: []api.EndpointSubset{ - { - Addresses: []api.EndpointAddress{ - { - IP: "10.0.0.100", - Hostname: "ep1a", - }, +func (APIConnReverseTest) EndpointsList() []*api.Endpoints { + eps := []*api.Endpoints{ + { + Subsets: []api.EndpointSubset{ + { + Addresses: []api.EndpointAddress{ + { + IP: "10.0.0.100", + Hostname: "ep1a", }, - Ports: []api.EndpointPort{ - { - Port: 80, - Protocol: "tcp", - Name: "http", - }, + }, + Ports: []api.EndpointPort{ + { + Port: 80, + Protocol: "tcp", + Name: "http", }, }, }, - ObjectMeta: api.ObjectMeta{ - Name: "svc1", - Namespace: "testns", - }, + }, + ObjectMeta: meta.ObjectMeta{ + Name: "svc1", + Namespace: "testns", }, }, } + return eps } -func (APIConnReverseTest) GetNodeByName(name string) (api.Node, error) { - return api.Node{ - ObjectMeta: api.ObjectMeta{ +func (APIConnReverseTest) GetNodeByName(name string) (*api.Node, error) { + return &api.Node{ + ObjectMeta: meta.ObjectMeta{ Name: "test.node.foo.bar", }, }, nil diff --git a/plugin/kubernetes/setup.go b/plugin/kubernetes/setup.go index e60239d42..cc823d54e 100644 --- a/plugin/kubernetes/setup.go +++ b/plugin/kubernetes/setup.go @@ -11,10 +11,10 @@ import ( "github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin/pkg/dnsutil" "github.com/coredns/coredns/plugin/proxy" - "github.com/miekg/dns" "github.com/mholt/caddy" - unversionedapi "k8s.io/client-go/1.5/pkg/api/unversioned" + "github.com/miekg/dns" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" ) func init() { @@ -150,7 +150,7 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, dnsControlOpts, error) { args := c.RemainingArgs() if len(args) > 0 { labelSelectorString := strings.Join(args, " ") - ls, err := unversionedapi.ParseToLabelSelector(labelSelectorString) + ls, err := meta.ParseToLabelSelector(labelSelectorString) if err != nil { return nil, opts, fmt.Errorf("unable to parse label selector value: '%v': %v", labelSelectorString, err) } diff --git a/plugin/kubernetes/setup_test.go b/plugin/kubernetes/setup_test.go index 2fdc38a9c..3f15ead06 100644 --- a/plugin/kubernetes/setup_test.go +++ b/plugin/kubernetes/setup_test.go @@ -6,7 +6,7 @@ import ( "time" "github.com/mholt/caddy" - "k8s.io/client-go/1.5/pkg/api/unversioned" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestKubernetesParse(t *testing.T) { @@ -431,7 +431,7 @@ func TestKubernetesParse(t *testing.T) { // Labels if opts.labelSelector != nil { - foundLabelSelectorString := unversioned.FormatLabelSelector(opts.labelSelector) + foundLabelSelectorString := meta.FormatLabelSelector(opts.labelSelector) if foundLabelSelectorString != test.expectedLabelSelector { t.Errorf("Test %d: Expected kubernetes controller to be initialized with label selector '%s'. Instead found selector '%s' for input '%s'", i, test.expectedLabelSelector, foundLabelSelectorString, test.input) } |