@@ -73,17 +73,21 @@ type Metal3ClusterReconciler struct {
7373// Reconcile reads that state of the cluster for a Metal3Cluster object and makes changes based on the state read
7474// and what is in the Metal3Cluster.Spec.
7575func (r * Metal3ClusterReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (_ ctrl.Result , rerr error ) {
76- clusterLog := log .Log .WithName (clusterControllerName ).WithValues ("metal3-cluster" , req .NamespacedName )
76+ clusterLog := log .Log .WithName (clusterControllerName ).WithValues (
77+ "metal3-cluster" , req .NamespacedName ,
78+ )
79+ defer logReconcileOutcome (clusterLog , clusterControllerName , & rerr )
7780
7881 // Fetch the Metal3Cluster instance
7982 metal3Cluster := & infrav1.Metal3Cluster {}
8083
8184 if err := r .Client .Get (ctx , req .NamespacedName , metal3Cluster ); err != nil {
8285 if apierrors .IsNotFound (err ) {
86+ logLogicGate (clusterLog , "Metal3Cluster resource not found in cache, skipping" )
8387 return ctrl.Result {}, nil
8488 }
8589
86- return ctrl.Result {}, err
90+ return ctrl.Result {}, errors . Wrap ( err , "Failed to get Metal3Cluster" )
8791 }
8892
8993 // This is checking if default values are changed or not if the default
@@ -121,6 +125,8 @@ func (r *Metal3ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
121125 // Fetch the Cluster.
122126 cluster , err := util .GetOwnerCluster (ctx , r .Client , metal3Cluster .ObjectMeta )
123127 if err != nil {
128+ // Only log error once, with full context
129+ clusterLog .Error (err , "Unable to get owner cluster for Metal3Cluster" )
124130 invalidConfigError := capierrors .InvalidConfigurationClusterError
125131 metal3Cluster .Status .FailureReason = & invalidConfigError
126132 metal3Cluster .Status .FailureMessage = ptr .To ("Unable to get owner cluster" )
@@ -142,6 +148,10 @@ func (r *Metal3ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
142148 // Return early if Metal3Cluster or Cluster is paused.
143149 var isPaused , requeue bool
144150 if isPaused , requeue , err = paused .EnsurePausedCondition (ctx , r .Client , cluster , metal3Cluster ); err != nil || isPaused || requeue {
151+ if err != nil {
152+ clusterLog .Error (err , "Failed to evaluate paused condition" )
153+ }
154+ logLogicGate (clusterLog , "Pause condition active" , "isPaused" , isPaused , "requeue" , requeue )
145155 return ctrl.Result {Requeue : requeue , RequeueAfter : requeueAfter }, nil
146156 }
147157
@@ -158,13 +168,14 @@ func (r *Metal3ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
158168
159169 // Handle deleted clusters
160170 if ! metal3Cluster .DeletionTimestamp .IsZero () {
171+ logLogicGate (clusterLog , "Metal3Cluster has deletion timestamp, entering delete reconciliation" )
161172 v1beta2conditions .Set (metal3Cluster , metav1.Condition {
162173 Type : infrav1 .Metal3ClusterReadyV1Beta2Condition ,
163174 Status : metav1 .ConditionFalse ,
164175 Reason : infrav1 .Metal3ClusterDeletingV1Beta2Reason ,
165176 })
166177 var res ctrl.Result
167- res , err = reconcileDelete (ctx , clusterMgr )
178+ res , err = reconcileDelete (ctx , clusterMgr , clusterLog )
168179 // Requeue if the reconcile failed because the ClusterCache was locked for
169180 // the current cluster because of concurrent access.
170181 if errors .Is (err , clustercache .ErrClusterNotConnected ) {
@@ -175,7 +186,8 @@ func (r *Metal3ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
175186 }
176187
177188 // Handle non-deleted clusters
178- res , err := reconcileNormal (ctx , clusterMgr )
189+ logLogicGate (clusterLog , "Metal3Cluster is active, entering normal reconciliation" )
190+ res , err := reconcileNormal (ctx , clusterMgr , clusterLog )
179191 // Requeue if the reconcile failed because the ClusterCache was locked for
180192 // the current cluster because of concurrent access.
181193 if errors .Is (err , clustercache .ErrClusterNotConnected ) {
@@ -228,42 +240,52 @@ func patchMetal3Cluster(ctx context.Context, patchHelper *v1beta1patch.Helper, m
228240 return patchHelper .Patch (ctx , metal3Cluster , options ... )
229241}
230242
231- func reconcileNormal (ctx context.Context , clusterMgr baremetal.ClusterManagerInterface ) (ctrl.Result , error ) { //nolint:unparam
243+ func reconcileNormal (ctx context.Context , clusterMgr baremetal.ClusterManagerInterface , logger logr. Logger ) (ctrl.Result , error ) { //nolint:unparam
232244 // If the Metal3Cluster doesn't have finalizer, add it.
245+ logLogicGate (logger , "Ensuring finalizer exists on Metal3Cluster" )
233246 clusterMgr .SetFinalizer ()
234247
235248 // Create the Metal3 cluster (no-op)
249+ logLogicGate (logger , "Invoking cluster creation/update flow" )
236250 if err := clusterMgr .Create (ctx ); err != nil {
237- return ctrl.Result {}, err
251+ return ctrl.Result {}, errors . Wrap ( err , "Failed during Metal3Cluster create" )
238252 }
239253
240254 // Set APIEndpoints so the Cluster API Cluster Controller can pull it
241255 if err := clusterMgr .UpdateClusterStatus (); err != nil {
242256 return ctrl.Result {}, errors .Wrap (err , "failed to get ip for the API endpoint" )
243257 }
258+ logLogicGate (logger , "Successfully reconciled Metal3Cluster status" )
244259
245260 return ctrl.Result {}, nil
246261}
247262
248263func reconcileDelete (ctx context.Context ,
249- clusterMgr baremetal.ClusterManagerInterface ) (ctrl.Result , error ) {
264+ clusterMgr baremetal.ClusterManagerInterface ,
265+ logger logr.Logger ,
266+ ) (ctrl.Result , error ) {
250267 // Verify that no metal3machine depend on the metal3cluster
268+ logLogicGate (logger , "Checking for descendant resources before delete" )
251269 descendants , err := clusterMgr .CountDescendants (ctx )
252270 if err != nil {
253- return ctrl.Result {}, err
271+ return ctrl.Result {}, errors . Wrap ( err , "Failed to count descendants" )
254272 }
255273 if descendants > 0 {
256274 // Requeue so we can check the next time to see if there are still any
257275 // descendants left.
276+ logLogicGate (logger , "Descendants still present, will requeue delete" , "descendantCount" , descendants )
258277 return ctrl.Result {Requeue : true , RequeueAfter : requeueAfter }, nil
259278 }
260279
280+ logLogicGate (logger , "No descendants found, deleting Metal3Cluster resources" )
261281 if err := clusterMgr .Delete (); err != nil {
262282 return ctrl.Result {}, errors .Wrap (err , "failed to delete Metal3Cluster" )
263283 }
264284
265285 // Cluster is deleted so remove the finalizer.
286+ logLogicGate (logger , "Removing Metal3Cluster finalizer" )
266287 clusterMgr .UnsetFinalizer ()
288+ logLogicGate (logger , "Delete reconciliation completed" )
267289
268290 return ctrl.Result {}, nil
269291}
0 commit comments