@@ -56,30 +56,30 @@ type PrefixReconciler struct {
5656 RestConfig * rest.Config
5757}
5858
59- // +kubebuilder:rbac:groups=netbox.dev,resources=prefixes,verbs=get;list;watch;create;update;patch;delete
60- // +kubebuilder:rbac:groups=netbox.dev,resources=prefixes/status,verbs=get;update;patch
61- // +kubebuilder:rbac:groups=netbox.dev,resources=prefixes/finalizers,verbs=update
59+ //+kubebuilder:rbac:groups=netbox.dev,resources=prefixes,verbs=get;list;watch;create;update;patch;delete
60+ //+kubebuilder:rbac:groups=netbox.dev,resources=prefixes/status,verbs=get;update;patch
61+ //+kubebuilder:rbac:groups=netbox.dev,resources=prefixes/finalizers,verbs=update
6262
6363// Reconcile is part of the main kubernetes reconciliation loop which aims to
6464// move the current state of the cluster closer to the desired state.
6565func (r * PrefixReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
6666 logger := log .FromContext (ctx )
6767 debugLogger := logger .V (4 )
6868
69- logger .Info ("prefix reconcile loop started" )
69+ logger .Info ("reconcile loop started" )
7070
7171 /* 0. check if the matching Prefix object exists */
72- prefix := & netboxv1.Prefix {}
73- if err := r .Client .Get (ctx , req .NamespacedName , prefix ); err != nil {
72+ o := & netboxv1.Prefix {}
73+ if err := r .Client .Get (ctx , req .NamespacedName , o ); err != nil {
7474 return ctrl.Result {}, client .IgnoreNotFound (err )
7575 }
7676
7777 // if being deleted
78- if ! prefix .ObjectMeta .DeletionTimestamp .IsZero () {
79- if controllerutil .ContainsFinalizer (prefix , PrefixFinalizerName ) {
80- if ! prefix .Spec .PreserveInNetbox {
81- if err := r .NetboxClient .DeletePrefix (prefix .Status .PrefixId ); err != nil {
82- if errReport := r .EventStatusRecorder .Report (ctx , prefix , netboxv1 .ConditionPrefixReadyFalseDeletionFailed , corev1 .EventTypeWarning , err ); errReport != nil {
78+ if ! o .ObjectMeta .DeletionTimestamp .IsZero () {
79+ if controllerutil .ContainsFinalizer (o , PrefixFinalizerName ) {
80+ if ! o .Spec .PreserveInNetbox {
81+ if err := r .NetboxClient .DeletePrefix (o .Status .PrefixId ); err != nil {
82+ if errReport := r .EventStatusRecorder .Report (ctx , o , netboxv1 .ConditionPrefixReadyFalseDeletionFailed , corev1 .EventTypeWarning , err ); errReport != nil {
8383 return ctrl.Result {}, errReport
8484 }
8585
@@ -88,11 +88,11 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
8888 }
8989
9090 debugLogger .Info ("removing the finalizer" )
91- if removed := controllerutil .RemoveFinalizer (prefix , PrefixFinalizerName ); ! removed {
91+ if removed := controllerutil .RemoveFinalizer (o , PrefixFinalizerName ); ! removed {
9292 return ctrl.Result {}, errors .New ("failed to remove the finalizer" )
9393 }
9494
95- if err := r .Update (ctx , prefix ); err != nil {
95+ if err := r .Update (ctx , o ); err != nil {
9696 return ctrl.Result {}, err
9797 }
9898 }
@@ -102,18 +102,18 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
102102 }
103103
104104 // Set ready to false initially
105- if apismeta .FindStatusCondition (prefix .Status .Conditions , netboxv1 .ConditionReadyFalseNewResource .Type ) == nil {
106- err := r .EventStatusRecorder .Report (ctx , prefix , netboxv1 .ConditionReadyFalseNewResource , corev1 .EventTypeNormal , nil )
105+ if apismeta .FindStatusCondition (o .Status .Conditions , netboxv1 .ConditionReadyFalseNewResource .Type ) == nil {
106+ err := r .EventStatusRecorder .Report (ctx , o , netboxv1 .ConditionReadyFalseNewResource , corev1 .EventTypeNormal , nil )
107107 if err != nil {
108108 return ctrl.Result {}, fmt .Errorf ("failed to initialise Ready condition: %w, " , err )
109109 }
110110 }
111111
112112 // register finalizer if not yet registered
113- if ! prefix .Spec .PreserveInNetbox && ! controllerutil .ContainsFinalizer (prefix , PrefixFinalizerName ) {
113+ if ! o .Spec .PreserveInNetbox && ! controllerutil .ContainsFinalizer (o , PrefixFinalizerName ) {
114114 debugLogger .Info ("adding the finalizer" )
115- controllerutil .AddFinalizer (prefix , PrefixFinalizerName )
116- if err := r .Update (ctx , prefix ); err != nil {
115+ controllerutil .AddFinalizer (o , PrefixFinalizerName )
116+ if err := r .Update (ctx , o ); err != nil {
117117 return ctrl.Result {}, err
118118 }
119119 }
@@ -123,10 +123,10 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
123123 - the prefix is owned by at least 1 prefixClaim
124124 - the prefix status condition is not ready
125125 */
126- ownerReferences := prefix .ObjectMeta .OwnerReferences
126+ ownerReferences := o .ObjectMeta .OwnerReferences
127127 var ll * leaselocker.LeaseLocker
128128 var err error
129- if len (ownerReferences ) > 0 /* len(nil array) = 0 */ && ! apismeta .IsStatusConditionTrue (prefix .Status .Conditions , "Ready" ) {
129+ if len (ownerReferences ) > 0 /* len(nil array) = 0 */ && ! apismeta .IsStatusConditionTrue (o .Status .Conditions , "Ready" ) {
130130 // get prefixClaim
131131 ownerReferencesLookupKey := types.NamespacedName {
132132 Name : ownerReferences [0 ].Name , // TODO(henrybear327): Under what condition would we have more than 1 ownerReferences? What should we do with it?
@@ -139,7 +139,7 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
139139
140140 if prefixClaim .Status .SelectedParentPrefix == "" {
141141 // the parent prefix is not selected
142- if errReport := r .EventStatusRecorder .Report (ctx , prefix , netboxv1 .ConditionPrefixReadyFalse , corev1 .EventTypeWarning , fmt .Errorf ("%s" , "the parent prefix is not selected" )); errReport != nil {
142+ if errReport := r .EventStatusRecorder .Report (ctx , o , netboxv1 .ConditionPrefixReadyFalse , corev1 .EventTypeWarning , fmt .Errorf ("%s" , "the parent prefix is not selected" )); errReport != nil {
143143 return ctrl.Result {}, errReport
144144 }
145145 return ctrl.Result {
@@ -164,9 +164,10 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
164164 defer cancel ()
165165
166166 // create lock
167- if locked := ll .TryLock (lockCtx ); ! locked {
167+ locked := ll .TryLock (lockCtx )
168+ if ! locked {
168169 errorMsg := fmt .Sprintf ("failed to lock parent prefix %s" , prefixClaim .Status .SelectedParentPrefix )
169- r .EventStatusRecorder .Recorder ().Eventf (prefix , corev1 .EventTypeWarning , "FailedToLockParentPrefix" , errorMsg )
170+ r .EventStatusRecorder .Recorder ().Eventf (o , corev1 .EventTypeWarning , "FailedToLockParentPrefix" , errorMsg )
170171 return ctrl.Result {
171172 RequeueAfter : 2 * time .Second ,
172173 }, nil
@@ -177,26 +178,26 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
177178
178179 /* 2. reserve or update Prefix in netbox */
179180 accessor := apismeta .NewAccessor ()
180- annotations , err := accessor .Annotations (prefix )
181+ annotations , err := accessor .Annotations (o )
181182 if err != nil {
182183 return ctrl.Result {}, err
183184 }
184185
185- prefixModel , err := generateNetboxPrefixModelFromPrefixSpec (& prefix .Spec , req , annotations [PXManagedCustomFieldsAnnotationName ])
186+ prefixModel , err := generateNetboxPrefixModelFromPrefixSpec (& o .Spec , req , annotations [PXManagedCustomFieldsAnnotationName ])
186187 if err != nil {
187188 return ctrl.Result {}, err
188189 }
189190
190191 netboxPrefixModel , err := r .NetboxClient .ReserveOrUpdatePrefix (prefixModel )
191192 if err != nil {
192- if errors .Is (err , api .ErrRestorationHashMismatch ) && prefix .Status .PrefixId == 0 {
193+ if errors .Is (err , api .ErrRestorationHashMismatch ) && o .Status .PrefixId == 0 {
193194 // if there is a restoration hash mismatch and the PrefixId status field is not set,
194195 // delete the prefix so it can be recreated by the prefix claim controller
195196 // this will only affect resources that are created by a claim controller (and have a restoration hash custom field
196- logger .Info ("restoration hash mismatch, deleting prefix custom resource" , "prefix" , prefix .Spec .Prefix )
197- err = r .Client .Delete (ctx , prefix )
197+ logger .Info ("restoration hash mismatch, deleting prefix custom resource" , "prefix" , o .Spec .Prefix )
198+ err = r .Client .Delete (ctx , o )
198199 if err != nil {
199- if updateStatusErr := r .EventStatusRecorder .Report (ctx , prefix , netboxv1 .ConditionPrefixReadyFalse ,
200+ if updateStatusErr := r .EventStatusRecorder .Report (ctx , o , netboxv1 .ConditionPrefixReadyFalse ,
200201 corev1 .EventTypeWarning , err ); updateStatusErr != nil {
201202 return ctrl.Result {}, fmt .Errorf ("failed to update prefix status: %w, " +
202203 "after deletion of prefix cr failed: %w" , updateStatusErr , err )
@@ -206,7 +207,7 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
206207 return ctrl.Result {}, nil
207208 }
208209
209- if updateStatusErr := r .EventStatusRecorder .Report (ctx , prefix , netboxv1 .ConditionPrefixReadyFalse ,
210+ if updateStatusErr := r .EventStatusRecorder .Report (ctx , o , netboxv1 .ConditionPrefixReadyFalse ,
210211 corev1 .EventTypeWarning , err ); updateStatusErr != nil {
211212 return ctrl.Result {}, fmt .Errorf ("failed to update prefix status: %w, " +
212213 "after reservation of prefix netbox failed: %w" , updateStatusErr , err )
@@ -220,9 +221,9 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
220221 }
221222
222223 /* 4. update status fields */
223- prefix .Status .PrefixId = netboxPrefixModel .ID
224- prefix .Status .PrefixUrl = config .GetBaseUrl () + "/ipam/prefixes/" + strconv .FormatInt (netboxPrefixModel .ID , 10 )
225- err = r .Client .Status ().Update (ctx , prefix )
224+ o .Status .PrefixId = netboxPrefixModel .ID
225+ o .Status .PrefixUrl = config .GetBaseUrl () + "/ipam/prefixes/" + strconv .FormatInt (netboxPrefixModel .ID , 10 )
226+ err = r .Client .Status ().Update (ctx , o )
226227 if err != nil {
227228 return ctrl.Result {}, err
228229 }
@@ -231,33 +232,33 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
231232 annotations = make (map [string ]string , 1 )
232233 }
233234
234- annotations [PXManagedCustomFieldsAnnotationName ], err = generateManagedCustomFieldsAnnotation (prefix .Spec .CustomFields )
235+ annotations [PXManagedCustomFieldsAnnotationName ], err = generateManagedCustomFieldsAnnotation (o .Spec .CustomFields )
235236 if err != nil {
236237 logger .Error (err , "failed to update last metadata annotation" )
237238 return ctrl.Result {Requeue : true }, nil
238239 }
239240
240- err = accessor .SetAnnotations (prefix , annotations )
241+ err = accessor .SetAnnotations (o , annotations )
241242 if err != nil {
242243 return ctrl.Result {}, err
243244 }
244245
245246 // update object to store lastIpAddressMetadata annotation
246- if err := r .Update (ctx , prefix ); err != nil {
247+ if err := r .Update (ctx , o ); err != nil {
247248 return ctrl.Result {}, err
248249 }
249250
250251 // check if the created prefix contains the entire description from spec
251- if _ , found := strings .CutPrefix (netboxPrefixModel .Description , req .NamespacedName .String ()+ " // " + prefix .Spec .Description ); ! found {
252- r .EventStatusRecorder .Recorder ().Event (prefix , corev1 .EventTypeWarning , "PrefixDescriptionTruncated" , "prefix was created with truncated description" )
252+ if _ , found := strings .CutPrefix (netboxPrefixModel .Description , req .NamespacedName .String ()+ " // " + o .Spec .Description ); ! found {
253+ r .EventStatusRecorder .Recorder ().Event (o , corev1 .EventTypeWarning , "PrefixDescriptionTruncated" , "prefix was created with truncated description" )
253254 }
254255
255- debugLogger .Info (fmt .Sprintf ("reserved prefix in netbox, prefix: %s" , prefix .Spec .Prefix ))
256- if err = r .EventStatusRecorder .Report (ctx , prefix , netboxv1 .ConditionPrefixReadyTrue , corev1 .EventTypeNormal , nil ); err != nil {
256+ debugLogger .Info (fmt .Sprintf ("reserved prefix in netbox, prefix: %s" , o .Spec .Prefix ))
257+ if err = r .EventStatusRecorder .Report (ctx , o , netboxv1 .ConditionPrefixReadyTrue , corev1 .EventTypeNormal , nil ); err != nil {
257258 return ctrl.Result {}, err
258259 }
259260
260- logger .Info ("prefix reconcile loop finished" )
261+ logger .Info ("reconcile loop finished" )
261262
262263 return ctrl.Result {}, nil
263264}
0 commit comments