Skip to content

Commit

Permalink
mysqlcluster: fix the bug for replace config items radondb#562
Browse files Browse the repository at this point in the history
  • Loading branch information
acekingke committed Jun 30, 2022
1 parent 7cbcb88 commit 0c361aa
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions mysqlcluster/syncer/mysql_cm.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func NewMysqlCMSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.In
}

return syncer.NewObjectSyncer("ConfigMap", c.Unwrap(), cm, cli, func() error {
replaceExistConfig(c)

data, err := buildMysqlConf(c)
if err != nil {
return fmt.Errorf("failed to create mysql configs: %s", err)
Expand All @@ -66,6 +68,34 @@ func NewMysqlCMSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.In
})
}

// Replace exist config items
func replaceExistConfig(c *mysqlcluster.MysqlCluster) {
// Pre alloc 10 cap
existKeys := make([]string, 0, 10)
// Check
needChecks := []map[string]string{mysqlSysConfigs,
mysqlCommonConfigs, mysqlStaticConfigs, pluginConfigs}
switch c.Spec.MysqlVersion {
case "8.0":
needChecks = append(needChecks, mysql80Configs)
case "5.7":
needChecks = append(needChecks, mysql57Configs)
}
for _, onecheck := range needChecks {
for k, v := range c.Spec.MysqlOpts.MysqlConf {
if _, ok := onecheck[k]; ok {
//replace it
onecheck[k] = v
existKeys = append(existKeys, k)
}
}
}
// Delete exist from c.Spec.MysqlOpts.MysqlConf
for _, key := range existKeys {
delete(c.Spec.MysqlOpts.MysqlConf, key)
}
}

// buildMysqlConf build the mysql config.
func buildMysqlConf(c *mysqlcluster.MysqlCluster) (string, error) {
var log = logf.Log.WithName("mysqlcluster.syncer.buildMysqlConf")
Expand All @@ -81,7 +111,7 @@ func buildMysqlConf(c *mysqlcluster.MysqlCluster) (string, error) {
addKVConfigsToSection(sec, mysql57Configs)
}

addKVConfigsToSection(sec, mysqlSysConfigs, mysqlCommonConfigs, mysqlStaticConfigs)
addKVConfigsToSection(sec, mysqlSysConfigs, mysqlCommonConfigs, mysqlStaticConfigs, c.Spec.MysqlOpts.MysqlConf)

if c.Spec.MysqlOpts.InitTokuDB {
addKVConfigsToSection(sec, mysqlTokudbConfigs)
Expand All @@ -96,12 +126,6 @@ func buildMysqlConf(c *mysqlcluster.MysqlCluster) (string, error) {
addKVConfigsToSection(sec, mysqlSSLConfigs)
}

for k, v := range c.Spec.MysqlOpts.MysqlConf {
if sec.HasKey(k) {
sec.Key(k).SetValue(v)
}
}

data, err := writeConfigs(cfg)
if err != nil {
return "", err
Expand All @@ -115,12 +139,6 @@ func buildMysqlPluginConf(c *mysqlcluster.MysqlCluster) (string, error) {
cfg := ini.Empty(ini.LoadOptions{IgnoreInlineComment: true})
sec := cfg.Section("mysqld")

for k, v := range c.Spec.MysqlOpts.MysqlConf {
if _, ok := pluginConfigs[k]; ok {
pluginConfigs[k] = v
}
}

addKVConfigsToSection(sec, pluginConfigs)
data, err := writeConfigs(cfg)
if err != nil {
Expand Down

0 comments on commit 0c361aa

Please sign in to comment.