/
options.go
126 lines (100 loc) · 3.48 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
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 options
import (
"os"
"strings"
"sync"
"time"
"github.com/spf13/pflag"
"k8s.io/api/core/v1"
"github.com/presslabs/mysql-operator/pkg/util"
)
func EnvDefault(key, def string) string {
value := os.Getenv(key)
if len(value) == 0 {
return def
}
return value
}
type Options struct {
mysqlImage string
MysqlImage string
MysqlImageTag string
HelperImage string
MetricsExporterImage string
ImagePullSecretName string
ImagePullPolicy v1.PullPolicy
OrchestratorUri string
OrchestratorTopologyPassword string
OrchestratorTopologyUser string
JobCompleteSuccessGraceTime time.Duration
}
const (
defaultMysqlImage = "percona:5.7"
defaultExporterImage = "prom/mysqld-exporter:latest"
defaultImagePullPolicy = v1.PullIfNotPresent
defaultOrchestratorTopologyUser = ""
defaultOrchestratorTopologyPassword = ""
)
var (
defaultHelperImage = "quay.io/presslabs/mysql-helper:" + util.AppVersion
defaultJobGraceTime = 24 * time.Hour
)
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, "pull-secret", "",
"The secret name for used as pull secret.")
fs.StringVar(&o.OrchestratorUri, "orchestrator-uri", "",
"The orchestrator uri")
fs.StringVar(&o.OrchestratorTopologyPassword, "orchestrator-topology-password", defaultOrchestratorTopologyUser,
"The orchestrator topology password. Can also be set as ORC_TOPOLOGY_PASSWORD environment variable.")
fs.StringVar(&o.OrchestratorTopologyUser, "orchestrator-topology-user", defaultOrchestratorTopologyPassword,
"The orchestrator topology user. Can also be set as ORC_TOPOLOGY_USER environment variable.")
fs.DurationVar(&o.JobCompleteSuccessGraceTime, "job-grace-time", defaultJobGraceTime,
"The time in hours how jobs after completion are keept.")
}
var instance *Options
var once sync.Once
func GetOptions() *Options {
once.Do(func() {
instance = &Options{
mysqlImage: defaultMysqlImage,
HelperImage: defaultHelperImage,
MetricsExporterImage: defaultExporterImage,
ImagePullPolicy: defaultImagePullPolicy,
JobCompleteSuccessGraceTime: defaultJobGraceTime,
OrchestratorTopologyUser: "",
OrchestratorTopologyPassword: "",
}
})
return instance
}
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 = EnvDefault("ORC_TOPOLOGY_USER", "")
}
if len(o.OrchestratorTopologyPassword) == 0 {
o.OrchestratorTopologyPassword = EnvDefault("ORC_TOPOLOGY_PASSWORD", "")
}
return nil
}