From b09fc9028df8e9cb53bc8870792949aaae8f56ca Mon Sep 17 00:00:00 2001 From: superflo22 Date: Sat, 12 Apr 2025 13:26:27 +0200 Subject: [PATCH] use controller runtime createorupdate --- .../externaldnswatcher_controller.go | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/internal/controller/externaldnswatcher_controller.go b/internal/controller/externaldnswatcher_controller.go index 413dc4f..e8459c8 100644 --- a/internal/controller/externaldnswatcher_controller.go +++ b/internal/controller/externaldnswatcher_controller.go @@ -21,7 +21,6 @@ import ( "fmt" dnsv1alpha1 "git.mayers.cloud/superflo22/split-horizon-operator/api/v1alpha1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "reflect" "sigs.k8s.io/controller-runtime/pkg/handler" gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" "strings" @@ -100,6 +99,7 @@ func (r *ExternalDNSWatcherReconciler) Reconcile(ctx context.Context, req ctrl.R Namespace: route.Namespace, Name: recordName, }, &existingRecord) + if err != nil { log.Info("Creating new Record", "recordName", recordName) @@ -132,32 +132,23 @@ func (r *ExternalDNSWatcherReconciler) Reconcile(ctx context.Context, req ctrl.R } else { log.Info("Created new Record", "name", record.Name) } + return ctrl.Result{}, nil } // Step 6: Update existing TechnitiumRecord - if existingRecord.Spec.RecordData[network] != nil { - // Check if the IPs are different - if !reflect.DeepEqual(existingRecord.Spec.RecordData[network], gatewayIPs) { - log.Info("Updating existing Record", "recordName", recordName) + log.Info("Updating existing Record", "recordName", recordName) - existingRecord.Spec.RecordData[network] = gatewayIPs - if err := r.Update(ctx, &existingRecord); err != nil { - log.Error(err, "Failed to update TechnitiumRecord") - } else { - log.Info("Updated IPs in existing Record", "name", recordName) - } - } - } else { - log.Info("Adding new network to existing Record", "recordName", recordName) - - // Add the new network and IPs + // Add the new network and IPs + _, err = ctrl.CreateOrUpdate(ctx, r.Client, &existingRecord, func() error { existingRecord.Spec.RecordData[network] = gatewayIPs - if err := r.Update(ctx, &existingRecord); err != nil { - log.Error(err, "Failed to update TechnitiumRecord") - } else { - log.Info("Updated new network in existing Record", "name", recordName) - } + return nil + }) + if err != nil { + log.Error(err, "Failed to update TechnitiumRecord") + } else { + log.Info("Updated existing Record", "name", recordName) } + } }