util.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 secretstore
  14. import (
  15. "fmt"
  16. v1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/labels"
  19. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  21. "github.com/external-secrets/external-secrets/pkg/controllers/secretstore/metrics"
  22. )
  23. // NewSecretStoreCondition a set of default options for creating an External Secret Condition.
  24. func NewSecretStoreCondition(condType esapi.SecretStoreConditionType, status v1.ConditionStatus, reason, message string) *esapi.SecretStoreStatusCondition {
  25. return &esapi.SecretStoreStatusCondition{
  26. Type: condType,
  27. Status: status,
  28. LastTransitionTime: metav1.Now(),
  29. Reason: reason,
  30. Message: message,
  31. }
  32. }
  33. // GetSecretStoreCondition returns the condition with the provided type.
  34. func GetSecretStoreCondition(status esapi.SecretStoreStatus, condType esapi.SecretStoreConditionType) *esapi.SecretStoreStatusCondition {
  35. for i := range status.Conditions {
  36. c := status.Conditions[i]
  37. if c.Type == condType {
  38. return &c
  39. }
  40. }
  41. return nil
  42. }
  43. // SetExternalSecretCondition updates the external secret to include the provided
  44. // condition.
  45. func SetExternalSecretCondition(gs esapi.GenericStore, condition esapi.SecretStoreStatusCondition, gaugeVecGetter metrics.GaugeVevGetter) {
  46. metrics.UpdateStatusCondition(gs, condition, gaugeVecGetter)
  47. status := gs.GetStatus()
  48. currentCond := GetSecretStoreCondition(status, condition.Type)
  49. if currentCond != nil && currentCond.Status == condition.Status &&
  50. currentCond.Reason == condition.Reason && currentCond.Message == condition.Message {
  51. return
  52. }
  53. // Do not update lastTransitionTime if the status of the condition doesn't change.
  54. if currentCond != nil && currentCond.Status == condition.Status {
  55. condition.LastTransitionTime = currentCond.LastTransitionTime
  56. }
  57. status.Conditions = append(filterOutCondition(status.Conditions, condition.Type), condition)
  58. gs.SetStatus(status)
  59. }
  60. // filterOutCondition returns an empty set of conditions with the provided type.
  61. func filterOutCondition(conditions []esapi.SecretStoreStatusCondition, condType esapi.SecretStoreConditionType) []esapi.SecretStoreStatusCondition {
  62. newConditions := make([]esapi.SecretStoreStatusCondition, 0, len(conditions))
  63. for _, c := range conditions {
  64. if c.Type == condType {
  65. continue
  66. }
  67. newConditions = append(newConditions, c)
  68. }
  69. return newConditions
  70. }
  71. // storeMatchesRef checks if a given store matches a store reference (PushSecretStoreRef).
  72. // This helper function should be shared to avoid code duplication.
  73. // A match can be by name or by label selector, respecting the Kind.
  74. func storeMatchesRef(store esapi.GenericStore, ref esv1alpha1.PushSecretStoreRef) bool {
  75. storeKind := store.GetKind()
  76. storeName := store.GetName()
  77. // Check if the Kind of the reference is compatible with the store's Kind.
  78. // A reference with an empty Kind is compatible with both SecretStore and ClusterSecretStore.
  79. kindMatches := (ref.Kind == storeKind) || (ref.Kind == "" && (storeKind == esapi.SecretStoreKind || storeKind == esapi.ClusterSecretStoreKind))
  80. if !kindMatches {
  81. return false
  82. }
  83. // Check for a name match.
  84. if ref.Name == storeName {
  85. return true
  86. }
  87. // Check for a label selector match.
  88. if ref.LabelSelector != nil {
  89. selector, err := metav1.LabelSelectorAsSelector(ref.LabelSelector)
  90. // Skips invalid selectors.
  91. if err != nil {
  92. return false
  93. }
  94. if selector.Matches(labels.Set(store.GetLabels())) {
  95. return true
  96. }
  97. }
  98. return false
  99. }
  100. // shouldReconcileSecretStoreForPushSecret determines if a SecretStore should be reconciled
  101. // when a PushSecret changes, based on whether the PushSecret references this store.
  102. func shouldReconcileSecretStoreForPushSecret(store esapi.GenericStore, ps *esv1alpha1.PushSecret) bool {
  103. // Check if this PushSecret has pushed to this store
  104. storeKey := fmt.Sprintf("%s/%s", store.GetKind(), store.GetName())
  105. if _, hasPushed := ps.Status.SyncedPushSecrets[storeKey]; hasPushed {
  106. return true
  107. }
  108. // Also check if the PushSecret references this store in its spec
  109. for _, storeRef := range ps.Spec.SecretStoreRefs {
  110. if storeMatchesRef(store, storeRef) {
  111. return true
  112. }
  113. }
  114. return false
  115. }