psmetrics.go 3.3 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 psmetrics
  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. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  19. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  20. )
  21. const (
  22. PushSecretSubsystem = "pushsecret"
  23. PushSecretReconcileDurationKey = "reconcile_duration"
  24. PushSecretStatusConditionKey = "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. pushSecretCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  31. Subsystem: PushSecretSubsystem,
  32. Name: PushSecretStatusConditionKey,
  33. Help: "The status condition of a specific Push Secret",
  34. }, ctrlmetrics.ConditionMetricLabelNames)
  35. pushSecretReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  36. Subsystem: PushSecretSubsystem,
  37. Name: PushSecretReconcileDurationKey,
  38. Help: "The duration time to reconcile the Push Secret",
  39. }, ctrlmetrics.NonConditionMetricLabelNames)
  40. metrics.Registry.MustRegister(pushSecretReconcileDuration, pushSecretCondition)
  41. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  42. PushSecretStatusConditionKey: pushSecretCondition,
  43. PushSecretReconcileDurationKey: pushSecretReconcileDuration,
  44. }
  45. }
  46. func UpdatePushSecretCondition(ps *esapi.PushSecret, condition *esapi.PushSecretStatusCondition, value float64) {
  47. psInfo := make(map[string]string)
  48. psInfo["name"] = ps.Name
  49. psInfo["namespace"] = ps.Namespace
  50. for k, v := range ps.Labels {
  51. psInfo[k] = v
  52. }
  53. conditionLabels := ctrlmetrics.RefineConditionMetricLabels(psInfo)
  54. pushSecretCondition := GetGaugeVec(PushSecretStatusConditionKey)
  55. switch condition.Type {
  56. case esapi.PushSecretReady:
  57. // Toggle opposite Status to 0
  58. switch condition.Status {
  59. case v1.ConditionFalse:
  60. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  61. map[string]string{
  62. "condition": string(esapi.PushSecretReady),
  63. "status": string(v1.ConditionTrue),
  64. })).Set(0)
  65. case v1.ConditionTrue:
  66. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  67. map[string]string{
  68. "condition": string(esapi.PushSecretReady),
  69. "status": string(v1.ConditionFalse),
  70. })).Set(0)
  71. case v1.ConditionUnknown:
  72. break
  73. default:
  74. break
  75. }
  76. default:
  77. break
  78. }
  79. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  80. map[string]string{
  81. "condition": string(condition.Type),
  82. "status": string(condition.Status),
  83. })).Set(value)
  84. }
  85. func GetGaugeVec(key string) *prometheus.GaugeVec {
  86. return gaugeVecMetrics[key]
  87. }