Skip to content

Commit

Permalink
syncer: Set the role tag into the corresponding raft role. #330
Browse files Browse the repository at this point in the history
  • Loading branch information
runkecheng committed Dec 27, 2021
1 parent a5d6d9f commit 7ab7fbb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion mysqlcluster/syncer/follower_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion mysqlcluster/syncer/leader_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions mysqlcluster/syncer/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 12 additions & 4 deletions mysqlcluster/syncer/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 &&
Expand All @@ -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
}
Expand Down

0 comments on commit 7ab7fbb

Please sign in to comment.