Skip to content

Commit 248c18a

Browse files
authored
Merge pull request #334 from runkecheng/support_more_role_labels
*: Support more role labels.
2 parents d55025a + 7ab7fbb commit 248c18a

File tree

7 files changed

+54
-45
lines changed

7 files changed

+54
-45
lines changed

mysqlcluster/syncer/follower_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewFollowerSVCSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) synce
4848
service.Spec.Type = "ClusterIP"
4949
}
5050
service.Spec.Selector = c.GetSelectorLabels()
51-
service.Spec.Selector["role"] = "follower"
51+
service.Spec.Selector["role"] = string(utils.Follower)
5252
service.Spec.Selector["healthy"] = "yes"
5353

5454
if len(service.Spec.Ports) != 2 {

mysqlcluster/syncer/leader_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewLeaderSVCSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.
4848
service.Spec.Type = "ClusterIP"
4949
}
5050
service.Spec.Selector = c.GetSelectorLabels()
51-
service.Spec.Selector["role"] = "leader"
51+
service.Spec.Selector["role"] = string(utils.Leader)
5252

5353
if len(service.Spec.Ports) != 2 {
5454
service.Spec.Ports = make([]corev1.ServicePort, 2)

mysqlcluster/syncer/statefulset.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,12 @@ func (s *StatefulSetSyncer) updatePod(ctx context.Context) error {
332332
return err
333333
}
334334
}
335-
// Update the leader.
336-
if err := s.applyNWait(ctx, &leaderPod); err != nil {
337-
return err
335+
// There may be a case where Leader does not exist during the update process.
336+
if leaderPod.Name != "" {
337+
// Update the leader.
338+
if err := s.applyNWait(ctx, &leaderPod); err != nil {
339+
return err
340+
}
338341
}
339342
return nil
340343
}

mysqlcluster/syncer/status.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ func (s *StatusSyncer) updateNodeStatus(ctx context.Context, cli client.Client,
232232
// update apiv1alpha1.NodeConditionReadOnly.
233233
s.updateNodeCondition(node, int(apiv1alpha1.IndexReadOnly), isReadOnly)
234234

235-
if err = s.setPodHealthy(ctx, &pod, node); err != nil {
236-
log.Error(err, "cannot update pod", "name", podName, "namespace", pod.Namespace)
235+
if err = s.updatePodLabel(ctx, &pod, node); err != nil {
236+
log.Error(err, "failed to update labels", "pod", pod.Name, "namespace", pod.Namespace)
237237
}
238238
}
239239

@@ -358,9 +358,10 @@ func (s *StatusSyncer) addNodesInXenon(host string, toAdd []string) error {
358358
return nil
359359
}
360360

361-
// setPodHealthy set the pod lable healthy.
362-
func (s *StatusSyncer) setPodHealthy(ctx context.Context, pod *corev1.Pod, node *apiv1alpha1.NodeStatus) error {
361+
// updatePodLabel update the pod lables.
362+
func (s *StatusSyncer) updatePodLabel(ctx context.Context, pod *corev1.Pod, node *apiv1alpha1.NodeStatus) error {
363363
healthy := "no"
364+
isPodLabelsUpdated := false
364365
if node.Conditions[apiv1alpha1.IndexLagged].Status == corev1.ConditionFalse {
365366
if node.Conditions[apiv1alpha1.IndexLeader].Status == corev1.ConditionFalse &&
366367
node.Conditions[apiv1alpha1.IndexReadOnly].Status == corev1.ConditionTrue &&
@@ -375,6 +376,13 @@ func (s *StatusSyncer) setPodHealthy(ctx context.Context, pod *corev1.Pod, node
375376

376377
if pod.Labels["healthy"] != healthy {
377378
pod.Labels["healthy"] = healthy
379+
isPodLabelsUpdated = true
380+
}
381+
if pod.Labels["role"] != node.RaftStatus.Role {
382+
pod.Labels["role"] = node.RaftStatus.Role
383+
isPodLabelsUpdated = true
384+
}
385+
if isPodLabelsUpdated {
378386
if err := s.cli.Update(ctx, pod); client.IgnoreNotFound(err) != nil {
379387
return err
380388
}

sidecar/config.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,6 @@ func (cfg *Config) buildXenonConf() []byte {
345345
"admit-defeat-hearbeat-count": %d,
346346
"heartbeat-timeout": %d,
347347
"meta-datadir": "/var/lib/xenon/",
348-
"leader-start-command": "/scripts/leader-start.sh",
349-
"leader-stop-command": "/scripts/leader-stop.sh",
350348
"semi-sync-degrade": true,
351349
"purge-binlog-disabled": true,
352350
"super-idle": false
@@ -408,25 +406,25 @@ func (cfg *Config) buildClientConfig() (*ini.File, error) {
408406
return conf, nil
409407
}
410408

411-
// buildLeaderStart build the leader-start.sh.
412-
func (cfg *Config) buildLeaderStart() []byte {
413-
str := fmt.Sprintf(`#!/usr/bin/env bash
414-
curl -X PATCH -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" \
415-
--cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/%s/pods/$HOSTNAME \
416-
-d '[{"op": "replace", "path": "/metadata/labels/role", "value": "leader"}]'
417-
`, cfg.NameSpace)
418-
return utils.StringToBytes(str)
419-
}
420-
421-
// buildLeaderStop build the leader-stop.sh.
422-
func (cfg *Config) buildLeaderStop() []byte {
423-
str := fmt.Sprintf(`#!/usr/bin/env bash
424-
curl -X PATCH -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" \
425-
--cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/%s/pods/$HOSTNAME \
426-
-d '[{"op": "replace", "path": "/metadata/labels/role", "value": "follower"}]'
427-
`, cfg.NameSpace)
428-
return utils.StringToBytes(str)
429-
}
409+
// // buildLeaderStart build the leader-start.sh.
410+
// func (cfg *Config) buildLeaderStart() []byte {
411+
// str := fmt.Sprintf(`#!/usr/bin/env bash
412+
// curl -X PATCH -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" \
413+
// --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/%s/pods/$HOSTNAME \
414+
// -d '[{"op": "replace", "path": "/metadata/labels/role", "value": "leader"}]'
415+
// `, cfg.NameSpace)
416+
// return utils.StringToBytes(str)
417+
// }
418+
419+
// // buildLeaderStop build the leader-stop.sh.
420+
// func (cfg *Config) buildLeaderStop() []byte {
421+
// str := fmt.Sprintf(`#!/usr/bin/env bash
422+
// curl -X PATCH -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" \
423+
// --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/%s/pods/$HOSTNAME \
424+
// -d '[{"op": "replace", "path": "/metadata/labels/role", "value": "follower"}]'
425+
// `, cfg.NameSpace)
426+
// return utils.StringToBytes(str)
427+
// }
430428

431429
/* The function is equivalent to the following shell script template:
432430
#!/bin/sh

sidecar/init.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,19 @@ func runInitCommand(cfg *Config) error {
177177
return fmt.Errorf("failed to save extra.cnf: %s", err)
178178
}
179179

180-
// build leader-start.sh.
181-
bashLeaderStart := cfg.buildLeaderStart()
182-
leaderStartPath := path.Join(scriptsPath, "leader-start.sh")
183-
if err = ioutil.WriteFile(leaderStartPath, bashLeaderStart, os.FileMode(0755)); err != nil {
184-
return fmt.Errorf("failed to write leader-start.sh: %s", err)
185-
}
186-
187-
// build leader-stop.sh.
188-
bashLeaderStop := cfg.buildLeaderStop()
189-
leaderStopPath := path.Join(scriptsPath, "leader-stop.sh")
190-
if err = ioutil.WriteFile(leaderStopPath, bashLeaderStop, os.FileMode(0755)); err != nil {
191-
return fmt.Errorf("failed to write leader-stop.sh: %s", err)
192-
}
180+
// // build leader-start.sh.
181+
// bashLeaderStart := cfg.buildLeaderStart()
182+
// leaderStartPath := path.Join(scriptsPath, "leader-start.sh")
183+
// if err = ioutil.WriteFile(leaderStartPath, bashLeaderStart, os.FileMode(0755)); err != nil {
184+
// return fmt.Errorf("failed to write leader-start.sh: %s", err)
185+
// }
186+
187+
// // build leader-stop.sh.
188+
// bashLeaderStop := cfg.buildLeaderStop()
189+
// leaderStopPath := path.Join(scriptsPath, "leader-stop.sh")
190+
// if err = ioutil.WriteFile(leaderStopPath, bashLeaderStop, os.FileMode(0755)); err != nil {
191+
// return fmt.Errorf("failed to write leader-stop.sh: %s", err)
192+
// }
193193

194194
// for install tokudb.
195195
if cfg.InitTokuDB {

sidecar/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ var (
4646
// dataPath is the mysql data path.
4747
dataPath = utils.DataVolumeMountPath
4848

49-
// scriptsPath is the scripts path used for xenon.
50-
scriptsPath = utils.ScriptsVolumeMountPath
49+
// // scriptsPath is the scripts path used for xenon.
50+
// scriptsPath = utils.ScriptsVolumeMountPath
5151

5252
// sysPath is the linux kernel path used for install tokudb.
5353
sysPath = utils.SysVolumeMountPath

0 commit comments

Comments
 (0)