Skip to content

Commit

Permalink
registry: Use txt registry for owned records
Browse files Browse the repository at this point in the history
Use the TXT Registry when an `ownerID` is specified for a record being
processed.  Adds an optional field `ownerID` to the DNSRecord spec. This
is immutable since changing it after initial creation will likely not
result in desirable behaviour.
  • Loading branch information
mikenairn committed Mar 11, 2024
1 parent 41d07c2 commit 088d861
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
22 changes: 22 additions & 0 deletions api/v1alpha1/dnsrecord_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ package v1alpha1
import (
"fmt"
"strings"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
externaldns "sigs.k8s.io/external-dns/endpoint"
externaldnsprovider "sigs.k8s.io/external-dns/provider"
externaldnsregistry "sigs.k8s.io/external-dns/registry"
)

// DNSRecordSpec defines the desired state of DNSRecord
type DNSRecordSpec struct {
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="OwnerID is immutable"
// +optional
OwnerID *string `json:"ownerID,omitempty"`
// +kubebuilder:validation:Required
// +required
ManagedZoneRef *ManagedZoneReference `json:"managedZone,omitempty"`
Expand Down Expand Up @@ -99,6 +105,13 @@ const (
NSRecordType DNSRecordType = "NS"

DefaultGeo string = "default"

txtRegistryPrefix = "kuadrant-"
txtRegistrySuffix = ""
txtRegistryWildcardReplacement = "wildcard"
txtRegistryEncryptEnabled = false
txtRegistryEncryptAESKey = ""
txtRegistryCacheInterval = time.Duration(0)
)

// GetRootDomain returns the shortest domain that is shared across all spec.Endpoints dns names.
Expand Down Expand Up @@ -128,6 +141,15 @@ func (s *DNSRecord) GetRootDomain() (string, error) {
return domain, nil
}

func (s *DNSRecord) GetRegistry(provider externaldnsprovider.Provider, managedDNSRecordTypes, excludeDNSRecordTypes []string) (externaldnsregistry.Registry, error) {
if s.Spec.OwnerID != nil {
return externaldnsregistry.NewTXTRegistry(provider, txtRegistryPrefix, txtRegistrySuffix, *s.Spec.OwnerID, txtRegistryCacheInterval,
txtRegistryWildcardReplacement, managedDNSRecordTypes, excludeDNSRecordTypes, txtRegistryEncryptEnabled, []byte(txtRegistryEncryptAESKey))
} else {
return externaldnsregistry.NewNoopRegistry(provider)
}
}

func init() {
SchemeBuilder.Register(&DNSRecord{}, &DNSRecordList{})
}
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config/crd/bases/kuadrant.io_dnsrecords.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ spec:
required:
- name
type: object
ownerID:
type: string
x-kubernetes-validations:
- message: OwnerID is immutable
rule: self == oldSelf
type: object
status:
description: DNSRecordStatus defines the observed state of DNSRecord
Expand Down
15 changes: 8 additions & 7 deletions internal/controller/dnsrecord_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package controller
import (
"context"
"fmt"
"strings"

"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -34,7 +32,7 @@ import (
externaldnsendpoint "sigs.k8s.io/external-dns/endpoint"
externaldnsplan "sigs.k8s.io/external-dns/plan"
externaldnsprovider "sigs.k8s.io/external-dns/provider"
externaldnsregistry "sigs.k8s.io/external-dns/registry"
"strings"

"github.com/kuadrant/dns-operator/api/v1alpha1"
"github.com/kuadrant/dns-operator/internal/common/conditions"
Expand Down Expand Up @@ -80,6 +78,9 @@ func (r *DNSRecordReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
logger.Info("Removing Finalizer", "name", DNSRecordFinalizer)
controllerutil.RemoveFinalizer(dnsRecord, DNSRecordFinalizer)
if err = r.Update(ctx, dnsRecord); client.IgnoreNotFound(err) != nil {
if apierrors.IsConflict(err) {
return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
Expand Down Expand Up @@ -241,7 +242,10 @@ func (r *DNSRecordReconciler) applyChanges(ctx context.Context, dnsRecord *v1alp
return err
}

registry, err := externaldnsregistry.NewNoopRegistry(dnsProvider)
managedDNSRecordTypes := []string{externaldnsendpoint.RecordTypeA, externaldnsendpoint.RecordTypeAAAA, externaldnsendpoint.RecordTypeCNAME}
excludeDNSRecordTypes := []string{}

registry, err := dnsRecord.GetRegistry(dnsProvider, managedDNSRecordTypes, excludeDNSRecordTypes)
if err != nil {
return err
}
Expand All @@ -252,9 +256,6 @@ func (r *DNSRecordReconciler) applyChanges(ctx context.Context, dnsRecord *v1alp
return fmt.Errorf("unknown policy: %s", policyID)
}

managedDNSRecordTypes := []string{externaldnsendpoint.RecordTypeA, externaldnsendpoint.RecordTypeAAAA, externaldnsendpoint.RecordTypeCNAME}
excludeDNSRecordTypes := []string{}

//If we are deleting set the expected endpoints to an empty array
if isDelete {
dnsRecord.Spec.Endpoints = []*externaldnsendpoint.Endpoint{}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/single_cluster_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ var _ = Describe("Single Cluster Record Test", func() {

dnsRecord = &v1alpha1.DNSRecord{
ObjectMeta: metav1.ObjectMeta{
Name: "test-record",
Name: testID,
Namespace: testNamespace,
},
Spec: v1alpha1.DNSRecordSpec{
Expand Down

0 comments on commit 088d861

Please sign in to comment.