cpsmetrics.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 provides functionality for tracking and exposing metrics related to ClusterPushSecret resources.
  14. package cpsmetrics
  15. import (
  16. "github.com/prometheus/client_golang/prometheus"
  17. v1 "k8s.io/api/core/v1"
  18. "sigs.k8s.io/controller-runtime/pkg/metrics"
  19. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  20. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  21. )
  22. // Constants for metrics subsystem and keys.
  23. const (
  24. ClusterPushSecretSubsystem = "clusterpushsecret"
  25. ClusterPushSecretReconcileDurationKey = "reconcile_duration"
  26. ClusterPushSecretStatusConditionKey = "status_condition"
  27. )
  28. var gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
  29. // SetUpMetrics is called at the root to set-up the metric logic using the
  30. // config flags provided.
  31. func SetUpMetrics() {
  32. ClusterPushSecretReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  33. Subsystem: ClusterPushSecretSubsystem,
  34. Name: ClusterPushSecretReconcileDurationKey,
  35. Help: "The duration time to reconcile the Cluster Push Secret",
  36. }, ctrlmetrics.NonConditionMetricLabelNames)
  37. ClusterPushSecretCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  38. Subsystem: ClusterPushSecretSubsystem,
  39. Name: ClusterPushSecretStatusConditionKey,
  40. Help: "The status condition of a specific Cluster Push Secret",
  41. }, ctrlmetrics.ConditionMetricLabelNames)
  42. metrics.Registry.MustRegister(ClusterPushSecretReconcileDuration, ClusterPushSecretCondition)
  43. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  44. ClusterPushSecretStatusConditionKey: ClusterPushSecretCondition,
  45. ClusterPushSecretReconcileDurationKey: ClusterPushSecretReconcileDuration,
  46. }
  47. }
  48. // GetGaugeVec returns a GaugeVec for the given metric key.
  49. func GetGaugeVec(key string) *prometheus.GaugeVec {
  50. return gaugeVecMetrics[key]
  51. }
  52. // UpdateClusterPushSecretCondition updates the metrics for a ClusterPushSecret based on its condition.
  53. func UpdateClusterPushSecretCondition(ces *v1alpha1.ClusterPushSecret, condition *v1alpha1.PushSecretStatusCondition) {
  54. if condition.Status != v1.ConditionTrue {
  55. // This should not happen
  56. return
  57. }
  58. cesInfo := make(map[string]string)
  59. cesInfo["name"] = ces.Name
  60. for k, v := range ces.Labels {
  61. cesInfo[k] = v
  62. }
  63. conditionLabels := ctrlmetrics.RefineConditionMetricLabels(cesInfo)
  64. ClusterPushSecretCondition := GetGaugeVec(ClusterPushSecretStatusConditionKey)
  65. // This handles cases where labels may have changed
  66. baseLabels := prometheus.Labels{
  67. "name": ces.Name,
  68. "condition": string(condition.Type),
  69. "status": string(v1.ConditionFalse),
  70. }
  71. ClusterPushSecretCondition.DeletePartialMatch(baseLabels)
  72. ClusterPushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  73. map[string]string{
  74. "condition": string(condition.Type),
  75. "status": string(condition.Status),
  76. })).Set(1)
  77. ClusterPushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  78. map[string]string{
  79. "condition": string(condition.Type),
  80. "status": string(v1.ConditionFalse),
  81. })).Set(0)
  82. }
  83. // RemoveMetrics deletes all metrics published by the resource.
  84. func RemoveMetrics(namespace, name string) {
  85. for _, gaugeVecMetric := range gaugeVecMetrics {
  86. gaugeVecMetric.DeletePartialMatch(
  87. map[string]string{
  88. "namespace": namespace,
  89. "name": name,
  90. },
  91. )
  92. }
  93. }