Skip to content

Commit

Permalink
*: support MySQL subversion
Browse files Browse the repository at this point in the history
  • Loading branch information
zhl003 committed Aug 25, 2022
1 parent 0304a80 commit 0347d0d
Show file tree
Hide file tree
Showing 26 changed files with 69 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN if [ $(cat /etc/timezone) = "Asia/Shanghai" ] ; then go env -w GOPROXY=https://goproxy.cn,direct; fi
RUN go env -w GOPROXY=https://goproxy.cn,direct
RUN go mod download

# Copy the go source
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.sidecar
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ COPY go.sum go.sum

# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN if [ $(cat /etc/timezone) = "Asia/Shanghai" ] ; then go env -w GOPROXY=https://goproxy.cn,direct; fi
RUN go env -w GOPROXY=https://goproxy.cn,direct
# go mod download
RUN go mod download

Expand Down
17 changes: 9 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Image URL to use all building/pushing image targets
IMG ?= controller:latest
SIDECAR_IMG ?= sidecar:latest
XENON_IMG ?= xenon:latest
IMGPREFIX ?=radondb/
IMG ?= $(IMGPREFIX)mysql-operator:latest
SIDECAR57_IMG ?= $(IMGPREFIX)mysql57-sidecar:latest
SIDECAR80_IMG ?= $(IMGPREFIX)mysql80-sidecar:latest
XENON_IMG ?= $(IMGPREFIX)xenon:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"

Expand Down Expand Up @@ -58,7 +60,7 @@ ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests generate fmt vet ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test $(shell go list ./... | grep -v /test/) -coverprofile cover.out

##@ Build

Expand All @@ -71,10 +73,9 @@ run: manifests generate fmt vet ## Run a controller from your host.

docker-build: test ## Build docker image with the manager.
docker build -t ${IMG} .
docker build -f Dockerfile.sidecar -t ${SIDECAR_IMG} .
docker build -f hack/xenon/Dockerfile -t ${XENON_IMG} hack/xenon
mysql8-sidecar:
docker build --build-arg XTRABACKUP_PKG=percona-xtrabackup-80 -f Dockerfile.sidecar -t ${SIDECAR_IMG} .
docker build -f Dockerfile.sidecar -t ${SIDECAR57_IMG} .
docker build -f build/xenon/Dockerfile -t ${XENON_IMG} .
docker build --build-arg XTRABACKUP_PKG=percona-xtrabackup-80 -f Dockerfile.sidecar -t ${SIDECAR80_IMG} .
docker-push: ## Push docker image with the manager.
docker push ${IMG}
docker push ${SIDECAR_IMG}
Expand Down
4 changes: 4 additions & 0 deletions api/v1alpha1/mysqlcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ type MysqlClusterSpec struct {

// MysqlOpts defines the options of MySQL container.
type MysqlOpts struct {
// Specifies mysql image to use.
// +optional
// +kubebuilder:default:="percona/percona-server:5.7.34"
Image string `json:"image,omitempty"`
// Unchangeable: Use super users instead
// Password for the root user, can be empty or 8~32 characters long.
// Only be a combination of uppercase letters, lowercase letters, numbers or special characters.
Expand Down
3 changes: 2 additions & 1 deletion api/v1alpha1/mysqlcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1

import (
"fmt"
"strings"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -98,7 +99,7 @@ func (r *MysqlCluster) validateVolumeSize(oldCluster *MysqlCluster) error {
func (r *MysqlCluster) validateLowTableCase(oldCluster *MysqlCluster) error {
oldmyconf := oldCluster.Spec.MysqlOpts.MysqlConf
newmyconf := r.Spec.MysqlOpts.MysqlConf
if r.Spec.MysqlVersion == "8.0" &&
if strings.Contains(r.Spec.MysqlOpts.Image, "8.0") &&
oldmyconf["lower_case_table_names"] != newmyconf["lower_case_table_names"] {
return apierrors.NewForbidden(schema.GroupResource{}, "", fmt.Errorf("lower_case_table_names cannot be changed in MySQL8.0+"))
}
Expand Down
2 changes: 1 addition & 1 deletion build/xenon/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
FROM golang:1.16 as builder

ARG XENON_BRANCH=master
RUN if [ $(cat /etc/timezone) = "Asia/Shanghai" ] ; then go env -w GOPROXY=https://goproxy.cn,direct; fi && go env -w GO111MODULE=off;
RUN go env -w GOPROXY=https://goproxy.cn,direct && go env -w GO111MODULE=off;
RUN set -ex; \
mkdir -p /go/src/github.com/radondb; \
cd /go/src/github.com/radondb; \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ spec:
default: radondb
description: Name for new database to create.
type: string
image:
default: percona/percona-server:5.7.34
description: Specifies mysql image to use.
type: string
initTokuDB:
default: false
description: InitTokuDB represents if install tokudb engine.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/mysql.radondb.com_mysqlclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ spec:
default: radondb
description: Name for new database to create.
type: string
image:
default: percona/percona-server:5.7.34
description: Specifies mysql image to use.
type: string
initTokuDB:
default: false
description: InitTokuDB represents if install tokudb engine.
Expand Down
2 changes: 1 addition & 1 deletion config/samples/mysql_v1alpha1_mysqlcluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ metadata:
spec:
replicas: 3
mysqlVersion: "5.7"

# the backupSecretName specify the secret file name which store S3 information,
# if you want S3 backup or restore, please create backup_secret.yaml, uncomment below and fill secret name:
# backupSecretName:
Expand All @@ -18,6 +17,7 @@ spec:
# such as nfsServerAddress: "10.233.55.172"
# nfsServerAddress:
mysqlOpts:
image: percona/percona-server:5.7.34
user: radondb_usr
password: RadonDB@123
database: radondb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ metadata:
spec:
replicas: 3
mysqlVersion: "5.7"

# the backupSecretName specify the secret file name which store S3 information,
# if you want S3 backup or restore, please create backup_secret.yaml, uncomment below and fill secret name:
backupSecretName: sample-backup-secret
Expand All @@ -14,6 +13,7 @@ spec:
# restoreFrom:
BackupSchedule: "0 50 * * * *"
mysqlOpts:
image: percona/percona-server:5.7.34
rootPassword: "RadonDB@123"
rootHost: localhost
user: radondb_usr
Expand Down
4 changes: 2 additions & 2 deletions config/samples/mysql_v1alpha1_mysqlcluster_mysql8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ metadata:
name: sample
spec:
replicas: 3
mysqlVersion: "8.0"

mysqlVersion: "5.7"
# the backupSecretName specify the secret file name which store S3 information,
# if you want S3 backup or restore, please create backup_secret.yaml, uncomment below and fill secret name:
# backupSecretName:
Expand All @@ -14,6 +13,7 @@ spec:
# restoreFrom:

mysqlOpts:
image: percona/percona-server:8.0.25
user: radondb_usr
password: RadonDB@123
database: radondb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ metadata:
spec:
replicas: 3
mysqlVersion: "5.7"

# the backupSecretName specify the secret file name which store S3 information,
# if you want S3 backup or restore, please create backup_secret.yaml, uncomment below and fill secret name:
# backupSecretName:
Expand All @@ -14,6 +13,7 @@ spec:
# restoreFrom:

mysqlOpts:
image: percona/percona-server:5.7.34
user: radondb_usr
password: RadonDB@123
database: radondb
Expand Down
1 change: 0 additions & 1 deletion docs/en-us/backup_and_restoration_s3.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ Configure the `backupSecretName` property in `mysql_v1alpha1_mysqlcluster.yaml`,
```yaml
spec:
replicas: 3
mysqlVersion: "5.7"
backupSecretName: sample-backup-secret
...
```
Expand Down
1 change: 0 additions & 1 deletion docs/en-us/backup_cron.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ The scheduled backup is currently supported for both S3 and NFS backups. You can
...
spec:
replicas: 3
mysqlVersion: "5.7"
backupSchedule: "0 0 0 * * *" # daily
...
```
Expand Down
2 changes: 0 additions & 2 deletions docs/zh-cn/backup_and_restoration_s3.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ kubectl create -f config/samples/backup_secret.yaml
```yaml
spec:
replicas: 3
mysqlVersion: "5.7"
backupSecretName: sample-backup-secret
...
```
Expand Down Expand Up @@ -94,7 +93,6 @@ backup-sample sample_2022526155115 2022-05-26T15:51:15 S3
...
spec:
replicas: 3
mysqlVersion: "5.7"
backupSecretName: sample-backup-secret
restoreFrom: "sample_2022526155115"
...
Expand Down
2 changes: 1 addition & 1 deletion mysqlcluster/container/init_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *initMysql) getName() string {

// getImage get the container image.
func (c *initMysql) getImage() string {
img := utils.MysqlImageVersions[c.GetMySQLVersion()]
img := c.Spec.MysqlOpts.Image
return img
}

Expand Down
2 changes: 1 addition & 1 deletion mysqlcluster/container/init_mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ var (
Requests: nil,
},
},
MysqlVersion: "5.7",
MysqlOpts: mysqlv1alpha1.MysqlOpts{
RootHost: "localhost",
InitTokuDB: false,
Image: "percona/percona-server:5.7.34",
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion mysqlcluster/container/init_sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ var (
MetricsOpts: mysqlv1alpha1.MetricsOpts{
Enabled: false,
},
MysqlVersion: "5.7",
MysqlOpts: mysqlv1alpha1.MysqlOpts{
InitTokuDB: false,
Image: "percona/percona-server:5.7.34",
},
Persistence: mysqlv1alpha1.Persistence{
Enabled: false,
Expand Down
2 changes: 1 addition & 1 deletion mysqlcluster/container/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *mysql) getName() string {

// getImage get the container image.
func (c *mysql) getImage() string {
img := utils.MysqlImageVersions[c.GetMySQLVersion()]
img := c.Spec.MysqlOpts.Image
return img
}

Expand Down
2 changes: 1 addition & 1 deletion mysqlcluster/container/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ var (
Requests: nil,
},
},
MysqlVersion: "5.7",
MysqlOpts: mysqlv1alpha1.MysqlOpts{
InitTokuDB: false,
Image: "percona/percona-server:5.7.34",
},
},
}
Expand Down
18 changes: 3 additions & 15 deletions mysqlcluster/mysqlcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package mysqlcluster

import (
"errors"
"fmt"
"math"
"os"
Expand Down Expand Up @@ -74,7 +73,7 @@ func (c *MysqlCluster) Validate() error {
}
// MySQL8 nerver support TokuDB
// https://www.percona.com/blog/2021/05/21/tokudb-support-changes-and-future-removal-from-percona-server-for-mysql-8-0/
if c.Spec.MysqlVersion == "8.0" && c.Spec.MysqlOpts.InitTokuDB {
if strings.Contains(c.Spec.MysqlOpts.Image, "8.0") && c.Spec.MysqlOpts.InitTokuDB {
c.log.Info("TokuDB is not supported in MySQL 8.0 any more, the value in Cluster.spec.mysqlOpts.initTokuDB should be set false")
return nil
}
Expand Down Expand Up @@ -131,22 +130,11 @@ func (c *MysqlCluster) GetSelectorLabels() labels.Set {

// GetMySQLVersion returns the MySQL server version.
func (c *MysqlCluster) GetMySQLVersion() string {
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
if _, _, imageTag, err := utils.ParseImageName(c.Spec.MysqlOpts.Image); err == nil {
return imageTag
} else {
errmsg := "Invalid mysql version option:" + c.Spec.MysqlVersion
c.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
}

return version
}

// CreatePeers create peers for xenon.
Expand Down
13 changes: 7 additions & 6 deletions mysqlcluster/mysqlcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ var (
Name: "sample",
},
Spec: mysqlv1alpha1.MysqlClusterSpec{
MysqlVersion: "5.7",
MysqlOpts: mysqlv1alpha1.MysqlOpts{
Image: "percona/percona-server:5.7.35",
},
},
}
testCluster = MysqlCluster{
Expand Down Expand Up @@ -139,7 +141,7 @@ func TestGetMySQLVersion(t *testing.T) {
// Invalid, currently not support: 8.0 -> 0.0.0
{
testMysqlCluster := mysqlCluster
testMysqlCluster.Spec.MysqlVersion = "8.0"
testMysqlCluster.Spec.MysqlOpts.Image = "percona/percona-server:8.0.25"
testCase := MysqlCluster{
MysqlCluster: &testMysqlCluster,
log: logf.Log.WithName("mysqlcluster"),
Expand All @@ -150,18 +152,18 @@ func TestGetMySQLVersion(t *testing.T) {
// MySQLTagsToSemVer 5.7 -> 5.7.34
{
testMysqlCluster := mysqlCluster
testMysqlCluster.Spec.MysqlVersion = "5.7"
testMysqlCluster.Spec.MysqlOpts.Image = "percona/percona-server:5.7.35"
testCase := MysqlCluster{
MysqlCluster: &testMysqlCluster,
log: logf.Log.WithName("mysqlcluster"),
}
want := "5.7.34"
want := "5.7.35"
assert.Equal(t, want, testCase.GetMySQLVersion())
}
// Invalid, not support MysqlImageVersions 5.7.34 -> 5.7.34
{
testMysqlCluster := mysqlCluster
testMysqlCluster.Spec.MysqlVersion = "5.7.34"
testMysqlCluster.Spec.MysqlOpts.Image = "5.7.35"
testCase := MysqlCluster{
MysqlCluster: &testMysqlCluster,
log: logf.Log.WithName("mysqlcluster"),
Expand Down Expand Up @@ -387,7 +389,6 @@ func TestEnsureVolumeClaimTemplates(t *testing.T) {
Labels: nil,
},
Spec: mysqlv1alpha1.MysqlClusterSpec{
MysqlVersion: "5.7",
Persistence: mysqlv1alpha1.Persistence{
AccessModes: nil,
StorageClass: &storageClass,
Expand Down
10 changes: 8 additions & 2 deletions mysqlcluster/syncer/mysql_cm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"sort"
"strings"

"github.com/go-ini/ini"
"github.com/presslabs/controller-util/syncer"
Expand Down Expand Up @@ -73,8 +74,13 @@ func buildMysqlConf(c *mysqlcluster.MysqlCluster) (string, error) {
sec := cfg.Section("mysqld")

c.EnsureMysqlConf()

switch c.Spec.MysqlVersion {
mysqlVersion := ""
if strings.Contains(c.Spec.MysqlOpts.Image, "8.0") {
mysqlVersion = "8.0"
} else if strings.Contains(c.Spec.MysqlOpts.Image, "5.7") {
mysqlVersion = "5.7"
}
switch mysqlVersion {
case "8.0":
addKVConfigsToSection(sec, mysql80Configs)
case "5.7":
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/framework/cluster_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func newCluster(name, ns string, replicas int32) *apiv1alpha1.MysqlCluster {
Namespace: ns,
},
Spec: apiv1alpha1.MysqlClusterSpec{
Replicas: &replicas,
MysqlVersion: TestContext.MysqlVersion,
Replicas: &replicas,
PodPolicy: apiv1alpha1.PodPolicy{
SidecarImage: TestContext.SidecarImagePath,
},
Expand Down
Loading

0 comments on commit 0347d0d

Please sign in to comment.