Skip to content

Commit

Permalink
Keep dns records in ExternalDNS Status (#4)
Browse files Browse the repository at this point in the history
Signed-off-by: rasel [email protected]
  • Loading branch information
Superm4n97 authored Oct 26, 2022
1 parent 70bfe36 commit 62d1a34
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 22 deletions.
15 changes: 15 additions & 0 deletions apis/external-dns/v1alpha1/externaldns_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,17 @@ type ExternalDNSSpec struct {
TXTWildcardReplacement *string `json:"txtWildcardReplacement,omitempty"`
}

// DNSRecord hold the DNS name and target address, if there are multiple target address then the addresses are joint by separator ';' between them (ex: 1:2:3:4;6:7:8:9)
type DNSRecord struct {
// target is the list of target address
// +optional
Target string `json:"target,omitempty"`

// dns name is the domain name for this record
// +optional
Name string `json:"name,omitempty"`
}

// ExternalDNSStatus defines the observed state of ExternalDNS
type ExternalDNSStatus struct {
// Phase indicates the current state of the controller (ex: Failed,InProgress,Current)
Expand All @@ -502,6 +513,10 @@ type ExternalDNSStatus struct {
// Conditions describe the current condition of the CRD
// +optional
Conditions []kmapi.Condition `json:"conditions,omitempty"`

// DNSRecord is the list of records that this external dns operator registered
// +optional
DNSRecords []DNSRecord `json:"dnsRecords,omitempty"`
}

//+kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.phase"
Expand Down
20 changes: 20 additions & 0 deletions apis/external-dns/v1alpha1/zz_generated.deepcopy.go

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

13 changes: 13 additions & 0 deletions config/crd/bases/external-dns.appscode.com_externaldns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,19 @@ spec:
- type
type: object
type: array
dnsRecords:
description: DNSRecord is the list of records that this external dns
operator registered
items:
properties:
name:
description: dns name is the domain name for this record
type: string
target:
description: target is the list of target address
type: string
type: object
type: array
observedGeneration:
description: ObservedGeneration indicates the latest generation that
successfully reconciled
Expand Down
20 changes: 17 additions & 3 deletions controllers/external-dns/externaldns_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ func (r *ExternalDNSReconciler) updateEdnsStatus(ctx context.Context, edns *exte
return patchErr
}

func (r ExternalDNSReconciler) patchDNSRecords(ctx context.Context, edns *externaldnsv1alpha1.ExternalDNS, dnsRecs []externaldnsv1alpha1.DNSRecord) error {
_, _, patchErr := kmc.PatchStatus(ctx, r.Client, edns, func(obj client.Object) client.Object {
in := obj.(*externaldnsv1alpha1.ExternalDNS)
in.Status.DNSRecords = dnsRecs
return in
})

return patchErr
}

func (r *ExternalDNSReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)

Expand Down Expand Up @@ -130,13 +140,17 @@ func (r *ExternalDNSReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// APPLY DNS RECORD
//SetDNSRecords creates the dns record according to user information
//successMsg is used to identify whether the 'plan applied' or 'already up to date'
successMsg, err := plan.SetDNSRecords(edns, ctx)

dnsRecs, err := plan.SetDNSRecords(ctx, edns)
if err != nil {
return ctrl.Result{}, r.updateEdnsStatus(ctx, edns, newConditionPtr(externaldnsv1alpha1.CreateAndApplyPlan, err.Error(), edns.Generation, false), phasePointer(externaldnsv1alpha1.ExternalDNSPhaseFailed))
}

return ctrl.Result{}, r.updateEdnsStatus(ctx, edns, newConditionPtr(externaldnsv1alpha1.CreateAndApplyPlan, successMsg, edns.Generation, true), phasePointer(externaldnsv1alpha1.ExternalDNSPhaseCurrent))
err = r.patchDNSRecords(ctx, edns, dnsRecs)
if err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, r.updateEdnsStatus(ctx, edns, newConditionPtr(externaldnsv1alpha1.CreateAndApplyPlan, "plan applied", edns.Generation, true), phasePointer(externaldnsv1alpha1.ExternalDNSPhaseCurrent))
}

// SetupWithManager sets up the controller with the Manager.
Expand Down
40 changes: 21 additions & 19 deletions pkg/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ var defaultConfig = externaldns.Config{
IBMCloudConfigFile: "/etc/kubernetes/ibmcloud.json",
}

func createAndApplyPlan(ctx context.Context, cfg *externaldns.Config, r registry.Registry, endpointSource source.Source) (string, error) {
//create and apply dns plan, If plan is successfully applied then returns dns record, which defines the desired records of the plan
func createAndApplyPlan(ctx context.Context, cfg *externaldns.Config, r registry.Registry, endpointSource source.Source) ([]externaldnsv1alpha1.DNSRecord, error) {

var domainFilter endpoint.DomainFilter
if cfg.RegexDomainFilter.String() != "" {
Expand All @@ -195,15 +196,15 @@ func createAndApplyPlan(ctx context.Context, cfg *externaldns.Config, r registry

records, err := r.Records(ctx)
if err != nil {
return "", err
return nil, err
}

missingRecords := r.MissingRecords()

ctx = context.WithValue(ctx, provider.RecordsContextKey, records)
endpoints, err := endpointSource.Endpoints(ctx)
if err != nil {
return "", err
return nil, err
}

endpoints = r.AdjustEndpoints(endpoints)
Expand All @@ -221,7 +222,7 @@ func createAndApplyPlan(ctx context.Context, cfg *externaldns.Config, r registry
if missingRecordsPlan.Changes.HasChanges() {
err = r.ApplyChanges(ctx, missingRecordsPlan.Changes)
if err != nil {
return "", err
return nil, err
}
klog.Info("all missing records are created")
}
Expand All @@ -240,22 +241,24 @@ func createAndApplyPlan(ctx context.Context, cfg *externaldns.Config, r registry
klog.Info("Desired: ", pln.Desired)
klog.Info("Current: ", pln.Current)

var successMsg = ""
dnsRecs := make([]externaldnsv1alpha1.DNSRecord, 0)

if pln.Changes.HasChanges() {
err = r.ApplyChanges(ctx, pln.Changes)
if err != nil {
klog.Error("failed to apply plan")
return "", err
return nil, err
}
klog.Info("plan applied")
successMsg = "plan applied"

} else {
klog.Info("all records are already up to date")
successMsg = "all records are already up to date"
}

return successMsg, nil
for _, rec := range pln.Desired {
dnsRecs = append(dnsRecs, externaldnsv1alpha1.DNSRecord{Name: rec.DNSName, Target: rec.Targets.String()})
}
return dnsRecs, nil
}

func convertEDNSObjectToCfg(crd *externaldnsv1alpha1.ExternalDNS) (*externaldns.Config, error) {
Expand Down Expand Up @@ -748,37 +751,36 @@ func createRegistry(cfg *externaldns.Config, p provider.Provider) (registry.Regi
return r, err
}

func SetDNSRecords(edns *externaldnsv1alpha1.ExternalDNS, ctx context.Context) (string, error) {
func SetDNSRecords(ctx context.Context, edns *externaldnsv1alpha1.ExternalDNS) ([]externaldnsv1alpha1.DNSRecord, error) {

cfg, err := convertEDNSObjectToCfg(edns)
if err != nil {
klog.Error("failed to convert crd into cfg.", err.Error())
return "", err
return nil, err
}
endpointsSource, err := createEndpointsSource(ctx, cfg)
if err != nil {
klog.Error("failed to create endpoints source.", err.Error())
return "", err
return nil, err
}

pvdr, err := createProviderFromCfg(ctx, cfg, endpointsSource)
if err != nil {
klog.Error("failed to create provider.", err.Error())
return "", err
return nil, err
}

reg, err := createRegistry(cfg, *pvdr)
if err != nil {
klog.Errorf("failed to create Registry.", err.Error())
return "", err
return nil, err
}

var successMsg string
successMsg, err = createAndApplyPlan(ctx, cfg, reg, endpointsSource)
if err != nil {
dnsRecs, e := createAndApplyPlan(ctx, cfg, reg, endpointsSource)
if e != nil {
klog.Errorf("failed to create and apply plan: %s", err.Error())
return "", err
return nil, e
}

return successMsg, nil
return dnsRecs, nil
}

0 comments on commit 62d1a34

Please sign in to comment.