Skip to content

Commit

Permalink
*: Support MySQL subversion (#647)
Browse files Browse the repository at this point in the history
* *: support MySQL subversion

* webhook:add webhook for prevent update whe  image and mysqlversion are conflict
  • Loading branch information
zhl003 authored Sep 20, 2022
1 parent daa8ce7 commit f5e4077
Show file tree
Hide file tree
Showing 28 changed files with 133 additions and 68 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
ARG GO_PROXY=off
RUN if [ "$GO_PROXY" = "on" ]; then \
go env -w GOPROXY=https://goproxy.cn,direct; \
fi
# 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 mod download

# Copy the go source
Expand Down
6 changes: 4 additions & 2 deletions Dockerfile.sidecar
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum

ARG GO_PROXY=off
RUN if [ "$GO_PROXY" = "on" ]; then \
go env -w GOPROXY=https://goproxy.cn,direct; \
fi
# 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
# go mod download
RUN go mod download

Expand Down
20 changes: 11 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# 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
GO_PORXY ?= off
# 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 +61,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 @@ -70,11 +73,10 @@ run: manifests generate fmt vet ## Run a controller from your host.
go run ./cmd/manager/main.go

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 --build-arg GO_PROXY=${GO_PORXY} -t ${IMG} .
docker build -f Dockerfile.sidecar --build-arg GO_PROXY=${GO_PORXY} -t ${SIDECAR57_IMG} .
docker build -f build/xenon/Dockerfile --build-arg GO_PROXY=${GO_PORXY} -t ${XENON_IMG} .
docker build --build-arg XTRABACKUP_PKG=percona-xtrabackup-80 --build-arg GO_PROXY=${GO_PORXY} -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
16 changes: 15 additions & 1 deletion api/v1alpha1/mysqlcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha1
import (
"fmt"
"net"
"strings"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -70,6 +71,9 @@ func (r *MysqlCluster) ValidateUpdate(old runtime.Object) error {
if err := r.validateLowTableCase(oldCluster); err != nil {
return err
}
if err := r.validateMysqlVersionAndImage(); err != nil {
return err
}
if err := r.validateNFSServerAddress(oldCluster); err != nil {
return err
}
Expand Down Expand Up @@ -117,9 +121,19 @@ 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+"))
}
return nil
}

// Validate MysqlVersion and spec.MysqlOpts.image are conflict.
func (r *MysqlCluster) validateMysqlVersionAndImage() error {
if r.Spec.MysqlOpts.Image != "" && r.Spec.MysqlVersion != "" {
if !strings.Contains(r.Spec.MysqlOpts.Image, r.Spec.MysqlVersion) {
return apierrors.NewForbidden(schema.GroupResource{}, "", fmt.Errorf("spec.MysqlOpts.Image and spec.MysqlVersion are conflict"))
}
}
return nil
}
29 changes: 29 additions & 0 deletions api/v1alpha1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"

admissionv1beta1 "k8s.io/api/admission/v1beta1"
//+kubebuilder:scaffold:imports
Expand Down Expand Up @@ -126,3 +127,31 @@ var _ = AfterSuite(func() {
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})

// test validateMysqlVersionAndImage for webhook
func TestValidateUpdate(t *testing.T) {
{
mysqlcluster := &MysqlCluster{
Spec: MysqlClusterSpec{
MysqlVersion: "8.0",
MysqlOpts: MysqlOpts{
Image: "percona/percona-server:5.7.34",
},
},
}
err := mysqlcluster.validateMysqlVersionAndImage()
assert.Error(t, err)
}
{
mysqlcluster := &MysqlCluster{
Spec: MysqlClusterSpec{
MysqlVersion: "8.0",
MysqlOpts: MysqlOpts{
Image: "percona/percona-server:8.0.25",
},
},
}
err := mysqlcluster.validateMysqlVersionAndImage()
assert.NoError(t, err)
}
}
5 changes: 4 additions & 1 deletion build/mysql/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ WORKDIR /
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
ARG GO_PROXY=off
RUN if [ "$GO_PROXY" = "on" ]; then \
go env -w GOPROXY=https://goproxy.cn,direct; \
fi
# 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
# go mod download
RUN go mod download

Expand Down
6 changes: 5 additions & 1 deletion build/xenon/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
FROM golang:1.17.13 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 GO111MODULE=off;
RUN set -ex; \
mkdir -p /go/src/github.com/radondb; \
cd /go/src/github.com/radondb; \
Expand All @@ -18,6 +18,10 @@ COPY cmd/xenon/main.go cmd/xenon/main.go
COPY utils/ utils/
COPY go.mod go.mod
COPY go.sum go.sum
ARG GO_PROXY=off
RUN if [ "$GO_PROXY" = "on" ]; then \
go env -w GOPROXY=https://goproxy.cn,direct; \
fi
RUN go env -w GO111MODULE=on && go mod download
# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o xenonchecker cmd/xenon/main.go
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
2 changes: 1 addition & 1 deletion config/samples/mysql_v1alpha1_mysqlcluster_mysql8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ metadata:
spec:
replicas: 3
mysqlVersion: "8.0"

# 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
Loading

0 comments on commit f5e4077

Please sign in to comment.