From 7d01a001f45e1ef1c360973b01c4787295e878b2 Mon Sep 17 00:00:00 2001 From: runkecheng <1131648942@qq.com> Date: Tue, 8 Feb 2022 11:08:50 +0800 Subject: [PATCH] *: Support custom repository address of images. #378 --- backup/syncer/job.go | 3 ++- .../mysql-operator/templates/deployment.yaml | 13 +++++++++- charts/mysql-operator/values.yaml | 1 + mysqlcluster/container/auditlog_test.go | 3 ++- mysqlcluster/container/container.go | 4 ++- mysqlcluster/container/init_mysql_test.go | 3 ++- mysqlcluster/container/init_sidecar_test.go | 3 ++- mysqlcluster/container/metrics_test.go | 3 ++- mysqlcluster/container/mysql_test.go | 3 ++- mysqlcluster/container/slowlog_test.go | 3 ++- mysqlcluster/container/xenon_test.go | 3 ++- mysqlcluster/mysqlcluster.go | 10 +++++++ mysqlcluster/mysqlcluster_test.go | 26 +++++++++++++++++++ 13 files changed, 68 insertions(+), 10 deletions(-) diff --git a/backup/syncer/job.go b/backup/syncer/job.go index 366e1c62..3e85c775 100644 --- a/backup/syncer/job.go +++ b/backup/syncer/job.go @@ -29,6 +29,7 @@ import ( v1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1" "github.com/radondb/radondb-mysql-kubernetes/backup" + "github.com/radondb/radondb-mysql-kubernetes/mysqlcluster" "github.com/radondb/radondb-mysql-kubernetes/utils" ) @@ -114,7 +115,7 @@ func (s *jobSyncer) ensurePodSpec(in corev1.PodSpec) corev1.PodSpec { in.RestartPolicy = corev1.RestartPolicyNever sctName := fmt.Sprintf("%s-secret", s.backup.Spec.ClusterName) in.Containers[0].Name = utils.ContainerBackupName - in.Containers[0].Image = s.backup.Spec.Image + in.Containers[0].Image = fmt.Sprintf("%s%s", mysqlcluster.GetPrefixFromEnv(), s.backup.Spec.Image) in.Containers[0].Args = []string{ "request_a_backup", s.backup.GetBackupURL(s.backup.Spec.ClusterName, s.backup.Spec.HostName), diff --git a/charts/mysql-operator/templates/deployment.yaml b/charts/mysql-operator/templates/deployment.yaml index 8af28112..c876c628 100644 --- a/charts/mysql-operator/templates/deployment.yaml +++ b/charts/mysql-operator/templates/deployment.yaml @@ -24,7 +24,11 @@ spec: containers: {{- if .Values.rbacProxy.create }} - name: kube-rbac-proxy - image: {{ .Values.rbacProxy.image }} + {{- if not .Values.imagePrefix }} + image: "{{ .Values.rbacProxy.image }}" + {{- else }} + image: "{{ .Values.imagePrefix }}/{{ .Values.rbacProxy.image }}" + {{- end }} args: - "--secure-listen-address=0.0.0.0:8443" - "--upstream=http://127.0.0.1:8080/" @@ -41,8 +45,15 @@ spec: - --health-probe-bind-address=:8081 - --metrics-bind-address=127.0.0.1:8080 - --leader-elect + {{- if not .Values.imagePrefix }} image: "{{ .Values.manager.image }}:{{ .Values.manager.tag }}" + {{- else }} + image: "{{ .Values.imagePrefix }}/{{ .Values.manager.image }}:{{ .Values.manager.tag }}" + {{- end }} imagePullPolicy: {{ .Values.imagePullPolicy | quote }} + env: + - name: IMAGE_PREFIX + value: {{ .Values.imagePrefix }} securityContext: allowPrivilegeEscalation: false livenessProbe: diff --git a/charts/mysql-operator/values.yaml b/charts/mysql-operator/values.yaml index 1c8eb1e7..bc86ca46 100644 --- a/charts/mysql-operator/values.yaml +++ b/charts/mysql-operator/values.yaml @@ -12,6 +12,7 @@ installCRDS: true imagePullPolicy: IfNotPresent nameOverride: "" fullnameOverride: "" +imagePrefix: "" manager: image: radondb/mysql-operator diff --git a/mysqlcluster/container/auditlog_test.go b/mysqlcluster/container/auditlog_test.go index f80d6e48..18d3e00b 100644 --- a/mysqlcluster/container/auditlog_test.go +++ b/mysqlcluster/container/auditlog_test.go @@ -17,6 +17,7 @@ limitations under the License. package container import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -56,7 +57,7 @@ func TestGetAuditlogName(t *testing.T) { } func TestGetAuditlogImage(t *testing.T) { - assert.Equal(t, "busybox", auditLogCase.Image) + assert.Equal(t, fmt.Sprintf("%s%s", mysqlcluster.GetPrefixFromEnv(), "busybox"), auditLogCase.Image) } func TestGetAuditlogCommand(t *testing.T) { diff --git a/mysqlcluster/container/container.go b/mysqlcluster/container/container.go index 727f16a3..c940a8ff 100644 --- a/mysqlcluster/container/container.go +++ b/mysqlcluster/container/container.go @@ -17,6 +17,8 @@ limitations under the License. package container import ( + "fmt" + corev1 "k8s.io/api/core/v1" "github.com/radondb/radondb-mysql-kubernetes/mysqlcluster" @@ -61,7 +63,7 @@ func EnsureContainer(name string, c *mysqlcluster.MysqlCluster) corev1.Container return corev1.Container{ Name: ctr.getName(), - Image: ctr.getImage(), + Image: fmt.Sprintf("%s%s", mysqlcluster.GetPrefixFromEnv(), ctr.getImage()), ImagePullPolicy: c.Spec.PodPolicy.ImagePullPolicy, Command: ctr.getCommand(), Env: ctr.getEnvVars(), diff --git a/mysqlcluster/container/init_mysql_test.go b/mysqlcluster/container/init_mysql_test.go index 45b45e27..b855be1e 100644 --- a/mysqlcluster/container/init_mysql_test.go +++ b/mysqlcluster/container/init_mysql_test.go @@ -17,6 +17,7 @@ limitations under the License. package container import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -109,7 +110,7 @@ func TestGetInitMysqlName(t *testing.T) { } func TestGetInitMysqlImage(t *testing.T) { - assert.Equal(t, "percona/percona-server:5.7.34", initMysqlCase.Image) + assert.Equal(t, fmt.Sprintf("%s%s", mysqlcluster.GetPrefixFromEnv(), "percona/percona-server:5.7.34"), initMysqlCase.Image) } func TestGetInitMysqlCommand(t *testing.T) { diff --git a/mysqlcluster/container/init_sidecar_test.go b/mysqlcluster/container/init_sidecar_test.go index 01709f5c..2ea421d4 100644 --- a/mysqlcluster/container/init_sidecar_test.go +++ b/mysqlcluster/container/init_sidecar_test.go @@ -17,6 +17,7 @@ limitations under the License. package container import ( + "fmt" "strconv" "testing" @@ -303,7 +304,7 @@ func TestGetInitSidecarName(t *testing.T) { } func TestGetInitSidecarImage(t *testing.T) { - assert.Equal(t, "sidecar image", initSidecarCase.Image) + assert.Equal(t, fmt.Sprintf("%s%s", mysqlcluster.GetPrefixFromEnv(), "sidecar image"), initSidecarCase.Image) } func TestGetInitSidecarCommand(t *testing.T) { diff --git a/mysqlcluster/container/metrics_test.go b/mysqlcluster/container/metrics_test.go index c1fab3ca..70e93f12 100644 --- a/mysqlcluster/container/metrics_test.go +++ b/mysqlcluster/container/metrics_test.go @@ -17,6 +17,7 @@ limitations under the License. package container import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -56,7 +57,7 @@ func TestGetMetricsName(t *testing.T) { } func TestGetMetricsImage(t *testing.T) { - assert.Equal(t, "metrics-image", metricsCase.Image) + assert.Equal(t, fmt.Sprintf("%s%s", mysqlcluster.GetPrefixFromEnv(), "metrics-image"), metricsCase.Image) } func TestGetMetricsCommand(t *testing.T) { diff --git a/mysqlcluster/container/mysql_test.go b/mysqlcluster/container/mysql_test.go index ba0a8c78..8648dcba 100644 --- a/mysqlcluster/container/mysql_test.go +++ b/mysqlcluster/container/mysql_test.go @@ -17,6 +17,7 @@ limitations under the License. package container import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -53,7 +54,7 @@ func TestGetMysqlName(t *testing.T) { } func TestGetMysqlImage(t *testing.T) { - assert.Equal(t, "percona/percona-server:5.7.34", mysqlCase.Image) + assert.Equal(t, fmt.Sprintf("%s%s", mysqlcluster.GetPrefixFromEnv(), "percona/percona-server:5.7.34"), mysqlCase.Image) } func TestGetMysqlCommand(t *testing.T) { diff --git a/mysqlcluster/container/slowlog_test.go b/mysqlcluster/container/slowlog_test.go index 5ac8ebaf..bd2f9f70 100644 --- a/mysqlcluster/container/slowlog_test.go +++ b/mysqlcluster/container/slowlog_test.go @@ -17,6 +17,7 @@ limitations under the License. package container import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -49,7 +50,7 @@ func TestGetSlowlogName(t *testing.T) { } func TestGetSlowlogImage(t *testing.T) { - assert.Equal(t, "sidecar image", slowlogCase.Image) + assert.Equal(t, fmt.Sprintf("%s%s", mysqlcluster.GetPrefixFromEnv(), "sidecar image"), slowlogCase.Image) } func TestGetSlowlogCommand(t *testing.T) { diff --git a/mysqlcluster/container/xenon_test.go b/mysqlcluster/container/xenon_test.go index ece6c71a..6f5c081f 100644 --- a/mysqlcluster/container/xenon_test.go +++ b/mysqlcluster/container/xenon_test.go @@ -17,6 +17,7 @@ limitations under the License. package container import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -59,7 +60,7 @@ func TestGetXenonName(t *testing.T) { } func TestGetXenonImage(t *testing.T) { - assert.Equal(t, "xenon image", xenonCase.Image) + assert.Equal(t, fmt.Sprintf("%s%s", mysqlcluster.GetPrefixFromEnv(), "xenon image"), xenonCase.Image) } func TestGetXenonCommand(t *testing.T) { diff --git a/mysqlcluster/mysqlcluster.go b/mysqlcluster/mysqlcluster.go index 570bcf44..ddbb7ee0 100644 --- a/mysqlcluster/mysqlcluster.go +++ b/mysqlcluster/mysqlcluster.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "math" + "os" "strconv" "strings" "unicode" @@ -375,3 +376,12 @@ func (c *MysqlCluster) GetKey() client.ObjectKey { Name: c.Name, } } + +// GetPrefixFromEnv get the image prefix from the environment variable. +func GetPrefixFromEnv() string { + prefix := os.Getenv("IMAGE_PREFIX") + if len(prefix) == 0 { + return "" + } + return prefix + "/" +} diff --git a/mysqlcluster/mysqlcluster_test.go b/mysqlcluster/mysqlcluster_test.go index 34eeb414..e6baf956 100644 --- a/mysqlcluster/mysqlcluster_test.go +++ b/mysqlcluster/mysqlcluster_test.go @@ -18,6 +18,7 @@ package mysqlcluster import ( "fmt" + "os" "reflect" "strconv" "testing" @@ -658,3 +659,28 @@ func TestSizeToBytes(t *testing.T) { assert.Equal(t, err, fmt.Errorf("error")) } } + +func TestGetPrefixFromEnv(t *testing.T) { + // Prefix is empty. + { + guard := Patch(os.Getenv, func(key string) string { + return "" + }) + defer guard.Unpatch() + + want := "" + result := GetPrefixFromEnv() + assert.Equal(t, want, result) + } + // Prefix is not empty. + { + guard := Patch(os.Getenv, func(key string) string { + return "docker.io" + }) + defer guard.Unpatch() + + want := "docker.io/" + result := GetPrefixFromEnv() + assert.Equal(t, want, result) + } +}