Skip to content

Commit 2e82696

Browse files
committed
sidecar: Optimized mysql configuration application logic.
Package the logic of applying configuration to improve code readability.
1 parent 0543c43 commit 2e82696

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

sidecar/init.go

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"strconv"
2828
"strings"
2929

30+
"github.com/go-ini/ini"
3031
"github.com/radondb/radondb-mysql-kubernetes/utils"
3132
"github.com/spf13/cobra"
3233
)
@@ -181,43 +182,24 @@ func runInitCommand(cfg *Config) error {
181182
if err != nil {
182183
return fmt.Errorf("failed to build extra.cnf: %s", err)
183184
}
184-
// Notice: special.cnf cannot be copied to extra-conf when initialized.
185-
// check /var/lib/mysql/mysql exists. if exists it means that been initialized.
185+
186+
// Notice: plugin.cnf cannot be copied to /etc/mysql/conf.d when initialized.
187+
// Check /var/lib/mysql/mysql exists. if exists it means that been initialized.
186188
if exists, _ := checkIfPathExists(path.Join(dataPath, "mysql")); exists || strings.HasPrefix(getEnvValue("MYSQL_VERSION"), "5") {
187-
if err = copyFile(path.Join(configMapPath, utils.SpecialConfig), path.Join(extraConfPath, utils.SpecialConfig)); err != nil {
188-
return fmt.Errorf("failed to copy special.cnf: %s", err)
189-
}
190-
// save extra.cnf to conf.d.
191-
if err := extraConfig.SaveTo(path.Join(extraConfPath, "extra.cnf")); err != nil {
192-
return fmt.Errorf("failed to save extra.cnf: %s", err)
193-
}
189+
// Save plugin.cnf and extra.cnf to /etc/mysql/conf.d.
190+
saveCnfTo(extraConfPath, extraConfig)
194191
} else {
195-
log.Info("mysql is not initialized, use shell script copying special.cnf")
196-
// save extra.cnf to conf.d.
197-
if err := extraConfig.SaveTo(path.Join(initFilePath, "extra.cnf")); err != nil {
198-
return fmt.Errorf("failed to save extra.cnf: %s", err)
199-
}
200-
if err = copyFile(path.Join(configMapPath, utils.SpecialConfig), path.Join(initFilePath, utils.SpecialConfig)); err != nil {
201-
return fmt.Errorf("failed to copy special.cnf: %s", err)
192+
log.Info("mysql is not initialized, use shell script copying plugin.cnf")
193+
// Save plugin.cnf and extra.cnf to /docker-entrypoint-initdb.d.
194+
saveCnfTo(initFilePath, extraConfig)
195+
196+
src := PluginConfigsSh()
197+
// Write plugin.sh to docker-entrypoint-initdb.d/plugin.sh.
198+
// In this way, plugin.sh will be performed automatically when Percona docker-entrypoint.sh is executed.
199+
// plugin.sh will copy plugin.cnf and extra.cnf to /etc/mysql/conf.d.
200+
if err = ioutil.WriteFile(initFilePath+"/plugin.sh", []byte(src), 0755); err != nil {
201+
return fmt.Errorf("failed to write plugin.sh: %s", err)
202202
}
203-
src := fmt.Sprintf(`#!/bin/bash
204-
cp %s %s
205-
cp %s %s
206-
chown -R mysql.mysql %s
207-
chown -R mysql.mysql %s`,
208-
// cp special.cnf to /etc/mysql/conf.d/
209-
path.Join(initFilePath, utils.SpecialConfig),
210-
path.Join(extraConfPath, utils.SpecialConfig),
211-
// cp extra.cnf to /etc/mysql/conf.d/
212-
path.Join(initFilePath, "extra.cnf"),
213-
path.Join(extraConfPath, "extra.cnf"),
214-
// chown -R mysql.mysql cnf files
215-
path.Join(extraConfPath, utils.SpecialConfig),
216-
path.Join(extraConfPath, "extra.cnf"))
217-
if err = ioutil.WriteFile(initFilePath+"/special.sh", []byte(src), 0755); err != nil {
218-
return fmt.Errorf("failed to write special.sh: %s", err)
219-
}
220-
221203
}
222204

223205
// // build leader-start.sh.
@@ -285,3 +267,30 @@ func RunRequestBackup(cfg *Config, host string) error {
285267
_, err := requestABackup(cfg, host, serverBackupEndpoint)
286268
return err
287269
}
270+
271+
// Save plugin.cnf and extra.cnf to specified path.
272+
func saveCnfTo(targetPath string, extraCnf *ini.File) error {
273+
if err := copyFile(path.Join(configMapPath, utils.PluginConfigs), path.Join(targetPath, utils.PluginConfigs)); err != nil {
274+
return fmt.Errorf("failed to copy plugin.cnf: %s", err)
275+
}
276+
if err := extraCnf.SaveTo(path.Join(targetPath, "extra.cnf")); err != nil {
277+
return fmt.Errorf("failed to save extra.cnf: %s", err)
278+
}
279+
return nil
280+
}
281+
282+
func PluginConfigsSh() string {
283+
return fmt.Sprintf(`#!/bin/bash
284+
cp %s %s
285+
cp %s %s
286+
chown -R mysql.mysql %s
287+
chown -R mysql.mysql %s`,
288+
// cp plugin.cnf to /etc/mysql/conf.d/
289+
path.Join(initFilePath, utils.PluginConfigs), path.Join(extraConfPath, utils.PluginConfigs),
290+
// cp extra.cnf to /etc/mysql/conf.d/
291+
path.Join(initFilePath, "extra.cnf"), path.Join(extraConfPath, "extra.cnf"),
292+
// chown -R mysql.mysql plugin.cnf
293+
path.Join(extraConfPath, utils.PluginConfigs),
294+
// chown -R mysql.mysql extra.cnf
295+
path.Join(extraConfPath, "extra.cnf"))
296+
}

utils/constants.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ const (
132132
// LeaderHost is the alias for leader`s host.
133133
LeaderHost = "leader-host"
134134

135-
// For MySQL8, the configs should deal with alone
136-
SpecialConfig = "special.cnf"
135+
//
136+
PluginConfigs = "plugin.cnf"
137137
)
138138

139139
// ResourceName is the type for aliasing resources that will be created.

0 commit comments

Comments
 (0)