controller.go 3.8 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 clusterproviderclass implements readiness reconciliation for ClusterProviderClass runtimes.
  14. package clusterproviderclass
  15. import (
  16. "context"
  17. "fmt"
  18. "time"
  19. "github.com/go-logr/logr"
  20. apierrors "k8s.io/apimachinery/pkg/api/errors"
  21. "k8s.io/apimachinery/pkg/api/meta"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. ctrl "sigs.k8s.io/controller-runtime"
  25. "sigs.k8s.io/controller-runtime/pkg/client"
  26. "sigs.k8s.io/controller-runtime/pkg/controller"
  27. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  28. grpccommon "github.com/external-secrets/external-secrets/providers/v2/common/grpc"
  29. )
  30. // Reconciler reconciles ClusterProviderClass resources by checking runtime health.
  31. type Reconciler struct {
  32. client.Client
  33. Log logr.Logger
  34. Scheme *runtime.Scheme
  35. RequeueInterval time.Duration
  36. CheckHealth func(context.Context, string, *grpccommon.TLSConfig) error
  37. }
  38. // Reconcile updates the Ready condition based on the runtime health check result.
  39. func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  40. log := r.Log.WithValues("ClusterProviderClass", req.NamespacedName)
  41. var obj esv1alpha1.ClusterProviderClass
  42. if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
  43. if apierrors.IsNotFound(err) {
  44. return ctrl.Result{}, nil
  45. }
  46. return ctrl.Result{}, err
  47. }
  48. tlsSecretNamespace := grpccommon.ResolveTLSSecretNamespace(obj.Spec.Address, "", "", "")
  49. tlsConfig, err := grpccommon.LoadClientTLSConfig(ctx, r.Client, obj.Spec.Address, tlsSecretNamespace)
  50. if err != nil {
  51. log.Error(err, "failed to load runtime TLS config")
  52. return r.updateStatus(ctx, &obj, metav1.ConditionFalse, "TLSConfigFailed", err.Error())
  53. }
  54. if err := r.reconcileHealth(ctx, &obj, tlsConfig); err != nil {
  55. log.Error(err, "runtime health check failed")
  56. return r.updateStatus(ctx, &obj, metav1.ConditionFalse, "HealthCheckFailed", err.Error())
  57. }
  58. return r.updateStatus(ctx, &obj, metav1.ConditionTrue, "Healthy", "runtime is serving")
  59. }
  60. func (r *Reconciler) reconcileHealth(ctx context.Context, obj *esv1alpha1.ClusterProviderClass, tlsConfig *grpccommon.TLSConfig) error {
  61. checker := r.CheckHealth
  62. if checker == nil {
  63. checker = grpccommon.CheckHealth
  64. }
  65. return checker(ctx, obj.Spec.Address, tlsConfig)
  66. }
  67. func (r *Reconciler) updateStatus(ctx context.Context, obj *esv1alpha1.ClusterProviderClass, status metav1.ConditionStatus, reason, message string) (ctrl.Result, error) {
  68. meta.SetStatusCondition(&obj.Status.Conditions, metav1.Condition{
  69. Type: "Ready",
  70. Status: status,
  71. Reason: reason,
  72. Message: message,
  73. LastTransitionTime: metav1.Now(),
  74. ObservedGeneration: obj.GetGeneration(),
  75. })
  76. if err := r.Status().Update(ctx, obj); err != nil {
  77. return ctrl.Result{}, fmt.Errorf("failed to update ClusterProviderClass status: %w", err)
  78. }
  79. return ctrl.Result{RequeueAfter: r.RequeueInterval}, nil
  80. }
  81. // SetupWithManager wires the controller into controller-runtime.
  82. func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error {
  83. return ctrl.NewControllerManagedBy(mgr).
  84. WithOptions(opts).
  85. For(&esv1alpha1.ClusterProviderClass{}).
  86. Complete(r)
  87. }