Skip to content

Commit 4804f10

Browse files
authored
feat: update placement status metric (#16)
1 parent b89ea22 commit 4804f10

File tree

4 files changed

+1481
-142
lines changed

4 files changed

+1481
-142
lines changed

pkg/controllers/clusterresourceplacement/controller.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, key controller.QueueKey) (ct
8787
return ctrl.Result{}, controller.NewUpdateIgnoreConflictError(err)
8888
}
8989
}
90-
90+
defer emitPlacementStatusMetric(&crp)
9191
return r.handleUpdate(ctx, &crp)
9292
}
9393

@@ -105,14 +105,14 @@ func (r *Reconciler) handleDelete(ctx context.Context, crp *fleetv1beta1.Cluster
105105
return ctrl.Result{}, err
106106
}
107107

108+
metrics.FleetPlacementStatusLastTimeStampSeconds.DeletePartialMatch(prometheus.Labels{"name": crp.Name})
108109
controllerutil.RemoveFinalizer(crp, fleetv1beta1.ClusterResourcePlacementCleanupFinalizer)
109110
if err := r.Client.Update(ctx, crp); err != nil {
110111
klog.ErrorS(err, "Failed to remove crp finalizer", "clusterResourcePlacement", crpKObj)
111112
return ctrl.Result{}, err
112113
}
113114
klog.V(2).InfoS("Removed crp-cleanup finalizer", "clusterResourcePlacement", crpKObj)
114115
r.Recorder.Event(crp, corev1.EventTypeNormal, "PlacementCleanupFinalizerRemoved", "Deleted the snapshots and removed the placement cleanup finalizer")
115-
metrics.FleetPlacementStatus.Delete(prometheus.Labels{"name": crp.Name})
116116
return ctrl.Result{}, nil
117117
}
118118

@@ -233,12 +233,10 @@ func (r *Reconciler) handleUpdate(ctx context.Context, crp *fleetv1beta1.Cluster
233233
klog.V(2).InfoS("Placement has finished the rollout process and reached the desired status", "clusterResourcePlacement", crpKObj, "generation", crp.Generation)
234234
r.Recorder.Event(crp, corev1.EventTypeNormal, "PlacementRolloutCompleted", "Placement has finished the rollout process and reached the desired status")
235235
}
236-
metrics.FleetPlacementStatus.WithLabelValues(crp.Name).Set(1)
237236
// We don't need to requeue any request now by watching the binding changes
238237
return ctrl.Result{}, nil
239238
}
240239

241-
metrics.FleetPlacementStatus.WithLabelValues(crp.Name).Set(0)
242240
if !isClusterScheduled {
243241
// Note:
244242
// If the scheduledCondition is failed, it means the placement requirement cannot be satisfied fully. For example,
@@ -1046,3 +1044,33 @@ func isRolloutCompleted(crp *fleetv1beta1.ClusterResourcePlacement) bool {
10461044
func isCRPScheduled(crp *fleetv1beta1.ClusterResourcePlacement) bool {
10471045
return condition.IsConditionStatusTrue(crp.GetCondition(string(fleetv1beta1.ClusterResourcePlacementScheduledConditionType)), crp.Generation)
10481046
}
1047+
1048+
func emitPlacementStatusMetric(crp *fleetv1beta1.ClusterResourcePlacement) {
1049+
// Check CRP Scheduled condition.
1050+
status := "nil"
1051+
cond := crp.GetCondition(string(fleetv1beta1.ClusterResourcePlacementScheduledConditionType))
1052+
if !condition.IsConditionStatusTrue(cond, crp.Generation) {
1053+
if cond != nil && cond.ObservedGeneration == crp.Generation {
1054+
status = string(cond.Status)
1055+
}
1056+
metrics.FleetPlacementStatusLastTimeStampSeconds.WithLabelValues(crp.Name, strconv.FormatInt(crp.Generation, 10), string(fleetv1beta1.ClusterResourcePlacementScheduledConditionType), status).SetToCurrentTime()
1057+
return
1058+
}
1059+
1060+
// Check CRP expected conditions.
1061+
expectedCondTypes := determineExpectedCRPAndResourcePlacementStatusCondType(crp)
1062+
for _, condType := range expectedCondTypes {
1063+
cond = crp.GetCondition(string(condType.ClusterResourcePlacementConditionType()))
1064+
if !condition.IsConditionStatusTrue(cond, crp.Generation) {
1065+
if cond != nil && cond.ObservedGeneration == crp.Generation {
1066+
status = string(cond.Status)
1067+
}
1068+
metrics.FleetPlacementStatusLastTimeStampSeconds.WithLabelValues(crp.Name, strconv.FormatInt(crp.Generation, 10), string(condType.ClusterResourcePlacementConditionType()), status).SetToCurrentTime()
1069+
return
1070+
}
1071+
}
1072+
1073+
// Emit the "Completed" condition metric to indicate that the CRP has completed.
1074+
// This condition is used solely for metric reporting purposes.
1075+
metrics.FleetPlacementStatusLastTimeStampSeconds.WithLabelValues(crp.Name, strconv.FormatInt(crp.Generation, 10), "Completed", string(metav1.ConditionTrue)).SetToCurrentTime()
1076+
}

0 commit comments

Comments
 (0)