Skip to content

Commit 6f85e45

Browse files
committed
add placement status metrics
1 parent 6160725 commit 6f85e45

File tree

4 files changed

+427
-24
lines changed

4 files changed

+427
-24
lines changed

Diff for: pkg/controllers/clusterresourceplacement/controller.go

+30-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ func (r *Reconciler) handleDelete(ctx context.Context, crp *fleetv1beta1.Cluster
101101
}
102102
klog.V(2).InfoS("Removed crp-cleanup finalizer", "clusterResourcePlacement", crpKObj)
103103
r.Recorder.Event(crp, corev1.EventTypeNormal, "PlacementCleanupFinalizerRemoved", "Deleted the snapshots and removed the placement cleanup finalizer")
104-
metrics.FleetPlacementStatus.Delete(prometheus.Labels{"name": crp.Name})
104+
metrics.FleetPlacementComplete.DeletePartialMatch(prometheus.Labels{"name": crp.Name})
105+
metrics.FleetPlacementStatus.DeletePartialMatch(prometheus.Labels{"name": crp.Name})
105106
return ctrl.Result{}, nil
106107
}
107108

@@ -177,6 +178,8 @@ func (r *Reconciler) handleUpdate(ctx context.Context, crp *fleetv1beta1.Cluster
177178
klog.ErrorS(updateErr, "Failed to update the status", "clusterResourcePlacement", crpKObj)
178179
return ctrl.Result{}, controller.NewUpdateIgnoreConflictError(updateErr)
179180
}
181+
metrics.FleetPlacementStatus.DeletePartialMatch(prometheus.Labels{"name": crp.Name})
182+
metrics.FleetPlacementStatus.WithLabelValues(crp.Name, strconv.FormatInt(crp.Generation, 10), scheduleCondition.Type, string(scheduleCondition.Status)).SetToCurrentTime()
180183
return ctrl.Result{}, err
181184
}
182185

@@ -214,6 +217,7 @@ func (r *Reconciler) handleUpdate(ctx context.Context, crp *fleetv1beta1.Cluster
214217
r.Recorder.Event(crp, corev1.EventTypeNormal, i.EventReasonForTrue(), i.EventMessageForTrue())
215218
}
216219
}
220+
emitPlacementStatusMetric(crp)
217221

218222
// Rollout is considered to be completed when all the expected condition types are set to the
219223
// True status.
@@ -222,12 +226,15 @@ func (r *Reconciler) handleUpdate(ctx context.Context, crp *fleetv1beta1.Cluster
222226
klog.V(2).InfoS("Placement has finished the rollout process and reached the desired status", "clusterResourcePlacement", crpKObj, "generation", crp.Generation)
223227
r.Recorder.Event(crp, corev1.EventTypeNormal, "PlacementRolloutCompleted", "Placement has finished the rollout process and reached the desired status")
224228
}
225-
metrics.FleetPlacementStatus.WithLabelValues(crp.Name).Set(1)
229+
metrics.FleetPlacementComplete.DeletePartialMatch(prometheus.Labels{"name": crp.Name})
230+
metrics.FleetPlacementComplete.WithLabelValues(crp.Name, "true").SetToCurrentTime()
226231
// We don't need to requeue any request now by watching the binding changes
227232
return ctrl.Result{}, nil
228233
}
229234

230-
metrics.FleetPlacementStatus.WithLabelValues(crp.Name).Set(0)
235+
metrics.FleetPlacementComplete.DeletePartialMatch(prometheus.Labels{"name": crp.Name})
236+
metrics.FleetPlacementComplete.WithLabelValues(crp.Name, "false").SetToCurrentTime()
237+
231238
if !isClusterScheduled {
232239
// Note:
233240
// If the scheduledCondition is failed, it means the placement requirement cannot be satisfied fully. For example,
@@ -1035,3 +1042,23 @@ func isRolloutCompleted(crp *fleetv1beta1.ClusterResourcePlacement) bool {
10351042
func isCRPScheduled(crp *fleetv1beta1.ClusterResourcePlacement) bool {
10361043
return condition.IsConditionStatusTrue(crp.GetCondition(string(fleetv1beta1.ClusterResourcePlacementScheduledConditionType)), crp.Generation)
10371044
}
1045+
1046+
func emitPlacementStatusMetric(crp *fleetv1beta1.ClusterResourcePlacement) {
1047+
// Check CRP Scheduled condition
1048+
cond := crp.GetCondition(string(fleetv1beta1.ClusterResourcePlacementScheduledConditionType))
1049+
if cond != nil {
1050+
metrics.FleetPlacementStatus.DeletePartialMatch(prometheus.Labels{"name": crp.Name})
1051+
metrics.FleetPlacementStatus.WithLabelValues(crp.Name, strconv.FormatInt(crp.Generation, 10), cond.Type, string(cond.Status)).SetToCurrentTime()
1052+
}
1053+
// Check CRP expected conditions
1054+
expectedCondTypes := determineExpectedCRPAndResourcePlacementStatusCondType(crp)
1055+
for _, i := range expectedCondTypes {
1056+
cond := crp.GetCondition(string(i.ClusterResourcePlacementConditionType()))
1057+
if cond != nil { // emit if found
1058+
metrics.FleetPlacementStatus.DeletePartialMatch(prometheus.Labels{"name": crp.Name})
1059+
metrics.FleetPlacementStatus.WithLabelValues(crp.Name, strconv.FormatInt(crp.Generation, 10), cond.Type, string(cond.Status)).SetToCurrentTime()
1060+
} else {
1061+
return // return if not found
1062+
}
1063+
}
1064+
}

0 commit comments

Comments
 (0)