|
|
@@ -16,14 +16,17 @@ package cesmetrics
|
|
|
|
|
|
import (
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
+ v1 "k8s.io/api/core/v1"
|
|
|
"sigs.k8s.io/controller-runtime/pkg/metrics"
|
|
|
|
|
|
+ esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
|
|
|
ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
ClusterExternalSecretSubsystem = "clusterexternalsecret"
|
|
|
ClusterExternalSecretReconcileDurationKey = "reconcile_duration"
|
|
|
+ ClusterExternalSecretStatusConditionKey = "status_condition"
|
|
|
)
|
|
|
|
|
|
var gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
|
|
|
@@ -37,9 +40,16 @@ func SetUpMetrics() {
|
|
|
Help: "The duration time to reconcile the Cluster External Secret",
|
|
|
}, ctrlmetrics.NonConditionMetricLabelNames)
|
|
|
|
|
|
- metrics.Registry.MustRegister(clusterExternalSecretReconcileDuration)
|
|
|
+ clusterExternalSecretCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
+ Subsystem: ClusterExternalSecretSubsystem,
|
|
|
+ Name: ClusterExternalSecretStatusConditionKey,
|
|
|
+ Help: "The status condition of a specific Cluster External Secret",
|
|
|
+ }, ctrlmetrics.ConditionMetricLabelNames)
|
|
|
+
|
|
|
+ metrics.Registry.MustRegister(clusterExternalSecretReconcileDuration, clusterExternalSecretCondition)
|
|
|
|
|
|
gaugeVecMetrics = map[string]*prometheus.GaugeVec{
|
|
|
+ ClusterExternalSecretStatusConditionKey: clusterExternalSecretCondition,
|
|
|
ClusterExternalSecretReconcileDurationKey: clusterExternalSecretReconcileDuration,
|
|
|
}
|
|
|
}
|
|
|
@@ -47,3 +57,56 @@ func SetUpMetrics() {
|
|
|
func GetGaugeVec(key string) *prometheus.GaugeVec {
|
|
|
return gaugeVecMetrics[key]
|
|
|
}
|
|
|
+
|
|
|
+func UpdateClusterExternalSecretCondition(ces *esv1beta1.ClusterExternalSecret, condition *esv1beta1.ClusterExternalSecretStatusCondition) {
|
|
|
+ if condition.Status != v1.ConditionTrue {
|
|
|
+ // This should not happen
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ cesInfo := make(map[string]string)
|
|
|
+ cesInfo["name"] = ces.Name
|
|
|
+ for k, v := range ces.Labels {
|
|
|
+ cesInfo[k] = v
|
|
|
+ }
|
|
|
+ conditionLabels := ctrlmetrics.RefineConditionMetricLabels(cesInfo)
|
|
|
+ clusterExternalSecretCondition := GetGaugeVec(ClusterExternalSecretStatusConditionKey)
|
|
|
+
|
|
|
+ targetConditionType := condition.Type
|
|
|
+ var theOtherConditionTypes []esv1beta1.ClusterExternalSecretConditionType
|
|
|
+ for _, ct := range []esv1beta1.ClusterExternalSecretConditionType{
|
|
|
+ esv1beta1.ClusterExternalSecretReady,
|
|
|
+ esv1beta1.ClusterExternalSecretPartiallyReady,
|
|
|
+ esv1beta1.ClusterExternalSecretNotReady,
|
|
|
+ } {
|
|
|
+ if ct != targetConditionType {
|
|
|
+ theOtherConditionTypes = append(theOtherConditionTypes, ct)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Set the target condition metric
|
|
|
+ clusterExternalSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
|
|
|
+ map[string]string{
|
|
|
+ "condition": string(targetConditionType),
|
|
|
+ "status": string(v1.ConditionTrue),
|
|
|
+ })).Set(1)
|
|
|
+ clusterExternalSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
|
|
|
+ map[string]string{
|
|
|
+ "condition": string(targetConditionType),
|
|
|
+ "status": string(v1.ConditionFalse),
|
|
|
+ })).Set(0)
|
|
|
+
|
|
|
+ // Remove the other condition metrics
|
|
|
+ for _, ct := range theOtherConditionTypes {
|
|
|
+ clusterExternalSecretCondition.Delete(ctrlmetrics.RefineLabels(conditionLabels,
|
|
|
+ map[string]string{
|
|
|
+ "condition": string(ct),
|
|
|
+ "status": string(v1.ConditionFalse),
|
|
|
+ }))
|
|
|
+ clusterExternalSecretCondition.Delete(ctrlmetrics.RefineLabels(conditionLabels,
|
|
|
+ map[string]string{
|
|
|
+ "condition": string(ct),
|
|
|
+ "status": string(v1.ConditionTrue),
|
|
|
+ }))
|
|
|
+ }
|
|
|
+}
|