common.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "github.com/external-secrets/external-secrets/pkg/controllers/secretstore/metrics"
  24. )
  25. const (
  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: %s"
  31. msgStoreValidated = "store validated"
  32. )
  33. type Opts struct {
  34. ControllerClass string
  35. GaugeVecGetter metrics.GaugeVevGetter
  36. Recorder record.EventRecorder
  37. RequeueInterval time.Duration
  38. }
  39. func reconcile(ctx context.Context, req ctrl.Request, ss esapi.GenericStore, cl client.Client, log logr.Logger, opts Opts) (ctrl.Result, error) {
  40. if !ShouldProcessStore(ss, opts.ControllerClass) {
  41. log.V(1).Info("skip store")
  42. return ctrl.Result{}, nil
  43. }
  44. requeueInterval := opts.RequeueInterval
  45. if ss.GetSpec().RefreshInterval != 0 {
  46. requeueInterval = time.Second * time.Duration(ss.GetSpec().RefreshInterval)
  47. }
  48. // patch status when done processing
  49. p := client.MergeFrom(ss.Copy())
  50. defer func() {
  51. err := cl.Status().Patch(ctx, ss, p)
  52. if err != nil {
  53. log.Error(err, errPatchStatus)
  54. }
  55. }()
  56. // validateStore modifies the store conditions
  57. // we have to patch the status
  58. log.V(1).Info("validating")
  59. err := validateStore(ctx, req.Namespace, opts.ControllerClass, ss, cl, opts.GaugeVecGetter, opts.Recorder)
  60. if err != nil {
  61. log.Error(err, "unable to validate store")
  62. return ctrl.Result{}, err
  63. }
  64. storeProvider, err := esapi.GetProvider(ss)
  65. if err != nil {
  66. return ctrl.Result{}, err
  67. }
  68. capStatus := esapi.SecretStoreStatus{
  69. Capabilities: storeProvider.Capabilities(),
  70. Conditions: ss.GetStatus().Conditions,
  71. }
  72. ss.SetStatus(capStatus)
  73. opts.Recorder.Event(ss, v1.EventTypeNormal, esapi.ReasonStoreValid, msgStoreValidated)
  74. cond := NewSecretStoreCondition(esapi.SecretStoreReady, v1.ConditionTrue, esapi.ReasonStoreValid, msgStoreValidated)
  75. SetExternalSecretCondition(ss, *cond, opts.GaugeVecGetter)
  76. return ctrl.Result{
  77. RequeueAfter: requeueInterval,
  78. }, err
  79. }
  80. // validateStore tries to construct a new client
  81. // if it fails sets a condition and writes events.
  82. func validateStore(ctx context.Context, namespace, controllerClass string, store esapi.GenericStore,
  83. client client.Client, gaugeVecGetter metrics.GaugeVevGetter, recorder record.EventRecorder) error {
  84. mgr := NewManager(client, controllerClass, false)
  85. defer mgr.Close(ctx)
  86. cl, err := mgr.GetFromStore(ctx, store, namespace)
  87. if err != nil {
  88. cond := NewSecretStoreCondition(esapi.SecretStoreReady, v1.ConditionFalse, esapi.ReasonInvalidProviderConfig, errUnableCreateClient)
  89. SetExternalSecretCondition(store, *cond, gaugeVecGetter)
  90. recorder.Event(store, v1.EventTypeWarning, esapi.ReasonInvalidProviderConfig, err.Error())
  91. return fmt.Errorf(errStoreClient, err)
  92. }
  93. validationResult, err := cl.Validate()
  94. if err != nil && validationResult != esapi.ValidationResultUnknown {
  95. cond := NewSecretStoreCondition(esapi.SecretStoreReady, v1.ConditionFalse, esapi.ReasonValidationFailed, fmt.Sprintf(errUnableValidateStore, err))
  96. SetExternalSecretCondition(store, *cond, gaugeVecGetter)
  97. recorder.Event(store, v1.EventTypeWarning, esapi.ReasonValidationFailed, err.Error())
  98. return fmt.Errorf(errValidationFailed, err)
  99. }
  100. return nil
  101. }
  102. // ShouldProcessStore returns true if the store should be processed.
  103. func ShouldProcessStore(store esapi.GenericStore, class string) bool {
  104. if store == nil || store.GetSpec().Controller == "" || store.GetSpec().Controller == class {
  105. return true
  106. }
  107. return false
  108. }