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

sidecar: Use leader start and stop hook. #451

Closed
wants to merge 1 commit into from
Closed
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
sidecar: Use leader start and stop hook.
  • Loading branch information
runkecheng committed Apr 14, 2022
commit 0d20349f69270c255540d3ddc35651db342a416a
40 changes: 21 additions & 19 deletions sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ func (cfg *Config) buildXenonConf() []byte {
"admit-defeat-hearbeat-count": %d,
"heartbeat-timeout": %d,
"meta-datadir": "%s",
"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
Expand Down Expand Up @@ -428,25 +430,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
Expand Down
26 changes: 13 additions & 13 deletions sidecar/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,19 @@ func runInitCommand(cfg *Config) error {
}
}

// // 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 {
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