From 7ab7fbb6c68a72f59124e83c2c89c44180f65b82 Mon Sep 17 00:00:00 2001 From: runkecheng <1131648942@qq.com> Date: Mon, 27 Dec 2021 15:40:10 +0800 Subject: [PATCH] syncer: Set the role tag into the corresponding raft role. #330 --- mysqlcluster/syncer/follower_service.go | 2 +- mysqlcluster/syncer/leader_service.go | 2 +- mysqlcluster/syncer/statefulset.go | 9 ++++++--- mysqlcluster/syncer/status.go | 16 ++++++++++++---- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/mysqlcluster/syncer/follower_service.go b/mysqlcluster/syncer/follower_service.go index 37a62f6f..6960c00c 100644 --- a/mysqlcluster/syncer/follower_service.go +++ b/mysqlcluster/syncer/follower_service.go @@ -48,7 +48,7 @@ func NewFollowerSVCSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) synce service.Spec.Type = "ClusterIP" } service.Spec.Selector = c.GetSelectorLabels() - service.Spec.Selector["role"] = "follower" + service.Spec.Selector["role"] = string(utils.Follower) service.Spec.Selector["healthy"] = "yes" if len(service.Spec.Ports) != 2 { diff --git a/mysqlcluster/syncer/leader_service.go b/mysqlcluster/syncer/leader_service.go index 1251e19c..2bae1904 100644 --- a/mysqlcluster/syncer/leader_service.go +++ b/mysqlcluster/syncer/leader_service.go @@ -48,7 +48,7 @@ func NewLeaderSVCSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer. service.Spec.Type = "ClusterIP" } service.Spec.Selector = c.GetSelectorLabels() - service.Spec.Selector["role"] = "leader" + service.Spec.Selector["role"] = string(utils.Leader) if len(service.Spec.Ports) != 2 { service.Spec.Ports = make([]corev1.ServicePort, 2) diff --git a/mysqlcluster/syncer/statefulset.go b/mysqlcluster/syncer/statefulset.go index 4ea76dcc..b8b88a16 100644 --- a/mysqlcluster/syncer/statefulset.go +++ b/mysqlcluster/syncer/statefulset.go @@ -332,9 +332,12 @@ func (s *StatefulSetSyncer) updatePod(ctx context.Context) error { return err } } - // Update the leader. - if err := s.applyNWait(ctx, &leaderPod); err != nil { - return err + // There may be a case where Leader does not exist during the update process. + if leaderPod.Name != "" { + // Update the leader. + if err := s.applyNWait(ctx, &leaderPod); err != nil { + return err + } } return nil } diff --git a/mysqlcluster/syncer/status.go b/mysqlcluster/syncer/status.go index 13d93908..d8ed00ea 100644 --- a/mysqlcluster/syncer/status.go +++ b/mysqlcluster/syncer/status.go @@ -232,8 +232,8 @@ func (s *StatusSyncer) updateNodeStatus(ctx context.Context, cli client.Client, // update apiv1alpha1.NodeConditionReadOnly. s.updateNodeCondition(node, int(apiv1alpha1.IndexReadOnly), isReadOnly) - if err = s.setPodHealthy(ctx, &pod, node); err != nil { - log.Error(err, "cannot update pod", "name", podName, "namespace", pod.Namespace) + if err = s.updatePodLabel(ctx, &pod, node); err != nil { + log.Error(err, "failed to update labels", "pod", pod.Name, "namespace", pod.Namespace) } } @@ -358,9 +358,10 @@ func (s *StatusSyncer) addNodesInXenon(host string, toAdd []string) error { return nil } -// setPodHealthy set the pod lable healthy. -func (s *StatusSyncer) setPodHealthy(ctx context.Context, pod *corev1.Pod, node *apiv1alpha1.NodeStatus) error { +// updatePodLabel update the pod lables. +func (s *StatusSyncer) updatePodLabel(ctx context.Context, pod *corev1.Pod, node *apiv1alpha1.NodeStatus) error { healthy := "no" + isPodLabelsUpdated := false if node.Conditions[apiv1alpha1.IndexLagged].Status == corev1.ConditionFalse { if node.Conditions[apiv1alpha1.IndexLeader].Status == corev1.ConditionFalse && node.Conditions[apiv1alpha1.IndexReadOnly].Status == corev1.ConditionTrue && @@ -375,6 +376,13 @@ func (s *StatusSyncer) setPodHealthy(ctx context.Context, pod *corev1.Pod, node if pod.Labels["healthy"] != healthy { pod.Labels["healthy"] = healthy + isPodLabelsUpdated = true + } + if pod.Labels["role"] != node.RaftStatus.Role { + pod.Labels["role"] = node.RaftStatus.Role + isPodLabelsUpdated = true + } + if isPodLabelsUpdated { if err := s.cli.Update(ctx, pod); client.IgnoreNotFound(err) != nil { return err }