This commit is contained in:
Manuel de Brito Fontes 2016-09-23 10:13:02 -03:00
parent 6e0944eb98
commit 8e6257c51f
3 changed files with 20 additions and 62 deletions

View file

@ -66,12 +66,15 @@ func newdnsController(kubeClient *client.Client, resyncPeriod time.Duration, lse
}, },
&api.Endpoints{}, resyncPeriod, cache.ResourceEventHandlerFuncs{}) &api.Endpoints{}, resyncPeriod, cache.ResourceEventHandlerFuncs{})
dns.svcLister.Store, dns.svcController = cache.NewInformer( dns.svcLister.Indexer, dns.svcController = cache.NewIndexerInformer(
&cache.ListWatch{ &cache.ListWatch{
ListFunc: serviceListFunc(dns.client, namespace, dns.selector), ListFunc: serviceListFunc(dns.client, namespace, dns.selector),
WatchFunc: serviceWatchFunc(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( dns.nsLister.Store, dns.nsController = cache.NewInformer(
&cache.ListWatch{ &cache.ListWatch{
@ -174,27 +177,23 @@ func (dns *dnsController) GetNamespaceList() *api.NamespaceList {
return &nsList return &nsList
} }
func (dns *dnsController) GetServiceList() *api.ServiceList { func (dns *dnsController) GetServiceList() []*api.Service {
svcList, err := dns.svcLister.List() svcs, err := dns.svcLister.List(labels.Everything())
if err != nil { if err != nil {
return &api.ServiceList{} return []*api.Service{}
} }
return &svcList return svcs
} }
// GetServicesByNamespace returns a map of // GetServicesByNamespace returns a map of
// namespacename :: [ kubernetesService ] // namespacename :: [ kubernetesService ]
func (dns *dnsController) GetServicesByNamespace() map[string][]api.Service { func (dns *dnsController) GetServicesByNamespace() map[string][]api.Service {
k8sServiceList := dns.GetServiceList() k8sServiceList := dns.GetServiceList()
if k8sServiceList == nil { items := make(map[string][]api.Service, len(k8sServiceList))
return nil for _, i := range k8sServiceList {
}
items := make(map[string][]api.Service, len(k8sServiceList.Items))
for _, i := range k8sServiceList.Items {
namespace := i.Namespace namespace := i.Namespace
items[namespace] = append(items[namespace], i) items[namespace] = append(items[namespace], *i)
} }
return items return items
@ -203,18 +202,10 @@ func (dns *dnsController) GetServicesByNamespace() map[string][]api.Service {
// GetServiceInNamespace returns the Service that matches // GetServiceInNamespace returns the Service that matches
// servicename in the namespace // servicename in the namespace
func (dns *dnsController) GetServiceInNamespace(namespace string, servicename string) *api.Service { func (dns *dnsController) GetServiceInNamespace(namespace string, servicename string) *api.Service {
svcKey := fmt.Sprintf("%v/%v", namespace, servicename) svcObj, err := dns.svcLister.Services(namespace).Get(servicename)
svcObj, svcExists, err := dns.svcLister.Store.GetByKey(svcKey)
if err != nil { if err != nil {
// TODO(...): should return err here // TODO(...): should return err here
return nil return nil
} }
return svcObj
if !svcExists {
// TODO(...): should return err here
return nil
}
return svcObj.(*api.Service)
} }

View file

@ -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 // 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 var records []msg.Service
for _, item := range serviceItems { 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. // 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() 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 symbolMatches(namespace, item.Namespace, nsWildcard) && symbolMatches(servicename, item.Name, serviceWildcard) {
// If namespace has a wildcard, filter results against Corefile namespace list. // If namespace has a wildcard, filter results against Corefile namespace list.
// (Namespaces without a wildcard were filtered before the call to this function.) // (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 { 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 { if err != nil {
return nil return nil
} }
for _, service := range svcList.Items { for _, service := range svcList {
if service.Spec.ClusterIP == ip { if service.Spec.ClusterIP == ip {
return []msg.Service{msg.Service{Host: ip}} return []msg.Service{msg.Service{Host: ip}}
} }

View file

@ -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)
}
}
}