From b076468f78e2f84036a0b60f03076ba24869c236 Mon Sep 17 00:00:00 2001 From: huanwei Date: Sat, 21 Jul 2018 08:29:24 +0800 Subject: [PATCH 1/5] tiny refactor code structure Signed-off-by: huanwei --- cmd/mysql-agent/app/mysql_agent.go | 6 +- cmd/mysql-agent/app/options/options.go | 103 -------------- cmd/mysql-agent/main.go | 4 +- cmd/mysql-operator/app/options/options.go | 129 ------------------ .../app/options/options_test.go | 74 ---------- 5 files changed, 5 insertions(+), 311 deletions(-) delete mode 100644 cmd/mysql-agent/app/options/options.go delete mode 100644 cmd/mysql-operator/app/options/options.go delete mode 100644 cmd/mysql-operator/app/options/options_test.go diff --git a/cmd/mysql-agent/app/mysql_agent.go b/cmd/mysql-agent/app/mysql_agent.go index d240a223b..cf0a791c4 100644 --- a/cmd/mysql-agent/app/mysql_agent.go +++ b/cmd/mysql-agent/app/mysql_agent.go @@ -32,7 +32,7 @@ import ( kubernetes "k8s.io/client-go/kubernetes" rest "k8s.io/client-go/rest" - options "github.com/oracle/mysql-operator/cmd/mysql-agent/app/options" + agentopts "github.com/oracle/mysql-operator/pkg/options/agent" cluster "github.com/oracle/mysql-operator/pkg/cluster" backupcontroller "github.com/oracle/mysql-operator/pkg/controllers/backup" clustermgr "github.com/oracle/mysql-operator/pkg/controllers/cluster/manager" @@ -49,7 +49,7 @@ const ( // resyncPeriod computes the time interval a shared informer waits before // resyncing with the api server. -func resyncPeriod(opts *options.MySQLAgentOpts) func() time.Duration { +func resyncPeriod(opts *agentopts.MySQLAgentOpts) func() time.Duration { return func() time.Duration { factor := rand.Float64() + 1 return time.Duration(float64(opts.MinResyncPeriod.Nanoseconds()) * factor) @@ -57,7 +57,7 @@ func resyncPeriod(opts *options.MySQLAgentOpts) func() time.Duration { } // Run runs the MySQL backup controller. It should never exit. -func Run(opts *options.MySQLAgentOpts) error { +func Run(opts *agentopts.MySQLAgentOpts) error { kubeconfig, err := rest.InClusterConfig() if err != nil { return err diff --git a/cmd/mysql-agent/app/options/options.go b/cmd/mysql-agent/app/options/options.go deleted file mode 100644 index f4c2341e1..000000000 --- a/cmd/mysql-agent/app/options/options.go +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright 2018 Oracle and/or its affiliates. All rights reserved. -// -// 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 options - -import ( - "fmt" - "os" - "time" - - "github.com/golang/glog" - "github.com/spf13/pflag" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -const ( - // DefaultMySQLAgentHeathcheckPort is the port on which the mysql-agent's - // healthcheck service runs on. - DefaultMySQLAgentHeathcheckPort int32 = 10512 -) - -// MySQLAgentOpts holds the configuration options required to -// run the backup controller. -type MySQLAgentOpts struct { - // HealthcheckPort is the port on which the mysql-agent healthcheck http - // service runs on. - HealthcheckPort int32 - - // Address is the IP address to serve the backon. Set to 0.0.0.0 to listen - // on all interfaces. - Address string - - // Namespace is the namespace in which the backup controller (and is - // associated Cluster) are running. - Namespace string - // ClusterName is the name of the Cluster the backup controller - // is responsible for. - ClusterName string - // Hostname of the pod the backup operator is running in. - Hostname string - - // minResyncPeriod is the resync period in reflectors; will be random - // between minResyncPeriod and 2*minResyncPeriod. - MinResyncPeriod metav1.Duration -} - -// NewMySQLAgentOpts instantiates a new default -// MySQLAgentOpts getting values from the env where possible. -func NewMySQLAgentOpts() *MySQLAgentOpts { - hostname, err := os.Hostname() - if err != nil { - glog.Fatalf("Failed to get the hostname: %v", err) - } - namespace := os.Getenv("POD_NAMESPACE") - clusterName := os.Getenv("MYSQL_CLUSTER_NAME") - return &MySQLAgentOpts{ - HealthcheckPort: DefaultMySQLAgentHeathcheckPort, - Address: "0.0.0.0", - Namespace: namespace, - ClusterName: clusterName, - Hostname: hostname, - MinResyncPeriod: metav1.Duration{Duration: 12 * time.Hour}, - } -} - -// AddFlags adds the mysql-agent flags to a given FlagSet. -func (s *MySQLAgentOpts) AddFlags(fs *pflag.FlagSet) *pflag.FlagSet { - fs.Int32Var(&s.HealthcheckPort, "healthcheck-port", s.HealthcheckPort, "The port that the mysql-agent's healthcheck http service runs on.") - fs.StringVar(&s.Address, "address", s.Address, "The IP address to serve the mysql-agent's http service on (set to 0.0.0.0 for all interfaces).") - - fs.StringVar(&s.Namespace, "namespace", s.Namespace, "The namespace to run in. Must be the same namespace as the associated MySQL cluster.") - fs.StringVar(&s.ClusterName, "cluster-name", s.ClusterName, "The name of the MySQL cluster the mysql-agent is responsible for.") - fs.StringVar(&s.Hostname, "hostname", s.Hostname, "The hostname of the pod the mysql-agent is running in.") - fs.DurationVar(&s.MinResyncPeriod.Duration, "min-resync-period", s.MinResyncPeriod.Duration, "The resync period in reflectors will be random between MinResyncPeriod and 2*MinResyncPeriod.") - - return fs -} - -// Validate checks that the required config options have been set. -func (s *MySQLAgentOpts) Validate() error { - if len(s.Namespace) == 0 { - return fmt.Errorf("must set --namespace or $POD_NAMESPACE") - } - if len(s.ClusterName) == 0 { - return fmt.Errorf("must set --cluster-name or $MYSQL_CLUSTER_NAME") - } - if len(s.ClusterName) == 0 { - return fmt.Errorf("failed to detect hostname. Set --hostname") - } - return nil -} diff --git a/cmd/mysql-agent/main.go b/cmd/mysql-agent/main.go index 24700ff1f..d5bafbd45 100644 --- a/cmd/mysql-agent/main.go +++ b/cmd/mysql-agent/main.go @@ -26,14 +26,14 @@ import ( "github.com/spf13/pflag" "github.com/oracle/mysql-operator/cmd/mysql-agent/app" - "github.com/oracle/mysql-operator/cmd/mysql-agent/app/options" + agentopts "github.com/oracle/mysql-operator/pkg/options/agent" "github.com/oracle/mysql-operator/pkg/version" ) func main() { fmt.Fprintf(os.Stderr, "Starting mysql-agent version %s\n", version.GetBuildVersion()) - opts := options.NewMySQLAgentOpts() + opts := agentopts.NewMySQLAgentOpts() opts.AddFlags(pflag.CommandLine) pflag.CommandLine.SetNormalizeFunc(utilflag.WordSepNormalizeFunc) diff --git a/cmd/mysql-operator/app/options/options.go b/cmd/mysql-operator/app/options/options.go deleted file mode 100644 index b8ab5a2aa..000000000 --- a/cmd/mysql-operator/app/options/options.go +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright 2018 Oracle and/or its affiliates. All rights reserved. -// -// 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 options - -import ( - "io/ioutil" - "os" - "path/filepath" - "time" - - "github.com/golang/glog" - "github.com/pkg/errors" - "github.com/spf13/pflag" - "gopkg.in/yaml.v2" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -const ( - mysqlServer = "mysql/mysql-server" - mysqlAgent = "iad.ocir.io/oracle/mysql-agent" -) - -// Images is the configuration of required MySQLOperator images. Remember to configure the appropriate -// credentials for the target repositories. -type Images struct { - MySQLServerImage string `yaml:"mysqlServer"` - MySQLAgentImage string `yaml:"mysqlAgent"` -} - -// MySQLOperatorServer holds the options for the MySQLOperator. -type MySQLOperatorServer struct { - // KubeConfig is the path to a kubeconfig file, specifying how to connect to - // the API server. - KubeConfig string `yaml:"kubeconfig"` - - // Master is the address of the Kubernetes API server (overrides any value - // in kubeconfig). - Master string `yaml:"master"` - - // Namespace is the (optional) namespace in which the MySQL operator will - // manage MySQL Clusters. Defaults to metav1.NamespaceAll. - Namespace string `yaml:"namespace"` - - // Hostname of the pod the operator is running in. - Hostname string `yaml:"hostname"` - - // Images defines the 'mysql-server' and 'mysql-agent' images to use. - Images Images `yaml:"images"` - - // minResyncPeriod is the resync period in reflectors; will be random - // between minResyncPeriod and 2*minResyncPeriod. - MinResyncPeriod metav1.Duration `yaml:"minResyncPeriod"` -} - -// NewMySQLOperatorServer will create a new MySQLOperatorServer. If a valid -// config file is specified and exists, it will be used to initialise the -// server. Otherwise, a default server will be created. -// -// The values specified by either default may later be customised and overidden -// by user specified commandline parameters. -func NewMySQLOperatorServer(filePath string) (*MySQLOperatorServer, error) { - var config MySQLOperatorServer - yamlPath, err := filepath.Abs(filePath) - if err != nil { - return nil, errors.Wrapf(err, "failed to determine MySQLOperator configuration absolute path: '%s'", filePath) - } - if _, err := os.Stat(filePath); err == nil { - yamlFile, err := ioutil.ReadFile(yamlPath) - if err != nil { - return nil, errors.Wrapf(err, "failed to read MySQLOperator configuration: '%s'", filePath) - } - err = yaml.Unmarshal(yamlFile, &config) - if err != nil { - return nil, errors.Wrapf(err, "failed to parse MySQLOperator configuration: '%s'", filePath) - } - } else { - config = MySQLOperatorServer{} - } - config.EnsureDefaults() - return &config, nil -} - -// EnsureDefaults provides a default configuration when required values have -// not been set. -func (s *MySQLOperatorServer) EnsureDefaults() { - if s.Hostname == "" { - hostname, err := os.Hostname() - if err != nil { - glog.Fatalf("Failed to get the hostname: %v", err) - } - s.Hostname = hostname - } - if &s.Images == nil { - s.Images = Images{} - } - if s.Images.MySQLServerImage == "" { - s.Images.MySQLServerImage = mysqlServer - } - if s.Images.MySQLAgentImage == "" { - s.Images.MySQLAgentImage = mysqlAgent - } - if s.MinResyncPeriod.Duration <= 0 { - s.MinResyncPeriod = metav1.Duration{Duration: 12 * time.Hour} - } -} - -// AddFlags adds the mysql-operator flags to a given FlagSet. -func (s *MySQLOperatorServer) AddFlags(fs *pflag.FlagSet) *pflag.FlagSet { - fs.StringVar(&s.KubeConfig, "kubeconfig", s.KubeConfig, "Path to Kubeconfig file with authorization and master location information.") - fs.StringVar(&s.Master, "master", s.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig).") - fs.StringVar(&s.Namespace, "namespace", metav1.NamespaceAll, "The namespace for which the MySQL operator manages MySQL clusters. Defaults to all.") - fs.StringVar(&s.Images.MySQLServerImage, "mysql-server-image", s.Images.MySQLServerImage, "The name of the target 'mysql-server' image. Defaults to: mysql/mysql-server.") - fs.StringVar(&s.Images.MySQLAgentImage, "mysql-agent-image", s.Images.MySQLAgentImage, "The name of the target 'mysql-agent' image. Defaults to: iad.ocir.io/oracle/mysql-agent.") - fs.DurationVar(&s.MinResyncPeriod.Duration, "min-resync-period", s.MinResyncPeriod.Duration, "The resync period in reflectors will be random between MinResyncPeriod and 2*MinResyncPeriod.") - return fs -} diff --git a/cmd/mysql-operator/app/options/options_test.go b/cmd/mysql-operator/app/options/options_test.go deleted file mode 100644 index f4ebb091c..000000000 --- a/cmd/mysql-operator/app/options/options_test.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2018 Oracle and/or its affiliates. All rights reserved. -// -// 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 options - -import ( - "testing" - - "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestEnsureDefaults(t *testing.T) { - server := MySQLOperatorServer{} - server.EnsureDefaults() - assertRequiredDefaults(t, server) -} - -func assertRequiredDefaults(t *testing.T, s MySQLOperatorServer) { - if &s == nil { - t.Error("MySQLOperatorServer: was nil, expected a valid configuration.") - } - if len(s.Hostname) <= 0 { - t.Errorf("MySQLOperatorServer: expected a non-zero length hostname, got '%s'.", s.Hostname) - } - if &s.Images == nil { - t.Error("MySQLOperatorServer.Images: was nil, expected a valid configuration.") - } - if s.Images.MySQLServerImage != mysqlServer { - t.Errorf("MySQLOperatorServer.Images.MySQLServerImage: was '%s', expected '%s'.", s.Images.MySQLServerImage, mysqlServer) - } - if s.Images.MySQLAgentImage != mysqlAgent { - t.Errorf("MySQLOperatorServer.Images.MySQLAgentImage: was '%s', expected '%s'.", s.Images.MySQLAgentImage, mysqlAgent) - } - expectedDuration := v1.Duration{Duration: 43200000000000} - if &s.MinResyncPeriod == nil { - t.Errorf("MySQLOperatorServer.MinResyncPeriod: was nil, expected '%s'.", expectedDuration) - } - if s.MinResyncPeriod != expectedDuration { - t.Errorf("MySQLOperatorServer.MinResyncPeriod: was '%s', expected '%s'.", s.MinResyncPeriod, expectedDuration) - } -} - -func TestEnsureDefaultsOverrideSafety(t *testing.T) { - expected := mockMySQLOperatorServer() - ensured := mockMySQLOperatorServer() - ensured.EnsureDefaults() - if expected != ensured { - t.Errorf("MySQLOperatorServer.EnsureDefaults() should not modify pre-configured values.") - } -} - -func mockMySQLOperatorServer() MySQLOperatorServer { - return MySQLOperatorServer{ - KubeConfig: "some-kube-config", - Master: "some-master", - Hostname: "some-hostname", - Images: Images{ - MySQLServerImage: "some-mysql-img", - MySQLAgentImage: "some-agent-img", - }, - MinResyncPeriod: v1.Duration{Duration: 42}, - } -} From 80a01a964a1df81011ad9b834e34ea4474b2f91a Mon Sep 17 00:00:00 2001 From: huanwei Date: Sat, 21 Jul 2018 08:30:55 +0800 Subject: [PATCH 2/5] tiny refactor code structure Signed-off-by: huanwei --- pkg/options/agent/options.go | 103 +++++++++++++++++++++ pkg/options/operator/options.go | 129 +++++++++++++++++++++++++++ pkg/options/operator/options_test.go | 74 +++++++++++++++ 3 files changed, 306 insertions(+) create mode 100644 pkg/options/agent/options.go create mode 100644 pkg/options/operator/options.go create mode 100644 pkg/options/operator/options_test.go diff --git a/pkg/options/agent/options.go b/pkg/options/agent/options.go new file mode 100644 index 000000000..77ccd7501 --- /dev/null +++ b/pkg/options/agent/options.go @@ -0,0 +1,103 @@ +// Copyright 2018 Oracle and/or its affiliates. All rights reserved. +// +// 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 agent + +import ( + "fmt" + "os" + "time" + + "github.com/golang/glog" + "github.com/spf13/pflag" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +const ( + // DefaultMySQLAgentHeathcheckPort is the port on which the mysql-agent's + // healthcheck service runs on. + DefaultMySQLAgentHeathcheckPort int32 = 10512 +) + +// MySQLAgentOpts holds the configuration options required to +// run the backup controller. +type MySQLAgentOpts struct { + // HealthcheckPort is the port on which the mysql-agent healthcheck http + // service runs on. + HealthcheckPort int32 + + // Address is the IP address to serve the backon. Set to 0.0.0.0 to listen + // on all interfaces. + Address string + + // Namespace is the namespace in which the backup controller (and is + // associated Cluster) are running. + Namespace string + // ClusterName is the name of the Cluster the backup controller + // is responsible for. + ClusterName string + // Hostname of the pod the backup operator is running in. + Hostname string + + // minResyncPeriod is the resync period in reflectors; will be random + // between minResyncPeriod and 2*minResyncPeriod. + MinResyncPeriod metav1.Duration +} + +// NewMySQLAgentOpts instantiates a new default +// MySQLAgentOpts getting values from the env where possible. +func NewMySQLAgentOpts() *MySQLAgentOpts { + hostname, err := os.Hostname() + if err != nil { + glog.Fatalf("Failed to get the hostname: %v", err) + } + namespace := os.Getenv("POD_NAMESPACE") + clusterName := os.Getenv("MYSQL_CLUSTER_NAME") + return &MySQLAgentOpts{ + HealthcheckPort: DefaultMySQLAgentHeathcheckPort, + Address: "0.0.0.0", + Namespace: namespace, + ClusterName: clusterName, + Hostname: hostname, + MinResyncPeriod: metav1.Duration{Duration: 12 * time.Hour}, + } +} + +// AddFlags adds the mysql-agent flags to a given FlagSet. +func (s *MySQLAgentOpts) AddFlags(fs *pflag.FlagSet) *pflag.FlagSet { + fs.Int32Var(&s.HealthcheckPort, "healthcheck-port", s.HealthcheckPort, "The port that the mysql-agent's healthcheck http service runs on.") + fs.StringVar(&s.Address, "address", s.Address, "The IP address to serve the mysql-agent's http service on (set to 0.0.0.0 for all interfaces).") + + fs.StringVar(&s.Namespace, "namespace", s.Namespace, "The namespace to run in. Must be the same namespace as the associated MySQL cluster.") + fs.StringVar(&s.ClusterName, "cluster-name", s.ClusterName, "The name of the MySQL cluster the mysql-agent is responsible for.") + fs.StringVar(&s.Hostname, "hostname", s.Hostname, "The hostname of the pod the mysql-agent is running in.") + fs.DurationVar(&s.MinResyncPeriod.Duration, "min-resync-period", s.MinResyncPeriod.Duration, "The resync period in reflectors will be random between MinResyncPeriod and 2*MinResyncPeriod.") + + return fs +} + +// Validate checks that the required config options have been set. +func (s *MySQLAgentOpts) Validate() error { + if len(s.Namespace) == 0 { + return fmt.Errorf("must set --namespace or $POD_NAMESPACE") + } + if len(s.ClusterName) == 0 { + return fmt.Errorf("must set --cluster-name or $MYSQL_CLUSTER_NAME") + } + if len(s.ClusterName) == 0 { + return fmt.Errorf("failed to detect hostname. Set --hostname") + } + return nil +} diff --git a/pkg/options/operator/options.go b/pkg/options/operator/options.go new file mode 100644 index 000000000..c337dfb8e --- /dev/null +++ b/pkg/options/operator/options.go @@ -0,0 +1,129 @@ +// Copyright 2018 Oracle and/or its affiliates. All rights reserved. +// +// 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 operator + +import ( + "io/ioutil" + "os" + "path/filepath" + "time" + + "github.com/golang/glog" + "github.com/pkg/errors" + "github.com/spf13/pflag" + "gopkg.in/yaml.v2" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +const ( + mysqlServer = "mysql/mysql-server" + mysqlAgent = "iad.ocir.io/oracle/mysql-agent" +) + +// Images is the configuration of required MySQLOperator images. Remember to configure the appropriate +// credentials for the target repositories. +type Images struct { + MySQLServerImage string `yaml:"mysqlServer"` + MySQLAgentImage string `yaml:"mysqlAgent"` +} + +// MySQLOperatorOpts holds the options for the MySQLOperator. +type MySQLOperatorOpts struct { + // KubeConfig is the path to a kubeconfig file, specifying how to connect to + // the API server. + KubeConfig string `yaml:"kubeconfig"` + + // Master is the address of the Kubernetes API server (overrides any value + // in kubeconfig). + Master string `yaml:"master"` + + // Namespace is the (optional) namespace in which the MySQL operator will + // manage MySQL Clusters. Defaults to metav1.NamespaceAll. + Namespace string `yaml:"namespace"` + + // Hostname of the pod the operator is running in. + Hostname string `yaml:"hostname"` + + // Images defines the 'mysql-server' and 'mysql-agent' images to use. + Images Images `yaml:"images"` + + // minResyncPeriod is the resync period in reflectors; will be random + // between minResyncPeriod and 2*minResyncPeriod. + MinResyncPeriod metav1.Duration `yaml:"minResyncPeriod"` +} + +// MySQLOperatorOpts will create a new MySQLOperatorOpts. If a valid +// config file is specified and exists, it will be used to initialise the +// server. Otherwise, a default server will be created. +// +// The values specified by either default may later be customised and overidden +// by user specified commandline parameters. +func NewMySQLOperatorOpts(filePath string) (*MySQLOperatorOpts, error) { + var config MySQLOperatorOpts + yamlPath, err := filepath.Abs(filePath) + if err != nil { + return nil, errors.Wrapf(err, "failed to determine MySQLOperator configuration absolute path: '%s'", filePath) + } + if _, err := os.Stat(filePath); err == nil { + yamlFile, err := ioutil.ReadFile(yamlPath) + if err != nil { + return nil, errors.Wrapf(err, "failed to read MySQLOperator configuration: '%s'", filePath) + } + err = yaml.Unmarshal(yamlFile, &config) + if err != nil { + return nil, errors.Wrapf(err, "failed to parse MySQLOperator configuration: '%s'", filePath) + } + } else { + config = MySQLOperatorOpts{} + } + config.EnsureDefaults() + return &config, nil +} + +// EnsureDefaults provides a default configuration when required values have +// not been set. +func (s *MySQLOperatorOpts) EnsureDefaults() { + if s.Hostname == "" { + hostname, err := os.Hostname() + if err != nil { + glog.Fatalf("Failed to get the hostname: %v", err) + } + s.Hostname = hostname + } + if &s.Images == nil { + s.Images = Images{} + } + if s.Images.MySQLServerImage == "" { + s.Images.MySQLServerImage = mysqlServer + } + if s.Images.MySQLAgentImage == "" { + s.Images.MySQLAgentImage = mysqlAgent + } + if s.MinResyncPeriod.Duration <= 0 { + s.MinResyncPeriod = metav1.Duration{Duration: 12 * time.Hour} + } +} + +// AddFlags adds the mysql-operator flags to a given FlagSet. +func (s *MySQLOperatorOpts) AddFlags(fs *pflag.FlagSet) *pflag.FlagSet { + fs.StringVar(&s.KubeConfig, "kubeconfig", s.KubeConfig, "Path to Kubeconfig file with authorization and master location information.") + fs.StringVar(&s.Master, "master", s.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig).") + fs.StringVar(&s.Namespace, "namespace", metav1.NamespaceAll, "The namespace for which the MySQL operator manages MySQL clusters. Defaults to all.") + fs.StringVar(&s.Images.MySQLServerImage, "mysql-server-image", s.Images.MySQLServerImage, "The name of the target 'mysql-server' image. Defaults to: mysql/mysql-server.") + fs.StringVar(&s.Images.MySQLAgentImage, "mysql-agent-image", s.Images.MySQLAgentImage, "The name of the target 'mysql-agent' image. Defaults to: iad.ocir.io/oracle/mysql-agent.") + fs.DurationVar(&s.MinResyncPeriod.Duration, "min-resync-period", s.MinResyncPeriod.Duration, "The resync period in reflectors will be random between MinResyncPeriod and 2*MinResyncPeriod.") + return fs +} diff --git a/pkg/options/operator/options_test.go b/pkg/options/operator/options_test.go new file mode 100644 index 000000000..95a4b1132 --- /dev/null +++ b/pkg/options/operator/options_test.go @@ -0,0 +1,74 @@ +// Copyright 2018 Oracle and/or its affiliates. All rights reserved. +// +// 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 operator + +import ( + "testing" + + "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestEnsureDefaults(t *testing.T) { + server := MySQLOperatorOpts{} + server.EnsureDefaults() + assertRequiredDefaults(t, server) +} + +func assertRequiredDefaults(t *testing.T, s MySQLOperatorOpts) { + if &s == nil { + t.Error("MySQLOperatorServer: was nil, expected a valid configuration.") + } + if len(s.Hostname) <= 0 { + t.Errorf("MySQLOperatorServer: expected a non-zero length hostname, got '%s'.", s.Hostname) + } + if &s.Images == nil { + t.Error("MySQLOperatorServer.Images: was nil, expected a valid configuration.") + } + if s.Images.MySQLServerImage != mysqlServer { + t.Errorf("MySQLOperatorServer.Images.MySQLServerImage: was '%s', expected '%s'.", s.Images.MySQLServerImage, mysqlServer) + } + if s.Images.MySQLAgentImage != mysqlAgent { + t.Errorf("MySQLOperatorServer.Images.MySQLAgentImage: was '%s', expected '%s'.", s.Images.MySQLAgentImage, mysqlAgent) + } + expectedDuration := v1.Duration{Duration: 43200000000000} + if &s.MinResyncPeriod == nil { + t.Errorf("MySQLOperatorServer.MinResyncPeriod: was nil, expected '%s'.", expectedDuration) + } + if s.MinResyncPeriod != expectedDuration { + t.Errorf("MySQLOperatorServer.MinResyncPeriod: was '%s', expected '%s'.", s.MinResyncPeriod, expectedDuration) + } +} + +func TestEnsureDefaultsOverrideSafety(t *testing.T) { + expected := mockMySQLOperatorOpts() + ensured := mockMySQLOperatorOpts() + ensured.EnsureDefaults() + if expected != ensured { + t.Errorf("MySQLOperatorOpts.EnsureDefaults() should not modify pre-configured values.") + } +} + +func mockMySQLOperatorOpts() MySQLOperatorOpts { + return MySQLOperatorOpts{ + KubeConfig: "some-kube-config", + Master: "some-master", + Hostname: "some-hostname", + Images: Images{ + MySQLServerImage: "some-mysql-img", + MySQLAgentImage: "some-agent-img", + }, + MinResyncPeriod: v1.Duration{Duration: 42}, + } +} From 3324a89158375290f35fad5963f4bbd25febe8f4 Mon Sep 17 00:00:00 2001 From: huanwei Date: Sat, 21 Jul 2018 08:32:06 +0800 Subject: [PATCH 3/5] tiny refactor code structure Signed-off-by: huanwei --- cmd/mysql-operator/app/mysql_operator.go | 6 +++--- cmd/mysql-operator/main.go | 4 ++-- pkg/controllers/cluster/controller.go | 6 +++--- pkg/controllers/cluster/controller_test.go | 6 +++--- pkg/resources/statefulsets/statefulset.go | 4 ++-- pkg/resources/statefulsets/statefulset_test.go | 6 +++--- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/cmd/mysql-operator/app/mysql_operator.go b/cmd/mysql-operator/app/mysql_operator.go index e53d0b1e0..c80708c33 100644 --- a/cmd/mysql-operator/app/mysql_operator.go +++ b/cmd/mysql-operator/app/mysql_operator.go @@ -28,7 +28,7 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" - options "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + opratoropts "github.com/oracle/mysql-operator/pkg/options/operator" backupcontroller "github.com/oracle/mysql-operator/pkg/controllers/backup" backupschedule "github.com/oracle/mysql-operator/pkg/controllers/backup/schedule" cluster "github.com/oracle/mysql-operator/pkg/controllers/cluster" @@ -45,7 +45,7 @@ const ( // resyncPeriod computes the time interval a shared informer waits before // resyncing with the api server. -func resyncPeriod(s *options.MySQLOperatorServer) func() time.Duration { +func resyncPeriod(s *opratoropts.MySQLOperatorOpts) func() time.Duration { return func() time.Duration { factor := rand.Float64() + 1 return time.Duration(float64(s.MinResyncPeriod.Nanoseconds()) * factor) @@ -53,7 +53,7 @@ func resyncPeriod(s *options.MySQLOperatorServer) func() time.Duration { } // Run starts the mysql-operator controllers. This should never exit. -func Run(s *options.MySQLOperatorServer) error { +func Run(s *opratoropts.MySQLOperatorOpts) error { kubeconfig, err := clientcmd.BuildConfigFromFlags(s.Master, s.KubeConfig) if err != nil { return err diff --git a/cmd/mysql-operator/main.go b/cmd/mysql-operator/main.go index 27418588c..e8f4ce7cd 100644 --- a/cmd/mysql-operator/main.go +++ b/cmd/mysql-operator/main.go @@ -26,7 +26,7 @@ import ( "k8s.io/apiserver/pkg/util/logs" "github.com/oracle/mysql-operator/cmd/mysql-operator/app" - "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + operatoropts "github.com/oracle/mysql-operator/pkg/options/operator" "github.com/oracle/mysql-operator/pkg/version" ) @@ -38,7 +38,7 @@ const ( func main() { fmt.Fprintf(os.Stderr, "Starting mysql-operator version '%s'\n", version.GetBuildVersion()) - opts, err := options.NewMySQLOperatorServer(configPath) + opts, err := operatoropts.NewMySQLOperatorOpts(configPath) if err != nil { fmt.Fprintf(os.Stderr, "error reading config: %v\n", err) os.Exit(1) diff --git a/pkg/controllers/cluster/controller.go b/pkg/controllers/cluster/controller.go index b770c6c32..17ca7265b 100644 --- a/pkg/controllers/cluster/controller.go +++ b/pkg/controllers/cluster/controller.go @@ -50,7 +50,7 @@ import ( informersv1alpha1 "github.com/oracle/mysql-operator/pkg/generated/informers/externalversions/mysql/v1alpha1" listersv1alpha1 "github.com/oracle/mysql-operator/pkg/generated/listers/mysql/v1alpha1" - options "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + operatoropts "github.com/oracle/mysql-operator/pkg/options/operator" secrets "github.com/oracle/mysql-operator/pkg/resources/secrets" services "github.com/oracle/mysql-operator/pkg/resources/services" statefulsets "github.com/oracle/mysql-operator/pkg/resources/statefulsets" @@ -80,7 +80,7 @@ const ( // The MySQLController watches the Kubernetes API for changes to MySQL resources type MySQLController struct { // Global MySQLOperator configuration options. - opConfig options.MySQLOperatorServer + opConfig operatoropts.MySQLOperatorOpts kubeClient kubernetes.Interface opClient clientset.Interface @@ -138,7 +138,7 @@ type MySQLController struct { // NewController creates a new MySQLController. func NewController( - opConfig options.MySQLOperatorServer, + opConfig operatoropts.MySQLOperatorOpts, opClient clientset.Interface, kubeClient kubernetes.Interface, clusterInformer informersv1alpha1.ClusterInformer, diff --git a/pkg/controllers/cluster/controller_test.go b/pkg/controllers/cluster/controller_test.go index a4f2b3985..637693d71 100644 --- a/pkg/controllers/cluster/controller_test.go +++ b/pkg/controllers/cluster/controller_test.go @@ -30,7 +30,7 @@ import ( "k8s.io/client-go/kubernetes/fake" cache "k8s.io/client-go/tools/cache" - options "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + operatoropts "github.com/oracle/mysql-operator/pkg/options/operator" "github.com/oracle/mysql-operator/pkg/apis/mysql/v1alpha1" "github.com/oracle/mysql-operator/pkg/constants" "github.com/oracle/mysql-operator/pkg/controllers/util" @@ -42,8 +42,8 @@ import ( buildversion "github.com/oracle/mysql-operator/pkg/version" ) -func mockOperatorConfig() options.MySQLOperatorServer { - opts := options.MySQLOperatorServer{} +func mockOperatorConfig() operatoropts.MySQLOperatorOpts { + opts := operatoropts.MySQLOperatorOpts{} opts.EnsureDefaults() return opts } diff --git a/pkg/resources/statefulsets/statefulset.go b/pkg/resources/statefulsets/statefulset.go index d4f24c767..a67f26806 100644 --- a/pkg/resources/statefulsets/statefulset.go +++ b/pkg/resources/statefulsets/statefulset.go @@ -26,8 +26,8 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/intstr" - agentopts "github.com/oracle/mysql-operator/cmd/mysql-agent/app/options" - operatoropts "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + agentopts "github.com/oracle/mysql-operator/pkg/options/agent" + operatoropts "github.com/oracle/mysql-operator/pkg/options/operator" "github.com/oracle/mysql-operator/pkg/apis/mysql/v1alpha1" "github.com/oracle/mysql-operator/pkg/constants" "github.com/oracle/mysql-operator/pkg/resources/secrets" diff --git a/pkg/resources/statefulsets/statefulset_test.go b/pkg/resources/statefulsets/statefulset_test.go index c16b46b58..4e23150eb 100644 --- a/pkg/resources/statefulsets/statefulset_test.go +++ b/pkg/resources/statefulsets/statefulset_test.go @@ -22,12 +22,12 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - options "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + operatoropts "github.com/oracle/mysql-operator/pkg/options/operator" "github.com/oracle/mysql-operator/pkg/apis/mysql/v1alpha1" ) -func mockOperatorConfig() options.MySQLOperatorServer { - opts := options.MySQLOperatorServer{} +func mockOperatorConfig() operatoropts.MySQLOperatorOpts { + opts := operatoropts.MySQLOperatorOpts{} opts.EnsureDefaults() return opts } From c7e5e53df0e4ad022369e2c40b75fa129ccdcec5 Mon Sep 17 00:00:00 2001 From: huanwei Date: Mon, 23 Jul 2018 14:41:16 +0800 Subject: [PATCH 4/5] refactor on options Signed-off-by: huanwei --- cmd/mysql-agent/app/mysql_agent.go | 6 +++--- cmd/mysql-agent/main.go | 4 ++-- cmd/mysql-operator/app/mysql_operator.go | 6 +++--- cmd/mysql-operator/main.go | 4 ++-- pkg/controllers/cluster/controller.go | 6 +++--- pkg/controllers/cluster/controller_test.go | 6 +++--- .../options => pkg/options/agent}/options.go | 2 +- .../options/operator}/options.go | 18 +++++++++--------- .../options/operator}/options_test.go | 16 ++++++++-------- pkg/resources/statefulsets/statefulset.go | 4 ++-- pkg/resources/statefulsets/statefulset_test.go | 6 +++--- 11 files changed, 39 insertions(+), 39 deletions(-) rename {cmd/mysql-agent/app/options => pkg/options/agent}/options.go (99%) rename {cmd/mysql-operator/app/options => pkg/options/operator}/options.go (90%) rename {cmd/mysql-operator/app/options => pkg/options/operator}/options_test.go (85%) diff --git a/cmd/mysql-agent/app/mysql_agent.go b/cmd/mysql-agent/app/mysql_agent.go index d240a223b..cf0a791c4 100644 --- a/cmd/mysql-agent/app/mysql_agent.go +++ b/cmd/mysql-agent/app/mysql_agent.go @@ -32,7 +32,7 @@ import ( kubernetes "k8s.io/client-go/kubernetes" rest "k8s.io/client-go/rest" - options "github.com/oracle/mysql-operator/cmd/mysql-agent/app/options" + agentopts "github.com/oracle/mysql-operator/pkg/options/agent" cluster "github.com/oracle/mysql-operator/pkg/cluster" backupcontroller "github.com/oracle/mysql-operator/pkg/controllers/backup" clustermgr "github.com/oracle/mysql-operator/pkg/controllers/cluster/manager" @@ -49,7 +49,7 @@ const ( // resyncPeriod computes the time interval a shared informer waits before // resyncing with the api server. -func resyncPeriod(opts *options.MySQLAgentOpts) func() time.Duration { +func resyncPeriod(opts *agentopts.MySQLAgentOpts) func() time.Duration { return func() time.Duration { factor := rand.Float64() + 1 return time.Duration(float64(opts.MinResyncPeriod.Nanoseconds()) * factor) @@ -57,7 +57,7 @@ func resyncPeriod(opts *options.MySQLAgentOpts) func() time.Duration { } // Run runs the MySQL backup controller. It should never exit. -func Run(opts *options.MySQLAgentOpts) error { +func Run(opts *agentopts.MySQLAgentOpts) error { kubeconfig, err := rest.InClusterConfig() if err != nil { return err diff --git a/cmd/mysql-agent/main.go b/cmd/mysql-agent/main.go index 24700ff1f..d5bafbd45 100644 --- a/cmd/mysql-agent/main.go +++ b/cmd/mysql-agent/main.go @@ -26,14 +26,14 @@ import ( "github.com/spf13/pflag" "github.com/oracle/mysql-operator/cmd/mysql-agent/app" - "github.com/oracle/mysql-operator/cmd/mysql-agent/app/options" + agentopts "github.com/oracle/mysql-operator/pkg/options/agent" "github.com/oracle/mysql-operator/pkg/version" ) func main() { fmt.Fprintf(os.Stderr, "Starting mysql-agent version %s\n", version.GetBuildVersion()) - opts := options.NewMySQLAgentOpts() + opts := agentopts.NewMySQLAgentOpts() opts.AddFlags(pflag.CommandLine) pflag.CommandLine.SetNormalizeFunc(utilflag.WordSepNormalizeFunc) diff --git a/cmd/mysql-operator/app/mysql_operator.go b/cmd/mysql-operator/app/mysql_operator.go index e53d0b1e0..c80708c33 100644 --- a/cmd/mysql-operator/app/mysql_operator.go +++ b/cmd/mysql-operator/app/mysql_operator.go @@ -28,7 +28,7 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" - options "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + opratoropts "github.com/oracle/mysql-operator/pkg/options/operator" backupcontroller "github.com/oracle/mysql-operator/pkg/controllers/backup" backupschedule "github.com/oracle/mysql-operator/pkg/controllers/backup/schedule" cluster "github.com/oracle/mysql-operator/pkg/controllers/cluster" @@ -45,7 +45,7 @@ const ( // resyncPeriod computes the time interval a shared informer waits before // resyncing with the api server. -func resyncPeriod(s *options.MySQLOperatorServer) func() time.Duration { +func resyncPeriod(s *opratoropts.MySQLOperatorOpts) func() time.Duration { return func() time.Duration { factor := rand.Float64() + 1 return time.Duration(float64(s.MinResyncPeriod.Nanoseconds()) * factor) @@ -53,7 +53,7 @@ func resyncPeriod(s *options.MySQLOperatorServer) func() time.Duration { } // Run starts the mysql-operator controllers. This should never exit. -func Run(s *options.MySQLOperatorServer) error { +func Run(s *opratoropts.MySQLOperatorOpts) error { kubeconfig, err := clientcmd.BuildConfigFromFlags(s.Master, s.KubeConfig) if err != nil { return err diff --git a/cmd/mysql-operator/main.go b/cmd/mysql-operator/main.go index 27418588c..e8f4ce7cd 100644 --- a/cmd/mysql-operator/main.go +++ b/cmd/mysql-operator/main.go @@ -26,7 +26,7 @@ import ( "k8s.io/apiserver/pkg/util/logs" "github.com/oracle/mysql-operator/cmd/mysql-operator/app" - "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + operatoropts "github.com/oracle/mysql-operator/pkg/options/operator" "github.com/oracle/mysql-operator/pkg/version" ) @@ -38,7 +38,7 @@ const ( func main() { fmt.Fprintf(os.Stderr, "Starting mysql-operator version '%s'\n", version.GetBuildVersion()) - opts, err := options.NewMySQLOperatorServer(configPath) + opts, err := operatoropts.NewMySQLOperatorOpts(configPath) if err != nil { fmt.Fprintf(os.Stderr, "error reading config: %v\n", err) os.Exit(1) diff --git a/pkg/controllers/cluster/controller.go b/pkg/controllers/cluster/controller.go index b770c6c32..17ca7265b 100644 --- a/pkg/controllers/cluster/controller.go +++ b/pkg/controllers/cluster/controller.go @@ -50,7 +50,7 @@ import ( informersv1alpha1 "github.com/oracle/mysql-operator/pkg/generated/informers/externalversions/mysql/v1alpha1" listersv1alpha1 "github.com/oracle/mysql-operator/pkg/generated/listers/mysql/v1alpha1" - options "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + operatoropts "github.com/oracle/mysql-operator/pkg/options/operator" secrets "github.com/oracle/mysql-operator/pkg/resources/secrets" services "github.com/oracle/mysql-operator/pkg/resources/services" statefulsets "github.com/oracle/mysql-operator/pkg/resources/statefulsets" @@ -80,7 +80,7 @@ const ( // The MySQLController watches the Kubernetes API for changes to MySQL resources type MySQLController struct { // Global MySQLOperator configuration options. - opConfig options.MySQLOperatorServer + opConfig operatoropts.MySQLOperatorOpts kubeClient kubernetes.Interface opClient clientset.Interface @@ -138,7 +138,7 @@ type MySQLController struct { // NewController creates a new MySQLController. func NewController( - opConfig options.MySQLOperatorServer, + opConfig operatoropts.MySQLOperatorOpts, opClient clientset.Interface, kubeClient kubernetes.Interface, clusterInformer informersv1alpha1.ClusterInformer, diff --git a/pkg/controllers/cluster/controller_test.go b/pkg/controllers/cluster/controller_test.go index a4f2b3985..637693d71 100644 --- a/pkg/controllers/cluster/controller_test.go +++ b/pkg/controllers/cluster/controller_test.go @@ -30,7 +30,7 @@ import ( "k8s.io/client-go/kubernetes/fake" cache "k8s.io/client-go/tools/cache" - options "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + operatoropts "github.com/oracle/mysql-operator/pkg/options/operator" "github.com/oracle/mysql-operator/pkg/apis/mysql/v1alpha1" "github.com/oracle/mysql-operator/pkg/constants" "github.com/oracle/mysql-operator/pkg/controllers/util" @@ -42,8 +42,8 @@ import ( buildversion "github.com/oracle/mysql-operator/pkg/version" ) -func mockOperatorConfig() options.MySQLOperatorServer { - opts := options.MySQLOperatorServer{} +func mockOperatorConfig() operatoropts.MySQLOperatorOpts { + opts := operatoropts.MySQLOperatorOpts{} opts.EnsureDefaults() return opts } diff --git a/cmd/mysql-agent/app/options/options.go b/pkg/options/agent/options.go similarity index 99% rename from cmd/mysql-agent/app/options/options.go rename to pkg/options/agent/options.go index f4c2341e1..77ccd7501 100644 --- a/cmd/mysql-agent/app/options/options.go +++ b/pkg/options/agent/options.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package options +package agent import ( "fmt" diff --git a/cmd/mysql-operator/app/options/options.go b/pkg/options/operator/options.go similarity index 90% rename from cmd/mysql-operator/app/options/options.go rename to pkg/options/operator/options.go index b8ab5a2aa..c337dfb8e 100644 --- a/cmd/mysql-operator/app/options/options.go +++ b/pkg/options/operator/options.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package options +package operator import ( "io/ioutil" @@ -40,8 +40,8 @@ type Images struct { MySQLAgentImage string `yaml:"mysqlAgent"` } -// MySQLOperatorServer holds the options for the MySQLOperator. -type MySQLOperatorServer struct { +// MySQLOperatorOpts holds the options for the MySQLOperator. +type MySQLOperatorOpts struct { // KubeConfig is the path to a kubeconfig file, specifying how to connect to // the API server. KubeConfig string `yaml:"kubeconfig"` @@ -65,14 +65,14 @@ type MySQLOperatorServer struct { MinResyncPeriod metav1.Duration `yaml:"minResyncPeriod"` } -// NewMySQLOperatorServer will create a new MySQLOperatorServer. If a valid +// MySQLOperatorOpts will create a new MySQLOperatorOpts. If a valid // config file is specified and exists, it will be used to initialise the // server. Otherwise, a default server will be created. // // The values specified by either default may later be customised and overidden // by user specified commandline parameters. -func NewMySQLOperatorServer(filePath string) (*MySQLOperatorServer, error) { - var config MySQLOperatorServer +func NewMySQLOperatorOpts(filePath string) (*MySQLOperatorOpts, error) { + var config MySQLOperatorOpts yamlPath, err := filepath.Abs(filePath) if err != nil { return nil, errors.Wrapf(err, "failed to determine MySQLOperator configuration absolute path: '%s'", filePath) @@ -87,7 +87,7 @@ func NewMySQLOperatorServer(filePath string) (*MySQLOperatorServer, error) { return nil, errors.Wrapf(err, "failed to parse MySQLOperator configuration: '%s'", filePath) } } else { - config = MySQLOperatorServer{} + config = MySQLOperatorOpts{} } config.EnsureDefaults() return &config, nil @@ -95,7 +95,7 @@ func NewMySQLOperatorServer(filePath string) (*MySQLOperatorServer, error) { // EnsureDefaults provides a default configuration when required values have // not been set. -func (s *MySQLOperatorServer) EnsureDefaults() { +func (s *MySQLOperatorOpts) EnsureDefaults() { if s.Hostname == "" { hostname, err := os.Hostname() if err != nil { @@ -118,7 +118,7 @@ func (s *MySQLOperatorServer) EnsureDefaults() { } // AddFlags adds the mysql-operator flags to a given FlagSet. -func (s *MySQLOperatorServer) AddFlags(fs *pflag.FlagSet) *pflag.FlagSet { +func (s *MySQLOperatorOpts) AddFlags(fs *pflag.FlagSet) *pflag.FlagSet { fs.StringVar(&s.KubeConfig, "kubeconfig", s.KubeConfig, "Path to Kubeconfig file with authorization and master location information.") fs.StringVar(&s.Master, "master", s.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig).") fs.StringVar(&s.Namespace, "namespace", metav1.NamespaceAll, "The namespace for which the MySQL operator manages MySQL clusters. Defaults to all.") diff --git a/cmd/mysql-operator/app/options/options_test.go b/pkg/options/operator/options_test.go similarity index 85% rename from cmd/mysql-operator/app/options/options_test.go rename to pkg/options/operator/options_test.go index f4ebb091c..95a4b1132 100644 --- a/cmd/mysql-operator/app/options/options_test.go +++ b/pkg/options/operator/options_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package options +package operator import ( "testing" @@ -21,12 +21,12 @@ import ( ) func TestEnsureDefaults(t *testing.T) { - server := MySQLOperatorServer{} + server := MySQLOperatorOpts{} server.EnsureDefaults() assertRequiredDefaults(t, server) } -func assertRequiredDefaults(t *testing.T, s MySQLOperatorServer) { +func assertRequiredDefaults(t *testing.T, s MySQLOperatorOpts) { if &s == nil { t.Error("MySQLOperatorServer: was nil, expected a valid configuration.") } @@ -52,16 +52,16 @@ func assertRequiredDefaults(t *testing.T, s MySQLOperatorServer) { } func TestEnsureDefaultsOverrideSafety(t *testing.T) { - expected := mockMySQLOperatorServer() - ensured := mockMySQLOperatorServer() + expected := mockMySQLOperatorOpts() + ensured := mockMySQLOperatorOpts() ensured.EnsureDefaults() if expected != ensured { - t.Errorf("MySQLOperatorServer.EnsureDefaults() should not modify pre-configured values.") + t.Errorf("MySQLOperatorOpts.EnsureDefaults() should not modify pre-configured values.") } } -func mockMySQLOperatorServer() MySQLOperatorServer { - return MySQLOperatorServer{ +func mockMySQLOperatorOpts() MySQLOperatorOpts { + return MySQLOperatorOpts{ KubeConfig: "some-kube-config", Master: "some-master", Hostname: "some-hostname", diff --git a/pkg/resources/statefulsets/statefulset.go b/pkg/resources/statefulsets/statefulset.go index d4f24c767..a67f26806 100644 --- a/pkg/resources/statefulsets/statefulset.go +++ b/pkg/resources/statefulsets/statefulset.go @@ -26,8 +26,8 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/intstr" - agentopts "github.com/oracle/mysql-operator/cmd/mysql-agent/app/options" - operatoropts "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + agentopts "github.com/oracle/mysql-operator/pkg/options/agent" + operatoropts "github.com/oracle/mysql-operator/pkg/options/operator" "github.com/oracle/mysql-operator/pkg/apis/mysql/v1alpha1" "github.com/oracle/mysql-operator/pkg/constants" "github.com/oracle/mysql-operator/pkg/resources/secrets" diff --git a/pkg/resources/statefulsets/statefulset_test.go b/pkg/resources/statefulsets/statefulset_test.go index c16b46b58..4e23150eb 100644 --- a/pkg/resources/statefulsets/statefulset_test.go +++ b/pkg/resources/statefulsets/statefulset_test.go @@ -22,12 +22,12 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - options "github.com/oracle/mysql-operator/cmd/mysql-operator/app/options" + operatoropts "github.com/oracle/mysql-operator/pkg/options/operator" "github.com/oracle/mysql-operator/pkg/apis/mysql/v1alpha1" ) -func mockOperatorConfig() options.MySQLOperatorServer { - opts := options.MySQLOperatorServer{} +func mockOperatorConfig() operatoropts.MySQLOperatorOpts { + opts := operatoropts.MySQLOperatorOpts{} opts.EnsureDefaults() return opts } From a5f06fd6912417e52cb72f5f950ac7c07b081cef Mon Sep 17 00:00:00 2001 From: huanwei Date: Wed, 25 Jul 2018 01:01:11 +0800 Subject: [PATCH 5/5] fix typo operatoropts Signed-off-by: huanwei --- cmd/mysql-operator/app/mysql_operator.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/mysql-operator/app/mysql_operator.go b/cmd/mysql-operator/app/mysql_operator.go index c80708c33..6f09d330d 100644 --- a/cmd/mysql-operator/app/mysql_operator.go +++ b/cmd/mysql-operator/app/mysql_operator.go @@ -28,7 +28,7 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" - opratoropts "github.com/oracle/mysql-operator/pkg/options/operator" + operatoropts "github.com/oracle/mysql-operator/pkg/options/operator" backupcontroller "github.com/oracle/mysql-operator/pkg/controllers/backup" backupschedule "github.com/oracle/mysql-operator/pkg/controllers/backup/schedule" cluster "github.com/oracle/mysql-operator/pkg/controllers/cluster" @@ -45,7 +45,7 @@ const ( // resyncPeriod computes the time interval a shared informer waits before // resyncing with the api server. -func resyncPeriod(s *opratoropts.MySQLOperatorOpts) func() time.Duration { +func resyncPeriod(s *operatoropts.MySQLOperatorOpts) func() time.Duration { return func() time.Duration { factor := rand.Float64() + 1 return time.Duration(float64(s.MinResyncPeriod.Nanoseconds()) * factor) @@ -53,7 +53,7 @@ func resyncPeriod(s *opratoropts.MySQLOperatorOpts) func() time.Duration { } // Run starts the mysql-operator controllers. This should never exit. -func Run(s *opratoropts.MySQLOperatorOpts) error { +func Run(s *operatoropts.MySQLOperatorOpts) error { kubeconfig, err := clientcmd.BuildConfigFromFlags(s.Master, s.KubeConfig) if err != nil { return err