/
configs.go
224 lines (176 loc) · 5.84 KB
/
configs.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
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 sidecar
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
// add mysql driver
_ "github.com/go-sql-driver/mysql"
"github.com/presslabs/controller-util/rand"
"github.com/presslabs/mysql-operator/pkg/internal/mysqlcluster"
)
// Config contains information related with the pod.
type Config struct {
// Hostname represents the pod hostname
Hostname string
// ClusterName is the MySQL cluster name
ClusterName string
// Namespace represents the namespace where the pod is in
Namespace string
// ServiceName is the name of the headless service
ServiceName string
// InitBucketURL represents the init bucket to initialize mysql
InitBucketURL string
// OperatorUser represents the credentials that the operator will use to connect to the mysql
OperatorUser string
OperatorPassword string
// backup user and password for http endpoint
BackupUser string
BackupPassword string
// replication user and password
ReplicationUser string
ReplicationPassword string
// metrics exporter user and password
MetricsUser string
MetricsPassword string
// orchestrator credentials
OrchestratorUser string
OrchestratorPassword string
// heartbeat credentials
HeartBeatUser string
HeartBeatPassword string
// ExistsMySQLData checks if MySQL data is initialized by checking if the mysql dir exists
ExistsMySQLData bool
// Offset for assigning MySQL Server ID
MyServerIDOffset int
}
// FQDNForServer returns the pod hostname for given MySQL server id
func (cfg *Config) FQDNForServer(id int) string {
base := mysqlcluster.GetNameForResource(mysqlcluster.StatefulSet, cfg.ClusterName)
return fmt.Sprintf("%s-%d.%s.%s", base, id-cfg.MyServerIDOffset, cfg.ServiceName, cfg.Namespace)
}
// ClusterFQDN returns the cluster FQ Name of the cluster from which the node belongs
func (cfg *Config) ClusterFQDN() string {
return fmt.Sprintf("%s.%s", cfg.ClusterName, cfg.Namespace)
}
// MasterFQDN the FQ Name of the cluster's master
func (cfg *Config) MasterFQDN() string {
return mysqlcluster.GetNameForResource(mysqlcluster.MasterService, cfg.ClusterName)
}
// ServerID returns the MySQL server id
func (cfg *Config) ServerID() int {
ordinal := getOrdinalFromHostname(cfg.Hostname)
return ordinal + cfg.MyServerIDOffset
}
// MysqlDSN returns the connection string to MySQL server
func (cfg *Config) MysqlDSN() string {
return fmt.Sprintf("%s:%s@tcp(127.0.0.1:%s)/?timeout=5s&multiStatements=true&interpolateParams=true",
cfg.OperatorUser, cfg.OperatorPassword, mysqlPort,
)
}
// ShouldCloneFromBucket returns true if it's time to initialize from a bucket URL provided
func (cfg *Config) ShouldCloneFromBucket() bool {
return !cfg.ExistsMySQLData && cfg.ServerID() == 100 && len(cfg.InitBucketURL) != 0
}
// NewConfig returns a pointer to Config configured from environment variables
func NewConfig() *Config {
var (
err error
hbPass string
eData bool
offset int
customOffset string
)
if hbPass, err = rand.AlphaNumericString(10); err != nil {
panic(err)
}
if eData, err = checkIfDataExists(); err != nil {
panic(err)
}
offset = MysqlServerIDOffset
customOffset = getEnvValue("MY_SERVER_ID_OFFSET")
if len(customOffset) != 0 {
if offset, err = strconv.Atoi(customOffset); err != nil {
offset = MysqlServerIDOffset
}
}
cfg := &Config{
Hostname: getEnvValue("HOSTNAME"),
ClusterName: getEnvValue("MY_CLUSTER_NAME"),
Namespace: getEnvValue("MY_NAMESPACE"),
ServiceName: getEnvValue("MY_SERVICE_NAME"),
InitBucketURL: getEnvValue("INIT_BUCKET_URI"),
OperatorUser: getEnvValue("OPERATOR_USER"),
OperatorPassword: getEnvValue("OPERATOR_PASSWORD"),
BackupUser: getEnvValue("BACKUP_USER"),
BackupPassword: getEnvValue("BACKUP_PASSWORD"),
ReplicationUser: getEnvValue("REPLICATION_USER"),
ReplicationPassword: getEnvValue("REPLICATION_PASSWORD"),
MetricsUser: getEnvValue("METRICS_EXPORTER_USER"),
MetricsPassword: getEnvValue("METRICS_EXPORTER_PASSWORD"),
OrchestratorUser: getEnvValue("ORC_TOPOLOGY_USER"),
OrchestratorPassword: getEnvValue("ORC_TOPOLOGY_PASSWORD"),
HeartBeatUser: heartBeatUserName,
HeartBeatPassword: hbPass,
ExistsMySQLData: eData,
MyServerIDOffset: offset,
}
return cfg
}
func getEnvValue(key string) string {
value := os.Getenv(key)
if len(value) == 0 {
log.Info("environment is not set", "key", key)
}
return value
}
func getOrdinalFromHostname(hn string) int {
// mysql-master-1
// or
// stateful-ceva-3
l := strings.Split(hn, "-")
for i := len(l) - 1; i >= 0; i-- {
if o, err := strconv.ParseInt(l[i], 10, 8); err == nil {
return int(o)
}
}
return 0
}
// retryLookupHost tries to figure out a host IPs with retries
func retryLookupHost(host string) ([]string, error) {
// try to find the host IP
IPs, err := net.LookupHost(host)
for count := 0; count < 20 && err != nil; count++ {
// retry looking up for ip because first query failed
IPs, err = net.LookupHost(host)
// sleep 100 milliseconds
time.Sleep(100 * time.Millisecond)
}
return IPs, err
}
// nolint: gosec
func checkIfDataExists() (bool, error) {
path := fmt.Sprintf("%s/mysql", dataDir)
_, err := os.Open(path)
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
log.Error(err, "failed to open file", "file", path)
return false, err
}
return true, nil
}