aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/setup/kubernetes.go34
-rw-r--r--core/setup/kubernetes_test.go197
2 files changed, 210 insertions, 21 deletions
diff --git a/core/setup/kubernetes.go b/core/setup/kubernetes.go
index aea9e334a..2f8b5be44 100644
--- a/core/setup/kubernetes.go
+++ b/core/setup/kubernetes.go
@@ -1,23 +1,14 @@
package setup
import (
- //"crypto/tls"
- //"crypto/x509"
"log"
- //"io/ioutil"
- //"net"
- //"net/http"
"strings"
- //"time"
"github.com/miekg/coredns/middleware"
"github.com/miekg/coredns/middleware/kubernetes"
k8sc "github.com/miekg/coredns/middleware/kubernetes/k8sclient"
"github.com/miekg/coredns/middleware/kubernetes/nametemplate"
"github.com/miekg/coredns/middleware/proxy"
- //"github.com/miekg/coredns/middleware/singleflight"
-
- "golang.org/x/net/context"
)
const (
@@ -43,19 +34,8 @@ func Kubernetes(c *Controller) (middleware.Middleware, error) {
}
func kubernetesParse(c *Controller) (kubernetes.Kubernetes, error) {
-
- /*
- * TODO: Remove unused state and simplify.
- * Inflight and Ctx might not be needed. Leaving in place until
- * we take a pass at API caching and optimizing connector to the
- * k8s API. Single flight (or limited upper-bound) for inflight
- * API calls may be desirable.
- */
-
k8s := kubernetes.Kubernetes{
Proxy: proxy.New([]string{}),
- Ctx: context.Background(),
- // Inflight: &singleflight.Group{},
}
var (
endpoints = []string{defaultK8sEndpoint}
@@ -78,7 +58,12 @@ func kubernetesParse(c *Controller) (kubernetes.Kubernetes, error) {
k8s.Zones = kubernetes.NormalizeZoneList(zones)
}
+ // TODO: clean this parsing up
+
middleware.Zones(k8s.Zones).FullyQualify()
+
+ log.Printf("[debug] c data: %v\n", c)
+
if c.NextBlock() {
// TODO(miek): 2 switches?
switch c.Val() {
@@ -89,6 +74,13 @@ func kubernetesParse(c *Controller) (kubernetes.Kubernetes, error) {
}
endpoints = args
k8s.APIConn = k8sc.NewK8sConnector(endpoints[0])
+ case "namespaces":
+ args := c.RemainingArgs()
+ if len(args) == 0 {
+ return kubernetes.Kubernetes{}, c.ArgErr()
+ }
+ namespaces = args
+ k8s.Namespaces = append(k8s.Namespaces, namespaces...)
}
for c.Next() {
switch c.Val() {
@@ -108,7 +100,7 @@ func kubernetesParse(c *Controller) (kubernetes.Kubernetes, error) {
return kubernetes.Kubernetes{}, c.ArgErr()
}
namespaces = args
- k8s.Namespaces = &namespaces
+ k8s.Namespaces = append(k8s.Namespaces, namespaces...)
}
}
}
diff --git a/core/setup/kubernetes_test.go b/core/setup/kubernetes_test.go
new file mode 100644
index 000000000..b0141bab0
--- /dev/null
+++ b/core/setup/kubernetes_test.go
@@ -0,0 +1,197 @@
+package setup
+
+import (
+ "strings"
+ "testing"
+)
+
+/*
+kubernetes coredns.local {
+ # Use url for k8s API endpoint
+ endpoint http://localhost:8080
+ # Assemble k8s record names with the template
+ template {service}.{namespace}.{zone}
+ # Only expose the k8s namespace "demo"
+ #namespaces demo
+ }
+*/
+
+func TestKubernetesParse(t *testing.T) {
+ tests := []struct {
+ input string
+ shouldErr bool
+ expectedErrContent string // substring from the expected error. Empty for positive cases.
+ expectedZoneCount int // expected count of defined zones. '-1' for negative cases.
+ expectedNTValid bool // NameTemplate to be initialized and valid
+ expectedNSCount int // expected count of namespaces. '-1' for negative cases.
+ }{
+ // positive
+ // TODO: not specifiying a zone maybe should error out.
+ {
+ `kubernetes`,
+ false,
+ "",
+ 0,
+ true,
+ 0,
+ },
+ {
+ `kubernetes coredns.local`,
+ false,
+ "",
+ 1,
+ true,
+ 0,
+ },
+ {
+ `kubernetes coredns.local test.local`,
+ false,
+ "",
+ 2,
+ true,
+ 0,
+ },
+ {
+ `kubernetes coredns.local {
+ endpoint http://localhost:9090
+}`,
+ false,
+ "",
+ 1,
+ true,
+ 0,
+ },
+ {
+ `kubernetes coredns.local {
+ template {service}.{namespace}.{zone}
+}`,
+ false,
+ "",
+ 1,
+ true,
+ 0,
+ },
+ {
+ `kubernetes coredns.local {
+ namespaces demo
+}`,
+ false,
+ "",
+ 1,
+ true,
+ 1,
+ },
+ {
+ `kubernetes coredns.local {
+ namespaces demo test
+}`,
+ false,
+ "",
+ 1,
+ true,
+ 2,
+ },
+
+ // negative
+ {
+ `kubernetes coredns.local {
+ endpoint
+}`,
+ true,
+ "Wrong argument count or unexpected line ending after 'endpoint'",
+ -1,
+ true,
+ -1,
+ },
+ // No template provided for template line.
+ {
+ `kubernetes coredns.local {
+ template
+}`,
+ true,
+ "",
+ -1,
+ false,
+ -1,
+ },
+ // Invalid template provided
+ {
+ `kubernetes coredns.local {
+ template {namespace}.{zone}
+}`,
+ true,
+ "",
+ -1,
+ false,
+ -1,
+ },
+ /*
+ // No valid provided for namespaces
+ {
+ `kubernetes coredns.local {
+ namespaces
+ }`,
+ true,
+ "",
+ -1,
+ true,
+ -1,
+ },
+ */
+ }
+
+ for i, test := range tests {
+ c := NewTestController(test.input)
+ k8sController, err := kubernetesParse(c)
+ t.Logf("i: %v\n", i)
+ t.Logf("controller: %v\n", k8sController)
+
+ if test.shouldErr && err == nil {
+ t.Errorf("Test %d: Expected error, but found one for input '%s'. Error was: '%v'", i, test.input, err)
+ }
+
+ if err != nil {
+ if !test.shouldErr {
+ t.Errorf("Test %d: Expected no error but found one for input %s. Error was: %v", i, test.input, err)
+ }
+
+ if test.shouldErr && (len(test.expectedErrContent) < 1) {
+ t.Fatalf("Test %d: Test marked as expecting an error, but no expectedErrContent provided for input '%s'. Error was: '%v'", i, test.input, err)
+ }
+
+ if test.shouldErr && (test.expectedZoneCount >= 0) {
+ t.Fatalf("Test %d: Test marked as expecting an error, but provides value for expectedZoneCount!=-1 for input '%s'. Error was: '%v'", i, test.input, err)
+ }
+
+ if !strings.Contains(err.Error(), test.expectedErrContent) {
+ t.Errorf("Test %d: Expected error to contain: %v, found error: %v, input: %s", i, test.expectedErrContent, err, test.input)
+ }
+
+ return
+ }
+
+ // No error was raised, so validate initialization of k8sController
+ // Zones
+ foundZoneCount := len(k8sController.Zones)
+ if foundZoneCount != test.expectedZoneCount {
+ t.Errorf("Test %d: Expected kubernetes controller to be initialized with %d zones, instead found %d zones: '%v' for input '%s'", i, test.expectedZoneCount, foundZoneCount, k8sController.Zones, test.input)
+ }
+
+ // NameTemplate
+ if k8sController.NameTemplate == nil {
+ t.Errorf("Test %d: Expected kubernetes controller to be initialized with a NameTemplate. Instead found '%v' for input '%s'", i, k8sController.NameTemplate, test.input)
+ } else {
+ foundNTValid := k8sController.NameTemplate.IsValid()
+ if foundNTValid != test.expectedNTValid {
+ t.Errorf("Test %d: Expected NameTemplate validity to be '%v', instead found '%v' for input '%s'", i, test.expectedNTValid, foundNTValid, test.input)
+ }
+ }
+
+ // Namespaces
+ foundNSCount := len(k8sController.Namespaces)
+ if foundNSCount != test.expectedNSCount {
+ t.Errorf("Test %d: Expected kubernetes controller to be initialized with %d namespaces. Instead found %d namespaces: '%v' for input '%s'", i, test.expectedNSCount, foundNSCount, k8sController.Namespaces, test.input)
+ }
+
+ }
+}