Skip to content

Commit

Permalink
Add .spec.image on MysqlCluster resource and image map for mysql version
Browse files Browse the repository at this point in the history
  • Loading branch information
AMecea authored and calind committed Dec 11, 2018
1 parent d79f80b commit dcacd6a
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 23 deletions.
2 changes: 2 additions & 0 deletions config/crds/mysql_v1alpha1_mysqlcluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ spec:
type: string
backupUri:
type: string
image:
type: string
initBucketSecretName:
type: string
initBucketURI:
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/mysql/v1alpha1/mysqlcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type MysqlClusterSpec struct {
// Defaults to 0
// +optional
Replicas *int32 `json:"replicas,omitempty"`

// The secret name that contains connection information to initialize database, like
// USER, PASSWORD, ROOT_PASSWORD and so on
// This secret will be updated with DB_CONNECT_URL and some more configs.
Expand All @@ -46,6 +47,11 @@ type MysqlClusterSpec struct {
// +optional
MysqlVersion string `json:"mysqlVersion,omitempty"`

// To specify the image that will be used for mysql server container.
// If this is specified then the mysqlVersion is ignored.
// +optional
Image string `json:"image,omitempty"`

// A bucket URI that contains a xtrabackup to initialize the mysql database.
// +optional
InitBucketURI string `json:"initBucketURI,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (s *sfsSyncer) ensureInitContainersSpec() []core.Container {
func (s *sfsSyncer) ensureContainersSpec() []core.Container {
// MYSQL container
mysql := s.ensureContainer(containerMysqlName,
s.opt.MysqlImage+":"+s.opt.MysqlImageTag,
s.cluster.GetMysqlImage(),
[]string{},
)
mysql.Ports = ensurePorts(core.ContainerPort{
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/mysqlcluster/mysqlcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ func (r *ReconcileMysqlCluster) Reconcile(request reconcile.Request) (reconcile.
// Set defaults on cluster
r.scheme.Default(cluster.Unwrap())
cluster.SetDefaults(r.opt)
if err = cluster.Validate(); err != nil {
return reconcile.Result{}, err
}

status := *cluster.Status.DeepCopy()
defer func() {
Expand Down
9 changes: 4 additions & 5 deletions pkg/internal/mysqlcluster/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ const (
// SetDefaults set defaults from options
// nolint: gocyclo
func (cluster *MysqlCluster) SetDefaults(opt *options.Options) {
// set default mysql version
if len(cluster.Spec.MysqlVersion) == 0 {
cluster.Spec.MysqlVersion = opt.MysqlImageTag
}

// set default image pull policy
if len(cluster.Spec.PodSpec.ImagePullPolicy) == 0 {
cluster.Spec.PodSpec.ImagePullPolicy = opt.ImagePullPolicy
}

if len(cluster.Spec.MysqlVersion) == 0 {
cluster.Spec.MysqlVersion = "5.7"
}

// set pod antiaffinity to nodes stay away from other nodes.
if cluster.Spec.PodSpec.Affinity.PodAntiAffinity == nil {
cluster.Spec.PodSpec.Affinity.PodAntiAffinity = &core.PodAntiAffinity{
Expand Down
17 changes: 17 additions & 0 deletions pkg/internal/mysqlcluster/mysqlcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"k8s.io/apimachinery/pkg/labels"

api "github.com/presslabs/mysql-operator/pkg/apis/mysql/v1alpha1"
"github.com/presslabs/mysql-operator/pkg/util/constants"
)

// MysqlCluster is the wrapper for api.MysqlCluster type
Expand Down Expand Up @@ -114,3 +115,19 @@ func (c *MysqlCluster) GetMasterHost() string {

return masterHost
}

// GetMysqlImage returns the mysql image for current mysql cluster
func (c *MysqlCluster) GetMysqlImage() string {
if len(c.Spec.Image) != 0 {
return c.Spec.Image
}

if len(c.Spec.MysqlVersion) != 0 {
if img, ok := constants.MysqlImageVersions[c.Spec.MysqlVersion]; ok {
return img
}
}

// this means the cluster has a wrong MysqlVersion set
return ""
}
35 changes: 35 additions & 0 deletions pkg/internal/mysqlcluster/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2018 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 (
"fmt"
)

// Validate checks if the cluster spec is validated
func (c *MysqlCluster) Validate() error {
// TODO: this validation should be done in an admission web-hook
if len(c.Spec.SecretName) == 0 {
return fmt.Errorf("spec.secretName is missing")
}

if len(c.GetMysqlImage()) == 0 {
return fmt.Errorf("%s is not a valid MySQL version", c.Spec.MysqlVersion)
}

return nil
}
19 changes: 2 additions & 17 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package options

import (
"os"
"strings"
"sync"
"time"

Expand All @@ -39,11 +38,6 @@ func getFromEnvOrDefault(key, def string) string {

// Options is the data structure that contains information about mysql operator configuration
type Options struct {
mysqlImage string

MysqlImage string
MysqlImageTag string

HelperImage string

MetricsExporterImage string
Expand Down Expand Up @@ -85,10 +79,6 @@ func newPullPolicyValue(defaultValue corev1.PullPolicy, v *corev1.PullPolicy) *p
}

const (
// because of moving percona docker images to centos they broke the mysql
// configuration files.
// TODO: fix this issue
defaultMysqlImage = "percona:5.7-stretch"
defaultExporterImage = "prom/mysqld-exporter:latest"

defaultImagePullPolicy = corev1.PullIfNotPresent
Expand All @@ -109,10 +99,9 @@ var (

// AddFlags registers all mysql-operator needed flags
func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.mysqlImage, "mysql-image", defaultMysqlImage,
"The mysql image.")
fs.StringVar(&o.HelperImage, "helper-image", defaultHelperImage,
"The image that instrumentate mysql.")

fs.StringVar(&o.MetricsExporterImage, "metrics-exporter-image", defaultExporterImage,
"The image for mysql metrics exporter.")
fs.StringVar(&o.ImagePullSecretName, "image-pull-secret", "",
Expand Down Expand Up @@ -146,7 +135,6 @@ var once sync.Once
func GetOptions() *Options {
once.Do(func() {
instance = &Options{
mysqlImage: defaultMysqlImage,
HelperImage: defaultHelperImage,
MetricsExporterImage: defaultExporterImage,

Expand All @@ -165,10 +153,7 @@ func GetOptions() *Options {

// Validate validate the command line values
func (o *Options) Validate() error {
// Update mysql image and tag.
i := strings.Split(o.mysqlImage, ":")
o.MysqlImage = i[0]
o.MysqlImageTag = i[1]

if len(o.OrchestratorTopologyUser) == 0 {
o.OrchestratorTopologyUser = getFromEnvOrDefault("ORC_TOPOLOGY_USER", "")
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ const (
// ConfDPath is the path to extra mysql configs dir
ConfDPath = "/etc/mysql/conf.d"
)

var (
// MysqlImageVersions is a map of supported mysql version and their image
MysqlImageVersions = map[string]string{
// TODO: modify operator to use percona centos images
// percona:5.7-stretch
"5.7": "percona@sha256:c8b69b3c753cb04f1cbf8a4a1f295f51675761ee6368a47808a5205e2d45cfeb",
}
)

0 comments on commit dcacd6a

Please sign in to comment.