Skip to content

Commit cb2b900

Browse files
egeguneshors
andauthored
K8SPSMDB-1054: Fix isMaster output during physical restore (#1555)
Co-authored-by: Viacheslav Sarzhan <[email protected]>
1 parent 6cd1efd commit cb2b900

File tree

1 file changed

+58
-20
lines changed

1 file changed

+58
-20
lines changed

pkg/controller/perconaservermongodbrestore/physical.go

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -634,37 +634,78 @@ func (r *ReconcilePerconaServerMongoDBRestore) getUserCredentials(ctx context.Co
634634
return creds, nil
635635
}
636636

637-
func (r *ReconcilePerconaServerMongoDBRestore) runMongosh(ctx context.Context, cluster *psmdbv1.PerconaServerMongoDB, pod *corev1.Pod, eval string) (*bytes.Buffer, *bytes.Buffer, error) {
637+
func (r *ReconcilePerconaServerMongoDBRestore) runMongosh(ctx context.Context, cluster *psmdbv1.PerconaServerMongoDB, pod *corev1.Pod, cmd []string) (*bytes.Buffer, *bytes.Buffer, error) {
638638
log := logf.FromContext(ctx)
639639

640640
stdoutBuf := &bytes.Buffer{}
641641
stderrBuf := &bytes.Buffer{}
642642

643+
if err := r.clientcmd.Exec(ctx, pod, "mongod", cmd, nil, stdoutBuf, stderrBuf, false); err != nil {
644+
log.V(1).Info("Cmd failed", "stdout", stdoutBuf.String(), "stderr", stderrBuf.String())
645+
return stdoutBuf, stderrBuf, errors.Wrap(err, "cmd failed")
646+
}
647+
log.V(1).Info("Cmd succeeded", "stdout", stdoutBuf.String(), "stderr", stderrBuf.String())
648+
649+
return stdoutBuf, stderrBuf, nil
650+
}
651+
652+
func (r *ReconcilePerconaServerMongoDBRestore) runIsMaster(ctx context.Context, cluster *psmdbv1.PerconaServerMongoDB, pod *corev1.Pod) (bool, error) {
643653
creds, err := r.getUserCredentials(ctx, cluster, psmdbv1.RoleClusterAdmin)
644654
if err != nil {
645-
return stdoutBuf, stderrBuf, errors.Wrapf(err, "get %s credentials", psmdbv1.RoleClusterAdmin)
655+
return false, errors.Wrapf(err, "get %s credentials", psmdbv1.RoleClusterAdmin)
646656
}
647657

648-
comp, err := cluster.CompareMongoDBVersion("6.0")
658+
mongo60, err := cluster.CompareMongoDBVersion("6.0")
649659
if err != nil {
650-
return stdoutBuf, stderrBuf, errors.Wrap(err, "compare mongo version")
660+
return false, errors.Wrap(err, "compare mongo version")
651661
}
652662

653663
mongoClient := "mongo"
654-
if comp >= 0 {
664+
if mongo60 >= 0 {
655665
mongoClient = "mongosh"
656666
}
657667

658-
cmd := []string{mongoClient, "--quiet", "-u", creds.Username, "-p", creds.Password, "--eval", eval}
668+
c := strings.Join([]string{
669+
mongoClient, "--quiet", "-u", creds.Username, "-p", creds.Password, "--eval", "'db.isMaster().ismaster'",
670+
"|", "tail", "-n", "1",
671+
"|", "grep", "-Eo", "'(true|false)'",
672+
}, " ")
673+
cmd := []string{"bash", "-c", c}
659674

660-
log.V(1).Info("Running cmd in pod", "eval", eval, "pod", pod.Name)
661-
if err := r.clientcmd.Exec(ctx, pod, "mongod", cmd, nil, stdoutBuf, stderrBuf, false); err != nil {
662-
log.V(1).Info("Cmd failed", "stdout", stdoutBuf.String(), "stderr", stderrBuf.String())
663-
return stdoutBuf, stderrBuf, errors.Wrap(err, "cmd failed")
675+
stdoutBuf, _, err := r.runMongosh(ctx, cluster, pod, cmd)
676+
if err != nil {
677+
return false, errors.Wrap(err, "run isMaster")
664678
}
665-
log.V(1).Info("Cmd succeeded", "stdout", stdoutBuf.String(), "stderr", stderrBuf.String())
666679

667-
return stdoutBuf, stderrBuf, nil
680+
return strings.TrimSuffix(stdoutBuf.String(), "\n") == "true", nil
681+
}
682+
683+
func (r *ReconcilePerconaServerMongoDBRestore) makePrimary(ctx context.Context, cluster *psmdbv1.PerconaServerMongoDB, pod *corev1.Pod, targetPod string) error {
684+
jsTempl := `cfg = rs.config(); podZero = cfg.members.find(member => member.tags.podName === "%s"); podZero.priority += 1; rs.reconfig(cfg)`
685+
686+
creds, err := r.getUserCredentials(ctx, cluster, psmdbv1.RoleClusterAdmin)
687+
if err != nil {
688+
return errors.Wrapf(err, "get %s credentials", psmdbv1.RoleClusterAdmin)
689+
}
690+
691+
mongo60, err := cluster.CompareMongoDBVersion("6.0")
692+
if err != nil {
693+
return errors.Wrap(err, "compare mongo version")
694+
}
695+
696+
mongoClient := "mongo"
697+
if mongo60 >= 0 {
698+
mongoClient = "mongosh"
699+
}
700+
701+
cmd := []string{mongoClient, "--quiet", "-u", creds.Username, "-p", creds.Password, "--eval", fmt.Sprintf(jsTempl, targetPod)}
702+
703+
_, _, err = r.runMongosh(ctx, cluster, pod, cmd)
704+
if err != nil {
705+
return errors.Wrap(err, "run isMaster")
706+
}
707+
708+
return nil
668709
}
669710

670711
func (r *ReconcilePerconaServerMongoDBRestore) prepareReplsetsForPhysicalRestore(ctx context.Context, cluster *psmdbv1.PerconaServerMongoDB) error {
@@ -675,8 +716,6 @@ func (r *ReconcilePerconaServerMongoDBRestore) prepareReplsetsForPhysicalRestore
675716
replsets = append(replsets, cluster.Spec.Sharding.ConfigsvrReplSet)
676717
}
677718

678-
jsTempl := "cfg = rs.config(); podZero = cfg.members.find(member => member.tags.podName === '%s'); podZero.priority += 1; rs.reconfig(cfg)"
679-
680719
for _, rs := range replsets {
681720
log.Info("Preparing replset for physical restore", "replset", rs.Name)
682721

@@ -686,19 +725,18 @@ func (r *ReconcilePerconaServerMongoDBRestore) prepareReplsetsForPhysicalRestore
686725
}
687726

688727
for _, pod := range podList.Items {
689-
stdoutBuf, _, err := r.runMongosh(ctx, cluster, &pod, "db.isMaster().ismaster")
728+
isMaster, err := r.runIsMaster(ctx, cluster, &pod)
690729
if err != nil {
691730
continue
692731
}
693732

694-
if strings.TrimSuffix(stdoutBuf.String(), "\n") != "true" {
733+
if !isMaster {
695734
log.V(1).Info("Skipping secondary pod", "pod", pod.Name)
696735
continue
697736
}
698737

699738
podZero := rs.PodName(cluster, 0)
700-
_, _, err = r.runMongosh(ctx, cluster, &pod, fmt.Sprintf(jsTempl, podZero))
701-
if err != nil {
739+
if err = r.makePrimary(ctx, cluster, &pod, podZero); err != nil {
702740
return errors.Wrapf(err, "make %s primary", podZero)
703741
}
704742
}
@@ -725,12 +763,12 @@ func (r *ReconcilePerconaServerMongoDBRestore) checkIfReplsetsAreReadyForPhysica
725763
return false, errors.Wrapf(err, "get pod %s", podZero)
726764
}
727765

728-
stdoutBuf, _, err := r.runMongosh(ctx, cluster, &pod, "db.isMaster().ismaster")
766+
isMaster, err := r.runIsMaster(ctx, cluster, &pod)
729767
if err != nil {
730768
return false, errors.Wrap(err, "check if pod zero is primary")
731769
}
732770

733-
if strings.TrimSuffix(stdoutBuf.String(), "\n") != "true" {
771+
if !isMaster {
734772
return false, nil
735773
}
736774

0 commit comments

Comments
 (0)