Skip to content

Commit

Permalink
K8SPSMDB-1075 use controller runtime builder (#1692)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
nmarukovich and github-actions[bot] authored Nov 26, 2024
1 parent 72ef246 commit 437ac69
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 76 deletions.
25 changes: 7 additions & 18 deletions pkg/controller/perconaservermongodb/psmdb_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit 437ac69

Please sign in to comment.