Skip to content

Commit

Permalink
move config to gslb instead of edgeDNSServers
Browse files Browse the repository at this point in the history
  • Loading branch information
kuritka committed Feb 10, 2025
1 parent 00f5cb6 commit 27de8f9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 22 deletions.
21 changes: 21 additions & 0 deletions controllers/depresolver/depresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
"reflect"
"sort"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -1558,10 +1559,30 @@ func arrangeVariablesAndAssert(t *testing.T, expected Config,
if config == nil {
t.Fatal("nil *config returned")
}
sortDelegationZoneInfoSlice(config.DelegationZones)
sortDelegationZoneInfoSlice(expected.DelegationZones)
assert.Equal(t, expected, *config)
errf(t, err)
}

// Sorts a slice of DelegationZoneInfo
func sortDelegationZoneInfoSlice(slice []DelegationZoneInfo) {
sort.Slice(slice, func(i, j int) bool {
if slice[i].Domain != slice[j].Domain {
return slice[i].Domain < slice[j].Domain
}
if slice[i].Zone != slice[j].Zone {
return slice[i].Zone < slice[j].Zone
}
// Sorting inner slices for proper comparison
sort.Strings(slice[i].NSNames)
sort.Strings(slice[i].IPs)
sort.Strings(slice[j].NSNames)
sort.Strings(slice[j].IPs)
return false
})
}

func cleanup() {
for _, s := range []string{ReconcileRequeueSecondsKey, NSRecordTTLKey, ClusterGeoTagKey, ExtClustersGeoTagsKey, EdgeDNSZoneKey, DNSZoneKey,
EdgeDNSServersKey, ExtDNSEnabledKey, InfobloxGridHostKey, InfobloxVersionKey, InfobloxPortKey, InfobloxUsernameKey,
Expand Down
2 changes: 1 addition & 1 deletion controllers/gslb_controller_reconciliation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ func provideSettings(t *testing.T, expected depresolver.Config) (settings testSe
t.Fatalf("reconcile: (%v)", err)
}
r.DNSProvider = f.Provider()
a := assistant.NewGslbAssistant(r.Client, r.Config.K8gbNamespace, r.Config.EdgeDNSServers)
a := assistant.NewGslbAssistant(r.Client, r.Config.K8gbNamespace, *r.Config)
res, err := r.Reconcile(context.TODO(), req)
if err != nil {
t.Fatalf("reconcile: (%v)", err)
Expand Down
41 changes: 25 additions & 16 deletions controllers/providers/assistant/gslb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
coreerrors "errors"
"fmt"
"github.com/k8gb-io/k8gb/controllers/depresolver"

Check failure on line 25 in controllers/providers/assistant/gslb.go

View workflow job for this annotation

GitHub Actions / Inspect packages

File is not properly formatted (goimports)
"strings"
"time"

Expand All @@ -43,18 +44,18 @@ const coreDNSServiceLabel = "app.kubernetes.io/name=coredns"
// Gslb is common wrapper operating on GSLB instance.
// It uses apimachinery client to call kubernetes API
type Gslb struct {
client client.Client
k8gbNamespace string
edgeDNSServers utils.DNSList
client client.Client
k8gbNamespace string
config depresolver.Config
}

var log = logging.Logger()

func NewGslbAssistant(client client.Client, k8gbNamespace string, edgeDNSServers []utils.DNSServer) *Gslb {
func NewGslbAssistant(client client.Client, k8gbNamespace string, config depresolver.Config) *Gslb {
return &Gslb{
client: client,
k8gbNamespace: k8gbNamespace,
edgeDNSServers: edgeDNSServers,
client: client,
k8gbNamespace: k8gbNamespace,
config: config,
}
}

Expand Down Expand Up @@ -109,7 +110,6 @@ func (r *Gslb) CoreDNSExposedIPs() ([]string, error) {
return coreDNSService.Spec.ClusterIPs, nil
}
// LoadBalancer / ExternalName / NodePort service
var lb corev1.LoadBalancerIngress
if len(coreDNSService.Status.LoadBalancer.Ingress) == 0 {
errMessage := "no LoadBalancer ExternalIPs are found"
log.Warn().
Expand All @@ -118,8 +118,17 @@ func (r *Gslb) CoreDNSExposedIPs() ([]string, error) {
err := coreerrors.New(errMessage)
return nil, err
}
lb = coreDNSService.Status.LoadBalancer.Ingress[0]
return extractIPFromLB(lb, r.edgeDNSServers)

var ipList []string
for _, ingressStatusIp := range coreDNSService.Status.LoadBalancer.Ingress {

Check failure on line 123 in controllers/providers/assistant/gslb.go

View workflow job for this annotation

GitHub Actions / Inspect packages

var-naming: range var ingressStatusIp should be ingressStatusIP (revive)
var confirmedIPs, err = extractIPFromLB(ingressStatusIp, r.config.EdgeDNSServers)
if err != nil {
return nil, err
}
ipList = append(ipList, confirmedIPs...)
}
return ipList, nil

}

func extractIPFromLB(lb corev1.LoadBalancerIngress, ns utils.DNSList) (ips []string, err error) {
Expand Down Expand Up @@ -215,10 +224,10 @@ func (r *Gslb) RemoveEndpoint(endpointName string) error {
func (r *Gslb) InspectTXTThreshold(fqdn string, splitBrainThreshold time.Duration) error {
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(fqdn), dns.TypeTXT)
txt, err := utils.Exchange(m, r.edgeDNSServers)
txt, err := utils.Exchange(m, r.config.EdgeDNSServers)
if err != nil {
log.Info().
Interface("edgeDNSServers", r.edgeDNSServers).
Interface("edgeDNSServers", r.config.EdgeDNSServers).
Err(err).
Msg("Contacting EdgeDNS server for TXT split brain record")
return err
Expand Down Expand Up @@ -250,7 +259,7 @@ func (r *Gslb) InspectTXTThreshold(fqdn string, splitBrainThreshold time.Duratio
return nil
}
}
return errors.NewResourceExpired(fmt.Sprintf("Can't find split brain TXT record at EdgeDNS servers(%+v) and record %s ", r.edgeDNSServers, fqdn))
return errors.NewResourceExpired(fmt.Sprintf("Can't find split brain TXT record at EdgeDNS servers(%+v) and record %s ", r.config.EdgeDNSServers, fqdn))

Check failure on line 262 in controllers/providers/assistant/gslb.go

View workflow job for this annotation

GitHub Actions / Inspect packages

The line is 153 characters long, which exceeds the maximum of 150 characters. (lll)
}

func getARecords(msg *dns.Msg) []string {
Expand Down Expand Up @@ -284,13 +293,13 @@ func (r *Gslb) GetExternalTargets(host string, extClusterNsNames map[string]stri
log.Info().
Str("cluster", cluster).
Msg("Adding external Gslb targets from cluster")
glueA, err := dnsQuery(cluster, r.edgeDNSServers)
glueA, err := dnsQuery(cluster, r.config.EdgeDNSServers)
if err != nil {
return targets
}
log.Info().
Str("nameserver", cluster).
Interface("edgeDNSServers", r.edgeDNSServers).
Interface("edgeDNSServers", r.config.EdgeDNSServers).
Interface("glueARecord", glueA.Answer).
Msg("Resolved glue A record for NS")
glueARecords := getARecords(glueA)
Expand All @@ -300,7 +309,7 @@ func (r *Gslb) GetExternalTargets(host string, extClusterNsNames map[string]stri
} else {
hostToUse = cluster
}
nameServersToUse := getNSCombinations(r.edgeDNSServers, hostToUse)
nameServersToUse := getNSCombinations(r.config.EdgeDNSServers, hostToUse)
lHost := fmt.Sprintf("localtargets-%s", host)
a, err := dnsQuery(lHost, nameServersToUse)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions controllers/providers/dns/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestSaveNewDNSEndpointOnExternalDNS(t *testing.T) {

var cl = fake.NewClientBuilder().WithScheme(runtimeScheme).WithObjects(ep).Build()

assistant := assistant.NewGslbAssistant(cl, a.Config.K8gbNamespace, a.Config.EdgeDNSServers)
assistant := assistant.NewGslbAssistant(cl, a.Config.K8gbNamespace, a.Config)
p := NewExternalDNS(a.Config, assistant)
// act, assert
err := p.SaveDNSEndpoint(a.Gslb, expectedDNSEndpoint)
Expand All @@ -173,7 +173,7 @@ func TestSaveExistingDNSEndpointOnExternalDNS(t *testing.T) {
require.NoError(t, schemeBuilder.AddToScheme(runtimeScheme))

var cl = fake.NewClientBuilder().WithScheme(runtimeScheme).WithObjects(endpointToSave).Build()
assistant := assistant.NewGslbAssistant(cl, a.Config.K8gbNamespace, a.Config.EdgeDNSServers)
assistant := assistant.NewGslbAssistant(cl, a.Config.K8gbNamespace, a.Config)
p := NewExternalDNS(a.Config, assistant)
// act, assert
err := p.SaveDNSEndpoint(a.Gslb, endpointToSave)
Expand Down
2 changes: 1 addition & 1 deletion controllers/providers/dns/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewDNSProviderFactory(client client.Client, config depresolver.Config) (f *
}

func (f *ProviderFactory) Provider() Provider {
a := assistant.NewGslbAssistant(f.client, f.config.K8gbNamespace, f.config.EdgeDNSServers)
a := assistant.NewGslbAssistant(f.client, f.config.K8gbNamespace, f.config)
switch f.config.EdgeDNSType {
case depresolver.DNSTypeExternal:
return NewExternalDNS(f.config, a)
Expand Down
4 changes: 2 additions & 2 deletions controllers/providers/dns/infoblox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestCanFilterOutDelegatedZoneEntryAccordingFQDNProvided(t *testing.T) {
customConfig := defaultConfig
customConfig.EdgeDNSZone = "example.com"
customConfig.ExtClustersGeoTags = []string{"za"}
a := assistant.NewGslbAssistant(nil, customConfig.K8gbNamespace, customConfig.EdgeDNSServers)
a := assistant.NewGslbAssistant(nil, customConfig.K8gbNamespace, customConfig)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockInfobloxClient(ctrl)
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestCanSanitizeDelegatedZone(t *testing.T) {
customConfig.EdgeDNSZone = "example.com"
customConfig.ExtClustersGeoTags = []string{"za"}
customConfig.ClusterGeoTag = "eu"
a := assistant.NewGslbAssistant(nil, customConfig.K8gbNamespace, customConfig.EdgeDNSServers)
a := assistant.NewGslbAssistant(nil, customConfig.K8gbNamespace, customConfig)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockInfobloxClient(ctrl)
Expand Down

0 comments on commit 27de8f9

Please sign in to comment.