secretstore_controller.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "time"
  16. "github.com/go-logr/logr"
  17. apierrors "k8s.io/apimachinery/pkg/api/errors"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/client-go/tools/record"
  20. ctrl "sigs.k8s.io/controller-runtime"
  21. "sigs.k8s.io/controller-runtime/pkg/client"
  22. "sigs.k8s.io/controller-runtime/pkg/controller"
  23. "sigs.k8s.io/controller-runtime/pkg/handler"
  24. ctrlreconcile "sigs.k8s.io/controller-runtime/pkg/reconcile"
  25. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  26. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  27. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  28. "github.com/external-secrets/external-secrets/pkg/controllers/secretstore/ssmetrics"
  29. // Loading registered providers.
  30. _ "github.com/external-secrets/external-secrets/pkg/provider/register"
  31. )
  32. // StoreReconciler reconciles a SecretStore object.
  33. type StoreReconciler struct {
  34. client.Client
  35. Log logr.Logger
  36. Scheme *runtime.Scheme
  37. recorder record.EventRecorder
  38. RequeueInterval time.Duration
  39. ControllerClass string
  40. PushSecretEnabled bool
  41. }
  42. func (r *StoreReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  43. log := r.Log.WithValues("secretstore", req.NamespacedName)
  44. resourceLabels := ctrlmetrics.RefineNonConditionMetricLabels(map[string]string{"name": req.Name, "namespace": req.Namespace})
  45. start := time.Now()
  46. secretStoreReconcileDuration := ssmetrics.GetGaugeVec(ssmetrics.SecretStoreReconcileDurationKey)
  47. defer func() { secretStoreReconcileDuration.With(resourceLabels).Set(float64(time.Since(start))) }()
  48. var ss esapi.SecretStore
  49. err := r.Get(ctx, req.NamespacedName, &ss)
  50. if apierrors.IsNotFound(err) {
  51. ssmetrics.RemoveMetrics(req.Namespace, req.Name)
  52. return ctrl.Result{}, nil
  53. } else if err != nil {
  54. log.Error(err, "unable to get SecretStore")
  55. return ctrl.Result{}, err
  56. }
  57. return reconcile(ctx, req, &ss, r.Client, r.PushSecretEnabled, log, Opts{
  58. ControllerClass: r.ControllerClass,
  59. GaugeVecGetter: ssmetrics.GetGaugeVec,
  60. Recorder: r.recorder,
  61. RequeueInterval: r.RequeueInterval,
  62. })
  63. }
  64. // SetupWithManager returns a new controller builder that will be started by the provided Manager.
  65. func (r *StoreReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
  66. r.recorder = mgr.GetEventRecorderFor("secret-store")
  67. builder := ctrl.NewControllerManagedBy(mgr)
  68. if r.PushSecretEnabled {
  69. return builder.WithOptions(opts).
  70. For(&esapi.SecretStore{}).
  71. Watches(
  72. &esv1alpha1.PushSecret{},
  73. handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []ctrlreconcile.Request {
  74. return findStoresForPushSecret(ctx, r.Client, obj, &esapi.SecretStoreList{})
  75. }),
  76. ).
  77. Complete(r)
  78. }
  79. return builder.WithOptions(opts).
  80. For(&esapi.SecretStore{}).
  81. Complete(r)
  82. }