cpsmetrics.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cpsmetrics
  14. import (
  15. "github.com/prometheus/client_golang/prometheus"
  16. v1 "k8s.io/api/core/v1"
  17. "sigs.k8s.io/controller-runtime/pkg/metrics"
  18. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  19. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  20. )
  21. const (
  22. ClusterPushSecretSubsystem = "clusterpushsecret"
  23. ClusterPushSecretReconcileDurationKey = "reconcile_duration"
  24. ClusterPushSecretStatusConditionKey = "status_condition"
  25. )
  26. var gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
  27. // SetUpMetrics is called at the root to set-up the metric logic using the
  28. // config flags provided.
  29. func SetUpMetrics() {
  30. ClusterPushSecretReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  31. Subsystem: ClusterPushSecretSubsystem,
  32. Name: ClusterPushSecretReconcileDurationKey,
  33. Help: "The duration time to reconcile the Cluster Push Secret",
  34. }, ctrlmetrics.NonConditionMetricLabelNames)
  35. ClusterPushSecretCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  36. Subsystem: ClusterPushSecretSubsystem,
  37. Name: ClusterPushSecretStatusConditionKey,
  38. Help: "The status condition of a specific Cluster Push Secret",
  39. }, ctrlmetrics.ConditionMetricLabelNames)
  40. metrics.Registry.MustRegister(ClusterPushSecretReconcileDuration, ClusterPushSecretCondition)
  41. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  42. ClusterPushSecretStatusConditionKey: ClusterPushSecretCondition,
  43. ClusterPushSecretReconcileDurationKey: ClusterPushSecretReconcileDuration,
  44. }
  45. }
  46. func GetGaugeVec(key string) *prometheus.GaugeVec {
  47. return gaugeVecMetrics[key]
  48. }
  49. func UpdateClusterPushSecretCondition(ces *v1alpha1.ClusterPushSecret, condition *v1alpha1.PushSecretStatusCondition) {
  50. if condition.Status != v1.ConditionTrue {
  51. // This should not happen
  52. return
  53. }
  54. cesInfo := make(map[string]string)
  55. cesInfo["name"] = ces.Name
  56. for k, v := range ces.Labels {
  57. cesInfo[k] = v
  58. }
  59. conditionLabels := ctrlmetrics.RefineConditionMetricLabels(cesInfo)
  60. ClusterPushSecretCondition := GetGaugeVec(ClusterPushSecretStatusConditionKey)
  61. theOtherStatus := v1.ConditionFalse
  62. if condition.Status == v1.ConditionFalse {
  63. theOtherStatus = v1.ConditionTrue
  64. }
  65. ClusterPushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  66. map[string]string{
  67. "condition": string(condition.Type),
  68. "status": string(condition.Status),
  69. })).Set(1)
  70. ClusterPushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  71. map[string]string{
  72. "condition": string(condition.Type),
  73. "status": string(theOtherStatus),
  74. })).Set(0)
  75. }
  76. // RemoveMetrics deletes all metrics published by the resource.
  77. func RemoveMetrics(namespace, name string) {
  78. for _, gaugeVecMetric := range gaugeVecMetrics {
  79. gaugeVecMetric.DeletePartialMatch(
  80. map[string]string{
  81. "namespace": namespace,
  82. "name": name,
  83. },
  84. )
  85. }
  86. }