common.go 4.3 KB

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