Skip to content

Commit 1e2c4ae

Browse files
committed
Controller improvements
1 parent f39d647 commit 1e2c4ae

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

controllers/operator/clustermongodbrole_controller.go

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import (
55
"encoding/json"
66
"fmt"
77
"strings"
8-
"time"
98

109
"go.uber.org/zap"
1110
"golang.org/x/xerrors"
11+
"k8s.io/apimachinery/pkg/api/errors"
1212
"k8s.io/apimachinery/pkg/fields"
13+
"k8s.io/apimachinery/pkg/types"
14+
"sigs.k8s.io/controller-runtime/pkg/builder"
1315
"sigs.k8s.io/controller-runtime/pkg/client"
1416
"sigs.k8s.io/controller-runtime/pkg/controller"
1517
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1618
"sigs.k8s.io/controller-runtime/pkg/handler"
1719
"sigs.k8s.io/controller-runtime/pkg/manager"
1820
"sigs.k8s.io/controller-runtime/pkg/reconcile"
19-
"sigs.k8s.io/controller-runtime/pkg/source"
2021

2122
ctrl "sigs.k8s.io/controller-runtime"
2223

@@ -47,10 +48,13 @@ func (r *ClusterMongoDBRoleReconciler) Reconcile(ctx context.Context, request ct
4748
log := zap.S().With("ClusterMongoDBRole", request.NamespacedName)
4849
log.Info("-> ClusterMongoDBRole.Reconcile")
4950

50-
role, err := r.getRole(ctx, request, log)
51+
role := &rolev1.ClusterMongoDBRole{}
52+
reconcileResult, err := r.prepareResourceForReconciliation(ctx, request, role, log)
5153
if err != nil {
52-
log.Warnf("error getting custom role %s", err)
53-
return reconcile.Result{RequeueAfter: time.Second * util.RetryTimeSec}, nil
54+
if errors.IsNotFound(err) {
55+
return workflow.Invalid("Object for reconciliation not found").ReconcileResult()
56+
}
57+
return reconcileResult, err
5458
}
5559

5660
log.Infow("ClusterMongoDBRole.Spec", "spec", role.Spec)
@@ -85,30 +89,25 @@ func (r *ClusterMongoDBRoleReconciler) Reconcile(ctx context.Context, request ct
8589
}
8690

8791
func AddClusterMongoDBRoleController(ctx context.Context, mgr manager.Manager) error {
88-
reconciler := newClusterMongoDBRoleReconciler(ctx, mgr.GetClient())
89-
c, err := controller.New(util.ClusterMongoDbRoleController, mgr, controller.Options{Reconciler: reconciler, MaxConcurrentReconciles: env.ReadIntOrDefault(util.MaxConcurrentReconcilesEnv, 1)}) // nolint:forbidigo
90-
if err != nil {
91-
return err
92-
}
93-
94-
// Watch for changes to ClusterMongoDBRole resources
95-
// We don't need a Delete handler as there is nothing to do after removing the finalizer
96-
// To not get into an infinite reconcile loop, we ignore the delete event since the cleanup was already performed
97-
err = c.Watch(source.Kind[client.Object](mgr.GetCache(), &rolev1.ClusterMongoDBRole{}, &handler.EnqueueRequestForObject{}, watch.PredicatesForClusterRole()))
98-
if err != nil {
99-
return err
100-
}
92+
r := newClusterMongoDBRoleReconciler(ctx, mgr.GetClient())
10193

102-
if err = mgr.GetFieldIndexer().IndexField(ctx, &mdbv1.MongoDB{}, ClusterMongoDBRoleIndexForMdb, findRolesForMongoDB); err != nil {
94+
if err := mgr.GetFieldIndexer().IndexField(ctx, &mdbv1.MongoDB{}, ClusterMongoDBRoleIndexForMdb, findRolesForMongoDB); err != nil {
10395
return err
10496
}
10597

106-
if err = mgr.GetFieldIndexer().IndexField(ctx, &mdbmultiv1.MongoDBMultiCluster{}, ClusterMongoDBRoleIndexForMdbMulti, findRolesForMongoDBMultiCluster); err != nil {
98+
if err := mgr.GetFieldIndexer().IndexField(ctx, &mdbmultiv1.MongoDBMultiCluster{}, ClusterMongoDBRoleIndexForMdbMulti, findRolesForMongoDBMultiCluster); err != nil {
10799
return err
108100
}
109101

110102
zap.S().Infof("Registered controller %s", util.ClusterMongoDbRoleController)
111-
return nil
103+
104+
return ctrl.NewControllerManagedBy(mgr).
105+
Named(util.ClusterMongoDbRoleController).
106+
For(&rolev1.ClusterMongoDBRole{}, builder.WithPredicates(watch.PredicatesForClusterRole())).
107+
WithOptions(controller.Options{MaxConcurrentReconciles: env.ReadIntOrDefault(util.MaxConcurrentReconcilesEnv, 1)}). // nolint:forbidigo
108+
Watches(&mdbv1.MongoDB{}, handler.EnqueueRequestsFromMapFunc(getReconcileRequestsForMongoDB)).
109+
Watches(&mdbmultiv1.MongoDBMultiCluster{}, handler.EnqueueRequestsFromMapFunc(getReconcileRequestsForMongoDBMultiCluster)).
110+
Complete(r)
112111
}
113112

114113
func newClusterMongoDBRoleReconciler(ctx context.Context, kubeClient client.Client) *ClusterMongoDBRoleReconciler {
@@ -117,15 +116,6 @@ func newClusterMongoDBRoleReconciler(ctx context.Context, kubeClient client.Clie
117116
}
118117
}
119118

120-
func (r *ClusterMongoDBRoleReconciler) getRole(ctx context.Context, request reconcile.Request, log *zap.SugaredLogger) (*rolev1.ClusterMongoDBRole, error) {
121-
role := &rolev1.ClusterMongoDBRole{}
122-
if _, err := r.GetResource(ctx, request, role, log); err != nil {
123-
return nil, err
124-
}
125-
126-
return role, nil
127-
}
128-
129119
// Delete handles the deletion of the ClusterMongoDBRole resource.
130120
// It ensures that no MongoDB or MongoDBMultiCluster resources are referencing this role.
131121
// If there are references, it moves the resource in a Pending phase with an appropriate message.
@@ -217,11 +207,12 @@ func getAnnotationsForCustomRoleResource(role *rolev1.ClusterMongoDBRole) (map[s
217207
// This is used to index the MongoDB resources by the ClusterMongoDBRole they reference.
218208
func findRolesForMongoDB(rawObj client.Object) []string {
219209
mdb, ok := rawObj.(*mdbv1.MongoDB)
210+
roles := make([]string, 0)
211+
220212
if !ok {
221-
return nil
213+
return roles
222214
}
223215

224-
roles := make([]string, 0)
225216
if mdb.Spec.Security == nil {
226217
return roles
227218
}
@@ -238,11 +229,12 @@ func findRolesForMongoDB(rawObj client.Object) []string {
238229
// This is used to index the MongoDBMultiCluster resources by the ClusterMongoDBRole they reference.
239230
func findRolesForMongoDBMultiCluster(rawObj client.Object) []string {
240231
mdb, ok := rawObj.(*mdbmultiv1.MongoDBMultiCluster)
232+
roles := make([]string, 0)
233+
241234
if !ok {
242-
return nil
235+
return roles
243236
}
244237

245-
roles := make([]string, 0)
246238
if mdb.Spec.Security == nil {
247239
return roles
248240
}
@@ -254,3 +246,25 @@ func findRolesForMongoDBMultiCluster(rawObj client.Object) []string {
254246
}
255247
return roles
256248
}
249+
250+
// getReconcileRequestsForMongoDB returns reconcile requests for ClusterMongoDBRole resources based on the roles referenced in MongoDB resources.
251+
func getReconcileRequestsForMongoDB(ctx context.Context, rawObj client.Object) []reconcile.Request {
252+
roles := findRolesForMongoDB(rawObj)
253+
requests := []reconcile.Request{}
254+
255+
for _, role := range roles {
256+
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Name: role}})
257+
}
258+
return requests
259+
}
260+
261+
// getReconcileRequestsForMongoDBMultiCluster returns reconcile requests for ClusterMongoDBRole resources based on the roles referenced in MongoDBMultiCluster resources.
262+
func getReconcileRequestsForMongoDBMultiCluster(ctx context.Context, rawObj client.Object) []reconcile.Request {
263+
roles := findRolesForMongoDBMultiCluster(rawObj)
264+
requests := []reconcile.Request{}
265+
266+
for _, role := range roles {
267+
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Name: role}})
268+
}
269+
return requests
270+
}

0 commit comments

Comments
 (0)