common.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "errors"
  16. "fmt"
  17. "time"
  18. "github.com/go-logr/logr"
  19. v1 "k8s.io/api/core/v1"
  20. "k8s.io/client-go/tools/record"
  21. ctrl "sigs.k8s.io/controller-runtime"
  22. "sigs.k8s.io/controller-runtime/pkg/client"
  23. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  24. "github.com/external-secrets/external-secrets/pkg/controllers/secretstore/metrics"
  25. )
  26. const (
  27. errStoreClient = "could not get provider client: %w"
  28. errValidationFailed = "could not validate provider: %w"
  29. errValidationUnknown = "could not determine validation status: %s"
  30. errPatchStatus = "unable to patch status: %w"
  31. errUnableCreateClient = "unable to create client"
  32. errUnableValidateStore = "unable to validate store: %s"
  33. msgStoreValidated = "store validated"
  34. msgStoreNotMaintained = "store isn't currently maintained. Please plan and prepare accordingly."
  35. )
  36. var validationUnknownError = errors.New("could not determine validation status")
  37. type Opts struct {
  38. ControllerClass string
  39. GaugeVecGetter metrics.GaugeVevGetter
  40. Recorder record.EventRecorder
  41. RequeueInterval time.Duration
  42. }
  43. func reconcile(ctx context.Context, req ctrl.Request, ss esapi.GenericStore, cl client.Client, log logr.Logger, opts Opts) (ctrl.Result, error) {
  44. if !ShouldProcessStore(ss, opts.ControllerClass) {
  45. log.V(1).Info("skip store")
  46. return ctrl.Result{}, nil
  47. }
  48. requeueInterval := opts.RequeueInterval
  49. if ss.GetSpec().RefreshInterval != 0 {
  50. requeueInterval = time.Second * time.Duration(ss.GetSpec().RefreshInterval)
  51. }
  52. // patch status when done processing
  53. p := client.MergeFrom(ss.Copy())
  54. defer func() {
  55. err := cl.Status().Patch(ctx, ss, p)
  56. if err != nil {
  57. log.Error(err, errPatchStatus)
  58. }
  59. }()
  60. // validateStore modifies the store conditions
  61. // we have to patch the status
  62. log.V(1).Info("validating")
  63. err := validateStore(ctx, req.Namespace, opts.ControllerClass, ss, cl, opts.GaugeVecGetter, opts.Recorder)
  64. if err != nil {
  65. log.Error(err, "unable to validate store")
  66. // in case of validation status unknown, validateStore will mark
  67. // the store as ready but we should show ReasonValidationUnknown
  68. if errors.Is(err, validationUnknownError) {
  69. return ctrl.Result{RequeueAfter: requeueInterval}, nil
  70. }
  71. return ctrl.Result{}, err
  72. }
  73. storeProvider, err := esapi.GetProvider(ss)
  74. if err != nil {
  75. return ctrl.Result{}, err
  76. }
  77. isMaintained, err := esapi.GetMaintenanceStatus(ss)
  78. if err != nil {
  79. return ctrl.Result{}, err
  80. }
  81. annotations := ss.GetAnnotations()
  82. _, ok := annotations["external-secrets.io/ignore-maintenance-checks"]
  83. if !bool(isMaintained) && !ok {
  84. opts.Recorder.Event(ss, v1.EventTypeWarning, esapi.StoreUnmaintained, msgStoreNotMaintained)
  85. }
  86. capStatus := esapi.SecretStoreStatus{
  87. Capabilities: storeProvider.Capabilities(),
  88. Conditions: ss.GetStatus().Conditions,
  89. }
  90. ss.SetStatus(capStatus)
  91. opts.Recorder.Event(ss, v1.EventTypeNormal, esapi.ReasonStoreValid, msgStoreValidated)
  92. cond := NewSecretStoreCondition(esapi.SecretStoreReady, v1.ConditionTrue, esapi.ReasonStoreValid, msgStoreValidated)
  93. SetExternalSecretCondition(ss, *cond, opts.GaugeVecGetter)
  94. return ctrl.Result{
  95. RequeueAfter: requeueInterval,
  96. }, err
  97. }
  98. // validateStore tries to construct a new client
  99. // if it fails sets a condition and writes events.
  100. func validateStore(ctx context.Context, namespace, controllerClass string, store esapi.GenericStore,
  101. client client.Client, gaugeVecGetter metrics.GaugeVevGetter, recorder record.EventRecorder) error {
  102. mgr := NewManager(client, controllerClass, false)
  103. defer func() {
  104. _ = mgr.Close(ctx)
  105. }()
  106. cl, err := mgr.GetFromStore(ctx, store, namespace)
  107. if err != nil {
  108. cond := NewSecretStoreCondition(esapi.SecretStoreReady, v1.ConditionFalse, esapi.ReasonInvalidProviderConfig, errUnableCreateClient)
  109. SetExternalSecretCondition(store, *cond, gaugeVecGetter)
  110. recorder.Event(store, v1.EventTypeWarning, esapi.ReasonInvalidProviderConfig, err.Error())
  111. return fmt.Errorf(errStoreClient, err)
  112. }
  113. validationResult, err := cl.Validate()
  114. if err != nil {
  115. if validationResult == esapi.ValidationResultUnknown {
  116. cond := NewSecretStoreCondition(esapi.SecretStoreReady, v1.ConditionTrue, esapi.ReasonValidationUnknown, fmt.Sprintf(errValidationUnknown, err))
  117. SetExternalSecretCondition(store, *cond, gaugeVecGetter)
  118. recorder.Event(store, v1.EventTypeWarning, esapi.ReasonValidationUnknown, err.Error())
  119. return validationUnknownError
  120. }
  121. cond := NewSecretStoreCondition(esapi.SecretStoreReady, v1.ConditionFalse, esapi.ReasonInvalidProviderConfig, fmt.Sprintf(errUnableValidateStore, err))
  122. SetExternalSecretCondition(store, *cond, gaugeVecGetter)
  123. recorder.Event(store, v1.EventTypeWarning, esapi.ReasonInvalidProviderConfig, err.Error())
  124. return fmt.Errorf(errValidationFailed, err)
  125. }
  126. return nil
  127. }
  128. // ShouldProcessStore returns true if the store should be processed.
  129. func ShouldProcessStore(store esapi.GenericStore, class string) bool {
  130. if store == nil || store.GetSpec().Controller == "" || store.GetSpec().Controller == class {
  131. return true
  132. }
  133. return false
  134. }