@@ -99,15 +99,6 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
9999 return ctrl.Result {}, nil
100100 }
101101
102- // register finalizer if not yet registered
103- if ! prefix .Spec .PreserveInNetbox && ! controllerutil .ContainsFinalizer (prefix , PrefixFinalizerName ) {
104- debugLogger .Info ("adding the finalizer" )
105- controllerutil .AddFinalizer (prefix , PrefixFinalizerName )
106- if err := r .Update (ctx , prefix ); err != nil {
107- return ctrl.Result {}, err
108- }
109- }
110-
111102 /*
112103 1. try to lock the lease of the parent prefix if all of the following conditions are met
113104 - the prefix is owned by at least 1 prefixClaim
@@ -168,6 +159,19 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
168159 return ctrl.Result {}, fmt .Errorf ("failed at update prefix status: %w, " + "after reservation of prefix in netbox failed: %w" , updateStatusErr , err )
169160 }
170161
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+
171175 // update lastPrefixMetadata annotation
172176 if annotations == nil {
173177 annotations = make (map [string ]string )
@@ -197,16 +201,17 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
197201 r .Recorder .Event (prefix , corev1 .EventTypeWarning , "PrefixDescriptionTruncated" , "prefix was created with truncated description" )
198202 }
199203
200- debugLogger .Info (fmt .Sprintf ("reserved prefix in netbox, prefix: %s" , prefix .Spec .Prefix ))
201-
202- /* 3. unlock lease of parent prefix */
203- if ll != nil {
204- ll .Unlock ()
204+ // register finalizer if not yet registered
205+ if ! prefix .Spec .PreserveInNetbox && ! controllerutil .ContainsFinalizer (prefix , PrefixFinalizerName ) {
206+ debugLogger .Info ("adding the finalizer" )
207+ controllerutil .AddFinalizer (prefix , PrefixFinalizerName )
208+ if err := r .Update (ctx , prefix ); err != nil {
209+ return ctrl.Result {}, err
210+ }
205211 }
206212
207- /* 4. update status conditions */
208- prefix .Status .PrefixId = netboxPrefixModel .ID
209- prefix .Status .PrefixUrl = config .GetBaseUrl () + "/ipam/prefixes/" + strconv .FormatInt (netboxPrefixModel .ID , 10 )
213+ debugLogger .Info (fmt .Sprintf ("reserved prefix in netbox, prefix: %s" , prefix .Spec .Prefix ))
214+
210215 if err = r .SetConditionAndCreateEvent (ctx , prefix , netboxv1 .ConditionPrefixReadyTrue , corev1 .EventTypeNormal , "" ); err != nil {
211216 return ctrl.Result {}, err
212217 }
0 commit comments