@@ -5,18 +5,19 @@ import (
5
5
"encoding/json"
6
6
"fmt"
7
7
"strings"
8
- "time"
9
8
10
9
"go.uber.org/zap"
11
10
"golang.org/x/xerrors"
11
+ "k8s.io/apimachinery/pkg/api/errors"
12
12
"k8s.io/apimachinery/pkg/fields"
13
+ "k8s.io/apimachinery/pkg/types"
14
+ "sigs.k8s.io/controller-runtime/pkg/builder"
13
15
"sigs.k8s.io/controller-runtime/pkg/client"
14
16
"sigs.k8s.io/controller-runtime/pkg/controller"
15
17
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
16
18
"sigs.k8s.io/controller-runtime/pkg/handler"
17
19
"sigs.k8s.io/controller-runtime/pkg/manager"
18
20
"sigs.k8s.io/controller-runtime/pkg/reconcile"
19
- "sigs.k8s.io/controller-runtime/pkg/source"
20
21
21
22
ctrl "sigs.k8s.io/controller-runtime"
22
23
@@ -47,10 +48,13 @@ func (r *ClusterMongoDBRoleReconciler) Reconcile(ctx context.Context, request ct
47
48
log := zap .S ().With ("ClusterMongoDBRole" , request .NamespacedName )
48
49
log .Info ("-> ClusterMongoDBRole.Reconcile" )
49
50
50
- role , err := r .getRole (ctx , request , log )
51
+ role := & rolev1.ClusterMongoDBRole {}
52
+ reconcileResult , err := r .prepareResourceForReconciliation (ctx , request , role , log )
51
53
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
54
58
}
55
59
56
60
log .Infow ("ClusterMongoDBRole.Spec" , "spec" , role .Spec )
@@ -85,30 +89,25 @@ func (r *ClusterMongoDBRoleReconciler) Reconcile(ctx context.Context, request ct
85
89
}
86
90
87
91
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 ())
101
93
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 {
103
95
return err
104
96
}
105
97
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 {
107
99
return err
108
100
}
109
101
110
102
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 )
112
111
}
113
112
114
113
func newClusterMongoDBRoleReconciler (ctx context.Context , kubeClient client.Client ) * ClusterMongoDBRoleReconciler {
@@ -117,15 +116,6 @@ func newClusterMongoDBRoleReconciler(ctx context.Context, kubeClient client.Clie
117
116
}
118
117
}
119
118
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
-
129
119
// Delete handles the deletion of the ClusterMongoDBRole resource.
130
120
// It ensures that no MongoDB or MongoDBMultiCluster resources are referencing this role.
131
121
// 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
217
207
// This is used to index the MongoDB resources by the ClusterMongoDBRole they reference.
218
208
func findRolesForMongoDB (rawObj client.Object ) []string {
219
209
mdb , ok := rawObj .(* mdbv1.MongoDB )
210
+ roles := make ([]string , 0 )
211
+
220
212
if ! ok {
221
- return nil
213
+ return roles
222
214
}
223
215
224
- roles := make ([]string , 0 )
225
216
if mdb .Spec .Security == nil {
226
217
return roles
227
218
}
@@ -238,11 +229,12 @@ func findRolesForMongoDB(rawObj client.Object) []string {
238
229
// This is used to index the MongoDBMultiCluster resources by the ClusterMongoDBRole they reference.
239
230
func findRolesForMongoDBMultiCluster (rawObj client.Object ) []string {
240
231
mdb , ok := rawObj .(* mdbmultiv1.MongoDBMultiCluster )
232
+ roles := make ([]string , 0 )
233
+
241
234
if ! ok {
242
- return nil
235
+ return roles
243
236
}
244
237
245
- roles := make ([]string , 0 )
246
238
if mdb .Spec .Security == nil {
247
239
return roles
248
240
}
@@ -254,3 +246,25 @@ func findRolesForMongoDBMultiCluster(rawObj client.Object) []string {
254
246
}
255
247
return roles
256
248
}
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