Skip to content
This repository was archived by the owner on May 28, 2021. It is now read-only.

Code refactor on options #189

Merged
merged 7 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/mysql-agent/app/mysql_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -49,15 +49,15 @@ 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)
}
}

// 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
Expand Down
4 changes: 2 additions & 2 deletions cmd/mysql-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions cmd/mysql-operator/app/mysql_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
operatoropts "github.com/oracle/mysql-operator/pkg/options/operator"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@owainlewis Pls review the change. Thanks.

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"
Expand All @@ -45,15 +45,15 @@ 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 *operatoropts.MySQLOperatorOpts) func() time.Duration {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@owainlewis Pls review the change. Thanks.

return func() time.Duration {
factor := rand.Float64() + 1
return time.Duration(float64(s.MinResyncPeriod.Nanoseconds()) * factor)
}
}

// Run starts the mysql-operator controllers. This should never exit.
func Run(s *options.MySQLOperatorServer) error {
func Run(s *operatoropts.MySQLOperatorOpts) error {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@owainlewis Pls review the change. Thanks.

kubeconfig, err := clientcmd.BuildConfigFromFlags(s.Master, s.KubeConfig)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/mysql-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/cluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/cluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package options
package agent

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand All @@ -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)
Expand All @@ -87,15 +87,15 @@ 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
}

// 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 {
Expand All @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package options
package operator

import (
"testing"
Expand All @@ -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.")
}
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/statefulsets/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions pkg/resources/statefulsets/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down