aboutsummaryrefslogtreecommitdiff
path: root/middleware/kubernetes/subzone.go
blob: 6c4a79126adcf2cf4e38b53f36d92d9eb55d654c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
}