Repository: radondb/radondb-mysql-kubernetes
Pull request #217 (2021-09-10 02:04:01)

Feature fix bug of not effective version of mysql56 #203
<!-- Thanks for sending a pull request!

PR Title Format:
1. pkg [, pkg2, pkg3]: what's changed #xxx
2. *: what's changed #xxx

-->

### What type of PR is this?

<!--
Add one of the following types:
/bug
/documentation
/cleanup
/enhancement
-->

### Which issue(s) this PR fixes?

<!--
Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes #

### What this PR does?

Summary:

### Special notes for your reviewer?



Patch:
diff --git a/Makefile b/Makefile
index 15e5cab..ef07a0f 100644
--- a/Makefile
+++ b/Makefile
@@ -41,10 +41,6 @@ help: ## Display this help.
 manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
 	$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
 
-update-crd:	## Synchronize the generated YAML files to operator Chart after make manifests.
-	make manifests
-	cp config/crd/bases/* charts/mysql-operator/crds/
-
 generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
 	$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
 
diff --git a/docs/mgt_mysqluser.md b/docs/mgt_mysqluser.md
index d28eb72..a5939e5 100644
--- a/docs/mgt_mysqluser.md
+++ b/docs/mgt_mysqluser.md
@@ -11,7 +11,7 @@
 使用如下指令，将查看到名称为 `mysqlusers.mysql.radondb.com` 的 CRD。
 
 ```plain
-kubectl get crd | grep mysqluser
+kubectl get crd | grep MysqlUser
 mysqlusers.mysql.radondb.com                          2021-09-21T09:15:08Z
 ```
 
diff --git a/mysqlcluster/mysqlcluster.go b/mysqlcluster/mysqlcluster.go
index d0d8c3b..e0e8421 100644
--- a/mysqlcluster/mysqlcluster.go
+++ b/mysqlcluster/mysqlcluster.go
@@ -17,6 +17,7 @@ limitations under the License.
 package mysqlcluster
 
 import (
+	"errors"
 	"fmt"
 	"math"
 	"strconv"
@@ -31,12 +32,13 @@ import (
 	"k8s.io/apimachinery/pkg/types"
 	"sigs.k8s.io/controller-runtime/pkg/client"
 	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
+	logf "sigs.k8s.io/controller-runtime/pkg/log"
 
 	apiv1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
 	"github.com/radondb/radondb-mysql-kubernetes/utils"
 )
 
-// nolint: megacheck, deadcode, varcheck
+// Nolint: megacheck, deadcode, varcheck.
 const (
 	_         = iota // ignore first value by assigning to blank identifier
 	kb uint64 = 1 << (10 * iota)
@@ -44,6 +46,8 @@ const (
 	gb
 )
 
+var log = logf.Log.WithName("mysqlcluster")
+
 // MysqlCluster is the wrapper for apiv1alpha1.MysqlCluster type.
 type MysqlCluster struct {
 	*apiv1alpha1.MysqlCluster
@@ -56,7 +60,7 @@ func New(mc *apiv1alpha1.MysqlCluster) *MysqlCluster {
 	}
 }
 
-// Unwrap returns the api mysqlcluster object
+// Unwrap returns the api mysqlcluster object.
 func (c *MysqlCluster) Unwrap() *apiv1alpha1.MysqlCluster {
 	return c.MysqlCluster
 }
@@ -75,7 +79,7 @@ func (c *MysqlCluster) Validate() error {
 	return nil
 }
 
-// GetLabels returns mysqlcluster labels
+// GetLabels returns mysqlcluster labels.
 func (c *MysqlCluster) GetLabels() labels.Set {
 	instance := c.Name
 	if inst, ok := c.Annotations["app.kubernetes.io/instance"]; ok {
@@ -103,7 +107,7 @@ func (c *MysqlCluster) GetLabels() labels.Set {
 	return labels
 }
 
-// GetSelectorLabels returns the labels that will be used as selector
+// GetSelectorLabels returns the labels that will be used as selector.
 func (c *MysqlCluster) GetSelectorLabels() labels.Set {
 	return labels.Set{
 		"mysql.radondb.com/cluster":    c.Name,
@@ -114,12 +118,17 @@ func (c *MysqlCluster) GetSelectorLabels() labels.Set {
 
 // GetMySQLVersion returns the MySQL server version.
 func (c *MysqlCluster) GetMySQLVersion() string {
-	version := c.Spec.MysqlVersion
-	// lookup for an alias, usually this will solve 5.7 to 5.7.x
-	if v, ok := utils.MySQLTagsToSemVer[version]; ok {
+	var version string
+	// Lookup for an alias, this will solve MySQL tags: 5.7 --> 5.7.x
+	if v, ok := utils.MySQLTagsToSemVer[c.Spec.MysqlVersion]; ok {
 		version = v
+	} else {
+		errmsg := "Invalid mysql version option:" + c.Spec.MysqlVersion
+		log.Error(errors.New(errmsg), "currently we do not support mysql 5.6 or earlier version, default mysql version option should be 5.7 or 8.0")
+		return utils.InvalidMySQLVersion
 	}
 
+	// Check if has specified image.
 	if _, ok := utils.MysqlImageVersions[version]; !ok {
 		version = utils.MySQLDefaultVersion
 	}
diff --git a/mysqlcluster/syncer/metrics_service.go b/mysqlcluster/syncer/metrics_service.go
index 66540f6..1dea7cd 100644
--- a/mysqlcluster/syncer/metrics_service.go
+++ b/mysqlcluster/syncer/metrics_service.go
@@ -56,6 +56,7 @@ func NewMetricsSVCSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer
 		service.Spec.Ports[0].Name = utils.MetricsPortName
 		service.Spec.Ports[0].Port = utils.MetricsPort
 		service.Spec.Ports[0].TargetPort = intstr.FromInt(utils.MetricsPort)
+
 		return nil
 	})
 }
diff --git a/sidecar/config.go b/sidecar/config.go
index 584bd34..cf9b701 100644
--- a/sidecar/config.go
+++ b/sidecar/config.go
@@ -76,7 +76,7 @@ type Config struct {
 
 	// The parameter in xenon means admit defeat count for hearbeat.
 	AdmitDefeatHearbeatCount int32
-	// The parameter in xenon means election timeout(ms)。
+	// The parameter in xenon means election timeout(ms).
 	ElectionTimeout int32
 
 	// Whether the MySQL data exists.
@@ -120,12 +120,18 @@ type Config struct {
 	XRestoreFrom string
 }
 
-// NewInitConfig returns the configuration file needed for initialization.
+// NewInitConfig returns a pointer to Config.
 func NewInitConfig() *Config {
-	mysqlVersion, err := semver.Parse(getEnvValue("MYSQL_VERSION"))
-	if err != nil {
-		log.Info("MYSQL_VERSION is not a semver version")
-		mysqlVersion, _ = semver.Parse(utils.MySQLDefaultVersion)
+	// check mysql version is supported or not and then get parse mysql semver version
+	var mysqlSemVer semver.Version
+	if ver := getEnvValue("MYSQL_VERSION"); ver == utils.InvalidMySQLVersion {
+		panic("invalid mysql version, currently we only support 5.7 or 8.0")
+	} else {
+		mysqlSemVer, err := semver.Parse(ver)
+		if err != nil {
+			log.Info("semver get from MYSQL_VERSION is invalid", "semver: ", mysqlSemVer)
+			panic(err)
+		}
 	}
 
 	replicaStr := getEnvValue("REPLICAS")
@@ -176,7 +182,7 @@ func NewInitConfig() *Config {
 
 		InitTokuDB: initTokuDB,
 
-		MySQLVersion: mysqlVersion,
+		MySQLVersion: mysqlSemVer,
 
 		AdmitDefeatHearbeatCount: int32(admitDefeatHearbeatCount),
 		ElectionTimeout:          int32(electionTimeout),
@@ -304,20 +310,16 @@ func (cfg *Config) buildXenonConf() []byte {
 
 	version := "mysql80"
 	if cfg.MySQLVersion.Major == 5 {
-		if cfg.MySQLVersion.Minor == 6 {
-			version = "mysql56"
-		} else {
-			version = "mysql57"
-		}
+		version = "mysql57"
 	}
 
-	var masterSysVars, slaveSysVars string
+	var srcSysVars, replicaSysVars string
 	if cfg.InitTokuDB {
-		masterSysVars = "tokudb_fsync_log_period=default;sync_binlog=default;innodb_flush_log_at_trx_commit=default"
-		slaveSysVars = "tokudb_fsync_log_period=1000;sync_binlog=1000;innodb_flush_log_at_trx_commit=1"
+		srcSysVars = "tokudb_fsync_log_period=default;sync_binlog=default;innodb_flush_log_at_trx_commit=default"
+		replicaSysVars = "tokudb_fsync_log_period=1000;sync_binlog=1000;innodb_flush_log_at_trx_commit=1"
 	} else {
-		masterSysVars = "sync_binlog=default;innodb_flush_log_at_trx_commit=default"
-		slaveSysVars = "sync_binlog=1000;innodb_flush_log_at_trx_commit=1"
+		srcSysVars = "sync_binlog=default;innodb_flush_log_at_trx_commit=default"
+		replicaSysVars = "sync_binlog=1000;innodb_flush_log_at_trx_commit=1"
 	}
 
 	hostName := fmt.Sprintf("%s.%s.%s", cfg.HostName, cfg.ServiceName, cfg.NameSpace)
@@ -363,7 +365,7 @@ func (cfg *Config) buildXenonConf() []byte {
     }
 }
 `, hostName, utils.XenonPort, hostName, utils.XenonPeerPort, cfg.ReplicationPassword, cfg.ReplicationUser, requestTimeout,
-		pingTimeout, cfg.RootPassword, version, masterSysVars, slaveSysVars, cfg.ElectionTimeout,
+		pingTimeout, cfg.RootPassword, version, srcSysVars, replicaSysVars, cfg.ElectionTimeout,
 		cfg.AdmitDefeatHearbeatCount, heartbeatTimeout)
 	return utils.StringToBytes(str)
 }
diff --git a/utils/constants.go b/utils/constants.go
index edc3a7a..e10a065 100644
--- a/utils/constants.go
+++ b/utils/constants.go
@@ -20,6 +20,9 @@ var (
 	// MySQLDefaultVersion is the version for mysql that should be used
 	MySQLDefaultVersion = "5.7.34"
 
+	// InvalidMySQLVersion is used for set invalid version that we do not support.
+	InvalidMySQLVersion = "0.0.0"
+
 	// MySQLTagsToSemVer maps simple version to semver versions
 	MySQLTagsToSemVer = map[string]string{
 		"5.7": "5.7.34",
@@ -29,6 +32,7 @@ var (
 	MysqlImageVersions = map[string]string{
 		"5.7.33": "percona/percona-server:5.7.33",
 		"5.7.34": "percona/percona-server:5.7.34",
+		"0.0.0":  "errimage",
 	}
 )
 


Test patch:
diff --git a/mysqlcluster/container/init_sidecar_test.go b/mysqlcluster/container/init_sidecar_test.go
index 45d388a..5940648 100644
--- a/mysqlcluster/container/init_sidecar_test.go
+++ b/mysqlcluster/container/init_sidecar_test.go
@@ -54,6 +54,7 @@ var (
 			MetricsOpts: mysqlv1alpha1.MetricsOpts{
 				Enabled: false,
 			},
+			MysqlVersion: "5.7",
 			MysqlOpts: mysqlv1alpha1.MysqlOpts{
 				InitTokuDB: false,
 			},
diff --git a/mysqlcluster/mysqlcluster_test.go b/mysqlcluster/mysqlcluster_test.go
index 8a21509..ddeff02 100644
--- a/mysqlcluster/mysqlcluster_test.go
+++ b/mysqlcluster/mysqlcluster_test.go
@@ -131,17 +131,17 @@ func TestGetSelectorLabels(t *testing.T) {
 }
 
 func TestGetMySQLVersion(t *testing.T) {
-	//other 8.0 ->  5.7.34
+	// Invalid, currently not support: 8.0 -> 0.0.0
 	{
 		testMysqlCluster := mysqlCluster
 		testMysqlCluster.Spec.MysqlVersion = "8.0"
 		testCase := MysqlCluster{
 			&testMysqlCluster,
 		}
-		want := "5.7.34"
+		want := utils.InvalidMySQLVersion
 		assert.Equal(t, want, testCase.GetMySQLVersion())
 	}
-	//MySQLTagsToSemVer 5.7 -> 5.7.34
+	// MySQLTagsToSemVer 5.7 -> 5.7.34
 	{
 		testMysqlCluster := mysqlCluster
 		testMysqlCluster.Spec.MysqlVersion = "5.7"
@@ -151,14 +151,14 @@ func TestGetMySQLVersion(t *testing.T) {
 		want := "5.7.34"
 		assert.Equal(t, want, testCase.GetMySQLVersion())
 	}
-	//MysqlImageVersions 5.7.34 -> 5.7.34
+	// Invalid, not support MysqlImageVersions 5.7.34 -> 5.7.34
 	{
 		testMysqlCluster := mysqlCluster
 		testMysqlCluster.Spec.MysqlVersion = "5.7.34"
 		testCase := MysqlCluster{
 			&testMysqlCluster,
 		}
-		want := "5.7.34"
+		want := utils.InvalidMySQLVersion
 		assert.Equal(t, want, testCase.GetMySQLVersion())
 	}
 }