util.go 4.8 KB

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