middleware/kubernetes: fixes (#837)

dedent code, use shorter names.
use strings.EqualFold instead ToLower to avoid create garbage.
This commit is contained in:
Miek Gieben 2017-08-05 12:29:43 -07:00 committed by GitHub
parent a235833be8
commit c84df38ac5
2 changed files with 48 additions and 48 deletions

View file

@ -316,7 +316,7 @@ func (k *Kubernetes) parseRequest(lowerCasedName string, qtype uint16) (r record
r.port = segs[0][1:] r.port = segs[0][1:]
} else { } else {
r.port = segs[0] r.port = segs[0]
if !symbolContainsWildcard(r.port) { if !wildcard(r.port) {
return r, errInvalidRequest return r, errInvalidRequest
} }
} }
@ -327,7 +327,7 @@ func (k *Kubernetes) parseRequest(lowerCasedName string, qtype uint16) (r record
} }
} else { } else {
r.protocol = segs[1] r.protocol = segs[1]
if !symbolContainsWildcard(r.protocol) { if !wildcard(r.protocol) {
return r, errInvalidRequest return r, errInvalidRequest
} }
} }
@ -372,7 +372,7 @@ func (k *Kubernetes) Entries(r recordRequest) ([]msg.Service, error) {
// Abort if the namespace does not contain a wildcard, and namespace is not published per CoreFile // Abort if the namespace does not contain a wildcard, and namespace is not published per CoreFile
// Case where namespace contains a wildcard is handled in Get(...) method. // Case where namespace contains a wildcard is handled in Get(...) method.
if (!symbolContainsWildcard(r.namespace)) && (len(k.Namespaces) > 0) && (!dnsstrings.StringInSlice(r.namespace, k.Namespaces)) { if (!wildcard(r.namespace)) && (len(k.Namespaces) > 0) && (!dnsstrings.StringInSlice(r.namespace, k.Namespaces)) {
return nil, errNsNotExposed return nil, errNsNotExposed
} }
services, pods, err := k.get(r) services, pods, err := k.get(r)
@ -426,7 +426,9 @@ func (k *Kubernetes) getRecordsForK8sItems(services []service, pods []pod, r rec
} }
records = append(records, s) records = append(records, s)
} }
} else { continue
}
// Create records for each exposed port... // Create records for each exposed port...
for _, p := range svc.ports { for _, p := range svc.ports {
s := msg.Service{ s := msg.Service{
@ -453,8 +455,6 @@ func (k *Kubernetes) getRecordsForK8sItems(services []service, pods []pod, r rec
} }
records = append(records, s) records = append(records, s)
} }
}
} }
for _, p := range pods { for _, p := range pods {
@ -511,7 +511,7 @@ func (k *Kubernetes) findPods(namespace, podname string) (pods []pod, err error)
// PodModeVerified // PodModeVerified
objList := k.APIConn.PodIndex(ip) objList := k.APIConn.PodIndex(ip)
nsWildcard := symbolContainsWildcard(namespace) nsWildcard := wildcard(namespace)
for _, o := range objList { for _, o := range objList {
p, ok := o.(*api.Pod) p, ok := o.(*api.Pod)
if !ok { if !ok {
@ -522,7 +522,7 @@ func (k *Kubernetes) findPods(namespace, podname string) (pods []pod, err error)
continue continue
} }
// check for matching ip and namespace // check for matching ip and namespace
if ip == p.Status.PodIP && symbolMatches(namespace, p.Namespace, nsWildcard) { if ip == p.Status.PodIP && match(namespace, p.Namespace, nsWildcard) {
s := pod{name: podname, namespace: namespace, addr: ip} s := pod{name: podname, namespace: namespace, addr: ip}
pods = append(pods, s) pods = append(pods, s)
return pods, nil return pods, nil
@ -547,13 +547,13 @@ func (k *Kubernetes) findServices(r recordRequest) ([]service, error) {
serviceList := k.APIConn.ServiceList() serviceList := k.APIConn.ServiceList()
var resultItems []service var resultItems []service
nsWildcard := symbolContainsWildcard(r.namespace) nsWildcard := wildcard(r.namespace)
serviceWildcard := symbolContainsWildcard(r.service) serviceWildcard := wildcard(r.service)
portWildcard := symbolContainsWildcard(r.port) || r.port == "" portWildcard := wildcard(r.port) || r.port == ""
protocolWildcard := symbolContainsWildcard(r.protocol) || r.protocol == "" protocolWildcard := wildcard(r.protocol) || r.protocol == ""
for _, svc := range serviceList { for _, svc := range serviceList {
if !(symbolMatches(r.namespace, svc.Namespace, nsWildcard) && symbolMatches(r.service, svc.Name, serviceWildcard)) { if !(match(r.namespace, svc.Namespace, nsWildcard) && match(r.service, svc.Name, serviceWildcard)) {
continue continue
} }
// If namespace has a wildcard, filter results against Corefile namespace list. // If namespace has a wildcard, filter results against Corefile namespace list.
@ -578,7 +578,7 @@ func (k *Kubernetes) findServices(r recordRequest) ([]service, error) {
if r.endpoint != "" && r.endpoint != ephostname { if r.endpoint != "" && r.endpoint != ephostname {
continue continue
} }
if !(symbolMatches(r.port, strings.ToLower(p.Name), portWildcard) && symbolMatches(r.protocol, strings.ToLower(string(p.Protocol)), protocolWildcard)) { if !(match(r.port, p.Name, portWildcard) && match(r.protocol, string(p.Protocol), protocolWildcard)) {
continue continue
} }
s.endpoints = append(s.endpoints, endpoint{addr: addr, port: p}) s.endpoints = append(s.endpoints, endpoint{addr: addr, port: p})
@ -602,7 +602,7 @@ func (k *Kubernetes) findServices(r recordRequest) ([]service, error) {
// ClusterIP service // ClusterIP service
s.addr = svc.Spec.ClusterIP s.addr = svc.Spec.ClusterIP
for _, p := range svc.Spec.Ports { for _, p := range svc.Spec.Ports {
if !(symbolMatches(r.port, strings.ToLower(p.Name), portWildcard) && symbolMatches(r.protocol, strings.ToLower(string(p.Protocol)), protocolWildcard)) { if !(match(r.port, p.Name, portWildcard) && match(r.protocol, string(p.Protocol), protocolWildcard)) {
continue continue
} }
s.ports = append(s.ports, p) s.ports = append(s.ports, p)
@ -613,11 +613,11 @@ func (k *Kubernetes) findServices(r recordRequest) ([]service, error) {
return resultItems, nil return resultItems, nil
} }
func symbolMatches(queryString, candidateString string, wildcard bool) bool { func match(a, b string, wildcard bool) bool {
if wildcard { if wildcard {
return true return true
} }
return queryString == candidateString return strings.EqualFold(a, b)
} }
// getServiceRecordForIP: Gets a service record with a cluster ip matching the ip argument // getServiceRecordForIP: Gets a service record with a cluster ip matching the ip argument
@ -653,9 +653,9 @@ func (k *Kubernetes) getServiceRecordForIP(ip, name string) []msg.Service {
return nil return nil
} }
// symbolContainsWildcard checks whether symbol contains a wildcard value // wildcard checks whether s contains a wildcard value
func symbolContainsWildcard(symbol string) bool { func wildcard(s string) bool {
return (symbol == "*" || symbol == "any") return (s == "*" || s == "any")
} }
func localPodIP() net.IP { func localPodIP() net.IP {

View file

@ -78,9 +78,9 @@ func TestIsNameError(t *testing.T) {
} }
func TestSymbolContainsWildcard(t *testing.T) { func TestSymbolContainsWildcard(t *testing.T) {
var testdataSymbolContainsWildcard = []struct { var tests = []struct {
Symbol string s string
ExpectedResult bool expected bool
}{ }{
{"mynamespace", false}, {"mynamespace", false},
{"*", true}, {"*", true},
@ -90,10 +90,10 @@ func TestSymbolContainsWildcard(t *testing.T) {
{"myname*", false}, {"myname*", false},
} }
for _, example := range testdataSymbolContainsWildcard { for _, te := range tests {
actualResult := symbolContainsWildcard(example.Symbol) got := wildcard(te.s)
if actualResult != example.ExpectedResult { if got != te.expected {
t.Errorf("Expected SymbolContainsWildcard result '%v' for example string='%v'. Instead got result '%v'.", example.ExpectedResult, example.Symbol, actualResult) t.Errorf("Expected Wildcard result '%v' for example '%v', got '%v'.", te.expected, te.s, got)
} }
} }
} }