common.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "context"
  15. "fmt"
  16. "time"
  17. "github.com/go-logr/logr"
  18. v1 "k8s.io/api/core/v1"
  19. "k8s.io/client-go/tools/record"
  20. ctrl "sigs.k8s.io/controller-runtime"
  21. "sigs.k8s.io/controller-runtime/pkg/client"
  22. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  23. )
  24. const (
  25. errStoreProvider = "could not get store provider: %w"
  26. errStoreClient = "could not get provider client: %w"
  27. errValidationFailed = "could not validate provider: %w"
  28. errPatchStatus = "unable to patch status: %w"
  29. errUnableCreateClient = "unable to create client"
  30. errUnableValidateStore = "unable to validate store"
  31. errUnableGetProvider = "unable to get store provider"
  32. msgStoreValidated = "store validated"
  33. )
  34. func reconcile(ctx context.Context, req ctrl.Request, ss esapi.GenericStore, cl client.Client,
  35. log logr.Logger, controllerClass string, recorder record.EventRecorder, requeueInterval time.Duration) (ctrl.Result, error) {
  36. if !ShouldProcessStore(ss, controllerClass) {
  37. log.V(1).Info("skip store")
  38. return ctrl.Result{}, nil
  39. }
  40. if ss.GetSpec().RefreshInterval != 0 {
  41. requeueInterval = time.Second * time.Duration(ss.GetSpec().RefreshInterval)
  42. }
  43. // patch status when done processing
  44. p := client.MergeFrom(ss.Copy())
  45. defer func() {
  46. err := cl.Status().Patch(ctx, ss, p)
  47. if err != nil {
  48. log.Error(err, errPatchStatus)
  49. }
  50. }()
  51. // validateStore modifies the store conditions
  52. // we have to patch the status
  53. log.V(1).Info("validating")
  54. err := validateStore(ctx, req.Namespace, ss, cl, recorder)
  55. if err != nil {
  56. return ctrl.Result{}, err
  57. }
  58. recorder.Event(ss, v1.EventTypeNormal, esapi.ReasonStoreValid, msgStoreValidated)
  59. cond := NewSecretStoreCondition(esapi.SecretStoreReady, v1.ConditionTrue, esapi.ReasonStoreValid, msgStoreValidated)
  60. SetExternalSecretCondition(ss, *cond)
  61. return ctrl.Result{
  62. RequeueAfter: requeueInterval,
  63. }, err
  64. }
  65. // validateStore tries to construct a new client
  66. // if it fails sets a condition and writes events.
  67. func validateStore(ctx context.Context, namespace string, store esapi.GenericStore,
  68. client client.Client, recorder record.EventRecorder) error {
  69. storeProvider, err := esapi.GetProvider(store)
  70. if err != nil {
  71. cond := NewSecretStoreCondition(esapi.SecretStoreReady, v1.ConditionFalse, esapi.ReasonInvalidStore, errUnableGetProvider)
  72. SetExternalSecretCondition(store, *cond)
  73. recorder.Event(store, v1.EventTypeWarning, esapi.ReasonInvalidStore, err.Error())
  74. return fmt.Errorf(errStoreProvider, err)
  75. }
  76. cl, err := storeProvider.NewClient(ctx, store, client, namespace)
  77. if err != nil {
  78. cond := NewSecretStoreCondition(esapi.SecretStoreReady, v1.ConditionFalse, esapi.ReasonInvalidProviderConfig, errUnableCreateClient)
  79. SetExternalSecretCondition(store, *cond)
  80. recorder.Event(store, v1.EventTypeWarning, esapi.ReasonInvalidProviderConfig, err.Error())
  81. return fmt.Errorf(errStoreClient, err)
  82. }
  83. defer cl.Close(ctx)
  84. validationResult, err := cl.Validate()
  85. if err != nil && validationResult != esapi.ValidationResultUnknown {
  86. cond := NewSecretStoreCondition(esapi.SecretStoreReady, v1.ConditionFalse, esapi.ReasonValidationFailed, errUnableValidateStore)
  87. SetExternalSecretCondition(store, *cond)
  88. recorder.Event(store, v1.EventTypeWarning, esapi.ReasonValidationFailed, err.Error())
  89. return fmt.Errorf(errValidationFailed, err)
  90. }
  91. return nil
  92. }
  93. // ShouldProcessStore returns true if the store should be processed.
  94. func ShouldProcessStore(store esapi.GenericStore, class string) bool {
  95. if store.GetSpec().Controller == "" || store.GetSpec().Controller == class {
  96. return true
  97. }
  98. return false
  99. }