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

Commit

Permalink
Refactor dns provider code
Browse files Browse the repository at this point in the history
* Move dns provider related code into provider package (dns/provider).
  Matches external-dns providers and also reduces the chance of import
cycle issues.
* Rename dns/provider/aws/dns/go -> dns/provider/aws/aws/go (Matches
  google provider and the naming we will use for future providers).
  • Loading branch information
mikenairn committed Jan 30, 2024
1 parent 89e1893 commit c58fdde
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 226 deletions.
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.

7 changes: 4 additions & 3 deletions pkg/controllers/dnspolicy/dns_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/Kuadrant/multicluster-gateway-controller/pkg/_internal/slice"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/provider"
)

const (
Expand Down Expand Up @@ -290,7 +291,7 @@ func (dh *dnsHelper) getLoadBalancedEndpoints(mcgTarget *dns.MultiClusterGateway

for _, hostValue := range hostValues {
endpoint = createOrUpdateEndpoint(geoLbName, []string{hostValue}, v1alpha1.CNAMERecordType, hostValue, dns.DefaultTTL, currentEndpoints)
endpoint.SetProviderSpecific(dns.ProviderSpecificWeight, strconv.Itoa(cgwTarget.GetWeight()))
endpoint.SetProviderSpecific(provider.ProviderSpecificWeight, strconv.Itoa(cgwTarget.GetWeight()))
clusterEndpoints = append(clusterEndpoints, endpoint)
}
}
Expand All @@ -312,14 +313,14 @@ func (dh *dnsHelper) getLoadBalancedEndpoints(mcgTarget *dns.MultiClusterGateway
defaultEndpoint = createOrUpdateEndpoint(lbName, []string{geoLbName}, v1alpha1.CNAMERecordType, "default", dns.DefaultCnameTTL, currentEndpoints)
}

endpoint.SetProviderSpecific(dns.ProviderSpecificGeoCode, string(geoCode))
endpoint.SetProviderSpecific(provider.ProviderSpecificGeoCode, string(geoCode))

endpoints = append(endpoints, endpoint)
}

if len(endpoints) > 0 {
// Add the `defaultEndpoint`, this should always be set by this point if `endpoints` isn't empty
defaultEndpoint.SetProviderSpecific(dns.ProviderSpecificGeoCode, string(dns.WildcardGeo))
defaultEndpoint.SetProviderSpecific(provider.ProviderSpecificGeoCode, string(dns.WildcardGeo))
endpoints = append(endpoints, defaultEndpoint)
//Create gwListenerHost CNAME (shop.example.com -> lb-a1b2.shop.example.com)
endpoint = createOrUpdateEndpoint(hostname, []string{lbName}, v1alpha1.CNAMERecordType, "", dns.DefaultCnameTTL, currentEndpoints)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/dnspolicy/dnspolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"github.com/Kuadrant/multicluster-gateway-controller/pkg/_internal/conditions"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/events"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/provider"
)

const (
Expand All @@ -57,7 +57,7 @@ func (c *DNSPolicyRefsConfig) PolicyRefsAnnotation() string {
// DNSPolicyReconciler reconciles a DNSPolicy object
type DNSPolicyReconciler struct {
reconcilers.TargetRefReconciler
DNSProvider dns.DNSProviderFactory
DNSProvider provider.DNSProviderFactory
dnsHelper dnsHelper
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/dnspolicy/dnspolicy_healthchecks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/controllers/gateway"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/provider"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/utils"
testutil "github.com/Kuadrant/multicluster-gateway-controller/test/util"
)
Expand All @@ -31,7 +31,7 @@ func TestDNSPolicyReconciler_expectedProbesForGateway(t *testing.T) {

type fields struct {
TargetRefReconciler reconcilers.TargetRefReconciler
DNSProvider dns.DNSProviderFactory
DNSProvider provider.DNSProviderFactory
dnsHelper dnsHelper
Placer gateway.GatewayPlacer
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/dnsrecord/dnsrecord_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

"github.com/Kuadrant/multicluster-gateway-controller/pkg/_internal/conditions"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/provider"
)

const (
Expand All @@ -47,7 +47,7 @@ var Clock clock.Clock = clock.RealClock{}
type DNSRecordReconciler struct {
client.Client
Scheme *runtime.Scheme
DNSProvider dns.DNSProviderFactory
DNSProvider provider.DNSProviderFactory
}

//+kubebuilder:rbac:groups=kuadrant.io,resources=dnsrecords,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -103,7 +103,7 @@ func (r *DNSRecordReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
if err != nil {
status = metav1.ConditionFalse
reason = "ProviderError"
message = fmt.Sprintf("The DNS provider failed to ensure the record: %v", dns.SanitizeError(err))
message = fmt.Sprintf("The DNS provider failed to ensure the record: %v", provider.SanitizeError(err))
} else {
dnsRecord.Status.ObservedGeneration = dnsRecord.Generation
dnsRecord.Status.Endpoints = dnsRecord.Spec.Endpoints
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/managedzone/managedzone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

"github.com/Kuadrant/multicluster-gateway-controller/pkg/_internal/conditions"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/provider"
)

const (
Expand All @@ -43,7 +43,7 @@ const (
type ManagedZoneReconciler struct {
client.Client
Scheme *runtime.Scheme
DNSProvider dns.DNSProviderFactory
DNSProvider provider.DNSProviderFactory
}

//+kubebuilder:rbac:groups=kuadrant.io,resources=managedzones,verbs=get;list;watch;create;update;patch;delete
Expand Down
65 changes: 2 additions & 63 deletions pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,68 +16,7 @@ limitations under the License.

package dns

import (
"context"
"errors"
"regexp"

"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
)

const (
DefaultTTL = 60
DefaultCnameTTL = 300
ProviderSpecificWeight = "weight"
ProviderSpecificGeoCode = "geo-code"
DefaultTTL = 60
DefaultCnameTTL = 300
)

type DNSProviderFactory func(ctx context.Context, managedZone *v1alpha1.ManagedZone) (Provider, error)

// Provider knows how to manage DNS zones only as pertains to routing.
type Provider interface {

// Ensure will create or update record.
Ensure(record *v1alpha1.DNSRecord, managedZone *v1alpha1.ManagedZone) error

// Delete will delete record.
Delete(record *v1alpha1.DNSRecord, managedZone *v1alpha1.ManagedZone) error

// Ensure will create or update a managed zone, returns an array of NameServers for that zone.
EnsureManagedZone(managedZone *v1alpha1.ManagedZone) (ManagedZoneOutput, error)

// Delete will delete a managed zone.
DeleteManagedZone(managedZone *v1alpha1.ManagedZone) error
}

type ProviderSpecificLabels struct {
Weight string
HealthCheckID string
}

type ManagedZoneOutput struct {
ID string
NameServers []*string
RecordCount int64
}

var _ Provider = &FakeProvider{}

type FakeProvider struct{}

func (*FakeProvider) Ensure(dnsRecord *v1alpha1.DNSRecord, managedZone *v1alpha1.ManagedZone) error {
return nil
}
func (*FakeProvider) Delete(dnsRecord *v1alpha1.DNSRecord, managedZone *v1alpha1.ManagedZone) error {
return nil
}
func (*FakeProvider) EnsureManagedZone(managedZone *v1alpha1.ManagedZone) (ManagedZoneOutput, error) {
return ManagedZoneOutput{}, nil
}
func (*FakeProvider) DeleteManagedZone(managedZone *v1alpha1.ManagedZone) error { return nil }

// SanitizeError removes request specific data from error messages in order to make them consistent across multiple similar requests to the provider. e.g AWS SDK Request ids `request id: 051c860b-9b30-4c19-be1a-1280c3e9fdc4`
func SanitizeError(err error) error {
re := regexp.MustCompile(`request id: [^\s]+`)
sanitizedErr := re.ReplaceAllString(err.Error(), "")
return errors.New(sanitizedErr)
}
8 changes: 4 additions & 4 deletions pkg/dns/dnsprovider/dnsProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/aws"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/google"
"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"
)

var errUnsupportedProvider = fmt.Errorf("provider type given is not supported")
Expand All @@ -29,7 +29,7 @@ func NewProvider(c client.Client) *providerFactory {
}

// depending on the provider type specified in the form of a custom secret type https://kubernetes.io/docs/concepts/configuration/secret/#secret-types in the dnsprovider secret it returns a dnsprovider.
func (p *providerFactory) DNSProviderFactory(ctx context.Context, managedZone *v1alpha1.ManagedZone) (dns.Provider, error) {
func (p *providerFactory) DNSProviderFactory(ctx context.Context, managedZone *v1alpha1.ManagedZone) (provider.Provider, error) {
providerSecret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: managedZone.Spec.SecretRef.Name,
Expand Down
20 changes: 7 additions & 13 deletions pkg/dns/aws/dns.go → pkg/dns/provider/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"github.com/Kuadrant/multicluster-gateway-controller/pkg/apis/v1alpha1"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns"
"github.com/Kuadrant/multicluster-gateway-controller/pkg/dns/provider"
)

const (
Expand All @@ -48,7 +49,7 @@ type Route53DNSProvider struct {
logger logr.Logger
}

var _ dns.Provider = &Route53DNSProvider{}
var _ provider.Provider = &Route53DNSProvider{}

func NewProviderFromSecret(s *v1.Secret) (*Route53DNSProvider, error) {

Expand Down Expand Up @@ -97,15 +98,15 @@ func (p *Route53DNSProvider) Delete(record *v1alpha1.DNSRecord, managedZone *v1a
return p.change(record, managedZone, deleteAction)
}

func (p *Route53DNSProvider) EnsureManagedZone(zone *v1alpha1.ManagedZone) (dns.ManagedZoneOutput, error) {
func (p *Route53DNSProvider) EnsureManagedZone(zone *v1alpha1.ManagedZone) (provider.ManagedZoneOutput, error) {
var zoneID string
if zone.Spec.ID != "" {
zoneID = zone.Spec.ID
} else {
zoneID = zone.Status.ID
}

var managedZoneOutput dns.ManagedZoneOutput
var managedZoneOutput provider.ManagedZoneOutput

if zoneID != "" {
getResp, err := p.client.GetHostedZone(&route53.GetHostedZoneInput{
Expand Down Expand Up @@ -165,13 +166,6 @@ func (p *Route53DNSProvider) DeleteManagedZone(zone *v1alpha1.ManagedZone) error
return nil
}

func (*Route53DNSProvider) ProviderSpecific() dns.ProviderSpecificLabels {
return dns.ProviderSpecificLabels{
Weight: dns.ProviderSpecificWeight,
HealthCheckID: ProviderSpecificHealthCheckID,
}
}

func (p *Route53DNSProvider) change(record *v1alpha1.DNSRecord, managedZone *v1alpha1.ManagedZone, action action) error {
// Configure records.
if len(record.Spec.Endpoints) == 0 {
Expand Down Expand Up @@ -264,10 +258,10 @@ func (p *Route53DNSProvider) changeForEndpoint(endpoint *v1alpha1.Endpoint, acti
if endpoint.SetIdentifier != "" {
resourceRecordSet.SetIdentifier = aws.String(endpoint.SetIdentifier)
}
if prop, ok := endpoint.GetProviderSpecificProperty(dns.ProviderSpecificWeight); ok {
if prop, ok := endpoint.GetProviderSpecificProperty(provider.ProviderSpecificWeight); ok {
weight, err := strconv.ParseInt(prop.Value, 10, 64)
if err != nil {
p.logger.Error(err, "Failed parsing value, using weight of 0", "weight", dns.ProviderSpecificWeight, "value", prop.Value)
p.logger.Error(err, "Failed parsing value, using weight of 0", "weight", provider.ProviderSpecificWeight, "value", prop.Value)
weight = 0
}
resourceRecordSet.Weight = aws.Int64(weight)
Expand All @@ -285,7 +279,7 @@ func (p *Route53DNSProvider) changeForEndpoint(endpoint *v1alpha1.Endpoint, acti
var geolocation = &route53.GeoLocation{}
useGeolocation := false

if prop, ok := endpoint.GetProviderSpecificProperty(dns.ProviderSpecificGeoCode); ok {
if prop, ok := endpoint.GetProviderSpecificProperty(provider.ProviderSpecificGeoCode); ok {
if dns.IsISO3166Alpha2Code(prop.Value) || dns.GeoCode(prop.Value).IsWildcard() {
geolocation.CountryCode = aws.String(prop.Value)
} else {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit c58fdde

Please sign in to comment.