metrics.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 metrics provides metrics for SecretStore controllers.
  14. package metrics
  15. import (
  16. "github.com/prometheus/client_golang/prometheus"
  17. v1 "k8s.io/api/core/v1"
  18. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  19. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  20. )
  21. // StatusConditionKey is the key for the status condition metric.
  22. const StatusConditionKey = "status_condition"
  23. // GaugeVevGetter is a function type that retrieves a Prometheus GaugeVec based on a provided key.
  24. type GaugeVevGetter func(key string) *prometheus.GaugeVec
  25. // UpdateStatusCondition updates the condition metrics for a SecretStore.
  26. func UpdateStatusCondition(ss esapi.GenericStore, condition esapi.SecretStoreStatusCondition, gaugeVecGetter GaugeVevGetter) {
  27. ssInfo := make(map[string]string)
  28. ssInfo["name"] = ss.GetName()
  29. ssInfo["namespace"] = ss.GetNamespace()
  30. for k, v := range ss.GetLabels() {
  31. ssInfo[k] = v
  32. }
  33. conditionLabels := ctrlmetrics.RefineConditionMetricLabels(ssInfo)
  34. secretStoreCondition := gaugeVecGetter(StatusConditionKey)
  35. if condition.Type == esapi.SecretStoreReady {
  36. switch condition.Status {
  37. case v1.ConditionFalse:
  38. secretStoreCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  39. map[string]string{
  40. "condition": string(esapi.SecretStoreReady),
  41. "status": string(v1.ConditionTrue),
  42. })).Set(0)
  43. case v1.ConditionTrue:
  44. secretStoreCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  45. map[string]string{
  46. "condition": string(esapi.SecretStoreReady),
  47. "status": string(v1.ConditionFalse),
  48. })).Set(0)
  49. case v1.ConditionUnknown:
  50. break
  51. }
  52. }
  53. secretStoreCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  54. map[string]string{
  55. "condition": string(condition.Type),
  56. "status": string(condition.Status),
  57. })).Set(1)
  58. }