From cf2205d13d39cb24f9f3a9c75b6efacd0a11c3d5 Mon Sep 17 00:00:00 2001 From: acekingke Date: Tue, 26 Jul 2022 15:38:01 +0800 Subject: [PATCH] *: use the fix clusterip to disconnect the session #619 --- controllers/mysqlcluster_controller.go | 21 +++++++++++++++++++++ mysqlcluster/container/xenon.go | 4 ++-- mysqlcluster/container/xenon_test.go | 4 ++-- mysqlcluster/syncer/leader_service.go | 5 +++++ mysqlcluster/syncer/role.go | 5 +++++ sidecar/config.go | 21 ++++++++++----------- sidecar/init.go | 12 ++++++------ sidecar/util.go | 4 ++-- 8 files changed, 53 insertions(+), 23 deletions(-) diff --git a/controllers/mysqlcluster_controller.go b/controllers/mysqlcluster_controller.go index 0cf7dd68..6ef55ea6 100644 --- a/controllers/mysqlcluster_controller.go +++ b/controllers/mysqlcluster_controller.go @@ -110,6 +110,16 @@ func (r *MysqlClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request return ctrl.Result{}, err } } + // Set the instance label of clusterip. + if _, ok := instance.Unwrap().ObjectMeta.Annotations["ClusterIP"]; !ok { + // not exist + if clusterip := r.getServerClusterIp(ctx, req, instance.Unwrap()); clusterip != "" { + instance.Unwrap().ObjectMeta.Annotations["ClusterIP"] = clusterip + } + if err = r.Update(ctx, instance.Unwrap()); err != nil { + return ctrl.Result{}, err + } + } if !instance.ObjectMeta.DeletionTimestamp.IsZero() { // Delete all the backup cr return ctrl.Result{}, r.deleteAllBackup(ctx, req, instance.Unwrap()) @@ -204,3 +214,14 @@ func (r *MysqlClusterReconciler) deleteAllBackup(ctx context.Context, req ctrl.R return nil } + +func (r *MysqlClusterReconciler) getServerClusterIp(ctx context.Context, req ctrl.Request, instance *apiv1alpha1.MysqlCluster) string { + log := log.FromContext(ctx).WithName("controllers").WithName("MysqlCluster") + // Get leader service + leaderSvc := &corev1.Service{} + if err := r.Get(ctx, client.ObjectKey{Namespace: instance.Namespace, Name: instance.Name + "-leader"}, leaderSvc); err != nil { + log.Error(err, "failed to get leader service") + return "" + } + return leaderSvc.Spec.ClusterIP +} diff --git a/mysqlcluster/container/xenon.go b/mysqlcluster/container/xenon.go index 37f520c5..47dd2c74 100644 --- a/mysqlcluster/container/xenon.go +++ b/mysqlcluster/container/xenon.go @@ -86,7 +86,7 @@ func (c *xenon) getLifecycle() *corev1.Lifecycle { Command: []string{ "/bin/bash", "-c", - "/xenonchecker preStop", + "/scripts/leader-stop.sh", }, }, }, @@ -95,7 +95,7 @@ func (c *xenon) getLifecycle() *corev1.Lifecycle { Command: []string{ "/bin/bash", "-c", - "/xenonchecker postStart", + "/scripts/leader-stop.sh", }, }, }, diff --git a/mysqlcluster/container/xenon_test.go b/mysqlcluster/container/xenon_test.go index f62eb6e7..d34f3958 100644 --- a/mysqlcluster/container/xenon_test.go +++ b/mysqlcluster/container/xenon_test.go @@ -101,7 +101,7 @@ func TestGetXenonLifecycle(t *testing.T) { Command: []string{ "/bin/bash", "-c", - "/xenonchecker preStop", + "/scripts/leader-stop.sh", }, }, }, @@ -110,7 +110,7 @@ func TestGetXenonLifecycle(t *testing.T) { Command: []string{ "/bin/bash", "-c", - "/xenonchecker postStart", + "/scripts/leader-stop.sh", }, }, }, diff --git a/mysqlcluster/syncer/leader_service.go b/mysqlcluster/syncer/leader_service.go index 32b5c66e..20e082f4 100644 --- a/mysqlcluster/syncer/leader_service.go +++ b/mysqlcluster/syncer/leader_service.go @@ -53,7 +53,12 @@ func NewLeaderSVCSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer. if len(service.Spec.Ports) != 2 { service.Spec.Ports = make([]corev1.ServicePort, 2) } + if service.Spec.Type == "ClusterIP" { + if ip, ok := c.ObjectMeta.Annotations["ClusterIP"]; ok { + service.Spec.ClusterIP = ip + } + } service.Spec.Ports[0].Name = utils.MysqlPortName service.Spec.Ports[0].Port = utils.MysqlPort service.Spec.Ports[0].TargetPort = intstr.FromInt(utils.MysqlPort) diff --git a/mysqlcluster/syncer/role.go b/mysqlcluster/syncer/role.go index 26e236e9..3d412177 100644 --- a/mysqlcluster/syncer/role.go +++ b/mysqlcluster/syncer/role.go @@ -56,6 +56,11 @@ func NewRoleSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.Inter APIGroups: []string{""}, Resources: []string{"pods/exec"}, }, + { + Verbs: []string{"delete"}, + APIGroups: []string{""}, + Resources: []string{"services"}, + }, } return nil }) diff --git a/sidecar/config.go b/sidecar/config.go index e8b46d96..5e6c4ef2 100644 --- a/sidecar/config.go +++ b/sidecar/config.go @@ -326,6 +326,7 @@ func (cfg *Config) buildXenonConf() []byte { hostName := fmt.Sprintf("%s.%s.%s", cfg.HostName, cfg.ServiceName, cfg.NameSpace) // Because go-sql-driver will translate localhost to 127.0.0.1 or ::1, but never set the hostname // so the host is set to "127.0.0.1" in config file. + str := fmt.Sprintf(`{ "log": { "level": "INFO" @@ -363,8 +364,8 @@ func (cfg *Config) buildXenonConf() []byte { "semi-sync-degrade": true, "purge-binlog-disabled": true, "super-idle": false, - "leader-start-command": "/xenonchecker leaderStart", - "leader-stop-command": "/xenonchecker leaderStop" + "leader-start-command": "/scripts/leader-stop.sh", + "leader-stop-command": "/scripts/leader-stop.sh" } } `, hostName, utils.XenonPort, hostName, utils.XenonPeerPort, cfg.ReplicationPassword, cfg.ReplicationUser, @@ -459,15 +460,13 @@ func (cfg *Config) buildClientConfig() (*ini.File, error) { // 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) -// } +// buildLeaderStop build the leader-stop.sh. +func (cfg *Config) buildLeaderStop() []byte { + str := fmt.Sprintf(`#!/usr/bin/env bash + curl -X DELETE -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json" --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/$NAMESPACE/services/%s-leader +`, cfg.ClusterName) + 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 33f55529..7ab9cfb6 100644 --- a/sidecar/init.go +++ b/sidecar/init.go @@ -213,12 +213,12 @@ func runInitCommand(cfg *Config) error { // 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-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 1787929d..9b8e8526 100644 --- a/sidecar/util.go +++ b/sidecar/util.go @@ -52,8 +52,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