clustersecretstore_controller.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package secretstore implements controllers for SecretStore and ClusterSecretStore resources.
  14. package secretstore
  15. import (
  16. "context"
  17. "time"
  18. "github.com/go-logr/logr"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/client-go/tools/record"
  22. ctrl "sigs.k8s.io/controller-runtime"
  23. "sigs.k8s.io/controller-runtime/pkg/client"
  24. "sigs.k8s.io/controller-runtime/pkg/controller"
  25. "sigs.k8s.io/controller-runtime/pkg/handler"
  26. ctrlreconcile "sigs.k8s.io/controller-runtime/pkg/reconcile"
  27. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  28. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  29. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  30. "github.com/external-secrets/external-secrets/pkg/controllers/secretstore/cssmetrics"
  31. // Loading registered providers.
  32. _ "github.com/external-secrets/external-secrets/pkg/register"
  33. )
  34. // ClusterStoreReconciler reconciles a SecretStore object.
  35. type ClusterStoreReconciler struct {
  36. client.Client
  37. Log logr.Logger
  38. Scheme *runtime.Scheme
  39. ControllerClass string
  40. RequeueInterval time.Duration
  41. recorder record.EventRecorder
  42. PushSecretEnabled bool
  43. }
  44. // Reconcile is part of the main kubernetes reconciliation loop which aims to
  45. // move the current state of the cluster closer to the desired state.
  46. // For more details, check Reconcile and its Result here:
  47. // - https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/reconcile
  48. func (r *ClusterStoreReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  49. log := r.Log.WithValues("clustersecretstore", req.NamespacedName)
  50. resourceLabels := ctrlmetrics.RefineNonConditionMetricLabels(map[string]string{"name": req.Name, "namespace": req.Namespace})
  51. start := time.Now()
  52. clusterSecretStoreReconcileDuration := cssmetrics.GetGaugeVec(cssmetrics.ClusterSecretStoreReconcileDurationKey)
  53. defer func() { clusterSecretStoreReconcileDuration.With(resourceLabels).Set(float64(time.Since(start))) }()
  54. var css esapi.ClusterSecretStore
  55. err := r.Get(ctx, req.NamespacedName, &css)
  56. if apierrors.IsNotFound(err) {
  57. cssmetrics.RemoveMetrics(req.Namespace, req.Name)
  58. return ctrl.Result{}, nil
  59. } else if err != nil {
  60. log.Error(err, "unable to get ClusterSecretStore")
  61. return ctrl.Result{}, err
  62. }
  63. return reconcile(ctx, req, &css, r.Client, r.PushSecretEnabled, log, Opts{
  64. ControllerClass: r.ControllerClass,
  65. GaugeVecGetter: cssmetrics.GetGaugeVec,
  66. Recorder: r.recorder,
  67. RequeueInterval: r.RequeueInterval,
  68. })
  69. }
  70. // SetupWithManager returns a new controller builder that will be started by the provided Manager.
  71. func (r *ClusterStoreReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
  72. r.recorder = mgr.GetEventRecorderFor("cluster-secret-store")
  73. builder := ctrl.NewControllerManagedBy(mgr)
  74. if r.PushSecretEnabled {
  75. return builder.WithOptions(opts).
  76. For(&esapi.ClusterSecretStore{}).
  77. Watches(
  78. &esv1alpha1.PushSecret{},
  79. handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []ctrlreconcile.Request {
  80. return findStoresForPushSecret(ctx, r.Client, obj, &esapi.ClusterSecretStoreList{})
  81. }),
  82. ).
  83. Complete(r)
  84. }
  85. return builder.WithOptions(opts).
  86. For(&esapi.ClusterSecretStore{}).
  87. Complete(r)
  88. }