@@ -188,7 +188,7 @@ func (r *PerconaServerMySQLReconciler) deleteMySQLPods(ctx context.Context, cr *
188
188
}
189
189
190
190
if cr .Spec .MySQL .IsAsync () {
191
- orcPod , err := getOrcPod (ctx , r .Client , cr , 0 )
191
+ orcPod , err := getReadyOrcPod (ctx , r .Client , cr )
192
192
if err != nil {
193
193
return nil
194
194
}
@@ -204,10 +204,9 @@ func (r *PerconaServerMySQLReconciler) deleteMySQLPods(ctx context.Context, cr *
204
204
return errors .Wrap (err , "get operator password" )
205
205
}
206
206
207
- firstPodFQDN := fmt .Sprintf ("%s.%s.%s" , firstPod .Name , mysql .ServiceName (cr ), cr .Namespace )
208
- firstPodUri := fmt .Sprintf ("%s:%s@%s" , apiv1alpha1 .UserOperator , operatorPass , firstPodFQDN )
207
+ firstPodUri := fmt .Sprintf ("%s:%s@%s" , apiv1alpha1 .UserOperator , operatorPass , mysql .PodFQDN (cr , & firstPod ))
209
208
210
- um := database .NewReplicationManager (& firstPod , r .ClientCmd , apiv1alpha1 .UserOperator , operatorPass , firstPodFQDN )
209
+ um := database .NewReplicationManager (& firstPod , r .ClientCmd , apiv1alpha1 .UserOperator , operatorPass , mysql . PodFQDN ( cr , & firstPod ) )
211
210
212
211
mysh , err := mysqlsh .NewWithExec (r .ClientCmd , & firstPod , firstPodUri )
213
212
if err != nil {
@@ -221,11 +220,11 @@ func (r *PerconaServerMySQLReconciler) deleteMySQLPods(ctx context.Context, cr *
221
220
222
221
log .Info ("Got primary" , "primary" , clusterStatus .DefaultReplicaSet .Primary )
223
222
224
- if ! strings .HasPrefix (clusterStatus .DefaultReplicaSet .Primary , firstPodFQDN ) {
223
+ if ! strings .HasPrefix (clusterStatus .DefaultReplicaSet .Primary , mysql . PodFQDN ( cr , & firstPod ) ) {
225
224
log .Info ("Primary is not pod-0" , "primary" , clusterStatus .DefaultReplicaSet .Primary )
226
225
227
226
log .Info ("Ensuring pod-0 is the primary" )
228
- err := mysh .SetPrimaryInstanceWithExec (ctx , cr .InnoDBClusterName (), firstPodFQDN )
227
+ err := mysh .SetPrimaryInstanceWithExec (ctx , cr .InnoDBClusterName (), mysql . PodFQDN ( cr , & firstPod ) )
229
228
if err != nil {
230
229
return errors .Wrap (err , "set primary instance" )
231
230
}
@@ -634,7 +633,7 @@ func (r *PerconaServerMySQLReconciler) reconcileOrchestrator(ctx context.Context
634
633
return nil
635
634
}
636
635
637
- orcPod , err := getOrcPod (ctx , r .Client , cr , 0 )
636
+ orcPod , err := getReadyOrcPod (ctx , r .Client , cr )
638
637
if err != nil {
639
638
return nil
640
639
}
@@ -785,7 +784,7 @@ func (r *PerconaServerMySQLReconciler) reconcileReplication(ctx context.Context,
785
784
return nil
786
785
}
787
786
788
- pod , err := getOrcPod (ctx , r .Client , cr , 0 )
787
+ pod , err := getReadyOrcPod (ctx , r .Client , cr )
789
788
if err != nil {
790
789
return nil
791
790
}
@@ -833,17 +832,17 @@ func (r *PerconaServerMySQLReconciler) reconcileGroupReplication(ctx context.Con
833
832
return nil
834
833
}
835
834
836
- firstPod , err := getMySQLPod (ctx , r .Client , cr , 0 )
835
+ pod , err := getReadyMySQLPod (ctx , r .Client , cr )
837
836
if err != nil {
838
- return err
837
+ return errors . Wrap ( err , "get ready mysql pod" )
839
838
}
840
839
841
840
operatorPass , err := k8s .UserPassword (ctx , r .Client , cr , apiv1alpha1 .UserOperator )
842
841
if err != nil {
843
842
return errors .Wrap (err , "get operator password" )
844
843
}
845
844
846
- db := database .NewReplicationManager (firstPod , r .ClientCmd , apiv1alpha1 .UserOperator , operatorPass , mysql .ServiceName (cr ))
845
+ db := database .NewReplicationManager (pod , r .ClientCmd , apiv1alpha1 .UserOperator , operatorPass , mysql .ServiceName (cr ))
847
846
cond := meta .FindStatusCondition (cr .Status .Conditions , apiv1alpha1 .ConditionInnoDBClusterBootstrapped )
848
847
if cond == nil || cond .Status == metav1 .ConditionFalse {
849
848
if exists , err := db .CheckIfDatabaseExists (ctx , "mysql_innodb_cluster_metadata" ); err != nil || ! exists {
@@ -863,7 +862,7 @@ func (r *PerconaServerMySQLReconciler) reconcileGroupReplication(ctx context.Con
863
862
}
864
863
865
864
if exists , err := db .CheckIfDatabaseExists (ctx , "mysql_innodb_cluster_metadata" ); err != nil || ! exists {
866
- return errors .New ( "InnoDB cluster is already bootstrapped, but failed to check its status" )
865
+ return errors .Wrap ( err , "InnoDB cluster is already bootstrapped, but failed to check its status" )
867
866
}
868
867
869
868
return nil
@@ -978,19 +977,17 @@ func (r *PerconaServerMySQLReconciler) reconcileMySQLRouter(ctx context.Context,
978
977
return nil
979
978
}
980
979
981
- firstPod , err := getMySQLPod (ctx , r .Client , cr , 0 )
980
+ pod , err := getReadyMySQLPod (ctx , r .Client , cr )
982
981
if err != nil {
983
- return err
982
+ return errors . Wrap ( err , "get ready mysql pod" )
984
983
}
985
984
986
985
operatorPass , err := k8s .UserPassword (ctx , r .Client , cr , apiv1alpha1 .UserOperator )
987
986
if err != nil {
988
987
return errors .Wrap (err , "get operator password" )
989
988
}
990
989
991
- firstPodUri := mysql .PodName (cr , 0 ) + "." + mysql .ServiceName (cr ) + "." + cr .Namespace
992
-
993
- db := database .NewReplicationManager (firstPod , r .ClientCmd , apiv1alpha1 .UserOperator , operatorPass , firstPodUri )
990
+ db := database .NewReplicationManager (pod , r .ClientCmd , apiv1alpha1 .UserOperator , operatorPass , mysql .PodFQDN (cr , pod ))
994
991
if exist , err := db .CheckIfDatabaseExists (ctx , "mysql_innodb_cluster_metadata" ); err != nil || ! exist {
995
992
log .V (1 ).Info ("Waiting for InnoDB Cluster" , "cluster" , cr .Name )
996
993
return nil
@@ -1026,7 +1023,7 @@ func (r *PerconaServerMySQLReconciler) cleanupOutdated(ctx context.Context, cr *
1026
1023
}
1027
1024
1028
1025
func (r * PerconaServerMySQLReconciler ) getPrimaryFromOrchestrator (ctx context.Context , cr * apiv1alpha1.PerconaServerMySQL ) (* orchestrator.Instance , error ) {
1029
- pod , err := getOrcPod (ctx , r .Client , cr , 0 )
1026
+ pod , err := getReadyOrcPod (ctx , r .Client , cr )
1030
1027
if err != nil {
1031
1028
return nil , err
1032
1029
}
@@ -1048,14 +1045,12 @@ func (r *PerconaServerMySQLReconciler) getPrimaryFromGR(ctx context.Context, cr
1048
1045
return "" , errors .Wrap (err , "get operator password" )
1049
1046
}
1050
1047
1051
- fqdn := mysql .FQDN (cr , 0 )
1052
-
1053
- firstPod , err := getMySQLPod (ctx , r .Client , cr , 0 )
1048
+ pod , err := getReadyMySQLPod (ctx , r .Client , cr )
1054
1049
if err != nil {
1055
- return "" , err
1050
+ return "" , errors . Wrap ( err , "get ready mysql pod" )
1056
1051
}
1057
1052
1058
- um := database .NewReplicationManager (firstPod , r .ClientCmd , apiv1alpha1 .UserOperator , operatorPass , fqdn )
1053
+ um := database .NewReplicationManager (pod , r .ClientCmd , apiv1alpha1 .UserOperator , operatorPass , mysql . PodFQDN ( cr , pod ) )
1059
1054
1060
1055
return um .GetGroupReplicationPrimary (ctx )
1061
1056
}
@@ -1079,7 +1074,7 @@ func (r *PerconaServerMySQLReconciler) getPrimaryHost(ctx context.Context, cr *a
1079
1074
func (r * PerconaServerMySQLReconciler ) stopAsyncReplication (ctx context.Context , cr * apiv1alpha1.PerconaServerMySQL , primary * orchestrator.Instance ) error {
1080
1075
log := logf .FromContext (ctx ).WithName ("stopAsyncReplication" )
1081
1076
1082
- orcPod , err := getOrcPod (ctx , r .Client , cr , 0 )
1077
+ orcPod , err := getReadyOrcPod (ctx , r .Client , cr )
1083
1078
if err != nil {
1084
1079
return err
1085
1080
}
@@ -1134,7 +1129,7 @@ func (r *PerconaServerMySQLReconciler) stopAsyncReplication(ctx context.Context,
1134
1129
func (r * PerconaServerMySQLReconciler ) startAsyncReplication (ctx context.Context , cr * apiv1alpha1.PerconaServerMySQL , replicaPass string , primary * orchestrator.Instance ) error {
1135
1130
log := logf .FromContext (ctx ).WithName ("startAsyncReplication" )
1136
1131
1137
- orcPod , err := getOrcPod (ctx , r .Client , cr , 0 )
1132
+ orcPod , err := getReadyOrcPod (ctx , r .Client , cr )
1138
1133
if err != nil {
1139
1134
return nil
1140
1135
}
@@ -1177,6 +1172,20 @@ func (r *PerconaServerMySQLReconciler) startAsyncReplication(ctx context.Context
1177
1172
return errors .Wrap (g .Wait (), "start replication on replicas" )
1178
1173
}
1179
1174
1175
+ func getReadyMySQLPod (ctx context.Context , cl client.Reader , cr * apiv1alpha1.PerconaServerMySQL ) (* corev1.Pod , error ) {
1176
+ pods , err := k8s .PodsByLabels (ctx , cl , mysql .MatchLabels (cr ))
1177
+ if err != nil {
1178
+ return nil , errors .Wrap (err , "get pods" )
1179
+ }
1180
+
1181
+ for i , pod := range pods {
1182
+ if k8s .IsPodReady (pod ) {
1183
+ return & pods [i ], nil
1184
+ }
1185
+ }
1186
+ return nil , errors .New ("no ready pods" )
1187
+ }
1188
+
1180
1189
func getMySQLPod (ctx context.Context , cl client.Reader , cr * apiv1alpha1.PerconaServerMySQL , idx int ) (* corev1.Pod , error ) {
1181
1190
pod := & corev1.Pod {}
1182
1191
@@ -1188,15 +1197,18 @@ func getMySQLPod(ctx context.Context, cl client.Reader, cr *apiv1alpha1.PerconaS
1188
1197
return pod , nil
1189
1198
}
1190
1199
1191
- func getOrcPod (ctx context.Context , cl client.Reader , cr * apiv1alpha1.PerconaServerMySQL , idx int ) (* corev1.Pod , error ) {
1192
- pod := & corev1.Pod {}
1193
-
1194
- nn := types.NamespacedName {Namespace : cr .Namespace , Name : orchestrator .PodName (cr , idx )}
1195
- if err := cl .Get (ctx , nn , pod ); err != nil {
1196
- return nil , err
1200
+ func getReadyOrcPod (ctx context.Context , cl client.Reader , cr * apiv1alpha1.PerconaServerMySQL ) (* corev1.Pod , error ) {
1201
+ pods , err := k8s .PodsByLabels (ctx , cl , orchestrator .MatchLabels (cr ))
1202
+ if err != nil {
1203
+ return nil , errors .Wrap (err , "get pods" )
1197
1204
}
1198
1205
1199
- return pod , nil
1206
+ for i , pod := range pods {
1207
+ if k8s .IsPodReady (pod ) {
1208
+ return & pods [i ], nil
1209
+ }
1210
+ }
1211
+ return nil , errors .New ("no ready pods" )
1200
1212
}
1201
1213
1202
1214
func getPodIndexFromHostname (hostname string ) (int , error ) {
0 commit comments