externalsecret_store_watch.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright © The ESO Authors
  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 externalsecret
  14. import (
  15. "context"
  16. "fmt"
  17. "sigs.k8s.io/controller-runtime/pkg/client"
  18. "sigs.k8s.io/controller-runtime/pkg/reconcile"
  19. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. esv2alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v2alpha1"
  21. )
  22. const indexESV2StoreRefField = ".spec.v2StoreRefs"
  23. func v2StoreRefIndexKey(kind, namespace, name string) string {
  24. if kind == esv1.ClusterProviderStoreKindStr {
  25. return fmt.Sprintf("%s/%s", kind, name)
  26. }
  27. return fmt.Sprintf("%s/%s/%s", kind, namespace, name)
  28. }
  29. func indexExternalSecretV2StoreRefs(obj client.Object) []string {
  30. es, ok := obj.(*esv1.ExternalSecret)
  31. if !ok {
  32. return nil
  33. }
  34. keys := make(map[string]struct{})
  35. addExternalSecretV2StoreRefIndexKey(keys, es.Namespace, es.Spec.SecretStoreRef)
  36. for i := range es.Spec.Data {
  37. if es.Spec.Data[i].SourceRef == nil {
  38. continue
  39. }
  40. addExternalSecretV2StoreRefIndexKey(keys, es.Namespace, es.Spec.Data[i].SourceRef.SecretStoreRef)
  41. }
  42. for i := range es.Spec.DataFrom {
  43. if es.Spec.DataFrom[i].SourceRef == nil || es.Spec.DataFrom[i].SourceRef.SecretStoreRef == nil {
  44. continue
  45. }
  46. addExternalSecretV2StoreRefIndexKey(keys, es.Namespace, *es.Spec.DataFrom[i].SourceRef.SecretStoreRef)
  47. }
  48. out := make([]string, 0, len(keys))
  49. for key := range keys {
  50. out = append(out, key)
  51. }
  52. return out
  53. }
  54. func addExternalSecretV2StoreRefIndexKey(keys map[string]struct{}, namespace string, ref esv1.SecretStoreRef) {
  55. switch ref.Kind {
  56. case esv1.ProviderStoreKindStr:
  57. keys[v2StoreRefIndexKey(esv1.ProviderStoreKindStr, namespace, ref.Name)] = struct{}{}
  58. case esv1.ClusterProviderStoreKindStr:
  59. keys[v2StoreRefIndexKey(esv1.ClusterProviderStoreKindStr, "", ref.Name)] = struct{}{}
  60. }
  61. }
  62. func (r *Reconciler) findExternalSecretsForV2Store(ctx context.Context, obj client.Object) []reconcile.Request {
  63. var (
  64. key string
  65. listOptions []client.ListOption
  66. )
  67. switch store := obj.(type) {
  68. case *esv2alpha1.ProviderStore:
  69. key = v2StoreRefIndexKey(esv1.ProviderStoreKindStr, store.Namespace, store.Name)
  70. listOptions = append(listOptions, client.InNamespace(store.Namespace))
  71. case *esv2alpha1.ClusterProviderStore:
  72. key = v2StoreRefIndexKey(esv1.ClusterProviderStoreKindStr, "", store.Name)
  73. default:
  74. return nil
  75. }
  76. listOptions = append(listOptions, client.MatchingFields{indexESV2StoreRefField: key})
  77. var externalSecrets esv1.ExternalSecretList
  78. if err := r.List(ctx, &externalSecrets, listOptions...); err != nil {
  79. return nil
  80. }
  81. requests := make([]reconcile.Request, 0, len(externalSecrets.Items))
  82. for i := range externalSecrets.Items {
  83. requests = append(requests, reconcile.Request{
  84. NamespacedName: client.ObjectKeyFromObject(&externalSecrets.Items[i]),
  85. })
  86. }
  87. return requests
  88. }