Skip to content

Commit 110cbb7

Browse files
authored
Make unlocking more reliable (#59)
Enhance unlocking to be more reliable by moving it further up
1 parent 6f1333d commit 110cbb7

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

internal/controller/ipaddress_controller.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
161161
"after reservation of ip in netbox failed: %w", updateStatusErr, err)
162162
}
163163

164+
// 3. unlock lease of parent prefix
165+
if ll != nil {
166+
ll.Unlock()
167+
}
168+
169+
// 4. update status fields
170+
o.Status.IpAddressId = netboxIpAddressModel.ID
171+
o.Status.IpAddressUrl = config.GetBaseUrl() + "/ipam/ip-addresses/" + strconv.FormatInt(netboxIpAddressModel.ID, 10)
172+
err = r.Client.Status().Update(ctx, o)
173+
if err != nil {
174+
return ctrl.Result{}, err
175+
}
176+
164177
// update lastIpAddressMetadata annotation
165178
if annotations == nil {
166179
annotations = make(map[string]string)
@@ -204,14 +217,6 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
204217

205218
debugLogger.Info(fmt.Sprintf("reserved ip address in netbox, ip: %s", o.Spec.IpAddress))
206219

207-
// 3. unlock lease of parent prefix
208-
if ll != nil {
209-
ll.Unlock()
210-
}
211-
212-
// 4. update status conditions
213-
o.Status.IpAddressId = netboxIpAddressModel.ID
214-
o.Status.IpAddressUrl = config.GetBaseUrl() + "/ipam/ip-addresses/" + strconv.FormatInt(netboxIpAddressModel.ID, 10)
215220
err = r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpaddressReadyTrue, corev1.EventTypeNormal, "")
216221
if err != nil {
217222
return ctrl.Result{}, err

internal/controller/prefix_controller.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
159159
return ctrl.Result{}, fmt.Errorf("failed at update prefix status: %w, "+"after reservation of prefix in netbox failed: %w", updateStatusErr, err)
160160
}
161161

162+
/* 3. unlock lease of parent prefix */
163+
if ll != nil {
164+
ll.Unlock()
165+
}
166+
167+
/* 4. update status fields */
168+
prefix.Status.PrefixId = netboxPrefixModel.ID
169+
prefix.Status.PrefixUrl = config.GetBaseUrl() + "/ipam/prefixes/" + strconv.FormatInt(netboxPrefixModel.ID, 10)
170+
err = r.Client.Status().Update(ctx, prefix)
171+
if err != nil {
172+
return ctrl.Result{}, err
173+
}
174+
162175
// update lastPrefixMetadata annotation
163176
if annotations == nil {
164177
annotations = make(map[string]string)
@@ -199,14 +212,6 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
199212

200213
debugLogger.Info(fmt.Sprintf("reserved prefix in netbox, prefix: %s", prefix.Spec.Prefix))
201214

202-
/* 3. unlock lease of parent prefix */
203-
if ll != nil {
204-
ll.Unlock()
205-
}
206-
207-
/* 4. update status conditions */
208-
prefix.Status.PrefixId = netboxPrefixModel.ID
209-
prefix.Status.PrefixUrl = config.GetBaseUrl() + "/ipam/prefixes/" + strconv.FormatInt(netboxPrefixModel.ID, 10)
210215
if err = r.SetConditionAndCreateEvent(ctx, prefix, netboxv1.ConditionPrefixReadyTrue, corev1.EventTypeNormal, ""); err != nil {
211216
return ctrl.Result{}, err
212217
}

0 commit comments

Comments
 (0)