From 437ac69df1c265831f1af10db3be2d71053a99c7 Mon Sep 17 00:00:00 2001 From: Natalia Marukovich Date: Tue, 26 Nov 2024 15:28:43 +0100 Subject: [PATCH] K8SPSMDB-1075 use controller runtime builder (#1692) * K8SPSMDB-1075 use controller runtime builder * fix * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * delete unused code * fix PR comments * fix controller * Update pkg/controller/perconaservermongodbbackup/perconaservermongodbbackup_controller.go Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix imports --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../perconaservermongodb/psmdb_controller.go | 25 +++------- .../perconaservermongodbbackup_controller.go | 48 ++++++------------- .../perconaservermongodbrestore_controller.go | 39 ++++++--------- 3 files changed, 36 insertions(+), 76 deletions(-) diff --git a/pkg/controller/perconaservermongodb/psmdb_controller.go b/pkg/controller/perconaservermongodb/psmdb_controller.go index 4a8efdac71..bc3f68f20d 100644 --- a/pkg/controller/perconaservermongodb/psmdb_controller.go +++ b/pkg/controller/perconaservermongodb/psmdb_controller.go @@ -25,15 +25,13 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/rest" "k8s.io/client-go/util/retry" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" "sigs.k8s.io/controller-runtime/pkg/client/config" - "sigs.k8s.io/controller-runtime/pkg/controller" - "sigs.k8s.io/controller-runtime/pkg/handler" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/source" "github.com/percona/percona-server-mongodb-operator/clientcmd" api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" @@ -134,21 +132,14 @@ func getOperatorPodImage(ctx context.Context) (string, error) { // add adds a new Controller to mgr with r as the reconcile.Reconciler func add(mgr manager.Manager, r reconcile.Reconciler) error { - // Create a new controller - c, err := controller.New("psmdb-controller", mgr, controller.Options{Reconciler: r}) - if err != nil { - return err - } - - // Watch for changes to primary resource PerconaServerMongoDB - err = c.Watch(source.Kind(mgr.GetCache(), &api.PerconaServerMongoDB{}, &handler.TypedEnqueueRequestForObject[*api.PerconaServerMongoDB]{})) - if err != nil { - return err - } - - return nil + return builder.ControllerManagedBy(mgr). + For(&api.PerconaServerMongoDB{}). + Named("psmdb-controller"). + Complete(r) } +var _ reconcile.Reconciler = &ReconcilePerconaServerMongoDB{} + type CronRegistry struct { crons *cron.Cron jobs map[string]Schedule @@ -172,8 +163,6 @@ func NewCronRegistry() CronRegistry { return c } -var _ reconcile.Reconciler = &ReconcilePerconaServerMongoDB{} - // ReconcilePerconaServerMongoDB reconciles a PerconaServerMongoDB object type ReconcilePerconaServerMongoDB struct { // This client, initialized using mgr.Client() above, is a split client diff --git a/pkg/controller/perconaservermongodbbackup/perconaservermongodbbackup_controller.go b/pkg/controller/perconaservermongodbbackup/perconaservermongodbbackup_controller.go index fe2fe90651..7e861939f4 100644 --- a/pkg/controller/perconaservermongodbbackup/perconaservermongodbbackup_controller.go +++ b/pkg/controller/perconaservermongodbbackup/perconaservermongodbbackup_controller.go @@ -14,20 +14,18 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/util/retry" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/handler" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/source" pbmBackup "github.com/percona/percona-backup-mongodb/pbm/backup" pbmErrors "github.com/percona/percona-backup-mongodb/pbm/errors" "github.com/percona/percona-backup-mongodb/pbm/storage" "github.com/percona/percona-backup-mongodb/pbm/storage/azure" "github.com/percona/percona-backup-mongodb/pbm/storage/s3" - "github.com/percona/percona-server-mongodb-operator/clientcmd" psmdbv1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" "github.com/percona/percona-server-mongodb-operator/pkg/naming" @@ -67,29 +65,20 @@ func newReconciler(mgr manager.Manager) (reconcile.Reconciler, error) { } // add adds a new Controller to mgr with r as the reconcile.Reconciler -func add(mgr manager.Manager, r reconcile.Reconciler) error { - // Create a new controller - c, err := controller.New("psmdbbackup-controller", mgr, controller.Options{Reconciler: r}) - if err != nil { - return err - } - - // Watch for changes to primary resource PerconaServerMongoDBBackup - err = c.Watch(source.Kind(mgr.GetCache(), &psmdbv1.PerconaServerMongoDBBackup{}, &handler.TypedEnqueueRequestForObject[*psmdbv1.PerconaServerMongoDBBackup]{})) - if err != nil { - return err - } - // TODO(user): Modify this to be the types you create that are owned by the primary resource - // Watch for changes to secondary resource Pods and requeue the owner PerconaServerMongoDBBackup - err = c.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}, handler.TypedEnqueueRequestForOwner[*corev1.Pod]( - mgr.GetScheme(), mgr.GetRESTMapper(), &psmdbv1.PerconaServerMongoDBBackup{}, handler.OnlyControllerOwner(), - ))) - if err != nil { - return err - } - - return nil +func add(mgr manager.Manager, r reconcile.Reconciler) error { + return builder.ControllerManagedBy(mgr). + Named("psmdbbackup-controller"). + For(&psmdbv1.PerconaServerMongoDBBackup{}). + Watches( + &corev1.Pod{}, + handler.EnqueueRequestForOwner( + mgr.GetScheme(), mgr.GetRESTMapper(), + &psmdbv1.PerconaServerMongoDBBackup{}, + handler.OnlyControllerOwner(), + ), + ). + Complete(r) } var _ reconcile.Reconciler = &ReconcilePerconaServerMongoDBBackup{} @@ -177,15 +166,6 @@ func (r *ReconcilePerconaServerMongoDBBackup) Reconcile(ctx context.Context, req if err != nil { return rr, errors.Wrapf(err, "set defaults for %s/%s", cluster.Namespace, cluster.Name) } - // TODO: Remove after 1.15 - if cluster.CompareVersion("1.12.0") >= 0 && cr.Spec.ClusterName == "" { - cr.Spec.ClusterName = cr.Spec.PSMDBCluster - cr.Spec.PSMDBCluster = "" - err = r.client.Update(ctx, cr) - if err != nil { - return rr, errors.Wrap(err, "failed to update clusterName") - } - } } bcp, err := r.newBackup(ctx, cluster) diff --git a/pkg/controller/perconaservermongodbrestore/perconaservermongodbrestore_controller.go b/pkg/controller/perconaservermongodbrestore/perconaservermongodbrestore_controller.go index ac2d8c784b..8cff8f259e 100644 --- a/pkg/controller/perconaservermongodbrestore/perconaservermongodbrestore_controller.go +++ b/pkg/controller/perconaservermongodbrestore/perconaservermongodbrestore_controller.go @@ -13,13 +13,12 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/util/retry" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/handler" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "sigs.k8s.io/controller-runtime/pkg/source" "github.com/percona/percona-backup-mongodb/pbm/defs" @@ -56,29 +55,21 @@ func newReconciler(mgr manager.Manager) (reconcile.Reconciler, error) { }, nil } -// add adds a new Controller to mgr with r as the reconcile.Reconciler -func add(mgr manager.Manager, r reconcile.Reconciler) error { - // Create a new controller - c, err := controller.New("psmdbrestore-controller", mgr, controller.Options{Reconciler: r}) - if err != nil { - return err - } +//add adds a new Controller to mgr with r as the reconcile.Reconciler - // Watch for changes to primary resource PerconaServerMongoDBRestore - err = c.Watch(source.Kind(mgr.GetCache(), &psmdbv1.PerconaServerMongoDBRestore{}, &handler.TypedEnqueueRequestForObject[*psmdbv1.PerconaServerMongoDBRestore]{})) - if err != nil { - return err - } - - // Watch for changes to secondary resource Pods and requeue the owner PerconaServerMongoDBRestore - err = c.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}, handler.TypedEnqueueRequestForOwner[*corev1.Pod]( - mgr.GetScheme(), mgr.GetRESTMapper(), &psmdbv1.PerconaServerMongoDBRestore{}, handler.OnlyControllerOwner(), - ))) - if err != nil { - return err - } - - return nil +func add(mgr manager.Manager, r reconcile.Reconciler) error { + return builder.ControllerManagedBy(mgr). + Named("psmdbrestore-controller"). + For(&psmdbv1.PerconaServerMongoDBRestore{}). + Watches( + &corev1.Pod{}, + handler.EnqueueRequestForOwner( + mgr.GetScheme(), mgr.GetRESTMapper(), + &psmdbv1.PerconaServerMongoDBRestore{}, + handler.OnlyControllerOwner(), + ), + ). + Complete(r) } var _ reconcile.Reconciler = &ReconcilePerconaServerMongoDBRestore{}