From 10ad6870d641d0af6988ed3cf4f710a0da4cc337 Mon Sep 17 00:00:00 2001 From: Joel Studler Date: Thu, 12 Sep 2024 13:51:41 +0200 Subject: [PATCH] Enhance unlocking to be more reliable by moving it further up --- internal/controller/ipaddress_controller.go | 21 +++++++++++++-------- internal/controller/prefix_controller.go | 21 +++++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/internal/controller/ipaddress_controller.go b/internal/controller/ipaddress_controller.go index 9ecfea4..b510251 100644 --- a/internal/controller/ipaddress_controller.go +++ b/internal/controller/ipaddress_controller.go @@ -161,6 +161,19 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( "after reservation of ip in netbox failed: %w", updateStatusErr, err) } + // 3. unlock lease of parent prefix + if ll != nil { + ll.Unlock() + } + + // 4. update status fields + o.Status.IpAddressId = netboxIpAddressModel.ID + o.Status.IpAddressUrl = config.GetBaseUrl() + "/ipam/ip-addresses/" + strconv.FormatInt(netboxIpAddressModel.ID, 10) + err = r.Client.Status().Update(ctx, o) + if err != nil { + return ctrl.Result{}, err + } + // update lastIpAddressMetadata annotation if annotations == nil { annotations = make(map[string]string) @@ -204,14 +217,6 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( debugLogger.Info(fmt.Sprintf("reserved ip address in netbox, ip: %s", o.Spec.IpAddress)) - // 3. unlock lease of parent prefix - if ll != nil { - ll.Unlock() - } - - // 4. update status conditions - o.Status.IpAddressId = netboxIpAddressModel.ID - o.Status.IpAddressUrl = config.GetBaseUrl() + "/ipam/ip-addresses/" + strconv.FormatInt(netboxIpAddressModel.ID, 10) err = r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpaddressReadyTrue, corev1.EventTypeNormal, "") if err != nil { return ctrl.Result{}, err diff --git a/internal/controller/prefix_controller.go b/internal/controller/prefix_controller.go index 301c535..d77e9f4 100644 --- a/internal/controller/prefix_controller.go +++ b/internal/controller/prefix_controller.go @@ -159,6 +159,19 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr return ctrl.Result{}, fmt.Errorf("failed at update prefix status: %w, "+"after reservation of prefix in netbox failed: %w", updateStatusErr, err) } + /* 3. unlock lease of parent prefix */ + if ll != nil { + ll.Unlock() + } + + /* 4. update status fields */ + prefix.Status.PrefixId = netboxPrefixModel.ID + prefix.Status.PrefixUrl = config.GetBaseUrl() + "/ipam/prefixes/" + strconv.FormatInt(netboxPrefixModel.ID, 10) + err = r.Client.Status().Update(ctx, prefix) + if err != nil { + return ctrl.Result{}, err + } + // update lastPrefixMetadata annotation if annotations == nil { annotations = make(map[string]string) @@ -199,14 +212,6 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr debugLogger.Info(fmt.Sprintf("reserved prefix in netbox, prefix: %s", prefix.Spec.Prefix)) - /* 3. unlock lease of parent prefix */ - if ll != nil { - ll.Unlock() - } - - /* 4. update status conditions */ - prefix.Status.PrefixId = netboxPrefixModel.ID - prefix.Status.PrefixUrl = config.GetBaseUrl() + "/ipam/prefixes/" + strconv.FormatInt(netboxPrefixModel.ID, 10) if err = r.SetConditionAndCreateEvent(ctx, prefix, netboxv1.ConditionPrefixReadyTrue, corev1.EventTypeNormal, ""); err != nil { return ctrl.Result{}, err }