Skip to content

Commit

Permalink
Enhance unlocking to be more reliable by moving it further up
Browse files Browse the repository at this point in the history
  • Loading branch information
jstudler authored and henrybear327 committed Sep 24, 2024
1 parent e473050 commit 10ad687
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
21 changes: 13 additions & 8 deletions internal/controller/ipaddress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
21 changes: 13 additions & 8 deletions internal/controller/prefix_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 10ad687

Please sign in to comment.