From a5d6d9f12385e9bc595fc328b5ca7152aae651f8 Mon Sep 17 00:00:00 2001 From: runkecheng <1131648942@qq.com> Date: Mon, 27 Dec 2021 15:39:51 +0800 Subject: [PATCH 1/2] sidecar: Remove the old role tag setting method. #330 --- sidecar/config.go | 40 +++++++++++++++++++--------------------- sidecar/init.go | 26 +++++++++++++------------- sidecar/util.go | 4 ++-- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/sidecar/config.go b/sidecar/config.go index 35dd4a0d..83624cbd 100644 --- a/sidecar/config.go +++ b/sidecar/config.go @@ -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 @@ -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 diff --git a/sidecar/init.go b/sidecar/init.go index 8929c1dc..e41466df 100644 --- a/sidecar/init.go +++ b/sidecar/init.go @@ -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 { diff --git a/sidecar/util.go b/sidecar/util.go index d3ef01db..cfbf140b 100644 --- a/sidecar/util.go +++ b/sidecar/util.go @@ -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 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 2/2] 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 }