mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 02:33:14 -04:00
Refactoring of k8s helpers
This commit is contained in:
@@ -5,8 +5,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/miekg/coredns/middleware/kubernetes/util"
|
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/api"
|
"k8s.io/kubernetes/pkg/api"
|
||||||
"k8s.io/kubernetes/pkg/client/cache"
|
"k8s.io/kubernetes/pkg/client/cache"
|
||||||
client "k8s.io/kubernetes/pkg/client/unversioned"
|
client "k8s.io/kubernetes/pkg/client/unversioned"
|
||||||
@@ -19,6 +17,19 @@ var (
|
|||||||
namespace = api.NamespaceAll
|
namespace = api.NamespaceAll
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
type dnsController struct {
|
type dnsController struct {
|
||||||
client *client.Client
|
client *client.Client
|
||||||
|
|
||||||
@@ -30,7 +41,7 @@ type dnsController struct {
|
|||||||
|
|
||||||
svcLister cache.StoreToServiceLister
|
svcLister cache.StoreToServiceLister
|
||||||
endpLister cache.StoreToEndpointsLister
|
endpLister cache.StoreToEndpointsLister
|
||||||
nsLister util.StoreToNamespaceLister
|
nsLister storeToNamespaceLister
|
||||||
|
|
||||||
// stopLock is used to enforce only a single call to Stop is active.
|
// stopLock is used to enforce only a single call to Stop is active.
|
||||||
// Needed because we allow stopping through an http endpoint and
|
// Needed because we allow stopping through an http endpoint and
|
||||||
|
|||||||
@@ -4,13 +4,14 @@ package kubernetes
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/miekg/coredns/middleware"
|
"github.com/miekg/coredns/middleware"
|
||||||
"github.com/miekg/coredns/middleware/etcd/msg"
|
"github.com/miekg/coredns/middleware/etcd/msg"
|
||||||
"github.com/miekg/coredns/middleware/kubernetes/nametemplate"
|
"github.com/miekg/coredns/middleware/kubernetes/nametemplate"
|
||||||
"github.com/miekg/coredns/middleware/kubernetes/util"
|
|
||||||
"github.com/miekg/coredns/middleware/pkg/dnsutil"
|
"github.com/miekg/coredns/middleware/pkg/dnsutil"
|
||||||
|
dns_strings "github.com/miekg/coredns/middleware/pkg/strings"
|
||||||
"github.com/miekg/coredns/middleware/proxy"
|
"github.com/miekg/coredns/middleware/proxy"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
@@ -126,21 +127,21 @@ func (k *Kubernetes) Records(name string, exact bool) ([]msg.Service, error) {
|
|||||||
if namespace == "" {
|
if namespace == "" {
|
||||||
err := errors.New("Parsing query string did not produce a namespace value. Assuming wildcard namespace.")
|
err := errors.New("Parsing query string did not produce a namespace value. Assuming wildcard namespace.")
|
||||||
log.Printf("[WARN] %v\n", err)
|
log.Printf("[WARN] %v\n", err)
|
||||||
namespace = util.WildcardStar
|
namespace = "*"
|
||||||
}
|
}
|
||||||
|
|
||||||
if serviceName == "" {
|
if serviceName == "" {
|
||||||
err := errors.New("Parsing query string did not produce a serviceName value. Assuming wildcard serviceName.")
|
err := errors.New("Parsing query string did not produce a serviceName value. Assuming wildcard serviceName.")
|
||||||
log.Printf("[WARN] %v\n", err)
|
log.Printf("[WARN] %v\n", err)
|
||||||
serviceName = util.WildcardStar
|
serviceName = "*"
|
||||||
}
|
}
|
||||||
|
|
||||||
nsWildcard := util.SymbolContainsWildcard(namespace)
|
nsWildcard := symbolContainsWildcard(namespace)
|
||||||
serviceWildcard := util.SymbolContainsWildcard(serviceName)
|
serviceWildcard := symbolContainsWildcard(serviceName)
|
||||||
|
|
||||||
// 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 (!nsWildcard) && (len(k.Namespaces) > 0) && (!util.StringInSlice(namespace, k.Namespaces)) {
|
if (!nsWildcard) && (len(k.Namespaces) > 0) && (!dns_strings.StringInSlice(namespace, k.Namespaces)) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,7 +191,7 @@ func (k *Kubernetes) Get(namespace string, nsWildcard bool, servicename string,
|
|||||||
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.)
|
||||||
if nsWildcard && (len(k.Namespaces) > 0) && (!util.StringInSlice(item.Namespace, k.Namespaces)) {
|
if nsWildcard && (len(k.Namespaces) > 0) && (!dns_strings.StringInSlice(item.Namespace, k.Namespaces)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
resultItems = append(resultItems, item)
|
resultItems = append(resultItems, item)
|
||||||
@@ -205,9 +206,9 @@ func symbolMatches(queryString string, candidateString string, wildcard bool) bo
|
|||||||
switch {
|
switch {
|
||||||
case !wildcard:
|
case !wildcard:
|
||||||
result = (queryString == candidateString)
|
result = (queryString == candidateString)
|
||||||
case queryString == util.WildcardStar:
|
case queryString == "*":
|
||||||
result = true
|
result = true
|
||||||
case queryString == util.WildcardAny:
|
case queryString == "any":
|
||||||
result = true
|
result = true
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
@@ -239,3 +240,8 @@ const (
|
|||||||
hostmaster = "hostmaster"
|
hostmaster = "hostmaster"
|
||||||
k8sTimeout = 5 * time.Second
|
k8sTimeout = 5 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// symbolContainsWildcard checks whether symbol contains a wildcard value
|
||||||
|
func symbolContainsWildcard(symbol string) bool {
|
||||||
|
return (strings.Contains(symbol, "*") || (symbol == "any"))
|
||||||
|
}
|
||||||
|
|||||||
25
middleware/kubernetes/kubernetes_test.go
Normal file
25
middleware/kubernetes/kubernetes_test.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package kubernetes
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// Test data for TestSymbolContainsWildcard cases.
|
||||||
|
var testdataSymbolContainsWildcard = []struct {
|
||||||
|
Symbol string
|
||||||
|
ExpectedResult bool
|
||||||
|
}{
|
||||||
|
{"mynamespace", false},
|
||||||
|
{"*", true},
|
||||||
|
{"any", true},
|
||||||
|
{"my*space", true},
|
||||||
|
{"*space", true},
|
||||||
|
{"myname*", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSymbolContainsWildcard(t *testing.T) {
|
||||||
|
for _, example := range testdataSymbolContainsWildcard {
|
||||||
|
actualResult := symbolContainsWildcard(example.Symbol)
|
||||||
|
if actualResult != example.ExpectedResult {
|
||||||
|
t.Errorf("Expected SymbolContainsWildcard result '%v' for example string='%v'. Instead got result '%v'.", example.ExpectedResult, example.Symbol, actualResult)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/miekg/coredns/middleware/kubernetes/util"
|
dns_strings "github.com/miekg/coredns/middleware/pkg/strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Likely symbols that require support:
|
// Likely symbols that require support:
|
||||||
@@ -125,7 +125,7 @@ func (t *NameTemplate) GetTypeFromSegmentArray(segments []string) string {
|
|||||||
typeSegment := t.GetSymbolFromSegmentArray("type", segments)
|
typeSegment := t.GetSymbolFromSegmentArray("type", segments)
|
||||||
|
|
||||||
// Limit type to known types symbols
|
// Limit type to known types symbols
|
||||||
if util.StringInSlice(typeSegment, types) {
|
if dns_strings.StringInSlice(typeSegment, types) {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
// Package kubernetes/util provides helper functions for the kubernetes middleware
|
|
||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/api"
|
|
||||||
"k8s.io/kubernetes/pkg/client/cache"
|
|
||||||
)
|
|
||||||
|
|
||||||
// StringInSlice check whether string a is a member of slice.
|
|
||||||
func StringInSlice(a string, slice []string) bool {
|
|
||||||
for _, b := range slice {
|
|
||||||
if b == a {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// SymbolContainsWildcard checks whether symbol contains a wildcard value
|
|
||||||
func SymbolContainsWildcard(symbol string) bool {
|
|
||||||
return (strings.Contains(symbol, WildcardStar) || (symbol == WildcardAny))
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
WildcardStar = "*"
|
|
||||||
WildcardAny = "any"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
11
middleware/pkg/strings/slice.go
Normal file
11
middleware/pkg/strings/slice.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package strings
|
||||||
|
|
||||||
|
// StringInSlice check whether string a is a member of slice.
|
||||||
|
func StringInSlice(a string, slice []string) bool {
|
||||||
|
for _, b := range slice {
|
||||||
|
if b == a {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package util
|
package strings
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -31,25 +31,3 @@ func TestStringInSlice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test data for TestSymbolContainsWildcard cases.
|
|
||||||
var testdataSymbolContainsWildcard = []struct {
|
|
||||||
Symbol string
|
|
||||||
ExpectedResult bool
|
|
||||||
}{
|
|
||||||
{"mynamespace", false},
|
|
||||||
{"*", true},
|
|
||||||
{"any", true},
|
|
||||||
{"my*space", true},
|
|
||||||
{"*space", true},
|
|
||||||
{"myname*", true},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSymbolContainsWildcard(t *testing.T) {
|
|
||||||
for _, example := range testdataSymbolContainsWildcard {
|
|
||||||
actualResult := SymbolContainsWildcard(example.Symbol)
|
|
||||||
if actualResult != example.ExpectedResult {
|
|
||||||
t.Errorf("Expected SymbolContainsWildcard result '%v' for example string='%v'. Instead got result '%v'.", example.ExpectedResult, example.Symbol, actualResult)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user