Skip to content

Commit

Permalink
Merge pull request #334 from runkecheng/support_more_role_labels
Browse files Browse the repository at this point in the history
*: Support more role labels.
  • Loading branch information
andyli029 authored Dec 29, 2021
2 parents d55025a + 7ab7fbb commit 248c18a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 45 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
40 changes: 19 additions & 21 deletions sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,6 @@ func (cfg *Config) buildXenonConf() []byte {
"admit-defeat-hearbeat-count": %d,
"heartbeat-timeout": %d,
"meta-datadir": "/var/lib/xenon/",
"leader-start-command": "/scripts/leader-start.sh",
"leader-stop-command": "/scripts/leader-stop.sh",
"semi-sync-degrade": true,
"purge-binlog-disabled": true,
"super-idle": false
Expand Down Expand Up @@ -408,25 +406,25 @@ func (cfg *Config) buildClientConfig() (*ini.File, error) {
return conf, nil
}

// buildLeaderStart build the leader-start.sh.
func (cfg *Config) buildLeaderStart() []byte {
str := fmt.Sprintf(`#!/usr/bin/env bash
curl -X PATCH -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" \
--cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/%s/pods/$HOSTNAME \
-d '[{"op": "replace", "path": "/metadata/labels/role", "value": "leader"}]'
`, cfg.NameSpace)
return utils.StringToBytes(str)
}

// buildLeaderStop build the leader-stop.sh.
func (cfg *Config) buildLeaderStop() []byte {
str := fmt.Sprintf(`#!/usr/bin/env bash
curl -X PATCH -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" \
--cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/%s/pods/$HOSTNAME \
-d '[{"op": "replace", "path": "/metadata/labels/role", "value": "follower"}]'
`, cfg.NameSpace)
return utils.StringToBytes(str)
}
// // buildLeaderStart build the leader-start.sh.
// func (cfg *Config) buildLeaderStart() []byte {
// str := fmt.Sprintf(`#!/usr/bin/env bash
// curl -X PATCH -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" \
// --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/%s/pods/$HOSTNAME \
// -d '[{"op": "replace", "path": "/metadata/labels/role", "value": "leader"}]'
// `, cfg.NameSpace)
// return utils.StringToBytes(str)
// }

// // buildLeaderStop build the leader-stop.sh.
// func (cfg *Config) buildLeaderStop() []byte {
// str := fmt.Sprintf(`#!/usr/bin/env bash
// curl -X PATCH -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" \
// --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/%s/pods/$HOSTNAME \
// -d '[{"op": "replace", "path": "/metadata/labels/role", "value": "follower"}]'
// `, cfg.NameSpace)
// return utils.StringToBytes(str)
// }

/* The function is equivalent to the following shell script template:
#!/bin/sh
Expand Down
26 changes: 13 additions & 13 deletions sidecar/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,19 @@ func runInitCommand(cfg *Config) error {
return fmt.Errorf("failed to save extra.cnf: %s", err)
}

// build leader-start.sh.
bashLeaderStart := cfg.buildLeaderStart()
leaderStartPath := path.Join(scriptsPath, "leader-start.sh")
if err = ioutil.WriteFile(leaderStartPath, bashLeaderStart, os.FileMode(0755)); err != nil {
return fmt.Errorf("failed to write leader-start.sh: %s", err)
}

// build leader-stop.sh.
bashLeaderStop := cfg.buildLeaderStop()
leaderStopPath := path.Join(scriptsPath, "leader-stop.sh")
if err = ioutil.WriteFile(leaderStopPath, bashLeaderStop, os.FileMode(0755)); err != nil {
return fmt.Errorf("failed to write leader-stop.sh: %s", err)
}
// // build leader-start.sh.
// bashLeaderStart := cfg.buildLeaderStart()
// leaderStartPath := path.Join(scriptsPath, "leader-start.sh")
// if err = ioutil.WriteFile(leaderStartPath, bashLeaderStart, os.FileMode(0755)); err != nil {
// return fmt.Errorf("failed to write leader-start.sh: %s", err)
// }

// // build leader-stop.sh.
// bashLeaderStop := cfg.buildLeaderStop()
// leaderStopPath := path.Join(scriptsPath, "leader-stop.sh")
// if err = ioutil.WriteFile(leaderStopPath, bashLeaderStop, os.FileMode(0755)); err != nil {
// return fmt.Errorf("failed to write leader-stop.sh: %s", err)
// }

// for install tokudb.
if cfg.InitTokuDB {
Expand Down
4 changes: 2 additions & 2 deletions sidecar/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ var (
// dataPath is the mysql data path.
dataPath = utils.DataVolumeMountPath

// scriptsPath is the scripts path used for xenon.
scriptsPath = utils.ScriptsVolumeMountPath
// // scriptsPath is the scripts path used for xenon.
// scriptsPath = utils.ScriptsVolumeMountPath

// sysPath is the linux kernel path used for install tokudb.
sysPath = utils.SysVolumeMountPath
Expand Down

0 comments on commit 248c18a

Please sign in to comment.