Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kuritka committed Feb 13, 2025
1 parent 6217133 commit f75f6ab
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 78 deletions.
24 changes: 23 additions & 1 deletion controllers/depresolver/depresolver_domaininfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"fmt"
"sort"
"strings"

k8gbv1beta1 "github.com/k8gb-io/k8gb/api/v1beta1"
)

type DelegationZones []DelegationZoneInfo
Expand All @@ -31,7 +33,6 @@ type DelegationZoneInfo struct {
Zone string
ClusterNSName string
ExtClusterNSNames map[string]string
ClusterHeartBeat string
}

func parseDelegationZones(config *Config) []DelegationZoneInfo {
Expand Down Expand Up @@ -97,6 +98,7 @@ func parseDelegationZones(config *Config) []DelegationZoneInfo {
return dzi
}

// GetNSServerList returns a sorted list of all NS servers for the delegation zone
func (z *DelegationZoneInfo) GetNSServerList() []string {
list := []string{z.ClusterNSName}
for _, v := range z.ExtClusterNSNames {
Expand All @@ -105,3 +107,23 @@ func (z *DelegationZoneInfo) GetNSServerList() []string {
sort.Strings(list)
return list
}

// GetExternalDNSEndpointName returns name of endpoint sitting in k8gb namespace
func (z *DelegationZoneInfo) GetExternalDNSEndpointName() string {
const externalDNSTypeCommon = "extdns"
var suffix = strings.Trim(strings.ReplaceAll(z.Domain, ".", "-"), " ")
return fmt.Sprintf("gslb-ns-%s-%s", externalDNSTypeCommon, suffix)
}

// FindByHostname returns DelegationZoneInfo for the hostname
func (d *DelegationZones) FindByHostname(gslb *k8gbv1beta1.Gslb) *DelegationZoneInfo {
if len(gslb.Status.Servers) == 0 {
return nil
}
for _, z := range *d {
if strings.HasSuffix(gslb.Status.Servers[0].Host, z.Domain) {
return &z
}
}
return nil
}
26 changes: 24 additions & 2 deletions controllers/gslb_controller_reconciliation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,14 @@ func TestCreatesDNSNSRecordsForExtDNS(t *testing.T) {
customConfig.ClusterGeoTag = "eu"
customConfig.ExtClustersGeoTags = []string{"za", "us"}
customConfig.DNSZone = dnsZone
customConfig.DelegationZones = depresolver.DelegationZones{
{
Domain: dnsZone,
Zone: "example.com",
ClusterNSName: customConfig.GetClusterNSName(),
ExtClusterNSNames: customConfig.GetExternalClusterNSNames(),
},
}
// apply new environment variables and update config only
settings.reconciler.Config = &customConfig
// If config is changed, new Route53 provider needs to be re-created. There is no way and reason to change provider
Expand All @@ -933,7 +941,10 @@ func TestCreatesDNSNSRecordsForExtDNS(t *testing.T) {
settings.reconciler.DNSProvider = f.Provider()

reconcileAndUpdateGslb(t, settings)
err = settings.client.Get(context.TODO(), client.ObjectKey{Namespace: predefinedConfig.K8gbNamespace, Name: "k8gb-ns-extdns"}, dnsEndpoint)
err = settings.client.Get(
context.TODO(),
client.ObjectKey{Namespace: predefinedConfig.K8gbNamespace, Name: "gslb-ns-extdns-cloud-example-com"},
dnsEndpoint)
require.NoError(t, err, "Failed to get expected DNSEndpoint")
got := dnsEndpoint.Annotations["k8gb.absa.oss/dnstype"]
gotEp := dnsEndpoint.Spec.Endpoints
Expand Down Expand Up @@ -1001,6 +1012,14 @@ func TestCreatesDNSNSRecordsForLoadBalancer(t *testing.T) {
customConfig.ClusterGeoTag = "eu"
customConfig.ExtClustersGeoTags = []string{"za", "us"}
customConfig.DNSZone = dnsZone
customConfig.DelegationZones = depresolver.DelegationZones{
{
Domain: dnsZone,
Zone: "example.com",
ClusterNSName: customConfig.GetClusterNSName(),
ExtClusterNSNames: customConfig.GetExternalClusterNSNames(),
},
}
// apply new environment variables and update config only
settings.reconciler.Config = &customConfig
// If config is changed, new Route53 provider needs to be re-created. There is no way and reason to change provider
Expand All @@ -1009,7 +1028,10 @@ func TestCreatesDNSNSRecordsForLoadBalancer(t *testing.T) {
settings.reconciler.DNSProvider = f.Provider()

reconcileAndUpdateGslb(t, settings)
err = settings.client.Get(context.TODO(), client.ObjectKey{Namespace: predefinedConfig.K8gbNamespace, Name: "k8gb-ns-extdns"}, dnsEndpoint)
err = settings.client.Get(
context.TODO(),
client.ObjectKey{Namespace: predefinedConfig.K8gbNamespace, Name: "gslb-ns-extdns-cloud-example-com"},
dnsEndpoint)
require.NoError(t, err, "Failed to get expected DNSEndpoint")
got := dnsEndpoint.Annotations["k8gb.absa.oss/dnstype"]
gotEp := dnsEndpoint.Spec.Endpoints
Expand Down
66 changes: 35 additions & 31 deletions controllers/providers/dns/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
import (
"context"
"fmt"
"sort"
"strings"

"github.com/k8gb-io/k8gb/controllers/logging"
Expand Down Expand Up @@ -58,11 +57,7 @@ func (p *ExternalDNSProvider) CreateZoneDelegationForExternalDNS(gslb *k8gbv1bet
log.Info().
Interface("provider", p).
Msg("Creating/Updating DNSEndpoint CRDs")
NSServerList := []string{p.config.GetClusterNSName()}
for _, v := range p.config.GetExternalClusterNSNames() {
NSServerList = append(NSServerList, v)
}
sort.Strings(NSServerList)

var NSServerIPs []string
var err error
if p.config.CoreDNSExposed {
Expand All @@ -78,32 +73,35 @@ func (p *ExternalDNSProvider) CreateZoneDelegationForExternalDNS(gslb *k8gbv1bet
if err != nil {
return err
}
NSRecord := &externaldns.DNSEndpoint{
ObjectMeta: metav1.ObjectMeta{
Name: p.endpointName,
Namespace: p.config.K8gbNamespace,
Annotations: map[string]string{"k8gb.absa.oss/dnstype": externalDNSTypeCommon},
},
Spec: externaldns.DNSEndpointSpec{
Endpoints: []*externaldns.Endpoint{
{
DNSName: p.config.DNSZone,
RecordTTL: ttl,
RecordType: "NS",
Targets: NSServerList,
},
{
DNSName: p.config.GetClusterNSName(),
RecordTTL: ttl,
RecordType: "A",
Targets: NSServerIPs,

for _, domainInfo := range p.config.DelegationZones {
NSRecord := &externaldns.DNSEndpoint{
ObjectMeta: metav1.ObjectMeta{
Name: domainInfo.GetExternalDNSEndpointName(),
Namespace: p.config.K8gbNamespace,
Annotations: map[string]string{"k8gb.absa.oss/dnstype": externalDNSTypeCommon},
},
Spec: externaldns.DNSEndpointSpec{
Endpoints: []*externaldns.Endpoint{
{
DNSName: domainInfo.Domain,
RecordTTL: ttl,
RecordType: "NS",
Targets: domainInfo.GetNSServerList(),
},
{
DNSName: domainInfo.ClusterNSName,
RecordTTL: ttl,
RecordType: "A",
Targets: NSServerIPs,
},
},
},
},
}
err = p.assistant.SaveDNSEndpoint(p.config.K8gbNamespace, NSRecord)
if err != nil {
return err
}
err = p.assistant.SaveDNSEndpoint(p.config.K8gbNamespace, NSRecord)
if err != nil {
return err
}
}
return nil
}
Expand All @@ -121,7 +119,13 @@ func (p *ExternalDNSProvider) Finalize(_ *k8gbv1beta1.Gslb, k8sClient client.Cli
return nil
}

return p.assistant.RemoveEndpoint(p.endpointName)
for _, domainInfo := range p.config.DelegationZones {
err = p.assistant.RemoveEndpoint(domainInfo.GetExternalDNSEndpointName())
if err != nil {
return err
}
}
return nil
}

func (p *ExternalDNSProvider) GetExternalTargets(host string) (targets assistant2.Targets) {
Expand Down
62 changes: 60 additions & 2 deletions controllers/providers/dns/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,71 @@ func TestCreateZoneDelegationOnExternalDNS(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockAssistant(ctrl)
a.Config.DelegationZones = []depresolver.DelegationZoneInfo{
{
Domain: a.Config.DNSZone,
Zone: a.Config.EdgeDNSZone,
ClusterNSName: a.Config.GetClusterNSName(),
ExtClusterNSNames: a.Config.GetExternalClusterNSNames(),
},
}
ep1 := expectedDNSEndpoint.DeepCopy()
ep1.Name = "gslb-ns-extdns-cloud-example-com"
p := NewExternalDNS(a.Config, m)
m.EXPECT().SaveDNSEndpoint(a.Config.K8gbNamespace, gomock.Eq(ep1)).Return(nil).Times(1).
Do(func(ns string, ep *externaldns.DNSEndpoint) {
require.True(t, reflect.DeepEqual(ep, ep1))
require.Equal(t, ns, a.Config.K8gbNamespace)
})

// act
err := p.CreateZoneDelegationForExternalDNS(a.Gslb)
// assert
assert.NoError(t, err)
}

func TestCreateZoneDelegationOnExternalDNSWithMultipleEndpoints(t *testing.T) {
// arrange
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockAssistant(ctrl)
a.Config.DelegationZones = []depresolver.DelegationZoneInfo{
{
Domain: a.Config.DNSZone,
Zone: a.Config.EdgeDNSZone,
ClusterNSName: a.Config.GetClusterNSName(),
ExtClusterNSNames: a.Config.GetExternalClusterNSNames(),
},
{
Domain: "common.sampledomain.com",
Zone: "sampledomain.com",
ClusterNSName: "gslb-ns-us-common.sampledomain.com",
ExtClusterNSNames: map[string]string{"za": "gslb-ns-za-common.sampledomain.com", "eu": "gslb-ns-eu-common.sampledomain.com"},
},
}
ep1 := expectedDNSEndpoint.DeepCopy()
ep1.Name = "gslb-ns-extdns-cloud-example-com"
ep2 := expectedDNSEndpoint.DeepCopy()
ep2.Name = "gslb-ns-extdns-common-sampledomain-com"
ep2.Spec.Endpoints[0].DNSName = "common.sampledomain.com"
ep2.Spec.Endpoints[1].DNSName = "gslb-ns-us-common.sampledomain.com"
ep2.Spec.Endpoints[0].Targets = []string{
"gslb-ns-eu-common.sampledomain.com",
"gslb-ns-us-common.sampledomain.com",
"gslb-ns-za-common.sampledomain.com",
}
p := NewExternalDNS(a.Config, m)
m.EXPECT().SaveDNSEndpoint(a.Config.K8gbNamespace, gomock.Eq(expectedDNSEndpoint)).Return(nil).Times(1).
m.EXPECT().SaveDNSEndpoint(a.Config.K8gbNamespace, gomock.Eq(ep1)).Return(nil).Times(1).
Do(func(ns string, ep *externaldns.DNSEndpoint) {
require.True(t, reflect.DeepEqual(ep, expectedDNSEndpoint))
require.True(t, reflect.DeepEqual(ep, ep1))
require.Equal(t, ns, a.Config.K8gbNamespace)
})

m.EXPECT().SaveDNSEndpoint(a.Config.K8gbNamespace, gomock.Eq(ep2)).Return(nil).Times(1).
Do(func(ns string, ep *externaldns.DNSEndpoint) {
require.True(t, reflect.DeepEqual(ep, ep2))
require.Equal(t, ns, a.Config.K8gbNamespace)
})
// act
err := p.CreateZoneDelegationForExternalDNS(a.Gslb)
// assert
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ require (
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
Expand Down
Loading

0 comments on commit f75f6ab

Please sign in to comment.