Skip to content

Commit 3c4502d

Browse files
pooknullmayankshah1607hors
authored
K8SPSMDB-1429: improve cluster status (#2148)
* K8SPSMDB-1429: improve cluster status https://perconadev.atlassian.net/browse/K8SPSMDB-1429 * small fix --------- Co-authored-by: Mayank Shah <mayank.shah@percona.com> Co-authored-by: Viacheslav Sarzhan <slava.sarzhan@percona.com>
1 parent c0b2541 commit 3c4502d

File tree

1 file changed

+44
-39
lines changed
  • pkg/controller/perconaservermongodb

1 file changed

+44
-39
lines changed

pkg/controller/perconaservermongodb/mgo.go

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
corev1 "k8s.io/api/core/v1"
1818
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1919
"k8s.io/apimachinery/pkg/types"
20+
"k8s.io/utils/ptr"
2021
logf "sigs.k8s.io/controller-runtime/pkg/log"
2122

2223
api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
@@ -177,58 +178,62 @@ func (r *ReconcilePerconaServerMongoDB) reconcileCluster(ctx context.Context, cr
177178
}
178179

179180
if cr.Spec.Sharding.Enabled &&
181+
cr.Spec.Sharding.Mongos.Size > 0 &&
180182
!rstRunning &&
181-
cr.Status.Replsets[replset.Name].Initialized &&
182-
cr.Status.Replsets[replset.Name].Status == api.AppStateReady &&
183-
cr.Status.Mongos != nil &&
184-
cr.Status.Mongos.Status == api.AppStateReady &&
185-
replset.ClusterRole == api.ClusterRoleShardSvr &&
186-
len(mongosPods) > 0 && cr.Spec.Sharding.Mongos.Size > 0 {
187-
188-
mongosSession, err := r.mongosClientWithRole(ctx, cr, api.RoleClusterAdmin)
189-
if err != nil {
190-
return api.AppStateError, nil, errors.Wrap(err, "failed to get mongos connection")
191-
}
183+
replset.ClusterRole == api.ClusterRoleShardSvr {
184+
185+
rsStatus := cr.Status.Replsets[replset.Name]
186+
187+
if len(mongosPods) > 0 &&
188+
rsStatus.Initialized &&
189+
rsStatus.Status == api.AppStateReady &&
190+
cr.Status.Mongos != nil &&
191+
cr.Status.Mongos.Status == api.AppStateReady {
192192

193-
defer func() {
194-
err := mongosSession.Disconnect(ctx)
193+
mongosSession, err := r.mongosClientWithRole(ctx, cr, api.RoleClusterAdmin)
195194
if err != nil {
196-
log.Error(err, "failed to close mongos connection")
195+
return api.AppStateError, nil, errors.Wrap(err, "failed to get mongos connection")
197196
}
198-
}()
199197

200-
err = mongosSession.SetDefaultRWConcern(ctx, mongo.DefaultReadConcern, mongo.DefaultWriteConcern)
201-
// SetDefaultRWConcern introduced in MongoDB 4.4
202-
if err != nil && !strings.Contains(err.Error(), "CommandNotFound") {
203-
return api.AppStateError, nil, errors.Wrap(err, "set default RW concern")
204-
}
198+
defer func() {
199+
err := mongosSession.Disconnect(ctx)
200+
if err != nil {
201+
log.Error(err, "failed to close mongos connection")
202+
}
203+
}()
205204

206-
rsName := replset.Name
207-
name, err := replset.CustomReplsetName()
208-
if err == nil {
209-
rsName = name
210-
}
205+
err = mongosSession.SetDefaultRWConcern(ctx, mongo.DefaultReadConcern, mongo.DefaultWriteConcern)
206+
// SetDefaultRWConcern introduced in MongoDB 4.4
207+
if err != nil && !strings.Contains(err.Error(), "CommandNotFound") {
208+
return api.AppStateError, nil, errors.Wrap(err, "set default RW concern")
209+
}
211210

212-
in, err := inShard(ctx, mongosSession, rsName)
213-
if err != nil {
214-
return api.AppStateError, nil, errors.Wrap(err, "get shard")
215-
}
211+
rsName := replset.Name
212+
name, err := replset.CustomReplsetName()
213+
if err == nil {
214+
rsName = name
215+
}
216216

217-
if !in {
218-
log.Info("adding rs to shard", "rs", rsName)
219-
err := r.handleRsAddToShard(ctx, cr, replset, pods.Items[0], mongosPods[0])
217+
in, err := inShard(ctx, mongosSession, rsName)
220218
if err != nil {
221-
return api.AppStateError, nil, errors.Wrap(err, "add shard")
219+
return api.AppStateError, nil, errors.Wrap(err, "get shard")
222220
}
223221

224-
log.Info("added to shard", "rs", rsName)
225-
}
222+
if !in {
223+
log.Info("adding rs to shard", "rs", rsName)
224+
err := r.handleRsAddToShard(ctx, cr, replset, pods.Items[0], mongosPods[0])
225+
if err != nil {
226+
return api.AppStateError, nil, errors.Wrap(err, "add shard")
227+
}
226228

227-
rs := cr.Status.Replsets[replset.Name]
228-
t := true
229-
rs.AddedAsShard = &t
230-
cr.Status.Replsets[replset.Name] = rs
229+
log.Info("added to shard", "rs", rsName)
230+
}
231231

232+
rsStatus.AddedAsShard = ptr.To(true)
233+
cr.Status.Replsets[replset.Name] = rsStatus
234+
} else {
235+
return api.AppStateInit, nil, nil
236+
}
232237
}
233238

234239
if replset.Arbiter.Enabled && !cr.Spec.Sharding.Enabled {

0 commit comments

Comments
 (0)