Skip to content

Commit 8a9e40a

Browse files
committed
*: use the fix clusterip to disconnect the session #619
1 parent 8f05191 commit 8a9e40a

File tree

7 files changed

+51
-21
lines changed

7 files changed

+51
-21
lines changed

controllers/mysqlcluster_controller.go

+21
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ func (r *MysqlClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
110110
return ctrl.Result{}, err
111111
}
112112
}
113+
// Set the instance label of clusterip.
114+
if _, ok := instance.Unwrap().ObjectMeta.Annotations["ClusterIP"]; !ok {
115+
// not exist
116+
if clusterip := r.getServerClusterIp(ctx, req, instance.Unwrap()); clusterip != "" {
117+
instance.Unwrap().ObjectMeta.Annotations["ClusterIP"] = clusterip
118+
}
119+
if err = r.Update(ctx, instance.Unwrap()); err != nil {
120+
return ctrl.Result{}, err
121+
}
122+
}
113123
if !instance.ObjectMeta.DeletionTimestamp.IsZero() {
114124
// Delete all the backup cr
115125
return ctrl.Result{}, r.deleteAllBackup(ctx, req, instance.Unwrap())
@@ -204,3 +214,14 @@ func (r *MysqlClusterReconciler) deleteAllBackup(ctx context.Context, req ctrl.R
204214

205215
return nil
206216
}
217+
218+
func (r *MysqlClusterReconciler) getServerClusterIp(ctx context.Context, req ctrl.Request, instance *apiv1alpha1.MysqlCluster) string {
219+
log := log.FromContext(ctx).WithName("controllers").WithName("MysqlCluster")
220+
// Get leader service
221+
leaderSvc := &corev1.Service{}
222+
if err := r.Get(ctx, client.ObjectKey{Namespace: instance.Namespace, Name: instance.Name + "-leader"}, leaderSvc); err != nil {
223+
log.Error(err, "failed to get leader service")
224+
return ""
225+
}
226+
return leaderSvc.Spec.ClusterIP
227+
}

mysqlcluster/container/xenon.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (c *xenon) getLifecycle() *corev1.Lifecycle {
8686
Command: []string{
8787
"/bin/bash",
8888
"-c",
89-
"/xenonchecker preStop",
89+
"/scripts/leader-stop.sh",
9090
},
9191
},
9292
},
@@ -95,7 +95,7 @@ func (c *xenon) getLifecycle() *corev1.Lifecycle {
9595
Command: []string{
9696
"/bin/bash",
9797
"-c",
98-
"/xenonchecker postStart",
98+
"/scripts/leader-stop.sh",
9999
},
100100
},
101101
},

mysqlcluster/syncer/leader_service.go

+5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ func NewLeaderSVCSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.
5353
if len(service.Spec.Ports) != 2 {
5454
service.Spec.Ports = make([]corev1.ServicePort, 2)
5555
}
56+
if service.Spec.Type == "ClusterIP" {
57+
if ip, ok := c.ObjectMeta.Annotations["ClusterIP"]; ok {
58+
service.Spec.ClusterIP = ip
59+
}
5660

61+
}
5762
service.Spec.Ports[0].Name = utils.MysqlPortName
5863
service.Spec.Ports[0].Port = utils.MysqlPort
5964
service.Spec.Ports[0].TargetPort = intstr.FromInt(utils.MysqlPort)

mysqlcluster/syncer/role.go

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ func NewRoleSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.Inter
5656
APIGroups: []string{""},
5757
Resources: []string{"pods/exec"},
5858
},
59+
{
60+
Verbs: []string{"delete"},
61+
APIGroups: []string{""},
62+
Resources: []string{"services"},
63+
},
5964
}
6065
return nil
6166
})

sidecar/config.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ func (cfg *Config) buildXenonConf() []byte {
326326
hostName := fmt.Sprintf("%s.%s.%s", cfg.HostName, cfg.ServiceName, cfg.NameSpace)
327327
// Because go-sql-driver will translate localhost to 127.0.0.1 or ::1, but never set the hostname
328328
// so the host is set to "127.0.0.1" in config file.
329+
329330
str := fmt.Sprintf(`{
330331
"log": {
331332
"level": "INFO"
@@ -363,8 +364,8 @@ func (cfg *Config) buildXenonConf() []byte {
363364
"semi-sync-degrade": true,
364365
"purge-binlog-disabled": true,
365366
"super-idle": false,
366-
"leader-start-command": "/xenonchecker leaderStart",
367-
"leader-stop-command": "/xenonchecker leaderStop"
367+
"leader-start-command": "/scripts/leader-stop.sh",
368+
"leader-stop-command": "/scripts/leader-stop.sh"
368369
}
369370
}
370371
`, hostName, utils.XenonPort, hostName, utils.XenonPeerPort, cfg.ReplicationPassword, cfg.ReplicationUser,
@@ -459,15 +460,13 @@ func (cfg *Config) buildClientConfig() (*ini.File, error) {
459460
// return utils.StringToBytes(str)
460461
// }
461462

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

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

sidecar/init.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,12 @@ func runInitCommand(cfg *Config) error {
213213
// return fmt.Errorf("failed to write leader-start.sh: %s", err)
214214
// }
215215

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

223223
// for install tokudb.
224224
if cfg.InitTokuDB {

sidecar/util.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ var (
5252
// dataPath is the mysql data path.
5353
dataPath = utils.DataVolumeMountPath
5454

55-
// // scriptsPath is the scripts path used for xenon.
56-
// scriptsPath = utils.ScriptsVolumeMountPath
55+
// scriptsPath is the scripts path used for xenon.
56+
scriptsPath = utils.ScriptsVolumeMountPath
5757

5858
// sysPath is the linux kernel path used for install tokudb.
5959
sysPath = utils.SysVolumeMountPath

0 commit comments

Comments
 (0)