Skip to content

Commit a9c6ac5

Browse files
committed
Implement PR feedback #1
1 parent 301e6b2 commit a9c6ac5

File tree

11 files changed

+77
-74
lines changed

11 files changed

+77
-74
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
* Added new **ClusterMongoDBRole** CRD to support reusable roles across multiple MongoDB clusters.
99
* This allows users to define roles once and reuse them in multiple **MongoDB** or **MongoDBMultiCluster** resources. The role can be referenced through the `.spec.security.roleRefs` field. Note that only one of `.spec.security.roles` and `.spec.security.roleRefs` can be used at a time.
10+
* **ClusterMongoDBRole** resources are treated by the operator as a custom role templates that are only used when referenced by the database resources.
1011
* The new resource is watched by default by the operator. This means that the operator will require a new **ClusterRole** and **ClusterRoleBinding** to be created in the cluster. **ClusterRole** and **ClusterRoleBinding** resources are created by default with the helm chart or the kubectl mongodb plugin. To disable this behavior, set the `operator.enableClusterMongoDBRoles` value to `false` in the helm chart values.
1112
* The new **ClusterMongoDBRole** resource is designed to be read-only, meaning it can be used by MongoDB deployments managed by different operators.
12-
* The **ClusterMongoDBRole** resource can be deleted at any time, but the operator will not delete any roles that were created using this resource. To properly remove access, you must remove the reference to the **ClusterMongoDBRole** in the **MongoDB** or **MongoDBMultiCluster** resources.
13+
* The **ClusterMongoDBRole** resource can be deleted at any time, but the operator will not delete any roles that were created using this resource. To properly remove access, you must **manually** remove the reference to the **ClusterMongoDBRole** in the **MongoDB** or **MongoDBMultiCluster** resources.
1314
* The reference for this resource can be found here: **TODO** (link to documentation)
1415
* For more information please see: **TODO** (link to documentation)
1516

controllers/operator/common_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func NewReconcileCommonController(ctx context.Context, client client.Client) *Re
101101
// ensureRoles will first check if both roles and roleRefs are populated. If both are, it will return an error, which is inline with the webhook validation rules.
102102
// Otherwise, if roles is populated, then it will extract the list of roles and check if they are already set in Ops Manager. If they are not, it will update the roles in Ops Manager.
103103
// If roleRefs is populated, it will extract the list of roles from the referenced resources and check if they are already set in Ops Manager. If they are not, it will update the roles in Ops Manager.
104-
func (r *ReconcileCommonController) ensureRoles(ctx context.Context, db mdbv1.DbCommonSpec, watchClusterMongoDBRoles bool, conn om.Connection, mongodbResourceNsName types.NamespacedName, log *zap.SugaredLogger) workflow.Status {
104+
func (r *ReconcileCommonController) ensureRoles(ctx context.Context, db mdbv1.DbCommonSpec, enableClusterMongoDBRoles bool, conn om.Connection, mongodbResourceNsName types.NamespacedName, log *zap.SugaredLogger) workflow.Status {
105105
localRoles := db.GetSecurity().Roles
106106
roleRefs := db.GetSecurity().RoleRefs
107107

@@ -111,7 +111,7 @@ func (r *ReconcileCommonController) ensureRoles(ctx context.Context, db mdbv1.Db
111111

112112
var roles []mdbv1.MongoDBRole
113113
if len(roleRefs) > 0 {
114-
if !watchClusterMongoDBRoles {
114+
if !enableClusterMongoDBRoles {
115115
return workflow.Failed(xerrors.Errorf("RoleRefs are not supported when ClusterMongoDBRoles are disabled. Please enable ClusterMongoDBRoles in the operator configuration."))
116116
}
117117
var err error
@@ -171,7 +171,7 @@ func (r *ReconcileCommonController) getRoleRefs(ctx context.Context, roleRefs []
171171
err := r.client.Get(ctx, types.NamespacedName{Name: ref.Name}, customRole)
172172
if err != nil {
173173
if apiErrors.IsNotFound(err) {
174-
return nil, xerrors.Errorf("ClusterMongoDBRole '%s' not found. If the resource was deleted, the role is still present in MongoDB. The correctly remove a role from MongoDB, please remove the reference from spec.security.roleRefs.", ref.Name)
174+
return nil, xerrors.Errorf("ClusterMongoDBRole '%s' not found. If the resource was deleted, the role is still present in MongoDB. To correctly remove a role from MongoDB, please remove the reference from spec.security.roleRefs.", ref.Name)
175175
}
176176
return nil, xerrors.Errorf("Failed to retrieve ClusterMongoDBRole '%s': %w", ref.Name, err)
177177
}

controllers/operator/mongodbmultireplicaset_controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type ReconcileMongoDbMultiReplicaSet struct {
8181
memberClusterClientsMap map[string]kubernetesClient.Client // holds the client for each of the memberclusters(where the MongoDB ReplicaSet is deployed)
8282
memberClusterSecretClientsMap map[string]secrets.SecretClient
8383
forceEnterprise bool
84-
watchClusterMongoDBRoles bool
84+
enableClusterMongoDBRoles bool
8585

8686
imageUrls images.ImageUrls
8787
initDatabaseNonStaticImageVersion string
@@ -90,7 +90,7 @@ type ReconcileMongoDbMultiReplicaSet struct {
9090

9191
var _ reconcile.Reconciler = &ReconcileMongoDbMultiReplicaSet{}
9292

93-
func newMultiClusterReplicaSetReconciler(ctx context.Context, kubeClient client.Client, imageUrls images.ImageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion string, forceEnterprise bool, watchClusterMongoDBRoles bool, omFunc om.ConnectionFactory, memberClustersMap map[string]client.Client) *ReconcileMongoDbMultiReplicaSet {
93+
func newMultiClusterReplicaSetReconciler(ctx context.Context, kubeClient client.Client, imageUrls images.ImageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion string, forceEnterprise bool, enableClusterMongoDBRoles bool, omFunc om.ConnectionFactory, memberClustersMap map[string]client.Client) *ReconcileMongoDbMultiReplicaSet {
9494
clientsMap := make(map[string]kubernetesClient.Client)
9595
secretClientsMap := make(map[string]secrets.SecretClient)
9696

@@ -112,7 +112,7 @@ func newMultiClusterReplicaSetReconciler(ctx context.Context, kubeClient client.
112112
imageUrls: imageUrls,
113113
initDatabaseNonStaticImageVersion: initDatabaseNonStaticImageVersion,
114114
databaseNonStaticImageVersion: databaseNonStaticImageVersion,
115-
watchClusterMongoDBRoles: watchClusterMongoDBRoles,
115+
enableClusterMongoDBRoles: enableClusterMongoDBRoles,
116116
}
117117
}
118118

@@ -363,7 +363,7 @@ func (r *ReconcileMongoDbMultiReplicaSet) reconcileMemberResources(ctx context.C
363363
}
364364
}
365365
// Ensure custom roles are created in OM
366-
if status := r.ensureRoles(ctx, mrs.Spec.DbCommonSpec, r.watchClusterMongoDBRoles, conn, kube.ObjectKeyFromApiObject(mrs), log); !status.IsOK() {
366+
if status := r.ensureRoles(ctx, mrs.Spec.DbCommonSpec, r.enableClusterMongoDBRoles, conn, kube.ObjectKeyFromApiObject(mrs), log); !status.IsOK() {
367367
return status
368368
}
369369

@@ -1075,9 +1075,9 @@ func (r *ReconcileMongoDbMultiReplicaSet) reconcileOMCAConfigMap(ctx context.Con
10751075

10761076
// AddMultiReplicaSetController creates a new MongoDbMultiReplicaset Controller and adds it to the Manager. The Manager will set fields on the Controller
10771077
// and Start it when the Manager is Started.
1078-
func AddMultiReplicaSetController(ctx context.Context, mgr manager.Manager, imageUrls images.ImageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion string, forceEnterprise bool, watchClusterMongoDBRoles bool, memberClustersMap map[string]cluster.Cluster) error {
1078+
func AddMultiReplicaSetController(ctx context.Context, mgr manager.Manager, imageUrls images.ImageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion string, forceEnterprise bool, enableClusterMongoDBRoles bool, memberClustersMap map[string]cluster.Cluster) error {
10791079
// Create a new controller
1080-
reconciler := newMultiClusterReplicaSetReconciler(ctx, mgr.GetClient(), imageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion, forceEnterprise, watchClusterMongoDBRoles, om.NewOpsManagerConnection, multicluster.ClustersMapToClientMap(memberClustersMap))
1080+
reconciler := newMultiClusterReplicaSetReconciler(ctx, mgr.GetClient(), imageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion, forceEnterprise, enableClusterMongoDBRoles, om.NewOpsManagerConnection, multicluster.ClustersMapToClientMap(memberClustersMap))
10811081
c, err := controller.New(util.MongoDbMultiClusterController, mgr, controller.Options{Reconciler: reconciler, MaxConcurrentReconciles: env.ReadIntOrDefault(util.MaxConcurrentReconcilesEnv, 1)}) // nolint:forbidigo
10821082
if err != nil {
10831083
return err
@@ -1116,7 +1116,7 @@ func AddMultiReplicaSetController(ctx context.Context, mgr manager.Manager, imag
11161116
return err
11171117
}
11181118

1119-
if watchClusterMongoDBRoles {
1119+
if enableClusterMongoDBRoles {
11201120
err = c.Watch(source.Kind[client.Object](mgr.GetCache(), &rolev1.ClusterMongoDBRole{},
11211121
&watch.ResourcesHandler{ResourceType: watch.ClusterMongoDBRole, ResourceWatcher: reconciler.resourceWatcher}))
11221122
if err != nil {

controllers/operator/mongodbreplicaset_controller.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,24 @@ import (
5959
// ReconcileMongoDbReplicaSet reconciles a MongoDB with a type of ReplicaSet
6060
type ReconcileMongoDbReplicaSet struct {
6161
*ReconcileCommonController
62-
omConnectionFactory om.ConnectionFactory
63-
imageUrls images.ImageUrls
64-
forceEnterprise bool
65-
watchClusterMongoDBRoles bool
62+
omConnectionFactory om.ConnectionFactory
63+
imageUrls images.ImageUrls
64+
forceEnterprise bool
65+
enableClusterMongoDBRoles bool
6666

6767
initDatabaseNonStaticImageVersion string
6868
databaseNonStaticImageVersion string
6969
}
7070

7171
var _ reconcile.Reconciler = &ReconcileMongoDbReplicaSet{}
7272

73-
func newReplicaSetReconciler(ctx context.Context, kubeClient client.Client, imageUrls images.ImageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion string, forceEnterprise bool, watchClusterMongoDBRoles bool, omFunc om.ConnectionFactory) *ReconcileMongoDbReplicaSet {
73+
func newReplicaSetReconciler(ctx context.Context, kubeClient client.Client, imageUrls images.ImageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion string, forceEnterprise bool, enableClusterMongoDBRoles bool, omFunc om.ConnectionFactory) *ReconcileMongoDbReplicaSet {
7474
return &ReconcileMongoDbReplicaSet{
7575
ReconcileCommonController: NewReconcileCommonController(ctx, kubeClient),
7676
omConnectionFactory: omFunc,
7777
imageUrls: imageUrls,
7878
forceEnterprise: forceEnterprise,
79-
watchClusterMongoDBRoles: watchClusterMongoDBRoles,
79+
enableClusterMongoDBRoles: enableClusterMongoDBRoles,
8080

8181
initDatabaseNonStaticImageVersion: initDatabaseNonStaticImageVersion,
8282
databaseNonStaticImageVersion: databaseNonStaticImageVersion,
@@ -220,7 +220,7 @@ func (r *ReconcileMongoDbReplicaSet) Reconcile(ctx context.Context, request reco
220220
}
221221

222222
sts := construct.DatabaseStatefulSet(*rs, rsConfig, log)
223-
if status := r.ensureRoles(ctx, rs.Spec.DbCommonSpec, r.watchClusterMongoDBRoles, conn, kube.ObjectKeyFromApiObject(rs), log); !status.IsOK() {
223+
if status := r.ensureRoles(ctx, rs.Spec.DbCommonSpec, r.enableClusterMongoDBRoles, conn, kube.ObjectKeyFromApiObject(rs), log); !status.IsOK() {
224224
return r.updateStatus(ctx, rs, status, log)
225225
}
226226

@@ -349,9 +349,9 @@ func (r *ReconcileMongoDbReplicaSet) reconcileHostnameOverrideConfigMap(ctx cont
349349

350350
// AddReplicaSetController creates a new MongoDbReplicaset Controller and adds it to the Manager. The Manager will set fields on the Controller
351351
// and Start it when the Manager is Started.
352-
func AddReplicaSetController(ctx context.Context, mgr manager.Manager, imageUrls images.ImageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion string, forceEnterprise bool, watchClusterMongoDBRoles bool) error {
352+
func AddReplicaSetController(ctx context.Context, mgr manager.Manager, imageUrls images.ImageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion string, forceEnterprise bool, enableClusterMongoDBRoles bool) error {
353353
// Create a new controller
354-
reconciler := newReplicaSetReconciler(ctx, mgr.GetClient(), imageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion, forceEnterprise, watchClusterMongoDBRoles, om.NewOpsManagerConnection)
354+
reconciler := newReplicaSetReconciler(ctx, mgr.GetClient(), imageUrls, initDatabaseNonStaticImageVersion, databaseNonStaticImageVersion, forceEnterprise, enableClusterMongoDBRoles, om.NewOpsManagerConnection)
355355
c, err := controller.New(util.MongoDbReplicaSetController, mgr, controller.Options{Reconciler: reconciler, MaxConcurrentReconciles: env.ReadIntOrDefault(util.MaxConcurrentReconcilesEnv, 1)}) // nolint:forbidigo
356356
if err != nil {
357357
return err
@@ -390,7 +390,7 @@ func AddReplicaSetController(ctx context.Context, mgr manager.Manager, imageUrls
390390
return err
391391
}
392392

393-
if watchClusterMongoDBRoles {
393+
if enableClusterMongoDBRoles {
394394
err = c.Watch(source.Kind[client.Object](mgr.GetCache(), &rolev1.ClusterMongoDBRole{},
395395
&watch.ResourcesHandler{ResourceType: watch.ClusterMongoDBRole, ResourceWatcher: reconciler.resourceWatcher}))
396396
if err != nil {

0 commit comments

Comments
 (0)