Skip to content

Commit 06a0c2a

Browse files
egegunesjvpasinatto
authored andcommitted
fixes
1 parent 05f0b31 commit 06a0c2a

File tree

2 files changed

+56
-51
lines changed

2 files changed

+56
-51
lines changed

pkg/controller/perconaservermongodb/mgo.go

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileCluster(ctx context.Context, cr
114114
return api.AppStateError, nil, errors.Wrap(err, "dial")
115115
}
116116

117-
err := r.handleReplsetInit(ctx, cr, replset, pods.Items)
117+
pod, primary, err := r.handleReplsetInit(ctx, cr, replset, pods.Items)
118118
if err != nil {
119119
return api.AppStateInit, nil, errors.Wrap(err, "handleReplsetInit")
120120
}
@@ -126,6 +126,7 @@ func (r *ReconcilePerconaServerMongoDB) reconcileCluster(ctx context.Context, cr
126126

127127
rs := cr.Status.Replsets[replset.Name]
128128
rs.Initialized = true
129+
rs.Members = map[string]api.ReplsetMemberStatus{pod.Name: *primary}
129130
cr.Status.Replsets[replset.Name] = rs
130131

131132
cr.Status.AddCondition(api.ClusterCondition{
@@ -242,25 +243,16 @@ func (r *ReconcilePerconaServerMongoDB) reconcileCluster(ctx context.Context, cr
242243
}
243244
}
244245

245-
rsMembers, err := r.updateConfigMembers(ctx, cli, cr, replset)
246+
rsMembers, liveMembers, err := r.updateConfigMembers(ctx, cli, cr, replset)
246247
if err != nil {
247248
return api.AppStateError, nil, errors.Wrap(err, "failed to update config members")
248249
}
249250

250-
membersLive := 0
251-
for _, member := range rsMembers {
252-
switch member.State {
253-
case mongo.MemberStatePrimary, mongo.MemberStateSecondary, mongo.MemberStateArbiter:
254-
membersLive++
255-
}
256-
257-
}
258-
259-
if membersLive == len(pods.Items) {
251+
if liveMembers == len(pods.Items) {
260252
return api.AppStateReady, rsMembers, nil
261253
}
262254

263-
log.V(1).Info("Replset is not ready", "liveMembers", membersLive, "pods", len(pods.Items))
255+
log.V(1).Info("Replset is not ready", "liveMembers", liveMembers, "pods", len(pods.Items))
264256

265257
return api.AppStateInit, rsMembers, nil
266258
}
@@ -373,10 +365,11 @@ func (r *ReconcilePerconaServerMongoDB) getConfigMemberForExternalNode(id int, e
373365
return member
374366
}
375367

376-
func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context, cli mongo.Client, cr *api.PerconaServerMongoDB, rs *api.ReplsetSpec) (map[string]api.ReplsetMemberStatus, error) {
368+
func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context, cli mongo.Client, cr *api.PerconaServerMongoDB, rs *api.ReplsetSpec) (map[string]api.ReplsetMemberStatus, int, error) {
377369
log := logf.FromContext(ctx)
378370
// Primary with a Secondary and an Arbiter (PSA)
379371
unsafePSA := false
372+
rsMembers := make(map[string]api.ReplsetMemberStatus)
380373

381374
if cr.CompareVersion("1.15.0") <= 0 {
382375
unsafePSA = cr.Spec.UnsafeConf && rs.Arbiter.Enabled && rs.Arbiter.Size == 1 && !rs.NonVoting.Enabled && rs.Size == 2
@@ -386,17 +379,17 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context,
386379

387380
pods, err := psmdb.GetRSPods(ctx, r.client, cr, rs.Name)
388381
if err != nil {
389-
return nil, errors.Wrap(err, "get rs pods")
382+
return rsMembers, 0, errors.Wrap(err, "get rs pods")
390383
}
391384

392385
cnf, err := cli.ReadConfig(ctx)
393386
if err != nil {
394-
return nil, errors.Wrap(err, "get replset config")
387+
return rsMembers, 0, errors.Wrap(err, "get replset config")
395388
}
396389

397390
rsStatus, err := cli.RSStatus(ctx)
398391
if err != nil {
399-
return nil, errors.Wrap(err, "get replset status")
392+
return rsMembers, 0, errors.Wrap(err, "get replset status")
400393
}
401394

402395
members := mongo.ConfigMembers{}
@@ -408,7 +401,7 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context,
408401

409402
member, err := r.getConfigMemberForPod(ctx, cr, rs, key, &pod)
410403
if err != nil {
411-
return nil, errors.Wrapf(err, "get config member for pod %s", pod.Name)
404+
return rsMembers, 0, errors.Wrapf(err, "get config member for pod %s", pod.Name)
412405
}
413406

414407
members = append(members, member)
@@ -433,7 +426,7 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context,
433426
if member.State == mongo.MemberStatePrimary {
434427
log.Info("Stepping down the primary", "member", member.Name)
435428
if err := cli.StepDown(ctx, 60, false); err != nil {
436-
return nil, errors.Wrap(err, "step down primary")
429+
return rsMembers, 0, errors.Wrap(err, "step down primary")
437430
}
438431
}
439432

@@ -442,10 +435,10 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context,
442435
log.Info("Fixing hostname of member", "replset", rs.Name, "id", member.Id, "member", member.Name)
443436

444437
if err := cli.WriteConfig(ctx, cnf, false); err != nil {
445-
return nil, errors.Wrap(err, "fix member hostname: write mongo config")
438+
return rsMembers, 0, errors.Wrap(err, "fix member hostname: write mongo config")
446439
}
447440

448-
return nil, nil
441+
return rsMembers, 0, nil
449442
}
450443

451444
if cnf.Members.FixMemberConfigs(ctx, members) {
@@ -454,7 +447,7 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context,
454447
log.Info("Fixing member configurations", "replset", rs.Name)
455448

456449
if err := cli.WriteConfig(ctx, cnf, false); err != nil {
457-
return nil, errors.Wrap(err, "fix member configurations: write mongo config")
450+
return rsMembers, 0, errors.Wrap(err, "fix member configurations: write mongo config")
458451
}
459452
}
460453

@@ -465,7 +458,7 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context,
465458

466459
err = cli.WriteConfig(ctx, cnf, false)
467460
if err != nil {
468-
return nil, errors.Wrap(err, "delete: write mongo config")
461+
return rsMembers, 0, errors.Wrap(err, "delete: write mongo config")
469462
}
470463
}
471464

@@ -476,7 +469,7 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context,
476469

477470
err = cli.WriteConfig(ctx, cnf, false)
478471
if err != nil {
479-
return nil, errors.Wrap(err, "add new: write mongo config")
472+
return rsMembers, 0, errors.Wrap(err, "add new: write mongo config")
480473
}
481474
}
482475

@@ -487,7 +480,7 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context,
487480

488481
err = cli.WriteConfig(ctx, cnf, false)
489482
if err != nil {
490-
return nil, errors.Wrap(err, "update external nodes: write mongo config")
483+
return rsMembers, 0, errors.Wrap(err, "update external nodes: write mongo config")
491484
}
492485
}
493486

@@ -500,41 +493,47 @@ func (r *ReconcilePerconaServerMongoDB) updateConfigMembers(ctx context.Context,
500493

501494
err := cli.WriteConfig(ctx, cnf, false)
502495
if err != nil {
503-
return nil, errors.Wrap(err, "set votes: write mongo config")
496+
return rsMembers, 0, errors.Wrap(err, "set votes: write mongo config")
504497
}
505498
}
506499

507500
rsStatus, err = cli.RSStatus(ctx)
508501
if err != nil {
509-
return nil, errors.Wrap(err, "unable to get replset members")
502+
return rsMembers, 0, errors.Wrap(err, "unable to get replset members")
510503
}
511504

512-
rsMembers := make(map[string]api.ReplsetMemberStatus)
505+
liveMembers := 0
513506
for _, member := range rsStatus.Members {
514507
var tags mongo.ReplsetTags
508+
515509
for i := range cnf.Members {
516510
if member.Id == cnf.Members[i].ID {
517511
tags = cnf.Members[i].Tags
518512
break
519513
}
520514
}
515+
521516
if _, ok := tags["external"]; ok {
522517
continue
523518
}
524519

525-
podName, ok := tags["podName"]
526-
if !ok {
527-
continue
520+
if podName, ok := tags["podName"]; ok {
521+
rsMembers[podName] = api.ReplsetMemberStatus{
522+
Name: member.Name,
523+
State: member.State,
524+
StateStr: member.StateStr,
525+
}
528526
}
529527

530-
rsMembers[podName] = api.ReplsetMemberStatus{
531-
Name: member.Name,
532-
State: member.State,
533-
StateStr: member.StateStr,
528+
log.V(1).Info("Member state", "member", member.Name, "state", member.State, "stateStr", member.StateStr)
529+
530+
switch member.State {
531+
case mongo.MemberStatePrimary, mongo.MemberStateSecondary, mongo.MemberStateArbiter:
532+
liveMembers++
534533
}
535534
}
536535

537-
return rsMembers, nil
536+
return rsMembers, liveMembers, nil
538537
}
539538

540539
func inShard(ctx context.Context, client mongo.Client, rsName string) (bool, error) {
@@ -645,7 +644,7 @@ func (r *ReconcilePerconaServerMongoDB) handleRsAddToShard(ctx context.Context,
645644
// This must be ran from within the running container to utilize the MongoDB Localhost Exception.
646645
//
647646
// See: https://www.mongodb.com/docs/manual/core/localhost-exception/
648-
func (r *ReconcilePerconaServerMongoDB) handleReplsetInit(ctx context.Context, cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec, pods []corev1.Pod) error {
647+
func (r *ReconcilePerconaServerMongoDB) handleReplsetInit(ctx context.Context, cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec, pods []corev1.Pod) (*corev1.Pod, *api.ReplsetMemberStatus, error) {
649648
log := logf.FromContext(ctx)
650649

651650
for _, pod := range pods {
@@ -663,19 +662,19 @@ func (r *ReconcilePerconaServerMongoDB) handleReplsetInit(ctx context.Context, c
663662

664663
member, err := r.getConfigMemberForPod(ctx, cr, replset, 0, &pod)
665664
if err != nil {
666-
return errors.Wrapf(err, "get config member for pod %s", pod.Name)
665+
return nil, nil, errors.Wrapf(err, "get config member for pod %s", pod.Name)
667666
}
668667

669668
memberBytes, err := json.Marshal(member)
670669
if err != nil {
671-
return errors.Wrap(err, "marshall member to json")
670+
return nil, nil, errors.Wrap(err, "marshall member to json")
672671
}
673672

674673
var errb, outb bytes.Buffer
675674

676675
err = r.clientcmd.Exec(ctx, &pod, "mongod", []string{"mongod", "--version"}, nil, &outb, &errb, false)
677676
if err != nil {
678-
return fmt.Errorf("exec --version: %v / %s / %s", err, outb.String(), errb.String())
677+
return nil, nil, fmt.Errorf("exec --version: %v / %s / %s", err, outb.String(), errb.String())
679678
}
680679

681680
mongoCmd := "mongosh"
@@ -709,7 +708,7 @@ func (r *ReconcilePerconaServerMongoDB) handleReplsetInit(ctx context.Context, c
709708
outb.Reset()
710709
err = r.clientcmd.Exec(ctx, &pod, "mongod", cmd, nil, &outb, &errb, false)
711710
if err != nil {
712-
return fmt.Errorf("exec rs.initiate: %v / %s / %s", err, outb.String(), errb.String())
711+
return nil, nil, fmt.Errorf("exec rs.initiate: %v / %s / %s", err, outb.String(), errb.String())
713712
}
714713

715714
log.Info("replset initialized", "replset", replsetName, "pod", pod.Name)
@@ -718,22 +717,26 @@ func (r *ReconcilePerconaServerMongoDB) handleReplsetInit(ctx context.Context, c
718717
log.Info("creating user admin", "replset", replsetName, "pod", pod.Name, "user", api.RoleUserAdmin)
719718
userAdmin, err := getInternalCredentials(ctx, r.client, cr, api.RoleUserAdmin)
720719
if err != nil {
721-
return errors.Wrap(err, "failed to get userAdmin credentials")
720+
return nil, nil, errors.Wrap(err, "failed to get userAdmin credentials")
722721
}
723722

724723
cmd[2] = fmt.Sprintf(`%s --eval %s`, mongoCmd, mongoInitAdminUser(userAdmin.Username, userAdmin.Password))
725724
errb.Reset()
726725
outb.Reset()
727726
err = r.clientcmd.Exec(ctx, &pod, "mongod", cmd, nil, &outb, &errb, false)
728727
if err != nil {
729-
return fmt.Errorf("exec add admin user: %v / %s / %s", err, outb.String(), errb.String())
728+
return nil, nil, fmt.Errorf("exec add admin user: %v / %s / %s", err, outb.String(), errb.String())
730729
}
731730
log.Info("user admin created", "replset", replsetName, "pod", pod.Name, "user", api.RoleUserAdmin)
732731

733-
return nil
732+
return &pod, &api.ReplsetMemberStatus{
733+
Name: member.Host,
734+
State: mongo.MemberStatePrimary,
735+
StateStr: mongo.MemberStateStrings[mongo.MemberStatePrimary],
736+
}, nil
734737
}
735738

736-
return errNoRunningMongodContainers
739+
return nil, nil, errNoRunningMongodContainers
737740
}
738741

739742
func (r *ReconcilePerconaServerMongoDB) handleReplicaSetNoPrimary(ctx context.Context, cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec, pods []corev1.Pod) error {

pkg/controller/perconaservermongodb/smart.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import (
2626
func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api.PerconaServerMongoDB, sfs *appsv1.StatefulSet,
2727
replset *api.ReplsetSpec,
2828
) error {
29-
log := logf.FromContext(ctx)
29+
log := logf.FromContext(ctx).WithName("SmartUpdate").WithValues("statefulset", sfs.Name, "replset", replset.Name)
30+
3031
if replset.Size == 0 {
3132
return nil
3233
}
@@ -85,7 +86,7 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api
8586
}
8687
}
8788

88-
log.Info("StatefulSet is changed, starting smart update", "name", sfs.Name)
89+
log.Info("StatefulSet is changed, starting smart update")
8990

9091
if sfs.Status.ReadyReplicas < sfs.Status.Replicas {
9192
log.Info("can't start/continue 'SmartUpdate': waiting for all replicas are ready")
@@ -112,7 +113,7 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api
112113
}
113114

114115
if rsStatus, ok := cr.Status.Replsets[replset.Name]; !ok || !rsStatus.Initialized {
115-
log.Info("replset wasn't initialized. Continuing smart update", "replset", replset.Name)
116+
log.Info("replset wasn't initialized. Continuing smart update")
116117

117118
for _, pod := range list.Items {
118119
log.Info("apply changes to pod", "pod", pod.Name)
@@ -122,15 +123,15 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api
122123
}
123124
}
124125

125-
log.Info("smart update finished for statefulset", "statefulset", sfs.Name)
126+
log.Info("smart update finished for statefulset")
126127

127128
return nil
128129
}
129130

130131
if rsStatus, ok := cr.Status.Replsets[replset.Name]; ok {
131132
for _, pod := range list.Items {
132133
if _, ok := rsStatus.Members[pod.Name]; !ok {
133-
log.Info("pod is not a member of replset, updating it", "pod", pod.Name, "replset", replset.Name)
134+
log.Info("pod is not a member of replset, updating it", "pod", pod.Name)
134135

135136
if err := updatePod(&pod); err != nil {
136137
return err
@@ -184,6 +185,7 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api
184185
}
185186
if isPrimary {
186187
primaryPod = pod
188+
log.Info("primary pod detected", "pod", pod.Name)
187189
continue
188190
}
189191

@@ -229,7 +231,7 @@ func (r *ReconcilePerconaServerMongoDB) smartUpdate(ctx context.Context, cr *api
229231
}
230232
}
231233

232-
log.Info("smart update finished for statefulset", "statefulset", sfs.Name)
234+
log.Info("smart update finished for statefulset")
233235

234236
return nil
235237
}

0 commit comments

Comments
 (0)