Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
DNS refactor
Browse files Browse the repository at this point in the history
Number of changes required(mostly) to isolate the dns code from the
policy and gateway logic in preparation for moving it to it's own repo.

Provider health checks were removed from the aws dns provider
implementation. These have been replaced by provder agnostic health
checks and are no longer a supported solution.

DNS provider implementations are moved into a provider package
(dns/provider) along with all other dns provider related interfaces and
types. Matches external-dns layout and naming and how we want future
providers to be added.

Updates the dns provider factory to have each provider register its
constructor with the factory on init.  Adds fake provider and factory
for testing and updates field names in reconcilers to better represent
what the Factory resource is (DNSProvider -> ProviderFactory). Factory
method (ProviderFor) now expects a resource implementing the
ProviderAccessor interface.

Move common dns consts and types into api (DefaultWeight, DefaultGeo,
WildcardGeo, GeoCode ), remove redundant code from DNSRecord and make
receiver name consistent.
Added common pkg for consistency with kuadrant-operator and moved target
and gateway wrapper resources into here.
  • Loading branch information
mikenairn committed Feb 1, 2024
1 parent aadac89 commit 655742e
Show file tree
Hide file tree
Showing 49 changed files with 562 additions and 1,484 deletions.
21 changes: 12 additions & 9 deletions cmd/policy_controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ import (
"github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/dnsrecord"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/managedzone"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/tlspolicy"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/dnsprovider"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/provider"
_ "github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/provider/aws"
_ "github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/provider/google"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/health"
)

Expand Down Expand Up @@ -90,7 +92,8 @@ func main() {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
provider := dnsprovider.NewProvider(mgr.GetClient())

dnsProviderFactory := provider.NewFactory(mgr.GetClient())

healthMonitor := health.NewMonitor()
healthCheckQueue := health.NewRequestQueue(time.Second * 5)
Expand All @@ -106,9 +109,9 @@ func main() {
}

if err = (&dnsrecord.DNSRecordReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
DNSProvider: provider.DNSProviderFactory,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ProviderFactory: dnsProviderFactory,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DNSRecord")
os.Exit(1)
Expand All @@ -124,7 +127,7 @@ func main() {
TargetRefReconciler: reconcilers.TargetRefReconciler{
BaseReconciler: dnsPolicyBaseReconciler,
},
DNSProvider: provider.DNSProviderFactory,
ProviderFactory: dnsProviderFactory,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DNSPolicy")
os.Exit(1)
Expand Down Expand Up @@ -157,9 +160,9 @@ func main() {
//+kubebuilder:scaffold:builder

if err = (&managedzone.ManagedZoneReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
DNSProvider: provider.DNSProviderFactory,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ProviderFactory: dnsProviderFactory,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ManagedZone")
os.Exit(1)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/onsi/gomega v1.27.10
github.com/operator-framework/api v0.17.5
github.com/prometheus/client_golang v1.17.0
github.com/rs/xid v1.4.0
golang.org/x/net v0.17.0
google.golang.org/api v0.126.0
k8s.io/api v0.28.3
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3c
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
Expand Down
4 changes: 2 additions & 2 deletions pkg/_internal/gracePeriod/gracePeriod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/Kuadrant/multicluster-gateway-controller/pkg/_internal/metadata"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
)

const (
GraceTimestampAnnotation = "kuadrant.io/grace-timeout"
DefaultGracePeriod = time.Second * dns.DefaultTTL * 10
DefaultTTL = 60 //The TTL value here needs to match the one used by the DNSPolicy. This value however will no longer be available to gateway controller packages directly.
DefaultGracePeriod = time.Second * DefaultTTL * 10
)

var ErrGracePeriodNotExpired = fmt.Errorf("grace period has not yet expired")
Expand Down
21 changes: 0 additions & 21 deletions pkg/_internal/policy/policy.go

This file was deleted.

77 changes: 0 additions & 77 deletions pkg/_internal/policy/policy_test.go

This file was deleted.

16 changes: 14 additions & 2 deletions pkg/apis/v1alpha1/dnspolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type RoutingStrategy string
const (
SimpleRoutingStrategy RoutingStrategy = "simple"
LoadBalancedRoutingStrategy RoutingStrategy = "loadbalanced"

DefaultWeight Weight = 120
DefaultGeo GeoCode = "default"
WildcardGeo GeoCode = "*"
)

// DNSPolicySpec defines the desired state of DNSPolicy
Expand Down Expand Up @@ -81,6 +85,16 @@ type LoadBalancingWeighted struct {
Custom []*CustomWeight `json:"custom,omitempty"`
}

type GeoCode string

func (gc GeoCode) IsDefaultCode() bool {
return gc == DefaultGeo
}

func (gc GeoCode) IsWildcard() bool {
return gc == WildcardGeo
}

type LoadBalancingGeo struct {
// defaultGeo is the country/continent/region code to use when no other can be determined for a dns target cluster.
//
Expand Down Expand Up @@ -228,5 +242,3 @@ type DNSRecordRef struct {
func init() {
SchemeBuilder.Register(&DNSPolicy{}, &DNSPolicyList{})
}

const DefaultWeight Weight = 120
109 changes: 24 additions & 85 deletions pkg/apis/v1alpha1/dnsrecord_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// SetID returns an id that should be unique across a set of endpoints
func (e *Endpoint) SetID() string {
return e.DNSName + e.SetIdentifier
}

// ProviderSpecificProperty holds the name and value of a configuration which is specific to individual DNS providers
type ProviderSpecificProperty struct {
Name string `json:"name,omitempty"`
Expand Down Expand Up @@ -66,26 +61,17 @@ type Endpoint struct {
ProviderSpecific ProviderSpecific `json:"providerSpecific,omitempty"`
}

// SetID returns an id that should be unique across a set of endpoints
func (e *Endpoint) SetID() string {
return e.DNSName + e.SetIdentifier
}

// WithSetIdentifier applies the given set identifier to the endpoint.
func (e *Endpoint) WithSetIdentifier(setIdentifier string) *Endpoint {
e.SetIdentifier = setIdentifier
return e
}

// WithProviderSpecific attaches a key/value pair to the Endpoint and returns the Endpoint.
// This can be used to pass additional data through the stages of ExternalDNS's Endpoint processing.
// The assumption is that most of the time this will be provider specific metadata that doesn't
// warrant its own field on the Endpoint object itself. It differs from Labels in the fact that it's
// not persisted in the Registry but only kept in memory during a single record synchronization.
func (e *Endpoint) WithProviderSpecific(key, value string) *Endpoint {
if e.ProviderSpecific == nil {
e.ProviderSpecific = ProviderSpecific{}
}

e.ProviderSpecific = append(e.ProviderSpecific, ProviderSpecificProperty{Name: key, Value: value})
return e
}

// GetProviderSpecificProperty returns a ProviderSpecificProperty if the property exists.
func (e *Endpoint) GetProviderSpecificProperty(key string) (ProviderSpecificProperty, bool) {
for _, providerSpecific := range e.ProviderSpecific {
Expand All @@ -96,6 +82,25 @@ func (e *Endpoint) GetProviderSpecificProperty(key string) (ProviderSpecificProp
return ProviderSpecificProperty{}, false
}

// SetProviderSpecific sets a provider specific key/value pair.
func (e *Endpoint) SetProviderSpecific(name, value string) {
if e.ProviderSpecific == nil {
e.ProviderSpecific = ProviderSpecific{}
}

for i, pair := range e.ProviderSpecific {
if pair.Name == name {
e.ProviderSpecific[i].Value = value
return
}
}

e.ProviderSpecific = append(e.ProviderSpecific, ProviderSpecificProperty{
Name: name,
Value: value,
})
}

func (e *Endpoint) String() string {
return fmt.Sprintf("%s %d IN %s %s %s %s", e.DNSName, e.RecordTTL, e.RecordType, e.SetIdentifier, e.Targets, e.ProviderSpecific)
}
Expand Down Expand Up @@ -175,72 +180,6 @@ const (
NSRecordType DNSRecordType = "NS"
)

const (
TargetTypeHost = "HOST"
TargetTypeIP = "IP"
)

type Target struct {
Cluster string
TargetType string
Value string
}

func (endpoint *Endpoint) GetAddress() (string, bool) {
if endpoint.SetIdentifier == "" || len(endpoint.Targets) == 0 {
return "", false
}

return string(endpoint.Targets[0]), true
}

func (endpoint *Endpoint) SetProviderSpecific(name, value string) {
if endpoint.ProviderSpecific == nil {
endpoint.ProviderSpecific = ProviderSpecific{}
}

for i, pair := range endpoint.ProviderSpecific {
if pair.Name == name {
endpoint.ProviderSpecific[i].Value = value
return
}
}

endpoint.ProviderSpecific = append(endpoint.ProviderSpecific, ProviderSpecificProperty{
Name: name,
Value: value,
})
}

func (endpoint *Endpoint) GetProviderSpecific(name string) (string, bool) {
for _, property := range endpoint.ProviderSpecific {
if property.Name == name {
return property.Value, true
}
}

return "", false
}

func (endpoint *Endpoint) DeleteProviderSpecific(name string) bool {
if endpoint.ProviderSpecific == nil {
return false
}

deleted := false
providerSpecific := make(ProviderSpecific, 0, len(endpoint.ProviderSpecific))
for _, pair := range endpoint.ProviderSpecific {
if pair.Name == name {
deleted = true
} else {
providerSpecific = append(providerSpecific, pair)
}
}

endpoint.ProviderSpecific = providerSpecific
return deleted
}

func init() {
SchemeBuilder.Register(&DNSRecord{}, &DNSRecordList{})
}
11 changes: 5 additions & 6 deletions pkg/apis/v1alpha1/managedzone_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ type ManagedZoneSpec struct {
// +optional
ParentManagedZone *ManagedZoneReference `json:"parentManagedZone,omitempty"`
// +required
SecretRef *SecretRef `json:"dnsProviderSecretRef"`
}

type SecretRef struct {
//+required
Name string `json:"name"`
SecretRef ProviderRef `json:"dnsProviderSecretRef"`
}

// ManagedZoneStatus defines the observed state of a Zone
Expand Down Expand Up @@ -89,6 +84,10 @@ type ManagedZone struct {
Status ManagedZoneStatus `json:"status,omitempty"`
}

func (mz *ManagedZone) GetProviderRef() ProviderRef {
return mz.Spec.SecretRef
}

//+kubebuilder:object:root=true

// ManagedZoneList contains a list of ManagedZone
Expand Down
Loading

0 comments on commit 655742e

Please sign in to comment.