Skip to content

Commit

Permalink
Fix reconciler error (#18)
Browse files Browse the repository at this point in the history
Signed-off-by: rasel <[email protected]>
  • Loading branch information
Superm4n97 authored Jan 17, 2023
1 parent 731d4b5 commit 5d3e475
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 53 deletions.
8 changes: 4 additions & 4 deletions pkg/cmds/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package cmds
import (
"os"

externaldnsv1alpha1 "kubeops.dev/external-dns-operator/apis/external/v1alpha1"
externaldnscontrollers "kubeops.dev/external-dns-operator/pkg/controllers/external-dns"
api "kubeops.dev/external-dns-operator/apis/external/v1alpha1"
controllers "kubeops.dev/external-dns-operator/pkg/controllers/external-dns"

"github.com/spf13/cobra"
v "gomodules.xyz/x/version"
Expand All @@ -45,7 +45,7 @@ var (

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(externaldnsv1alpha1.AddToScheme(scheme))
utilruntime.Must(api.AddToScheme(scheme))
}

func NewCmdRun() *cobra.Command {
Expand Down Expand Up @@ -94,7 +94,7 @@ func NewCmdRun() *cobra.Command {
os.Exit(1)
}

if err = (&externaldnscontrollers.ExternalDNSReconciler{
if err = (&controllers.ExternalDNSReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
Expand Down
89 changes: 67 additions & 22 deletions pkg/controllers/external-dns/externaldns_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
"context"
"sync"

externaldnsv1alpha1 "kubeops.dev/external-dns-operator/apis/external/v1alpha1"
api "kubeops.dev/external-dns-operator/apis/external/v1alpha1"
"kubeops.dev/external-dns-operator/pkg/credentials"
"kubeops.dev/external-dns-operator/pkg/informers"
"kubeops.dev/external-dns-operator/pkg/plan"

"github.com/pkg/errors"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
Expand All @@ -48,19 +49,19 @@ type ExternalDNSReconciler struct {
watcher *informers.ObjectTracker
}

func newConditionPtr(reason string, message string, generation int64, conditionStatus bool) *kmapi.Condition {
func newCondition(reason string, message string, generation int64, conditionStatus bool) *kmapi.Condition {
newCondition := kmapi.NewCondition(reason, message, generation, conditionStatus)
return &newCondition
}

func phasePointer(phase externaldnsv1alpha1.ExternalDNSPhase) *externaldnsv1alpha1.ExternalDNSPhase {
func newPhase(phase api.ExternalDNSPhase) *api.ExternalDNSPhase {
return &phase
}

// update the status of the crd, conditionType is the reason of the condition
func (r *ExternalDNSReconciler) updateEdnsStatus(ctx context.Context, edns *externaldnsv1alpha1.ExternalDNS, newCondition *kmapi.Condition, phase *externaldnsv1alpha1.ExternalDNSPhase) error {
func (r *ExternalDNSReconciler) updateEdnsStatus(ctx context.Context, edns *api.ExternalDNS, newCondition *kmapi.Condition, phase *api.ExternalDNSPhase) error {
_, _, patchErr := kmc.PatchStatus(ctx, r.Client, edns, func(obj client.Object) client.Object {
in := obj.(*externaldnsv1alpha1.ExternalDNS)
in := obj.(*api.ExternalDNS)
if phase != nil {
in.Status.Phase = *phase
}
Expand All @@ -72,9 +73,9 @@ 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 {
func (r ExternalDNSReconciler) patchDNSRecords(ctx context.Context, edns *api.ExternalDNS, dnsRecs []api.DNSRecord) error {
_, _, patchErr := kmc.PatchStatus(ctx, r.Client, edns, func(obj client.Object) client.Object {
in := obj.(*externaldnsv1alpha1.ExternalDNS)
in := obj.(*api.ExternalDNS)
in.Status.DNSRecords = dnsRecs
return in
})
Expand All @@ -87,25 +88,43 @@ func (r *ExternalDNSReconciler) Reconcile(ctx context.Context, req ctrl.Request)

// GET EXTERNAL DNS
ednsKey := req.NamespacedName
edns := &externaldnsv1alpha1.ExternalDNS{}
edns := &api.ExternalDNS{}

if err := r.Get(ctx, ednsKey, edns); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
edns = edns.DeepCopy()

if edns.Status.Phase == "" {
if patchErr := r.updateEdnsStatus(ctx, edns, nil, phasePointer(externaldnsv1alpha1.ExternalDNSPhaseInProgress)); patchErr != nil {
if patchErr := r.updateEdnsStatus(
ctx,
edns,
nil,
newPhase(api.ExternalDNSPhaseInProgress),
); patchErr != nil {
return ctrl.Result{}, patchErr
}
}

// REGISTER WATCHER
if err := informers.RegisterWatcher(ctx, edns, r.watcher, r.Client); err != nil {
return ctrl.Result{}, r.updateEdnsStatus(ctx, edns, newConditionPtr(externaldnsv1alpha1.CreateAndRegisterWatcher, err.Error(), edns.Generation, false), phasePointer(externaldnsv1alpha1.ExternalDNSPhaseFailed))
if patchErr := r.updateEdnsStatus(
ctx,
edns,
newCondition(api.CreateAndRegisterWatcher, err.Error(), edns.Generation, false),
newPhase(api.ExternalDNSPhaseFailed),
); patchErr != nil {
err = errors.Wrap(err, patchErr.Error())
}
return ctrl.Result{}, err
}

if patchErr := r.updateEdnsStatus(ctx, edns, newConditionPtr(externaldnsv1alpha1.CreateAndRegisterWatcher, "Watcher registered", edns.Generation, true), nil); patchErr != nil {
if patchErr := r.updateEdnsStatus(
ctx,
edns,
newCondition(api.CreateAndRegisterWatcher, "Watcher registered", edns.Generation, true),
nil,
); patchErr != nil {
return ctrl.Result{}, patchErr
}

Expand All @@ -116,10 +135,23 @@ func (r *ExternalDNSReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// create and set provider secret credentials and environment variables
err := credentials.SetCredential(ctx, r.Client, edns)
if err != nil {
return ctrl.Result{}, r.updateEdnsStatus(ctx, edns, newConditionPtr(externaldnsv1alpha1.GetProviderSecret, err.Error(), edns.Generation, false), phasePointer(externaldnsv1alpha1.ExternalDNSPhaseFailed))
if patchErr := r.updateEdnsStatus(
ctx,
edns,
newCondition(api.GetProviderSecret, err.Error(), edns.Generation, false),
newPhase(api.ExternalDNSPhaseFailed),
); patchErr != nil {
err = errors.Wrap(err, patchErr.Error())
}
return ctrl.Result{}, err
}

if patchErr := r.updateEdnsStatus(ctx, edns, newConditionPtr(externaldnsv1alpha1.GetProviderSecret, "Provider credential configured", edns.Generation, true), nil); patchErr != nil {
if patchErr := r.updateEdnsStatus(
ctx,
edns,
newCondition(api.GetProviderSecret, "Provider credential configured", edns.Generation, true),
nil,
); patchErr != nil {
return ctrl.Result{}, patchErr
}

Expand All @@ -128,46 +160,59 @@ func (r *ExternalDNSReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// successMsg is used to identify whether the 'plan applied' or 'already up to date'
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))
if patchErr := r.updateEdnsStatus(
ctx,
edns,
newCondition(api.CreateAndApplyPlan, err.Error(), edns.Generation, false),
newPhase(api.ExternalDNSPhaseFailed),
); patchErr != nil {
err = errors.Wrap(err, patchErr.Error())
}
return ctrl.Result{}, err
}

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))
return ctrl.Result{}, r.updateEdnsStatus(
ctx,
edns,
newCondition(api.CreateAndApplyPlan, "plan applied", edns.Generation, true),
newPhase(api.ExternalDNSPhaseCurrent),
)
}

// SetupWithManager sets up the controller with the Manager.
func (r *ExternalDNSReconciler) SetupWithManager(mgr ctrl.Manager) error {
secretToEdns := handler.EnqueueRequestsFromMapFunc(func(object client.Object) []reconcile.Request {
reconcileReq := make([]reconcile.Request, 0)
ctx := context.TODO()
ednsList := &externaldnsv1alpha1.ExternalDNSList{}
ednsList := &api.ExternalDNSList{}

if err := mgr.GetClient().List(ctx, ednsList, client.InNamespace(object.GetNamespace())); err != nil {
return reconcileReq
}

for _, edns := range ednsList.Items {
switch edns.Spec.Provider.String() {
case externaldnsv1alpha1.ProviderAWS.String():
switch edns.Spec.Provider {
case api.ProviderAWS:
if edns.Spec.AWS != nil && edns.Spec.AWS.SecretRef != nil && edns.Spec.AWS.SecretRef.Name == object.GetName() {
reconcileReq = append(reconcileReq, reconcile.Request{NamespacedName: client.ObjectKey{Name: edns.Name, Namespace: edns.Namespace}})
}

case externaldnsv1alpha1.ProviderAzure.String():
case api.ProviderAzure:
if edns.Spec.Azure != nil && edns.Spec.Azure.SecretRef != nil && edns.Spec.Azure.SecretRef.Name == object.GetName() {
reconcileReq = append(reconcileReq, reconcile.Request{NamespacedName: client.ObjectKey{Name: edns.Name, Namespace: edns.Namespace}})
}

case externaldnsv1alpha1.ProviderGoogle.String():
case api.ProviderGoogle:
if edns.Spec.Google != nil && edns.Spec.Google.SecretRef != nil && edns.Spec.Google.SecretRef.Name == object.GetName() {
reconcileReq = append(reconcileReq, reconcile.Request{NamespacedName: client.ObjectKey{Name: edns.Name, Namespace: edns.Namespace}})
}

case externaldnsv1alpha1.ProviderCloudflare.String():
case api.ProviderCloudflare:
if edns.Spec.Cloudflare != nil && edns.Spec.Cloudflare.SecretRef != nil && edns.Spec.Cloudflare.SecretRef.Name == object.GetName() {
reconcileReq = append(reconcileReq, reconcile.Request{NamespacedName: client.ObjectKey{Name: edns.Name, Namespace: edns.Namespace}})
}
Expand All @@ -179,7 +224,7 @@ func (r *ExternalDNSReconciler) SetupWithManager(mgr ctrl.Manager) error {

// for dynamic watcher
controller, err := ctrl.NewControllerManagedBy(mgr).
For(&externaldnsv1alpha1.ExternalDNS{}).
For(&api.ExternalDNS{}).
Watches(&source.Kind{Type: &core.Secret{}}, secretToEdns).
Build(r)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/external-dns/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

//+kubebuilder:scaffold:imports

externaldnsv1alpha1 "kubeops.dev/external-dns-operator/apis/external/v1alpha1"
api "kubeops.dev/external-dns-operator/apis/external/v1alpha1"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -67,7 +67,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

err = externaldnsv1alpha1.AddToScheme(scheme.Scheme)
err = api.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme
Expand Down
4 changes: 2 additions & 2 deletions pkg/credentials/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"
"os"

externaldnsv1alpha1 "kubeops.dev/external-dns-operator/apis/external/v1alpha1"
api "kubeops.dev/external-dns-operator/apis/external/v1alpha1"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -36,7 +36,7 @@ func validAWSSecret(secret *core.Secret, key string) bool {
return found
}

func setAWSCredential(ctx context.Context, kc client.Client, edns *externaldnsv1alpha1.ExternalDNS) error {
func setAWSCredential(ctx context.Context, kc client.Client, edns *api.ExternalDNS) error {
if err := resetEnvVariables(AWSSharedCredentialsFile); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/credentials/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"
"os"

externaldnsv1alpha1 "kubeops.dev/external-dns-operator/apis/external/v1alpha1"
api "kubeops.dev/external-dns-operator/apis/external/v1alpha1"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -34,7 +34,7 @@ func validAzureSecret(secret *core.Secret, key string) bool {
return found
}

func setAzureCredential(ctx context.Context, kc client.Client, edns *externaldnsv1alpha1.ExternalDNS) error {
func setAzureCredential(ctx context.Context, kc client.Client, edns *api.ExternalDNS) error {
// for azure, user must have to provide ProviderSecretRef
if edns.Spec.Azure == nil || edns.Spec.Azure.SecretRef == nil {
return errors.New("providerSecretRef is not given for azure provider")
Expand Down
4 changes: 2 additions & 2 deletions pkg/credentials/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"errors"
"os"

externaldnsv1alpha1 "kubeops.dev/external-dns-operator/apis/external/v1alpha1"
api "kubeops.dev/external-dns-operator/apis/external/v1alpha1"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -46,7 +46,7 @@ func validCFSecret(secret *core.Secret, tokenKey, apiKey, apiEmail string) bool
}
}

func setCloudflareCredentials(ctx context.Context, kc client.Client, edns *externaldnsv1alpha1.ExternalDNS) error {
func setCloudflareCredentials(ctx context.Context, kc client.Client, edns *api.ExternalDNS) error {
if err := resetEnvVariables(CFApiToken, CFApiKey, CFApiEmail, CFBaseURL); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/credentials/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"
"os"

externaldnsv1alpha1 "kubeops.dev/external-dns-operator/apis/external/v1alpha1"
api "kubeops.dev/external-dns-operator/apis/external/v1alpha1"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -36,7 +36,7 @@ func validGoogleSecret(secret *core.Secret, key string) bool {
return found
}

func setGoogleCredential(ctx context.Context, kc client.Client, edns *externaldnsv1alpha1.ExternalDNS) error {
func setGoogleCredential(ctx context.Context, kc client.Client, edns *api.ExternalDNS) error {
if err := resetEnvVariables(GoogleApplicationCredentials); err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/credentials/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"errors"
"os"

externaldnsv1alpha1 "kubeops.dev/external-dns-operator/apis/external/v1alpha1"
api "kubeops.dev/external-dns-operator/apis/external/v1alpha1"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -46,18 +46,18 @@ func resetEnvVariables(list ...string) error {
return nil
}

func SetCredential(ctx context.Context, kc client.Client, edns *externaldnsv1alpha1.ExternalDNS) error {
switch edns.Spec.Provider.String() {
case externaldnsv1alpha1.ProviderAWS.String():
func SetCredential(ctx context.Context, kc client.Client, edns *api.ExternalDNS) error {
switch edns.Spec.Provider {
case api.ProviderAWS:
return setAWSCredential(ctx, kc, edns)

case externaldnsv1alpha1.ProviderCloudflare.String():
case api.ProviderCloudflare:
return setCloudflareCredentials(ctx, kc, edns)

case externaldnsv1alpha1.ProviderAzure.String():
case api.ProviderAzure:
return setAzureCredential(ctx, kc, edns)

case externaldnsv1alpha1.ProviderGoogle.String():
case api.ProviderGoogle:
return setGoogleCredential(ctx, kc, edns)

default:
Expand Down
6 changes: 3 additions & 3 deletions pkg/informers/dynamicWatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"reflect"
"sync"

externaldnsv1alpha1 "kubeops.dev/external-dns-operator/apis/external/v1alpha1"
api "kubeops.dev/external-dns-operator/apis/external/v1alpha1"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -108,11 +108,11 @@ func getRuntimeObject(gvk schema.GroupVersionKind) runtime.Object {
return unObj
}

func RegisterWatcher(ctx context.Context, crd *externaldnsv1alpha1.ExternalDNS, watcher *ObjectTracker, r client.Client) error {
func RegisterWatcher(ctx context.Context, crd *api.ExternalDNS, watcher *ObjectTracker, r client.Client) error {
sourceHandler := func(object client.Object) []reconcile.Request {
reconcileReq := make([]reconcile.Request, 0)

dnsList := &externaldnsv1alpha1.ExternalDNSList{}
dnsList := &api.ExternalDNSList{}

if err := r.List(ctx, dnsList); err != nil {
klog.Errorf("failed to list the external dns resources: %s", err.Error())
Expand Down
Loading

0 comments on commit 5d3e475

Please sign in to comment.