diff --git a/middleware/kubernetes/controller.go b/middleware/kubernetes/controller.go index df3724ec4..943e4e32a 100644 --- a/middleware/kubernetes/controller.go +++ b/middleware/kubernetes/controller.go @@ -66,12 +66,15 @@ func newdnsController(kubeClient *client.Client, resyncPeriod time.Duration, lse }, &api.Endpoints{}, resyncPeriod, cache.ResourceEventHandlerFuncs{}) - dns.svcLister.Store, dns.svcController = cache.NewInformer( + dns.svcLister.Indexer, dns.svcController = cache.NewIndexerInformer( &cache.ListWatch{ ListFunc: serviceListFunc(dns.client, namespace, dns.selector), WatchFunc: serviceWatchFunc(dns.client, namespace, dns.selector), }, - &api.Service{}, resyncPeriod, cache.ResourceEventHandlerFuncs{}) + &api.Service{}, + resyncPeriod, + cache.ResourceEventHandlerFuncs{}, + cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) dns.nsLister.Store, dns.nsController = cache.NewInformer( &cache.ListWatch{ @@ -174,27 +177,23 @@ func (dns *dnsController) GetNamespaceList() *api.NamespaceList { return &nsList } -func (dns *dnsController) GetServiceList() *api.ServiceList { - svcList, err := dns.svcLister.List() +func (dns *dnsController) GetServiceList() []*api.Service { + svcs, err := dns.svcLister.List(labels.Everything()) if err != nil { - return &api.ServiceList{} + return []*api.Service{} } - return &svcList + return svcs } // GetServicesByNamespace returns a map of // namespacename :: [ kubernetesService ] func (dns *dnsController) GetServicesByNamespace() map[string][]api.Service { k8sServiceList := dns.GetServiceList() - if k8sServiceList == nil { - return nil - } - - items := make(map[string][]api.Service, len(k8sServiceList.Items)) - for _, i := range k8sServiceList.Items { + items := make(map[string][]api.Service, len(k8sServiceList)) + for _, i := range k8sServiceList { namespace := i.Namespace - items[namespace] = append(items[namespace], i) + items[namespace] = append(items[namespace], *i) } return items @@ -203,18 +202,10 @@ func (dns *dnsController) GetServicesByNamespace() map[string][]api.Service { // GetServiceInNamespace returns the Service that matches // servicename in the namespace func (dns *dnsController) GetServiceInNamespace(namespace string, servicename string) *api.Service { - svcKey := fmt.Sprintf("%v/%v", namespace, servicename) - svcObj, svcExists, err := dns.svcLister.Store.GetByKey(svcKey) - + svcObj, err := dns.svcLister.Services(namespace).Get(servicename) if err != nil { // TODO(...): should return err here return nil } - - if !svcExists { - // TODO(...): should return err here - return nil - } - - return svcObj.(*api.Service) + return svcObj } diff --git a/middleware/kubernetes/kubernetes.go b/middleware/kubernetes/kubernetes.go index d31562a25..ae57c6e98 100644 --- a/middleware/kubernetes/kubernetes.go +++ b/middleware/kubernetes/kubernetes.go @@ -159,7 +159,7 @@ func (k *Kubernetes) Records(name string, exact bool) ([]msg.Service, error) { } // TODO: assemble name from parts found in k8s data based on name template rather than reusing query string -func (k *Kubernetes) getRecordsForServiceItems(serviceItems []api.Service, values nametemplate.NameValues) []msg.Service { +func (k *Kubernetes) getRecordsForServiceItems(serviceItems []*api.Service, values nametemplate.NameValues) []msg.Service { var records []msg.Service for _, item := range serviceItems { @@ -182,12 +182,12 @@ func (k *Kubernetes) getRecordsForServiceItems(serviceItems []api.Service, value } // Get performs the call to the Kubernetes http API. -func (k *Kubernetes) Get(namespace string, nsWildcard bool, servicename string, serviceWildcard bool) ([]api.Service, error) { +func (k *Kubernetes) Get(namespace string, nsWildcard bool, servicename string, serviceWildcard bool) ([]*api.Service, error) { serviceList := k.APIConn.GetServiceList() - var resultItems []api.Service + var resultItems []*api.Service - for _, item := range serviceList.Items { + for _, item := range serviceList { if symbolMatches(namespace, item.Namespace, nsWildcard) && symbolMatches(servicename, item.Name, serviceWildcard) { // If namespace has a wildcard, filter results against Corefile namespace list. // (Namespaces without a wildcard were filtered before the call to this function.) @@ -220,11 +220,11 @@ func isKubernetesNameError(err error) bool { } func (k *Kubernetes) getServiceRecordForIP(ip, name string) []msg.Service { - svcList, err := k.APIConn.svcLister.List() + svcList, err := k.APIConn.svcLister.List(labels.Everything()) if err != nil { return nil } - for _, service := range svcList.Items { + for _, service := range svcList { if service.Spec.ClusterIP == ip { return []msg.Service{msg.Service{Host: ip}} } diff --git a/middleware/pkg/strings/slice_test.go b/middleware/pkg/strings/slice_test.go deleted file mode 100644 index 9816852cd..000000000 --- a/middleware/pkg/strings/slice_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package strings - -import ( - "testing" -) - -type InSliceData struct { - Slice []string - String string - InSlice bool -} - -// Test data for TestStringInSlice cases. -var testdataInSlice = []struct { - Slice []string - String string - ExpectedResult bool -}{ - {[]string{"a", "b", "c"}, "a", true}, - {[]string{"a", "b", "c"}, "d", false}, - {[]string{"a", "b", "c"}, "", false}, - {[]string{}, "a", false}, - {[]string{}, "", false}, -} - -func TestStringInSlice(t *testing.T) { - for _, example := range testdataInSlice { - actualResult := StringInSlice(example.String, example.Slice) - if actualResult != example.ExpectedResult { - t.Errorf("Expected stringInSlice result '%v' for example string='%v', slice='%v'. Instead got result '%v'.", example.ExpectedResult, example.String, example.Slice, actualResult) - } - } -}