mw/kubernetes: remove subzones (#878)

Only use was in k8s middleware; no tests other than subzone_test.go
existed; not exercised: remove.
This commit is contained in:
Miek Gieben 2017-08-10 20:53:15 +01:00 committed by GitHub
parent 4d0dae8deb
commit 10681c6bf0
3 changed files with 7 additions and 85 deletions

View file

@ -68,14 +68,16 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, error) {
if c.Val() == "kubernetes" {
zones := c.RemainingArgs()
if len(zones) == 0 {
if len(zones) != 0 {
k8s.Zones = zones
middleware.Zones(k8s.Zones).Normalize()
} else {
k8s.Zones = make([]string, len(c.ServerBlockKeys))
copy(k8s.Zones, c.ServerBlockKeys)
for i := 0; i < len(c.ServerBlockKeys); i++ {
k8s.Zones[i] = middleware.Host(c.ServerBlockKeys[i]).Normalize()
}
}
k8s.Zones = NormalizeZoneList(zones)
middleware.Zones(k8s.Zones).Normalize()
if k8s.Zones == nil || len(k8s.Zones) < 1 {
return nil, errors.New("zone name must be provided for kubernetes middleware")
}

View file

@ -1,48 +0,0 @@
package kubernetes
import (
"log"
"github.com/miekg/dns"
)
// NormalizeZoneList filters the zones argument to remove
// array items that conflict with other items in zones.
// For example, providing the following zones array:
// [ "a.b.c", "b.c", "a", "e.d.f", "a.b" ]
// Returns:
// [ "a.b.c", "a", "e.d.f", "a.b" ]
// Zones filted out:
// - "b.c" because "a.b.c" and "b.c" share the common top
// level "b.c". First listed zone wins if there is a conflict.
//
// Note: This may prove to be too restrictive in practice.
// Need to find counter-example use-cases.
func NormalizeZoneList(zones []string) []string {
filteredZones := []string{}
for _, z := range zones {
zoneConflict, _ := subzoneConflict(filteredZones, z)
if zoneConflict {
log.Printf("[WARN] new zone '%v' from Corefile conflicts with existing zones: %v\n Ignoring zone '%v'\n", z, filteredZones, z)
} else {
filteredZones = append(filteredZones, z)
}
}
return filteredZones
}
// subzoneConflict returns true if name is a child or parent zone of
// any element in zones. If conflicts exist, return the conflicting zones.
func subzoneConflict(zones []string, name string) (bool, []string) {
conflicts := []string{}
for _, z := range zones {
if dns.IsSubDomain(z, name) || dns.IsSubDomain(name, z) {
conflicts = append(conflicts, z)
}
}
return (len(conflicts) != 0), conflicts
}

View file

@ -1,32 +0,0 @@
package kubernetes
import (
"testing"
)
// List of configured zones to test against
var confZones = []string{
"a.b.c",
"d",
}
// Map of zonename :: expected boolean result
var examplesSubzoneConflict = map[string]bool{
"a.b.c": true, // conflicts with zone "a.b.c"
"b.c": true, // conflicts with zone "a.b.c"
"c": true, // conflicts with zone "a.b.c"
"e": false, // no conflict
"a.b.c.e": false, // no conflict
"a.b.c.d": true, // conflicts with zone "d"
"": false,
}
func TestSubzoneConflict(t *testing.T) {
for z, expected := range examplesSubzoneConflict {
actual, conflicts := subzoneConflict(confZones, z)
if actual != expected {
t.Errorf("Expected conflict result '%v' for example '%v'. Instead got '%v'. Conflicting zones are: %v", expected, z, actual, conflicts)
}
}
}