diff --git a/pkg/controller/mysqlcluster/internal/syncer/statefullset.go b/pkg/controller/mysqlcluster/internal/syncer/statefullset.go index 2ab7c4c6d..f18e7ae1e 100644 --- a/pkg/controller/mysqlcluster/internal/syncer/statefullset.go +++ b/pkg/controller/mysqlcluster/internal/syncer/statefullset.go @@ -49,7 +49,7 @@ const ( const ( // init containers containerCloneAndInitName = "init" - containerMySQLInit = "mysql-init-only" + containerMySQLInitName = "mysql-init-only" // containers containerSidecarName = "sidecar" @@ -228,7 +228,7 @@ func (s *sfsSyncer) getEnvFor(name string) []core.EnvVar { Name: "DATA_SOURCE_NAME", Value: fmt.Sprintf("$(USER):$(PASSWORD)@(127.0.0.1:%d)/", MysqlPort), }) - case containerMySQLInit: + case containerMySQLInitName: // set MySQL init only flag for init container env = append(env, core.EnvVar{ Name: "MYSQL_INIT_ONLY", @@ -240,7 +240,7 @@ func (s *sfsSyncer) getEnvFor(name string) []core.EnvVar { } // set MySQL root and application credentials - if name == containerMySQLInit || !s.cluster.ShouldHaveInitContainerForMysql() && name == containerMySQLInit { + if name == containerMySQLInitName || !s.cluster.ShouldHaveInitContainerForMysql() && name == containerMySQLInitName { env = append(env, s.envVarFromSecret(sctName, "MYSQL_ROOT_PASSWORD", "ROOT_PASSWORD", false)) env = append(env, s.envVarFromSecret(sctName, "MYSQL_USER", "USER", true)) env = append(env, s.envVarFromSecret(sctName, "MYSQL_PASSWORD", "PASSWORD", true)) @@ -261,7 +261,7 @@ func (s *sfsSyncer) ensureInitContainersSpec() []core.Container { // add init container for MySQL if docker image supports this if s.cluster.ShouldHaveInitContainerForMysql() { - initCs = append(initCs, s.ensureContainer(containerMySQLInit, + initCs = append(initCs, s.ensureContainer(containerMySQLInitName, s.cluster.GetMysqlImage(), []string{}, )) @@ -503,7 +503,7 @@ func (s *sfsSyncer) getVolumeMountsFor(name string) []core.VolumeMount { {Name: dataVolumeName, MountPath: DataVolumeMountPath}, } - case containerMysqlName, containerSidecarName, containerMySQLInit: + case containerMysqlName, containerSidecarName, containerMySQLInitName: return []core.VolumeMount{ {Name: confVolumeName, MountPath: ConfVolumeMountPath}, {Name: dataVolumeName, MountPath: DataVolumeMountPath}, diff --git a/pkg/internal/mysqlcluster/defaults.go b/pkg/internal/mysqlcluster/defaults.go index 936998012..057e45541 100644 --- a/pkg/internal/mysqlcluster/defaults.go +++ b/pkg/internal/mysqlcluster/defaults.go @@ -18,6 +18,7 @@ package mysqlcluster import ( "fmt" + "github.com/presslabs/mysql-operator/pkg/util/constants" core "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -53,6 +54,11 @@ func (cluster *MysqlCluster) SetDefaults(opt *options.Options) { } } + // set mysql version if not set to avoid spamming logs + if len(cluster.Spec.MysqlVersion) == 0 { + cluster.Spec.MysqlVersion = constants.MySQLDefaultVersion.String() + } + // set pod antiaffinity to nodes stay away from other nodes. if cluster.Spec.PodSpec.Affinity == nil { cluster.Spec.PodSpec.Affinity = &core.Affinity{ diff --git a/pkg/internal/mysqlcluster/mysqlcluster.go b/pkg/internal/mysqlcluster/mysqlcluster.go index 759e41822..e7b8b8f4b 100644 --- a/pkg/internal/mysqlcluster/mysqlcluster.go +++ b/pkg/internal/mysqlcluster/mysqlcluster.go @@ -213,5 +213,5 @@ func (c *MysqlCluster) UpdateSpec() { func (c *MysqlCluster) ShouldHaveInitContainerForMysql() bool { expectedRange := semver.MustParseRange(">=5.7.26 <8.0.0 || >=8.0.15") - return strings.HasPrefix("percona", c.GetMysqlImage()) && expectedRange(c.GetMySQLSemVer()) + return strings.HasPrefix(c.GetMysqlImage(), "percona") && expectedRange(c.GetMySQLSemVer()) } diff --git a/pkg/internal/mysqlcluster/mysqlcluster_test.go b/pkg/internal/mysqlcluster/mysqlcluster_test.go new file mode 100644 index 000000000..e308b9504 --- /dev/null +++ b/pkg/internal/mysqlcluster/mysqlcluster_test.go @@ -0,0 +1,77 @@ +/* +Copyright 2019 Pressinfra SRL + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package mysqlcluster + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + . "github.com/onsi/gomega/gstruct" + + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" + + logf "github.com/presslabs/controller-util/log" + api "github.com/presslabs/mysql-operator/pkg/apis/mysql/v1alpha1" + "github.com/presslabs/mysql-operator/pkg/options" + "github.com/presslabs/mysql-operator/pkg/util/constants" +) + +func TestMySQLClusterWrapper(t *testing.T) { + logf.SetLogger(logf.ZapLoggerTo(GinkgoWriter, true)) + + RegisterFailHandler(Fail) + RunSpecs(t, "Sidecar App Suite") +} + +var _ = Describe("Test MySQL cluster wrapper", func() { + var ( + cluster *MysqlCluster + ) + + BeforeEach(func() { + cluster = New(&api.MysqlCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "cl-name", + Namespace: "default", + }, + Spec: api.MysqlClusterSpec{ + SecretName: "sct-name", + MysqlConf: map[string]intstr.IntOrString{}, + }, + }) + // set defaults + api.SetDefaults_MysqlCluster(cluster.Unwrap()) + cluster.SetDefaults(options.GetOptions()) + }) + + It("should have defaults set", func() { + Expect(cluster.GetMySQLSemVer()).To(Equal(constants.MySQLDefaultVersion)) + Expect(cluster.GetMysqlImage()).To(ContainSubstring("percona")) + + Expect(cluster.Spec.PodSpec.Resources.Requests.Memory()).To(PointTo(Equal(resource.MustParse("1Gi")))) + Expect(cluster.Spec.MysqlConf).To(HaveKey(Equal("innodb-buffer-pool-size"))) + Expect(cluster.Spec.MysqlConf).To(HaveKey(Equal("innodb-log-file-size"))) + Expect(cluster.Spec.MysqlConf).NotTo(HaveKey(Equal("max-binlog-size"))) + }) + + It("should use init MySQL container", func() { + Expect(cluster.ShouldHaveInitContainerForMysql()).To(Equal(true)) + }) +})