providerstore_controller.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 providerstore implements controllers for ProviderStore and ClusterProviderStore resources.
  14. package providerstore
  15. import (
  16. "context"
  17. "time"
  18. "github.com/go-logr/logr"
  19. corev1 "k8s.io/api/core/v1"
  20. apierrors "k8s.io/apimachinery/pkg/api/errors"
  21. "k8s.io/apimachinery/pkg/runtime"
  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. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  27. esv2alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v2alpha1"
  28. providermetrics "github.com/external-secrets/external-secrets/pkg/controllers/provider"
  29. )
  30. // StoreReconciler reconciles ProviderStore resources.
  31. type StoreReconciler struct {
  32. client.Client
  33. Log logr.Logger
  34. Scheme *runtime.Scheme
  35. RequeueInterval time.Duration
  36. }
  37. // Reconcile validates the referenced runtime and backend for a ProviderStore.
  38. func (r *StoreReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  39. start := time.Now()
  40. log := r.Log.WithValues("ProviderStore", req.NamespacedName)
  41. var store esv2alpha1.ProviderStore
  42. if err := r.Get(ctx, req.NamespacedName, &store); err != nil {
  43. if apierrors.IsNotFound(err) {
  44. providermetrics.RemoveMetrics(req.Namespace, req.Name)
  45. return ctrl.Result{}, nil
  46. }
  47. return ctrl.Result{}, err
  48. }
  49. if err := validateStore(ctx, r.Client, &store, store.Namespace); err != nil {
  50. setReadyCondition(&store, corev1.ConditionFalse, "ValidationFailed", err.Error())
  51. recordProviderStoreCompatibilityMetrics(&store, time.Since(start))
  52. return updateStatus(ctx, r.Status(), &store, r.RequeueInterval, log)
  53. }
  54. setReadyCondition(&store, corev1.ConditionTrue, "StoreValid", "store validated")
  55. recordProviderStoreCompatibilityMetrics(&store, time.Since(start))
  56. return updateStatus(ctx, r.Status(), &store, r.RequeueInterval, log)
  57. }
  58. // SetupWithManager registers the ProviderStore controller and runtime-class watches.
  59. func (r *StoreReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
  60. return ctrl.NewControllerManagedBy(mgr).
  61. WithOptions(opts).
  62. For(&esv2alpha1.ProviderStore{}).
  63. Watches(
  64. &esv1alpha1.ClusterProviderClass{},
  65. handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []ctrl.Request {
  66. runtimeClass, ok := obj.(*esv1alpha1.ClusterProviderClass)
  67. if !ok {
  68. return nil
  69. }
  70. return findProviderStoresForRuntimeClass(ctx, r.Client, runtimeClass)
  71. }),
  72. ).
  73. Complete(r)
  74. }