common.go 4.8 KB

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