@@ -634,37 +634,78 @@ func (r *ReconcilePerconaServerMongoDBRestore) getUserCredentials(ctx context.Co
634
634
return creds , nil
635
635
}
636
636
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 ) {
638
638
log := logf .FromContext (ctx )
639
639
640
640
stdoutBuf := & bytes.Buffer {}
641
641
stderrBuf := & bytes.Buffer {}
642
642
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 ) {
643
653
creds , err := r .getUserCredentials (ctx , cluster , psmdbv1 .RoleClusterAdmin )
644
654
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 )
646
656
}
647
657
648
- comp , err := cluster .CompareMongoDBVersion ("6.0" )
658
+ mongo60 , err := cluster .CompareMongoDBVersion ("6.0" )
649
659
if err != nil {
650
- return stdoutBuf , stderrBuf , errors .Wrap (err , "compare mongo version" )
660
+ return false , errors .Wrap (err , "compare mongo version" )
651
661
}
652
662
653
663
mongoClient := "mongo"
654
- if comp >= 0 {
664
+ if mongo60 >= 0 {
655
665
mongoClient = "mongosh"
656
666
}
657
667
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 }
659
674
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" )
664
678
}
665
- log .V (1 ).Info ("Cmd succeeded" , "stdout" , stdoutBuf .String (), "stderr" , stderrBuf .String ())
666
679
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
668
709
}
669
710
670
711
func (r * ReconcilePerconaServerMongoDBRestore ) prepareReplsetsForPhysicalRestore (ctx context.Context , cluster * psmdbv1.PerconaServerMongoDB ) error {
@@ -675,8 +716,6 @@ func (r *ReconcilePerconaServerMongoDBRestore) prepareReplsetsForPhysicalRestore
675
716
replsets = append (replsets , cluster .Spec .Sharding .ConfigsvrReplSet )
676
717
}
677
718
678
- jsTempl := "cfg = rs.config(); podZero = cfg.members.find(member => member.tags.podName === '%s'); podZero.priority += 1; rs.reconfig(cfg)"
679
-
680
719
for _ , rs := range replsets {
681
720
log .Info ("Preparing replset for physical restore" , "replset" , rs .Name )
682
721
@@ -686,19 +725,18 @@ func (r *ReconcilePerconaServerMongoDBRestore) prepareReplsetsForPhysicalRestore
686
725
}
687
726
688
727
for _ , pod := range podList .Items {
689
- stdoutBuf , _ , err := r .runMongosh (ctx , cluster , & pod , "db.isMaster().ismaster" )
728
+ isMaster , err := r .runIsMaster (ctx , cluster , & pod )
690
729
if err != nil {
691
730
continue
692
731
}
693
732
694
- if strings . TrimSuffix ( stdoutBuf . String (), " \n " ) != "true" {
733
+ if ! isMaster {
695
734
log .V (1 ).Info ("Skipping secondary pod" , "pod" , pod .Name )
696
735
continue
697
736
}
698
737
699
738
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 {
702
740
return errors .Wrapf (err , "make %s primary" , podZero )
703
741
}
704
742
}
@@ -725,12 +763,12 @@ func (r *ReconcilePerconaServerMongoDBRestore) checkIfReplsetsAreReadyForPhysica
725
763
return false , errors .Wrapf (err , "get pod %s" , podZero )
726
764
}
727
765
728
- stdoutBuf , _ , err := r .runMongosh (ctx , cluster , & pod , "db.isMaster().ismaster" )
766
+ isMaster , err := r .runIsMaster (ctx , cluster , & pod )
729
767
if err != nil {
730
768
return false , errors .Wrap (err , "check if pod zero is primary" )
731
769
}
732
770
733
- if strings . TrimSuffix ( stdoutBuf . String (), " \n " ) != "true" {
771
+ if ! isMaster {
734
772
return false , nil
735
773
}
736
774
0 commit comments