Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: use the fix clusterip to disconnect the session #619 #620

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions controllers/mysqlcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions mysqlcluster/container/xenon.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *xenon) getLifecycle() *corev1.Lifecycle {
Command: []string{
"/bin/bash",
"-c",
"/xenonchecker preStop",
"/scripts/leader-stop.sh",
},
},
},
Expand All @@ -95,7 +95,7 @@ func (c *xenon) getLifecycle() *corev1.Lifecycle {
Command: []string{
"/bin/bash",
"-c",
"/xenonchecker postStart",
"/scripts/leader-stop.sh",
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions mysqlcluster/container/xenon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestGetXenonLifecycle(t *testing.T) {
Command: []string{
"/bin/bash",
"-c",
"/xenonchecker preStop",
"/scripts/leader-stop.sh",
},
},
},
Expand All @@ -110,7 +110,7 @@ func TestGetXenonLifecycle(t *testing.T) {
Command: []string{
"/bin/bash",
"-c",
"/xenonchecker postStart",
"/scripts/leader-stop.sh",
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions mysqlcluster/syncer/leader_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions mysqlcluster/syncer/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
21 changes: 10 additions & 11 deletions sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions sidecar/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions sidecar/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down