Skip to content

Commit

Permalink
Fix missing guarding condition in PrefixController
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybear327 committed Nov 12, 2024
1 parent 935a0e2 commit b4e22b4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
42 changes: 23 additions & 19 deletions internal/controller/prefix_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,32 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}, nil
}

// get the name of the parent prefix
leaseLockerNSN := types.NamespacedName{
Name: convertCIDRToLeaseLockName(prefixClaim.Status.ParentPrefix),
Namespace: r.OperatorNamespace,
}
ll, err = leaselocker.NewLeaseLocker(r.RestConfig, leaseLockerNSN, req.NamespacedName.String())
if err != nil {
return ctrl.Result{}, err
}
if prefixClaim.Status.ParentPrefix != msgCanNotInferParentPrefix {
// we can't restore from the restoration hash

lockCtx, cancel := context.WithCancel(ctx)
defer cancel()
// get the name of the parent prefix
leaseLockerNSN := types.NamespacedName{
Name: convertCIDRToLeaseLockName(prefixClaim.Status.ParentPrefix),
Namespace: r.OperatorNamespace,
}
ll, err = leaselocker.NewLeaseLocker(r.RestConfig, leaseLockerNSN, req.NamespacedName.String())
if err != nil {
return ctrl.Result{}, err
}

// create lock
if locked := ll.TryLock(lockCtx); !locked {
errorMsg := fmt.Sprintf("failed to lock parent prefix %s", prefixClaim.Status.ParentPrefix)
r.Recorder.Eventf(prefix, corev1.EventTypeWarning, "FailedToLockParentPrefix", errorMsg)
return ctrl.Result{
RequeueAfter: 2 * time.Second,
}, nil
lockCtx, cancel := context.WithCancel(ctx)
defer cancel()

// create lock
if locked := ll.TryLock(lockCtx); !locked {
errorMsg := fmt.Sprintf("failed to lock parent prefix %s", prefixClaim.Status.ParentPrefix)
r.Recorder.Eventf(prefix, corev1.EventTypeWarning, "FailedToLockParentPrefix", errorMsg)
return ctrl.Result{
RequeueAfter: 2 * time.Second,
}, nil
}
debugLogger.Info("successfully locked parent prefix %s", prefixClaim.Status.ParentPrefix)
}
debugLogger.Info("successfully locked parent prefix %s", prefixClaim.Status.ParentPrefix)
}

/* 2. reserve or update Prefix in netbox */
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/prefixclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (r *PrefixClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request)
debugLogger.Info("the prefix was not found, will create a new prefix object now")

if prefixClaim.Status.ParentPrefix != msgCanNotInferParentPrefix {
// in the case that we can't restore from the restoration hash, we do the following
// we can't restore from the restoration hash

/* 3. check if the lease for parent prefix is available */
leaseLockerNSN := types.NamespacedName{
Expand All @@ -215,7 +215,7 @@ func (r *PrefixClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
debugLogger.Info(fmt.Sprintf("successfully locked parent prefix %s", prefixClaim.Status.ParentPrefix))
} // else {
// in the case that we can restore from the restoration hash
// we can restore from the restoration hash
// we skip directly to try to reclaim Prefix using restorationHash
// }

Expand Down

0 comments on commit b4e22b4

Please sign in to comment.