Skip to content

Commit 9bf2482

Browse files
committed
remove common reconciler
1 parent 19de9e6 commit 9bf2482

25 files changed

+108
-146
lines changed

pkg/controller/common/common.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

pkg/controller/perconaservermongodb/backup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (r *ReconcilePerconaServerMongoDB) createOrUpdateBackupTask(ctx context.Con
6060
if err != nil {
6161
return errors.Wrap(err, "can't create job")
6262
}
63-
err = setControllerReference(cr, &cjob, r.Scheme())
63+
err = setControllerReference(cr, &cjob, r.scheme)
6464
if err != nil {
6565
return errors.Wrapf(err, "set owner reference for backup task %s", cjob.Name)
6666
}

pkg/controller/perconaservermongodb/balancer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (r *ReconcilePerconaServerMongoDB) enableBalancerIfNeeded(ctx context.Conte
8585
}
8686
}
8787

88-
mongosSession, err := r.MongosClientWithRole(ctx, cr, api.RoleClusterAdmin)
88+
mongosSession, err := r.MongoClient().Mongos(ctx, cr, api.RoleClusterAdmin)
8989
if err != nil {
9090
return errors.Wrap(err, "failed to get mongos connection")
9191
}
@@ -141,7 +141,7 @@ func (r *ReconcilePerconaServerMongoDB) disableBalancer(ctx context.Context, cr
141141
return errors.Wrapf(err, "get mongos statefulset %s", msSts.Name)
142142
}
143143

144-
mongosSession, err := r.MongosClientWithRole(ctx, cr, api.RoleClusterAdmin)
144+
mongosSession, err := r.MongoClient().Mongos(ctx, cr, api.RoleClusterAdmin)
145145
if err != nil {
146146
return errors.Wrap(err, "failed to get mongos connection")
147147
}

pkg/controller/perconaservermongodb/connections_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2020

2121
api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
22-
"github.com/percona/percona-server-mongodb-operator/pkg/controller/common"
2322
"github.com/percona/percona-server-mongodb-operator/pkg/naming"
2423
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb"
2524
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/mongo"
@@ -159,7 +158,7 @@ func TestConnectionLeaks(t *testing.T) {
159158
connectionCount := new(int)
160159

161160
r := buildFakeClient(obj...)
162-
r.CommonReconciler = common.New(r.Client(), r.Scheme(), r.NewPBMFunc(), &fakeMongoClientProvider{pods: rsPods, cr: cr, connectionCount: connectionCount})
161+
r.MongoProviderBase = psmdb.NewProviderBase(r.client, &fakeMongoClientProvider{pods: rsPods, cr: cr, connectionCount: connectionCount})
163162
r.serverVersion = &version.ServerVersion{Platform: version.PlatformKubernetes}
164163
r.crons = NewCronRegistry()
165164

@@ -396,18 +395,18 @@ func (g *fakeMongoClientProvider) Mongos(ctx context.Context, cr *api.PerconaSer
396395
return &fakeMongoClient{pods: g.pods, cr: g.cr, connectionCount: g.connectionCount, Client: fakeClient}, nil
397396
}
398397

399-
func (g *fakeMongoClientProvider) Standalone(ctx context.Context, cr *api.PerconaServerMongoDB, role api.SystemUserRole, host string, tlsEnabled bool) (mongo.Client, error) {
398+
func (g *fakeMongoClientProvider) Standalone(ctx context.Context, cr *api.PerconaServerMongoDB, rs *api.ReplsetSpec, role api.SystemUserRole, pod corev1.Pod) (mongo.Client, error) {
400399
*g.connectionCount++
401400

402401
fakeClient := mongoFake.NewClient()
403-
return &fakeMongoClient{pods: g.pods, cr: g.cr, connectionCount: g.connectionCount, Client: fakeClient, host: host}, nil
402+
return &fakeMongoClient{pods: g.pods, cr: g.cr, connectionCount: g.connectionCount, Client: fakeClient, pod: &pod}, nil
404403
}
405404

406405
type fakeMongoClient struct {
407406
pods []client.Object
408407
cr *api.PerconaServerMongoDB
409408
connectionCount *int
410-
host string
409+
pod *corev1.Pod
411410
mongo.Client
412411
}
413412

@@ -523,7 +522,7 @@ func (c *fakeMongoClient) IsMaster(ctx context.Context) (*mongo.IsMasterResp, er
523522
if err := c.cr.CheckNSetDefaults(ctx, version.PlatformKubernetes); err != nil {
524523
return nil, err
525524
}
526-
if c.host == psmdb.GetAddr(c.cr, c.pods[0].GetName(), c.cr.Spec.Replsets[0].Name, c.cr.Spec.Replsets[0].GetPort()) {
525+
if c.pod.GetName() == c.pods[0].GetName() {
527526
isMaster = true
528527
}
529528
return &mongo.IsMasterResp{

pkg/controller/perconaservermongodb/custom_users.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ func (r *ReconcilePerconaServerMongoDB) reconcileCustomUsers(ctx context.Context
3535
var err error
3636
var mongoCli mongo.Client
3737
if cr.Spec.Sharding.Enabled {
38-
mongoCli, err = r.MongosClientWithRole(ctx, cr, api.RoleUserAdmin)
38+
mongoCli, err = r.MongoClient().Mongos(ctx, cr, api.RoleUserAdmin)
3939
} else {
40-
mongoCli, err = r.MongoClientWithRole(ctx, cr, cr.Spec.Replsets[0], api.RoleUserAdmin)
40+
mongoCli, err = r.MongoClient().Mongo(ctx, cr, cr.Spec.Replsets[0], api.RoleUserAdmin)
4141
}
4242
if err != nil {
4343
return errors.Wrap(err, "failed to get mongo client")

pkg/controller/perconaservermongodb/fcv.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func (r *ReconcilePerconaServerMongoDB) getFCV(ctx context.Context, cr *api.PerconaServerMongoDB) (string, error) {
15-
c, err := r.MongoClientWithRole(ctx, cr, cr.Spec.Replsets[0], api.RoleClusterAdmin)
15+
c, err := r.MongoClient().Mongo(ctx, cr, cr.Spec.Replsets[0], api.RoleClusterAdmin)
1616
if err != nil {
1717
return "", errors.Wrap(err, "failed to get connection")
1818
}
@@ -40,9 +40,9 @@ func (r *ReconcilePerconaServerMongoDB) setFCV(ctx context.Context, cr *api.Perc
4040
var connErr error
4141

4242
if cr.Spec.Sharding.Enabled {
43-
cli, connErr = r.MongosClientWithRole(ctx, cr, api.RoleClusterAdmin)
43+
cli, connErr = r.MongoClient().Mongos(ctx, cr, api.RoleClusterAdmin)
4444
} else {
45-
cli, connErr = r.MongoClientWithRole(ctx, cr, cr.Spec.Replsets[0], api.RoleClusterAdmin)
45+
cli, connErr = r.MongoClient().Mongo(ctx, cr, cr.Spec.Replsets[0], api.RoleClusterAdmin)
4646
}
4747

4848
if connErr != nil {

pkg/controller/perconaservermongodb/finalizers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (r *ReconcilePerconaServerMongoDB) checkFinalizers(ctx context.Context, cr
8585
}
8686

8787
func (r *ReconcilePerconaServerMongoDB) deleteAllPITRChunks(ctx context.Context, cr *api.PerconaServerMongoDB) error {
88-
pbmc, err := r.NewPBM(ctx, cr)
88+
pbmc, err := r.newPBMFunc(ctx, r.client, cr)
8989
if err != nil {
9090
return errors.Wrap(err, "new pbm")
9191
}

pkg/controller/perconaservermongodb/mgo.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileCluster(ctx context.Context, cr
9595
}
9696
}
9797

98-
cli, err := r.MongoClientWithRole(ctx, cr, replset, api.RoleClusterAdmin)
98+
cli, err := r.MongoClient().Mongo(ctx, cr, replset, api.RoleClusterAdmin)
9999
if err != nil {
100100
if cr.Spec.Unmanaged {
101101
return api.AppStateInit, nil, nil
@@ -193,7 +193,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileCluster(ctx context.Context, cr
193193
replset.ClusterRole == api.ClusterRoleShardSvr &&
194194
len(mongosPods) > 0 && cr.Spec.Sharding.Mongos.Size > 0 {
195195

196-
mongosSession, err := r.MongosClientWithRole(ctx, cr, api.RoleClusterAdmin)
196+
mongosSession, err := r.MongoClient().Mongos(ctx, cr, api.RoleClusterAdmin)
197197
if err != nil {
198198
return api.AppStateError, nil, errors.Wrap(err, "failed to get mongos connection")
199199
}
@@ -571,7 +571,7 @@ func (r *ReconcilePerconaServerMongoDB) removeRSFromShard(ctx context.Context, c
571571
return nil
572572
}
573573

574-
cli, err := r.MongosClientWithRole(ctx, cr, api.RoleClusterAdmin)
574+
cli, err := r.MongoClient().Mongos(ctx, cr, api.RoleClusterAdmin)
575575
if err != nil {
576576
return errors.Errorf("failed to get mongos connection: %v", err)
577577
}
@@ -621,7 +621,7 @@ func (r *ReconcilePerconaServerMongoDB) handleRsAddToShard(ctx context.Context,
621621
return errors.Wrapf(err, "get rsPod %s host", rspod.Name)
622622
}
623623

624-
cli, err := r.MongosClientWithRole(ctx, cr, api.RoleClusterAdmin)
624+
cli, err := r.MongoClient().Mongos(ctx, cr, api.RoleClusterAdmin)
625625
if err != nil {
626626
return errors.Wrap(err, "failed to get mongos client")
627627
}
@@ -757,7 +757,7 @@ func (r *ReconcilePerconaServerMongoDB) handleReplicaSetNoPrimary(ctx context.Co
757757
}
758758

759759
log.Info("Connecting to pod", "pod", pod.Name, "user", api.RoleClusterAdmin)
760-
cli, err := r.StandaloneClientWithRole(ctx, cr, replset, api.RoleClusterAdmin, pod)
760+
cli, err := r.MongoClient().Standalone(ctx, cr, replset, api.RoleClusterAdmin, pod)
761761
if err != nil {
762762
return errors.Wrap(err, "get standalone mongo client")
763763
}
@@ -922,7 +922,7 @@ func compareRoles(x []mongo.Role, y []mongo.Role) bool {
922922
func (r *ReconcilePerconaServerMongoDB) createOrUpdateSystemUsers(ctx context.Context, cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec) error {
923923
log := logf.FromContext(ctx)
924924

925-
cli, err := r.MongoClientWithRole(ctx, cr, replset, api.RoleUserAdmin)
925+
cli, err := r.MongoClient().Mongo(ctx, cr, replset, api.RoleUserAdmin)
926926
if err != nil {
927927
return errors.Wrap(err, "failed to get mongo client")
928928
}

pkg/controller/perconaservermongodb/psmdb_controller.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ func newReconciler(mgr manager.Manager) (reconcile.Reconciler, error) {
8888
}
8989

9090
return &ReconcilePerconaServerMongoDB{
91-
CommonReconciler: common.New(client, mgr.GetScheme(), backup.NewPBM, nil),
9291
client: client,
92+
scheme: client.Scheme(),
93+
newPBMFunc: backup.NewPBM,
9394
serverVersion: sv,
9495
reconcileIn: time.Second * 5,
9596
crons: NewCronRegistry(),
@@ -172,16 +173,20 @@ func NewCronRegistry() CronRegistry {
172173
// ReconcilePerconaServerMongoDB reconciles a PerconaServerMongoDB object
173174
type ReconcilePerconaServerMongoDB struct {
174175
common.CommonReconciler
176+
psmdb.MongoProviderBase
175177
// This client, initialized using mgr.Client() above, is a split client
176178
// that reads objects from the cache and writes to the apiserver
177179
client client.Client
180+
scheme *runtime.Scheme
178181
restConfig *rest.Config
179182

180183
crons CronRegistry
181184
clientcmd *clientcmd.Client
182185
serverVersion *version.ServerVersion
183186
reconcileIn time.Duration
184187

188+
newPBMFunc backup.NewPBMFunc
189+
185190
newCertManagerCtrlFunc tls.NewCertManagerControllerFunc
186191

187192
initImage string
@@ -841,7 +846,7 @@ func (r *ReconcilePerconaServerMongoDB) checkIfUserDataExistInRS(ctx context.Con
841846
return errors.Wrap(err, "failed to set port")
842847
}
843848

844-
mc, err := r.MongoClientWithRole(ctx, cr, rs, api.RoleClusterAdmin)
849+
mc, err := r.MongoClient().Mongo(ctx, cr, rs, api.RoleClusterAdmin)
845850
if err != nil {
846851
return errors.Wrap(err, "dial:")
847852
}
@@ -884,7 +889,7 @@ func (r *ReconcilePerconaServerMongoDB) ensureSecurityKey(ctx context.Context, c
884889
if err != nil && k8serrors.IsNotFound(err) {
885890
created = true
886891
if setOwner {
887-
err = setControllerReference(cr, key, r.Scheme())
892+
err = setControllerReference(cr, key, r.scheme)
888893
if err != nil {
889894
return false, errors.Wrap(err, "set owner ref")
890895
}
@@ -1182,7 +1187,7 @@ func deleteConfigMapIfExists(ctx context.Context, cl client.Client, cr *api.Perc
11821187
}
11831188

11841189
func (r *ReconcilePerconaServerMongoDB) createOrUpdateConfigMap(ctx context.Context, cr *api.PerconaServerMongoDB, configMap *corev1.ConfigMap) error {
1185-
err := setControllerReference(cr, configMap, r.Scheme())
1190+
err := setControllerReference(cr, configMap, r.scheme)
11861191
if err != nil {
11871192
return errors.Wrapf(err, "failed to set controller ref for config map %s", configMap.Name)
11881193
}
@@ -1261,7 +1266,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileMongosStatefulset(ctx context.C
12611266
}
12621267

12631268
sts := psmdb.MongosStatefulset(cr)
1264-
err = setControllerReference(cr, sts, r.Scheme())
1269+
err = setControllerReference(cr, sts, r.scheme)
12651270
if err != nil {
12661271
return errors.Wrapf(err, "set owner ref for statefulset %s", sts.Name)
12671272
}
@@ -1488,7 +1493,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcilePDB(ctx context.Context, cr *ap
14881493
if cr.CompareVersion("1.17.0") < 0 {
14891494
pdb.Labels = nil
14901495
}
1491-
err := setControllerReference(owner, pdb, r.Scheme())
1496+
err := setControllerReference(owner, pdb, r.scheme)
14921497
if err != nil {
14931498
return errors.Wrap(err, "set owner reference")
14941499
}

pkg/controller/perconaservermongodb/service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileReplsetServices(ctx context.Con
3131
for _, rs := range repls {
3232
// Create headless service
3333
service := psmdb.Service(cr, rs)
34-
if err := setControllerReference(cr, service, r.Scheme()); err != nil {
34+
if err := setControllerReference(cr, service, r.scheme); err != nil {
3535
return errors.Wrapf(err, "set owner ref for service %s", service.Name)
3636
}
3737
if err := r.createOrUpdateSvc(ctx, cr, service, true); err != nil {
@@ -84,7 +84,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileMongosSvc(ctx context.Context,
8484
func (r *ReconcilePerconaServerMongoDB) ensureExternalServices(ctx context.Context, cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec, podList *corev1.PodList) error {
8585
for _, pod := range podList.Items {
8686
service := psmdb.ExternalService(cr, replset, pod.Name)
87-
err := setControllerReference(cr, service, r.Scheme())
87+
err := setControllerReference(cr, service, r.scheme)
8888
if err != nil {
8989
return errors.Wrapf(err, "set owner ref for Service %s", service.Name)
9090
}
@@ -111,7 +111,7 @@ func (r *ReconcilePerconaServerMongoDB) exportService(ctx context.Context, cr *a
111111
return nil
112112
}
113113
se := mcs.ServiceExport(cr.Namespace, svc.Name, ls)
114-
if err := setControllerReference(cr, se, r.Scheme()); err != nil {
114+
if err := setControllerReference(cr, se, r.scheme); err != nil {
115115
return errors.Wrapf(err, "set owner ref for serviceexport %s", se.Name)
116116
}
117117
if err := r.createOrUpdate(ctx, se); err != nil {
@@ -261,7 +261,7 @@ func (r *ReconcilePerconaServerMongoDB) removeOutdatedMongosSvc(ctx context.Cont
261261

262262
func (r *ReconcilePerconaServerMongoDB) createOrUpdateMongosSvc(ctx context.Context, cr *api.PerconaServerMongoDB, name string) error {
263263
svc := psmdb.MongosService(cr, name)
264-
err := setControllerReference(cr, &svc, r.Scheme())
264+
err := setControllerReference(cr, &svc, r.scheme)
265265
if err != nil {
266266
return errors.Wrapf(err, "set owner ref for service %s", svc.Name)
267267
}

0 commit comments

Comments
 (0)