Adding resyncperiod to Corefile (#205)

* Removing old unused inline k8s API code and tests.
* Adding parsing implementation for `resyncperiod` keyword from Corefile.
* Adding tests for parsing `resyncperiod` keyword from Corefile.
8 Updating README.md and conf/k8sCorefile.
This commit is contained in:
Michael Richmond
2016-08-08 14:30:04 -07:00
committed by GitHub
parent 51eaefc037
commit c079de65b5
9 changed files with 123 additions and 965 deletions

View File

@@ -2,6 +2,7 @@ package setup
import (
"errors"
"fmt"
"log"
"strings"
"time"
@@ -27,7 +28,6 @@ func Kubernetes(c *Controller) (middleware.Middleware, error) {
if err != nil {
return nil, err
}
log.Printf("[debug] after parse and start KubeCache, APIconn is: %v", kubernetes.APIConn)
return func(next middleware.Handler) middleware.Handler {
kubernetes.Next = next
@@ -51,7 +51,6 @@ func kubernetesParse(c *Controller) (kubernetes.Kubernetes, error) {
if c.Val() == "kubernetes" {
zones := c.RemainingArgs()
log.Printf("[debug] Zones: %v", zones)
if len(zones) == 0 {
k8s.Zones = c.ServerBlockHosts
log.Printf("[debug] Zones(from ServerBlockHosts): %v", zones)
@@ -97,6 +96,19 @@ func kubernetesParse(c *Controller) (kubernetes.Kubernetes, error) {
log.Printf("[debug] 'endpoint' keyword provided without any endpoint url value.")
return kubernetes.Kubernetes{}, c.ArgErr()
}
case "resyncperiod":
args := c.RemainingArgs()
if len(args) != 0 {
k8s.ResyncPeriod, err = time.ParseDuration(args[0])
if err != nil {
err = errors.New(fmt.Sprintf("Unable to parse resync duration value. Value provided was '%v'. Example valid values: '15s', '5m', '1h'. Error was: %v", args[0], err))
log.Printf("[ERROR] %v", err)
return kubernetes.Kubernetes{}, err
}
} else {
log.Printf("[debug] 'resyncperiod' keyword provided without any duration value.")
return kubernetes.Kubernetes{}, c.ArgErr()
}
}
}
return k8s, nil

View File

@@ -3,17 +3,19 @@ package setup
import (
"strings"
"testing"
"time"
)
func TestKubernetesParse(t *testing.T) {
tests := []struct {
description string
input string
shouldErr bool
expectedErrContent string // substring from the expected error. Empty for positive cases.
expectedZoneCount int // expected count of defined zones.
expectedNTValid bool // NameTemplate to be initialized and valid
expectedNSCount int // expected count of namespaces.
description string // Human-facing description of test case
input string // Corefile data as string
shouldErr bool // true if test case is exected to produce an error.
expectedErrContent string // substring from the expected error. Empty for positive cases.
expectedZoneCount int // expected count of defined zones.
expectedNTValid bool // NameTemplate to be initialized and valid
expectedNSCount int // expected count of namespaces.
expectedResyncPeriod time.Duration // expected resync period value
}{
// positive
{
@@ -24,6 +26,7 @@ func TestKubernetesParse(t *testing.T) {
1,
true,
0,
defaultResyncPeriod,
},
{
"kubernetes keyword with multiple zones",
@@ -33,6 +36,7 @@ func TestKubernetesParse(t *testing.T) {
2,
true,
0,
defaultResyncPeriod,
},
{
"kubernetes keyword with zone and empty braces",
@@ -43,6 +47,7 @@ func TestKubernetesParse(t *testing.T) {
1,
true,
0,
defaultResyncPeriod,
},
{
"endpoint keyword with url",
@@ -54,6 +59,7 @@ func TestKubernetesParse(t *testing.T) {
1,
true,
0,
defaultResyncPeriod,
},
{
"template keyword with valid template",
@@ -65,6 +71,7 @@ func TestKubernetesParse(t *testing.T) {
1,
true,
0,
defaultResyncPeriod,
},
{
"namespaces keyword with one namespace",
@@ -76,6 +83,7 @@ func TestKubernetesParse(t *testing.T) {
1,
true,
1,
defaultResyncPeriod,
},
{
"namespaces keyword with multiple namespaces",
@@ -87,10 +95,36 @@ func TestKubernetesParse(t *testing.T) {
1,
true,
2,
defaultResyncPeriod,
},
{
"resync period in seconds",
`kubernetes coredns.local {
resyncperiod 30s
}`,
false,
"",
1,
true,
0,
30 * time.Second,
},
{
"resync period in minutes",
`kubernetes coredns.local {
resyncperiod 15m
}`,
false,
"",
1,
true,
0,
15 * time.Minute,
},
{
"fully specified valid config",
`kubernetes coredns.local test.local {
resyncperiod 15m
endpoint http://localhost:8080
template {service}.{namespace}.{zone}
namespaces demo test
@@ -100,6 +134,7 @@ func TestKubernetesParse(t *testing.T) {
2,
true,
2,
15 * time.Minute,
},
// negative
{
@@ -110,6 +145,7 @@ func TestKubernetesParse(t *testing.T) {
-1,
false,
-1,
defaultResyncPeriod,
},
{
"kubernetes keyword without a zone",
@@ -119,6 +155,7 @@ func TestKubernetesParse(t *testing.T) {
-1,
true,
0,
defaultResyncPeriod,
},
{
"endpoint keyword without an endpoint value",
@@ -130,6 +167,7 @@ func TestKubernetesParse(t *testing.T) {
-1,
true,
-1,
defaultResyncPeriod,
},
{
"template keyword without a template value",
@@ -141,6 +179,7 @@ func TestKubernetesParse(t *testing.T) {
-1,
false,
0,
defaultResyncPeriod,
},
{
"template keyword with an invalid template value",
@@ -152,6 +191,7 @@ func TestKubernetesParse(t *testing.T) {
-1,
false,
0,
defaultResyncPeriod,
},
{
"namespace keyword without a namespace value",
@@ -163,6 +203,43 @@ func TestKubernetesParse(t *testing.T) {
-1,
true,
-1,
defaultResyncPeriod,
},
{
"resyncperiod keyword without a duration value",
`kubernetes coredns.local {
resyncperiod
}`,
true,
"Wrong argument count or unexpected line ending after 'resyncperiod'",
-1,
true,
0,
0 * time.Minute,
},
{
"resync period no units",
`kubernetes coredns.local {
resyncperiod 15
}`,
true,
"Unable to parse resync duration value. Value provided was ",
-1,
true,
0,
0 * time.Second,
},
{
"resync period invalid",
`kubernetes coredns.local {
resyncperiod abc
}`,
true,
"Unable to parse resync duration value. Value provided was ",
-1,
true,
0,
0 * time.Second,
},
}
@@ -218,8 +295,12 @@ func TestKubernetesParse(t *testing.T) {
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)
t.Logf("k8sController is: %v", k8sController)
t.Logf("k8sController.Namespaces is: %v", k8sController.Namespaces)
}
// ResyncPeriod
foundResyncPeriod := k8sController.ResyncPeriod
if foundResyncPeriod != test.expectedResyncPeriod {
t.Errorf("Test %d: Expected kubernetes controller to be initialized with resync period '%s'. Instead found period '%s' for input '%s'", test.expectedResyncPeriod, foundResyncPeriod, test.input)
}
}
}