Skip to content

Commit 6a28c38

Browse files
pooknullhors
andauthored
K8SPS-301: reduce error logs on pod deletion (#659)
* K8SPS-301: reduce error logs on pod deletion https://perconadev.atlassian.net/browse/K8SPS-301 * fix test * fix merge * fix regression * get ready pods for orchestrator --------- Co-authored-by: Viacheslav Sarzhan <[email protected]>
1 parent d5abd4d commit 6a28c38

File tree

5 files changed

+55
-36
lines changed

5 files changed

+55
-36
lines changed

pkg/controller/ps/controller.go

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (r *PerconaServerMySQLReconciler) deleteMySQLPods(ctx context.Context, cr *
188188
}
189189

190190
if cr.Spec.MySQL.IsAsync() {
191-
orcPod, err := getOrcPod(ctx, r.Client, cr, 0)
191+
orcPod, err := getReadyOrcPod(ctx, r.Client, cr)
192192
if err != nil {
193193
return nil
194194
}
@@ -204,10 +204,9 @@ func (r *PerconaServerMySQLReconciler) deleteMySQLPods(ctx context.Context, cr *
204204
return errors.Wrap(err, "get operator password")
205205
}
206206

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))
209208

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))
211210

212211
mysh, err := mysqlsh.NewWithExec(r.ClientCmd, &firstPod, firstPodUri)
213212
if err != nil {
@@ -221,11 +220,11 @@ func (r *PerconaServerMySQLReconciler) deleteMySQLPods(ctx context.Context, cr *
221220

222221
log.Info("Got primary", "primary", clusterStatus.DefaultReplicaSet.Primary)
223222

224-
if !strings.HasPrefix(clusterStatus.DefaultReplicaSet.Primary, firstPodFQDN) {
223+
if !strings.HasPrefix(clusterStatus.DefaultReplicaSet.Primary, mysql.PodFQDN(cr, &firstPod)) {
225224
log.Info("Primary is not pod-0", "primary", clusterStatus.DefaultReplicaSet.Primary)
226225

227226
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))
229228
if err != nil {
230229
return errors.Wrap(err, "set primary instance")
231230
}
@@ -634,7 +633,7 @@ func (r *PerconaServerMySQLReconciler) reconcileOrchestrator(ctx context.Context
634633
return nil
635634
}
636635

637-
orcPod, err := getOrcPod(ctx, r.Client, cr, 0)
636+
orcPod, err := getReadyOrcPod(ctx, r.Client, cr)
638637
if err != nil {
639638
return nil
640639
}
@@ -785,7 +784,7 @@ func (r *PerconaServerMySQLReconciler) reconcileReplication(ctx context.Context,
785784
return nil
786785
}
787786

788-
pod, err := getOrcPod(ctx, r.Client, cr, 0)
787+
pod, err := getReadyOrcPod(ctx, r.Client, cr)
789788
if err != nil {
790789
return nil
791790
}
@@ -833,17 +832,17 @@ func (r *PerconaServerMySQLReconciler) reconcileGroupReplication(ctx context.Con
833832
return nil
834833
}
835834

836-
firstPod, err := getMySQLPod(ctx, r.Client, cr, 0)
835+
pod, err := getReadyMySQLPod(ctx, r.Client, cr)
837836
if err != nil {
838-
return err
837+
return errors.Wrap(err, "get ready mysql pod")
839838
}
840839

841840
operatorPass, err := k8s.UserPassword(ctx, r.Client, cr, apiv1alpha1.UserOperator)
842841
if err != nil {
843842
return errors.Wrap(err, "get operator password")
844843
}
845844

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))
847846
cond := meta.FindStatusCondition(cr.Status.Conditions, apiv1alpha1.ConditionInnoDBClusterBootstrapped)
848847
if cond == nil || cond.Status == metav1.ConditionFalse {
849848
if exists, err := db.CheckIfDatabaseExists(ctx, "mysql_innodb_cluster_metadata"); err != nil || !exists {
@@ -863,7 +862,7 @@ func (r *PerconaServerMySQLReconciler) reconcileGroupReplication(ctx context.Con
863862
}
864863

865864
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")
867866
}
868867

869868
return nil
@@ -978,19 +977,17 @@ func (r *PerconaServerMySQLReconciler) reconcileMySQLRouter(ctx context.Context,
978977
return nil
979978
}
980979

981-
firstPod, err := getMySQLPod(ctx, r.Client, cr, 0)
980+
pod, err := getReadyMySQLPod(ctx, r.Client, cr)
982981
if err != nil {
983-
return err
982+
return errors.Wrap(err, "get ready mysql pod")
984983
}
985984

986985
operatorPass, err := k8s.UserPassword(ctx, r.Client, cr, apiv1alpha1.UserOperator)
987986
if err != nil {
988987
return errors.Wrap(err, "get operator password")
989988
}
990989

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))
994991
if exist, err := db.CheckIfDatabaseExists(ctx, "mysql_innodb_cluster_metadata"); err != nil || !exist {
995992
log.V(1).Info("Waiting for InnoDB Cluster", "cluster", cr.Name)
996993
return nil
@@ -1026,7 +1023,7 @@ func (r *PerconaServerMySQLReconciler) cleanupOutdated(ctx context.Context, cr *
10261023
}
10271024

10281025
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)
10301027
if err != nil {
10311028
return nil, err
10321029
}
@@ -1048,14 +1045,12 @@ func (r *PerconaServerMySQLReconciler) getPrimaryFromGR(ctx context.Context, cr
10481045
return "", errors.Wrap(err, "get operator password")
10491046
}
10501047

1051-
fqdn := mysql.FQDN(cr, 0)
1052-
1053-
firstPod, err := getMySQLPod(ctx, r.Client, cr, 0)
1048+
pod, err := getReadyMySQLPod(ctx, r.Client, cr)
10541049
if err != nil {
1055-
return "", err
1050+
return "", errors.Wrap(err, "get ready mysql pod")
10561051
}
10571052

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))
10591054

10601055
return um.GetGroupReplicationPrimary(ctx)
10611056
}
@@ -1079,7 +1074,7 @@ func (r *PerconaServerMySQLReconciler) getPrimaryHost(ctx context.Context, cr *a
10791074
func (r *PerconaServerMySQLReconciler) stopAsyncReplication(ctx context.Context, cr *apiv1alpha1.PerconaServerMySQL, primary *orchestrator.Instance) error {
10801075
log := logf.FromContext(ctx).WithName("stopAsyncReplication")
10811076

1082-
orcPod, err := getOrcPod(ctx, r.Client, cr, 0)
1077+
orcPod, err := getReadyOrcPod(ctx, r.Client, cr)
10831078
if err != nil {
10841079
return err
10851080
}
@@ -1134,7 +1129,7 @@ func (r *PerconaServerMySQLReconciler) stopAsyncReplication(ctx context.Context,
11341129
func (r *PerconaServerMySQLReconciler) startAsyncReplication(ctx context.Context, cr *apiv1alpha1.PerconaServerMySQL, replicaPass string, primary *orchestrator.Instance) error {
11351130
log := logf.FromContext(ctx).WithName("startAsyncReplication")
11361131

1137-
orcPod, err := getOrcPod(ctx, r.Client, cr, 0)
1132+
orcPod, err := getReadyOrcPod(ctx, r.Client, cr)
11381133
if err != nil {
11391134
return nil
11401135
}
@@ -1177,6 +1172,20 @@ func (r *PerconaServerMySQLReconciler) startAsyncReplication(ctx context.Context
11771172
return errors.Wrap(g.Wait(), "start replication on replicas")
11781173
}
11791174

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+
11801189
func getMySQLPod(ctx context.Context, cl client.Reader, cr *apiv1alpha1.PerconaServerMySQL, idx int) (*corev1.Pod, error) {
11811190
pod := &corev1.Pod{}
11821191

@@ -1188,15 +1197,18 @@ func getMySQLPod(ctx context.Context, cl client.Reader, cr *apiv1alpha1.PerconaS
11881197
return pod, nil
11891198
}
11901199

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")
11971204
}
11981205

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")
12001212
}
12011213

12021214
func getPodIndexFromHostname(hostname string) (int, error) {

pkg/controller/ps/status.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,12 @@ func (r *PerconaServerMySQLReconciler) isGRReady(ctx context.Context, cr *apiv1a
217217
return false, errors.Wrap(err, "get operator password")
218218
}
219219

220-
firstPod, err := getMySQLPod(ctx, r.Client, cr, 0)
220+
pod, err := getReadyMySQLPod(ctx, r.Client, cr)
221221
if err != nil {
222-
return false, err
222+
return false, errors.Wrap(err, "get ready mysql pod")
223223
}
224224

225-
firstPodUri := mysql.PodName(cr, 0) + "." + mysql.ServiceName(cr) + "." + cr.Namespace
226-
db := database.NewReplicationManager(firstPod, r.ClientCmd, apiv1alpha1.UserOperator, operatorPass, firstPodUri)
225+
db := database.NewReplicationManager(pod, r.ClientCmd, apiv1alpha1.UserOperator, operatorPass, mysql.PodFQDN(cr, pod))
227226

228227
dbExists, err := db.CheckIfDatabaseExists(ctx, "mysql_innodb_cluster_metadata")
229228
if err != nil {

pkg/controller/ps/status_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,7 @@ func makeFakeReadyPods(cr *apiv1alpha1.PerconaServerMySQL, amount int, podType s
11381138
},
11391139
},
11401140
Status: corev1.PodStatus{
1141+
Phase: corev1.PodRunning,
11411142
Conditions: []corev1.PodCondition{
11421143
{
11431144
Type: corev1.ContainersReady,

pkg/k8s/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ func IsPodWithNameReady(ctx context.Context, cl client.Client, nn types.Namespac
9494
}
9595

9696
func IsPodReady(pod corev1.Pod) bool {
97+
if pod.Status.Phase != corev1.PodRunning || pod.DeletionTimestamp != nil {
98+
return false
99+
}
97100
for _, cond := range pod.Status.Conditions {
98101
if cond.Type == corev1.ContainersReady && cond.Status == corev1.ConditionTrue {
99102
return true

pkg/mysql/mysql.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ func FQDN(cr *apiv1alpha1.PerconaServerMySQL, idx int) string {
109109
return fmt.Sprintf("%s.%s.%s", PodName(cr, idx), ServiceName(cr), cr.Namespace)
110110
}
111111

112+
func PodFQDN(cr *apiv1alpha1.PerconaServerMySQL, pod *corev1.Pod) string {
113+
return fmt.Sprintf("%s.%s.%s", pod.Name, ServiceName(cr), cr.Namespace)
114+
}
115+
112116
func MatchLabels(cr *apiv1alpha1.PerconaServerMySQL) map[string]string {
113117
return util.SSMapMerge(cr.MySQLSpec().Labels,
114118
map[string]string{naming.LabelComponent: ComponentName},

0 commit comments

Comments
 (0)