Skip to content

Commit

Permalink
refactor config binding
Browse files Browse the repository at this point in the history
  • Loading branch information
kuritka committed Feb 28, 2025
1 parent 8ba8e21 commit 33543c9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 91 deletions.
3 changes: 2 additions & 1 deletion controllers/depresolver/depresolver_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

// TODO: refactor with kong.CLI to read envvars into Config
// TODO: refactor with go-playground/validator to validate
// TODO: Remove EdgeDNSZone and DNSZone from Config

// Environment variables keys
const (
Expand Down Expand Up @@ -106,7 +107,7 @@ func (dr *DependencyResolver) ResolveOperatorConfig() (*Config, error) {
dr.errorConfig = dr.validateConfig(dr.config, recognizedDNSTypes)
// validation
if dr.errorConfig == nil {
dr.config.DelegationZones = parseDelegationZones(dr.config)
dr.config.DelegationZones, dr.errorConfig = parseDelegationZones(dr.config)
}
})
return dr.config, dr.errorConfig
Expand Down
59 changes: 30 additions & 29 deletions controllers/depresolver/depresolver_domaininfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
import (
"fmt"
"sort"
"strconv"
"strings"

k8gbv1beta1 "github.com/k8gb-io/k8gb/api/v1beta1"
Expand All @@ -31,15 +32,19 @@ type DelegationZones []DelegationZoneInfo
type DelegationZoneInfo struct {
Domain string
Zone string
NegativeTTL int
ClusterNSName string
ExtClusterNSNames map[string]string
}

func parseDelegationZones(config *Config) []DelegationZoneInfo {
func parseDelegationZones(config *Config) ([]DelegationZoneInfo, error) {
type info struct {
domain string
zone string
negTTL string
}

zones := config.dnsZones
edgeDNSZone := config.edgeDNSZone
dnsZone := config.dnsZone

getNsName := func(tag, zone, edge string) string {
const prefix = "gslb-ns"
Expand All @@ -48,51 +53,47 @@ func parseDelegationZones(config *Config) []DelegationZoneInfo {
return fmt.Sprintf("%s-%s-%s.%s", prefix, tag, domainX, edge)
}

// parse example.com:cloud.example.com;example.io:cloud.example.io into map[string]string
getEnvAsArrayOfPairsOrFallback := func(zones string, fallback map[string]string) map[string]string {
pairs := make(map[string]string)
// parse example.com:cloud.example.com:30;example.io:cloud.example.io:50
getEnvAsArrayOfPairsOrFallback := func(zones string) ([]info, error) {
tuples := make([]info, 0)
slice := strings.Split(zones, ";")
if len(slice) == 0 {
return fallback
}
for _, z := range slice {
pair := strings.Split(z, ":")
if len(pair) != 2 {
return fallback
touple := strings.Split(z, ":")
if len(touple) != 3 {
return tuples, fmt.Errorf("invalid format of delegation zones: %s", z)
}
pairs[strings.Trim(pair[0], " ")] = strings.Trim(pair[1], " ")
tuples = append(tuples, info{zone: strings.Trim(touple[0], " "), domain: strings.Trim(touple[1], " "), negTTL: strings.Trim(touple[2], " ")})
}
for k, v := range fallback {
if _, found := pairs[k]; !found {
pairs[k] = v
}
}
return pairs
return tuples, nil
}
var dzi []DelegationZoneInfo
zones = strings.TrimSuffix(strings.TrimSuffix(zones, ";"), " ")
fallbackDNSZone := map[string]string{}
if !(edgeDNSZone == "" && dnsZone == "") {
fallbackDNSZone[edgeDNSZone] = dnsZone
di, err := getEnvAsArrayOfPairsOrFallback(zones)
if err != nil {
return dzi, err
}
di := getEnvAsArrayOfPairsOrFallback(zones, fallbackDNSZone)

for edge, zone := range di {
for _, inf := range di {
negTTL, err := strconv.Atoi(inf.negTTL)
if err != nil {
return dzi, fmt.Errorf("invalid value of delegation zones: %s", zones)
}
zoneInfo := DelegationZoneInfo{
Domain: zone,
Zone: edge,
ClusterNSName: getNsName(config.ClusterGeoTag, zone, edge),
Domain: inf.domain,
Zone: inf.zone,
NegativeTTL: negTTL,
ClusterNSName: getNsName(config.ClusterGeoTag, inf.domain, inf.zone),
ExtClusterNSNames: func(zone, edge string) map[string]string {
m := map[string]string{}
for _, tag := range config.extClustersGeoTags {
m[tag] = getNsName(tag, zone, edge)
}
return m
}(zone, edge),
}(inf.domain, inf.zone),
}
dzi = append(dzi, zoneInfo)
}
return dzi
return dzi, nil
}

// GetNSServerList returns a sorted list of all NS servers for the delegation zone
Expand Down
111 changes: 50 additions & 61 deletions controllers/depresolver/depresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1524,43 +1524,45 @@ func TestParseDNSZones(t *testing.T) {
name string
expectedLen int
config *Config
assert func(zoneInfo []DelegationZoneInfo)
assert func(zoneInfo []DelegationZoneInfo, err error)
}{
{
name: "invalid negTTL",
config: &Config{
dnsZones: "example.com:cloud.example.com:30x",
EdgeDNSServers: []utils2.DNSServer{{Host: "edge.com", Port: 53}},
ClusterGeoTag: "us",
extClustersGeoTags: []string{"za", "eu"},
},
expectedLen: 0,
assert: func(zoneInfo []DelegationZoneInfo, err error) {

Check failure on line 1538 in controllers/depresolver/depresolver_test.go

View workflow job for this annotation

GitHub Actions / Inspect packages

unused-parameter: parameter 'zoneInfo' seems to be unused, consider removing or renaming it as _ (revive)
assert.Error(t, err)
},
},
{
name: "multiple zones",
config: &Config{
dnsZones: "example.com:cloud.example.com;example.io:cloud.example.io",
edgeDNSZone: "example.org",
dnsZone: "cloud.example.org",
dnsZones: "example.com:cloud.example.com:30;example.io:cloud.example.io:50",
EdgeDNSServers: []utils2.DNSServer{{Host: "edge.com", Port: 53}},
ClusterGeoTag: "us",
extClustersGeoTags: []string{"za", "eu"},
},
expectedLen: 3,
assert: func(zoneInfo []DelegationZoneInfo) {
expectedLen: 2,
assert: func(zoneInfo []DelegationZoneInfo, err error) {
assert.NoError(t, err)
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.com" && info.Domain == "cloud.example.com"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.io" && info.Domain == "cloud.example.io"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.org" && info.Domain == "cloud.example.org"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.ClusterNSName == "gslb-ns-us-cloud.example.com" &&
info.GetNSServerList()[0] == "gslb-ns-eu-cloud.example.com" &&
info.GetNSServerList()[1] == "gslb-ns-us-cloud.example.com" &&
info.GetNSServerList()[2] == "gslb-ns-za-cloud.example.com" &&
info.GetExternalDNSEndpointName() == "k8gb-ns-extdns-cloud-example-com"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.ClusterNSName == "gslb-ns-us-cloud.example.org" &&
info.GetNSServerList()[0] == "gslb-ns-eu-cloud.example.org" &&
info.GetNSServerList()[1] == "gslb-ns-us-cloud.example.org" &&
info.GetNSServerList()[2] == "gslb-ns-za-cloud.example.org" &&
info.GetExternalDNSEndpointName() == "k8gb-ns-extdns-cloud-example-org"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.ClusterNSName == "gslb-ns-us-cloud.example.io" &&
info.GetNSServerList()[0] == "gslb-ns-eu-cloud.example.io" &&
Expand All @@ -1571,100 +1573,87 @@ func TestParseDNSZones(t *testing.T) {
},
},
{
name: "backward compatibility",
config: &Config{
dnsZones: "",
edgeDNSZone: "example.org",
dnsZone: "cloud.example.org",
EdgeDNSServers: []utils2.DNSServer{{Host: "edge.com", Port: 53}},
ClusterGeoTag: "us",
extClustersGeoTags: []string{"za", "eu"},
},
expectedLen: 1,
assert: func(zoneInfo []DelegationZoneInfo) {
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.org" && info.Domain == "cloud.example.org"
}))
},
},
{
name: "override",
name: "multiple domains",
config: &Config{
dnsZones: "example.com:cloud.example.com;example.io:cloud.example.io",
edgeDNSZone: "example.com",
dnsZone: "dc.example.com",
dnsZones: "example.com:cloud.example.com:30;example.com:pair.example.com:50",
EdgeDNSServers: []utils2.DNSServer{{Host: "edge.com", Port: 53}},
ClusterGeoTag: "us",
extClustersGeoTags: []string{"za", "eu"},
},
expectedLen: 2,
assert: func(zoneInfo []DelegationZoneInfo) {
assert: func(zoneInfo []DelegationZoneInfo, err error) {
assert.NoError(t, err)
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.com" && info.Domain == "cloud.example.com"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.io" && info.Domain == "cloud.example.io"
return info.Zone == "example.com" && info.Domain == "pair.example.com"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.ClusterNSName == "gslb-ns-us-cloud.example.com" &&
info.GetNSServerList()[0] == "gslb-ns-eu-cloud.example.com" &&
info.GetNSServerList()[1] == "gslb-ns-us-cloud.example.com" &&
info.GetNSServerList()[2] == "gslb-ns-za-cloud.example.com" &&
info.GetExternalDNSEndpointName() == "k8gb-ns-extdns-cloud-example-com"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.ClusterNSName == "gslb-ns-us-pair.example.com" &&
info.GetNSServerList()[0] == "gslb-ns-eu-pair.example.com" &&
info.GetNSServerList()[1] == "gslb-ns-us-pair.example.com" &&
info.GetNSServerList()[2] == "gslb-ns-za-pair.example.com" &&
info.GetExternalDNSEndpointName() == "k8gb-ns-extdns-pair-example-com"
}))
},
},
{
name: "ends with semicolon",
config: &Config{
dnsZones: "example.com:cloud.example.com;example.io:cloud.example.io;",
edgeDNSZone: "example.org",
dnsZone: "cloud.example.org",
dnsZones: "example.com:cloud.example.com:30;example.io:cloud.example.io:300;",
EdgeDNSServers: []utils2.DNSServer{{Host: "edge.com", Port: 53}},
ClusterGeoTag: "us",
extClustersGeoTags: []string{"za", "eu"},
},
expectedLen: 3,
assert: func(zoneInfo []DelegationZoneInfo) {
expectedLen: 2,
assert: func(zoneInfo []DelegationZoneInfo, err error) {
assert.NoError(t, err)
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.com" && info.Domain == "cloud.example.com"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.io" && info.Domain == "cloud.example.io"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.org" && info.Domain == "cloud.example.org"
}))
},
},
{
name: "trimmed spaces and semicolons",
config: &Config{
dnsZones: "example.com: cloud.example.com; example.io:cloud.example.io ;",
edgeDNSZone: "example.org",
dnsZone: "cloud.example.org",
dnsZones: "example.com: cloud.example.com: 50; example.io:cloud.example.io: 30 ;",
EdgeDNSServers: []utils2.DNSServer{{Host: "edge.com", Port: 53}},
ClusterGeoTag: "us",
extClustersGeoTags: []string{"za", "eu"},
},
expectedLen: 3,
assert: func(zoneInfo []DelegationZoneInfo) {
expectedLen: 2,
assert: func(zoneInfo []DelegationZoneInfo, err error) {
assert.NoError(t, err)
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.com" && info.Domain == "cloud.example.com"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.io" && info.Domain == "cloud.example.io"
}))
assert.True(t, contains(zoneInfo, func(info DelegationZoneInfo) bool {
return info.Zone == "example.org" && info.Domain == "cloud.example.org"
}))
},
},
{
name: "check nsNames",
config: &Config{
dnsZones: "cloud.example.com: k8gb-test.gslb.cloud.example.com;",
edgeDNSZone: "",
dnsZone: "",
dnsZones: "cloud.example.com: k8gb-test.gslb.cloud.example.com :60;",
EdgeDNSServers: []utils2.DNSServer{{Host: "edge.com", Port: 53}},
ClusterGeoTag: "us",
extClustersGeoTags: []string{"za", "eu"},
},
expectedLen: 1,
assert: func(zoneInfo []DelegationZoneInfo) {
assert: func(zoneInfo []DelegationZoneInfo, err error) {
assert.NoError(t, err)
assert.True(t, zoneInfo[0].ClusterNSName == "gslb-ns-us-k8gb-test-gslb.cloud.example.com" &&
zoneInfo[0].ExtClusterNSNames["za"] == "gslb-ns-za-k8gb-test-gslb.cloud.example.com" &&
zoneInfo[0].ExtClusterNSNames["eu"] == "gslb-ns-eu-k8gb-test-gslb.cloud.example.com")
Expand All @@ -1673,8 +1662,8 @@ func TestParseDNSZones(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
zoneInfo := parseDelegationZones(test.config)
test.assert(zoneInfo)
zoneInfo, err := parseDelegationZones(test.config)
test.assert(zoneInfo, err)
assert.Equal(t, test.expectedLen, len(zoneInfo))
})
}
Expand Down

0 comments on commit 33543c9

Please sign in to comment.