@@ -22,6 +22,7 @@ import (
2222 "sigs.k8s.io/controller-runtime/pkg/client"
2323
2424 fleetv1alpha1 "go.goms.io/fleet/apis/v1alpha1"
25+ "go.goms.io/fleet/pkg/metrics"
2526 "go.goms.io/fleet/pkg/utils"
2627 "go.goms.io/fleet/pkg/utils/controller"
2728 "go.goms.io/fleet/pkg/utils/informer"
@@ -59,6 +60,7 @@ type Reconciler struct {
5960}
6061
6162func (r * Reconciler ) Reconcile (ctx context.Context , key controller.QueueKey ) (ctrl.Result , error ) {
63+ startTime := time .Now ()
6264 name , ok := key .(string )
6365 if ! ok {
6466 err := fmt .Errorf ("get place key %+v not of type string" , key )
@@ -73,9 +75,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, key controller.QueueKey) (ct
7375 }
7476 placeRef := klog .KObj (placementOld )
7577 placementNew := placementOld .DeepCopy ()
78+ // add latency log
79+ defer func () {
80+ klog .V (2 ).InfoS ("ClusterResourcePlacement reconciliation loop ends" , "placement" , placeRef , "latency" , time .Since (startTime ).Milliseconds ())
81+ }()
7682
7783 // TODO: add finalizer logic if we need it in the future
78-
7984 klog .V (2 ).InfoS ("Start to reconcile a ClusterResourcePlacement" , "placement" , placeRef )
8085 // select the new clusters and record that in the placementNew status
8186 selectedClusters , scheduleErr := r .selectClusters (placementNew )
@@ -120,7 +125,9 @@ func (r *Reconciler) Reconcile(ctx context.Context, key controller.QueueKey) (ct
120125 }
121126 klog .V (2 ).InfoS ("Successfully persisted the intermediate scheduling result" , "placement" , placementOld .Name ,
122127 "totalClusters" , totalCluster , "totalResources" , totalResources )
123- // pick up the new version so placementNew can continue to update
128+ // pick up the newly updated schedule condition so that the last schedule time will change every time we run the reconcile loop
129+ meta .SetStatusCondition (& placementNew .Status .Conditions , * placementOld .GetCondition (string (fleetv1alpha1 .ResourcePlacementConditionTypeScheduled )))
130+ // pick up the new version so that we can update placementNew without getting it again
124131 placementNew .SetResourceVersion (placementOld .GetResourceVersion ())
125132
126133 // schedule works for each cluster by placing them in the cluster scoped namespace
@@ -254,17 +261,17 @@ func (r *Reconciler) updatePlacementScheduledCondition(placement *fleetv1alpha1.
254261 placementRef := klog .KObj (placement )
255262 schedCond := placement .GetCondition (string (fleetv1alpha1 .ResourcePlacementConditionTypeScheduled ))
256263 if scheduleErr == nil {
264+ if schedCond == nil || schedCond .Status != metav1 .ConditionTrue {
265+ klog .V (2 ).InfoS ("successfully scheduled all selected resources to their clusters" , "placement" , placementRef )
266+ r .Recorder .Event (placement , corev1 .EventTypeNormal , "ResourceScheduled" , "successfully scheduled all selected resources to their clusters" )
267+ }
257268 placement .SetConditions (metav1.Condition {
258269 Status : metav1 .ConditionTrue ,
259270 Type : string (fleetv1alpha1 .ResourcePlacementConditionTypeScheduled ),
260271 Reason : "ScheduleSucceeded" ,
261272 Message : "Successfully scheduled resources for placement" ,
262273 ObservedGeneration : placement .Generation ,
263274 })
264- if schedCond == nil || schedCond .Status != metav1 .ConditionTrue {
265- klog .V (2 ).InfoS ("successfully scheduled all selected resources to their clusters" , "placement" , placementRef )
266- r .Recorder .Event (placement , corev1 .EventTypeNormal , "ResourceScheduled" , "successfully scheduled all selected resources to their clusters" )
267- }
268275 } else {
269276 placement .SetConditions (metav1.Condition {
270277 Status : metav1 .ConditionFalse ,
@@ -293,8 +300,9 @@ func (r *Reconciler) updatePlacementAppliedCondition(placement *fleetv1alpha1.Cl
293300 Message : "Successfully applied resources to member clusters" ,
294301 ObservedGeneration : placement .Generation ,
295302 })
296- klog .V (2 ).InfoS ("successfully applied all selected resources" , "placement" , placementRef )
297303 if preAppliedCond == nil || preAppliedCond .Status != metav1 .ConditionTrue {
304+ klog .V (2 ).InfoS ("successfully applied all selected resources" , "placement" , placementRef )
305+ metrics .PlacementApplySucceedCount .WithLabelValues (placement .GetName ()).Inc ()
298306 r .Recorder .Event (placement , corev1 .EventTypeNormal , "ResourceApplied" , "successfully applied all selected resources" )
299307 }
300308 case errors .Is (applyErr , ErrStillPendingManifest ):
@@ -305,8 +313,8 @@ func (r *Reconciler) updatePlacementAppliedCondition(placement *fleetv1alpha1.Cl
305313 Message : applyErr .Error (),
306314 ObservedGeneration : placement .Generation ,
307315 })
308- klog .V (2 ).InfoS ("Some selected resources are still waiting to be applied" , "placement" , placementRef )
309316 if preAppliedCond == nil || preAppliedCond .Status == metav1 .ConditionTrue {
317+ klog .V (2 ).InfoS ("Some selected resources are still waiting to be applied" , "placement" , placementRef )
310318 r .Recorder .Event (placement , corev1 .EventTypeWarning , "ResourceApplyPending" , "Some applied resources are now waiting to be applied to the member cluster" )
311319 }
312320 default :
@@ -318,8 +326,9 @@ func (r *Reconciler) updatePlacementAppliedCondition(placement *fleetv1alpha1.Cl
318326 Message : applyErr .Error (),
319327 ObservedGeneration : placement .Generation ,
320328 })
321- klog .V (2 ).InfoS ("failed to apply some selected resources" , "placement" , placementRef )
322329 if preAppliedCond == nil || preAppliedCond .Status != metav1 .ConditionFalse {
330+ klog .V (2 ).InfoS ("failed to apply some selected resources" , "placement" , placementRef )
331+ metrics .PlacementApplyFailedCount .WithLabelValues (placement .GetName ()).Inc ()
323332 r .Recorder .Event (placement , corev1 .EventTypeWarning , "ResourceApplyFailed" , "failed to apply some selected resources" )
324333 }
325334 }
0 commit comments