cesmetrics.go 3.5 KB

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