Skip to content

Commit 6b72a73

Browse files
AMeceacalind
authored andcommitted
Small changes to improve the code
1 parent 110283b commit 6b72a73

File tree

6 files changed

+48
-56
lines changed

6 files changed

+48
-56
lines changed

pkg/sidecar/app/configs.go

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ const (
4343

4444
// BaseConfig contains information related with the pod.
4545
type BaseConfig struct {
46-
StopCh <-chan struct{}
46+
// Stop represents the shutdown channel
47+
Stop <-chan struct{}
4748

4849
// Hostname represents the pod hostname
4950
Hostname string
@@ -54,81 +55,79 @@ type BaseConfig struct {
5455
// ServiceName is the name of the headless service
5556
ServiceName string
5657

57-
// NodeRole represents the MySQL role of the node, can be on of: msater, slave
58-
NodeRole NodeRole
5958
// ServerID represents the MySQL server id
6059
ServerID int
6160

6261
// InitBucketURL represents the init bucket to initialize mysql
63-
InitBucketURL *string
62+
InitBucketURL string
6463

6564
// OrchestratorURL is the URL to connect to orchestrator
66-
OrchestratorURL *string
67-
68-
// MasterHost represents the cluster master hostname
69-
MasterHost string
65+
OrchestratorURL string
7066

7167
// backup user and password for http endpoint
7268
BackupUser string
7369
BackupPassword string
7470
}
7571

76-
// GetHostFor returns the pod hostname for given MySQL server id
77-
func (cfg *BaseConfig) GetHostFor(id int) string {
72+
// FQDNForServer returns the pod hostname for given MySQL server id
73+
func (cfg *BaseConfig) FQDNForServer(id int) string {
7874
base := mysqlcluster.GetNameForResource(mysqlcluster.StatefulSet, cfg.ClusterName)
7975
return fmt.Sprintf("%s-%d.%s.%s", base, id-100, cfg.ServiceName, cfg.Namespace)
8076
}
8177

82-
func (cfg *BaseConfig) getOrcClient() orc.Interface {
83-
if cfg.OrchestratorURL == nil {
78+
func (cfg *BaseConfig) newOrcClient() orc.Interface {
79+
if len(cfg.OrchestratorURL) == 0 {
80+
log.Info("OrchestratorURL not set")
8481
return nil
8582
}
8683

87-
return orc.NewFromURI(*cfg.OrchestratorURL)
84+
return orc.NewFromURI(cfg.OrchestratorURL)
8885
}
8986

90-
func (cfg *BaseConfig) getFQClusterName() string {
87+
// ClusterFQDN returns the cluster FQ Name of the cluster from which the node belongs
88+
func (cfg *BaseConfig) ClusterFQDN() string {
9189
return fmt.Sprintf("%s.%s", cfg.ClusterName, cfg.Namespace)
9290
}
9391

94-
func (cfg *BaseConfig) getMasterHost() string {
95-
if client := cfg.getOrcClient(); client != nil {
96-
if master, err := client.Master(cfg.getFQClusterName()); err == nil {
92+
// MasterFQDN the FQ Name of the cluster's master
93+
func (cfg *BaseConfig) MasterFQDN() string {
94+
if client := cfg.newOrcClient(); client != nil {
95+
if master, err := client.Master(cfg.ClusterFQDN()); err == nil {
9796
return master.Key.Hostname
9897
}
9998
}
10099

101100
log.V(-1).Info("failed to obtain master from orchestrator, go for default master",
102-
"master", cfg.GetHostFor(100))
103-
return cfg.GetHostFor(100)
101+
"master", cfg.FQDNForServer(100))
102+
return cfg.FQDNForServer(100)
104103

105104
}
106105

106+
// NodeRole returns the role of the current node
107+
func (cfg *BaseConfig) NodeRole() NodeRole {
108+
if cfg.Hostname == cfg.MasterFQDN() {
109+
return MasterNode
110+
}
111+
112+
return SlaveNode
113+
}
114+
107115
// NewBasicConfig returns a pointer to BaseConfig configured from environment variables
108116
func NewBasicConfig(stop <-chan struct{}) *BaseConfig {
109117
cfg := &BaseConfig{
110-
StopCh: stop,
118+
Stop: stop,
111119
Hostname: getEnvValue("HOSTNAME"),
112120
ClusterName: getEnvValue("MY_CLUSTER_NAME"),
113121
Namespace: getEnvValue("MY_NAMESPACE"),
114122
ServiceName: getEnvValue("MY_SERVICE_NAME"),
115123

116-
InitBucketURL: getEnvP("INIT_BUCKET_URI"),
117-
OrchestratorURL: getEnvP("ORCHESTRATOR_URI"),
124+
InitBucketURL: getEnvValue("INIT_BUCKET_URI"),
125+
OrchestratorURL: getEnvValue("ORCHESTRATOR_URI"),
118126

119127
BackupUser: getEnvValue("MYSQL_BACKUP_USER"),
120128
BackupPassword: getEnvValue("MYSQL_BACKUP_PASSWORD"),
121129
}
122130

123-
// get master host
124-
cfg.MasterHost = cfg.getMasterHost()
125-
126-
// set node role
127-
cfg.NodeRole = SlaveNode
128-
if cfg.Hostname == cfg.MasterHost {
129-
cfg.NodeRole = MasterNode
130-
}
131-
132131
// get server id
133132
ordinal := getOrdinalFromHostname(cfg.Hostname)
134133
cfg.ServerID = ordinal + 100
@@ -145,13 +144,6 @@ func getEnvValue(key string) string {
145144
return value
146145
}
147146

148-
func getEnvP(key string) *string {
149-
if value := getEnvValue(key); len(value) != 0 {
150-
return &value
151-
}
152-
return nil
153-
}
154-
155147
func getOrdinalFromHostname(hn string) int {
156148
// mysql-master-1
157149
// or
@@ -171,7 +163,7 @@ type MysqlConfig struct {
171163
// inherit from base config
172164
BaseConfig
173165

174-
MysqlDSN *string
166+
MysqlDSN string
175167

176168
// replication user and password
177169
ReplicationUser string
@@ -211,24 +203,24 @@ func NewMysqlConfig(cfg *BaseConfig) *MysqlConfig {
211203
}
212204

213205
// getMySQLConnectionString returns the mysql DSN
214-
func getMySQLConnectionString() (*string, error) {
206+
func getMySQLConnectionString() (string, error) {
215207
cnfPath := path.Join(ConfigDir, "client.cnf")
216208
cfg, err := ini.Load(cnfPath)
217209
if err != nil {
218-
return nil, fmt.Errorf("Could not open %s: %s", cnfPath, err)
210+
return "", fmt.Errorf("Could not open %s: %s", cnfPath, err)
219211
}
220212

221213
client := cfg.Section("client")
222214
host := client.Key("host").String()
223215
user := client.Key("user").String()
224216
password := client.Key("password").String()
225217
port, err := client.Key("port").Int()
226-
227218
if err != nil {
228-
return nil, fmt.Errorf("Invalid port in %s: %s", cnfPath, err)
219+
return "", fmt.Errorf("Invalid port in %s: %s", cnfPath, err)
229220
}
221+
230222
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/?timeout=5s&multiStatements=true&interpolateParams=true",
231223
user, password, host, port,
232224
)
233-
return &dsn, nil
225+
return dsn, nil
234226
}

pkg/sidecar/app/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ var log = logf.Log.WithName("sidecar.app")
3333

3434
// RunQuery executes a query
3535
func RunQuery(cfg *MysqlConfig, q string, args ...interface{}) error {
36-
if cfg.MysqlDSN == nil {
36+
if len(cfg.MysqlDSN) == 0 {
3737
log.Info("could not get mysql connection DSN")
3838
return fmt.Errorf("no DSN specified")
3939
}
4040

41-
db, err := sql.Open("mysql", *cfg.MysqlDSN)
41+
db, err := sql.Open("mysql", cfg.MysqlDSN)
4242
if err != nil {
4343
return err
4444
}

pkg/sidecar/appclone/appclone.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ func RunCloneCommand(cfg *app.BaseConfig) error {
4949
return fmt.Errorf("removing lost+found: %s", err)
5050
}
5151

52-
if cfg.NodeRole == app.MasterNode {
53-
if cfg.InitBucketURL == nil {
52+
if cfg.NodeRole() == app.MasterNode {
53+
if len(cfg.InitBucketURL) == 0 {
5454
log.Info("skip cloning init bucket uri is not set.")
5555
// let mysqld initialize data dir
5656
return nil
5757
}
58-
err := cloneFromBucket(*cfg.InitBucketURL)
58+
err := cloneFromBucket(cfg.InitBucketURL)
5959
if err != nil {
6060
return fmt.Errorf("failed to clone from bucket, err: %s", err)
6161
}
6262
} else {
6363
// clonging from prior node
6464
if cfg.ServerID > 100 {
65-
sourceHost := cfg.GetHostFor(cfg.ServerID - 1)
65+
sourceHost := cfg.FQDNForServer(cfg.ServerID - 1)
6666
err := cloneFromSource(cfg, sourceHost)
6767
if err != nil {
6868
return fmt.Errorf("failed to clone from %s, err: %s", sourceHost, err)

pkg/sidecar/appconf/appconf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ const (
3636

3737
// RunConfigCommand generates my.cnf, client.cnf and 10-dynamic.cnf files.
3838
func RunConfigCommand(cfg *app.BaseConfig) error {
39-
log.Info("configuring server", "host", cfg.Hostname, "role", cfg.NodeRole)
39+
log.Info("configuring server", "host", cfg.Hostname, "role", cfg.NodeRole())
4040

4141
if err := app.CopyFile(app.MountConfigDir+"/my.cnf", app.ConfigDir+"/my.cnf"); err != nil {
4242
return fmt.Errorf("copy file my.cnf: %s", err)
4343
}
4444

4545
uPass := pkgutil.RandomString(rStrLen)
46-
reportHost := cfg.GetHostFor(cfg.ServerID)
46+
reportHost := cfg.FQDNForServer(cfg.ServerID)
4747

4848
var dynCFG, utilityCFG, clientCFG *ini.File
4949
var err error

pkg/sidecar/apphelper/apphelper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func waitForMysqlReady(cfg *app.MysqlConfig) error {
157157

158158
func configReadOnly(cfg *app.MysqlConfig) error {
159159
var query string
160-
if cfg.NodeRole == app.MasterNode {
160+
if cfg.NodeRole() == app.MasterNode {
161161
query = "SET GLOBAL READ_ONLY = 0"
162162
} else {
163163
query = "SET GLOBAL SUPER_READ_ONLY = 1"
@@ -169,7 +169,7 @@ func configReadOnly(cfg *app.MysqlConfig) error {
169169
}
170170

171171
func configTopology(cfg *app.MysqlConfig) error {
172-
if cfg.NodeRole == app.SlaveNode {
172+
if cfg.NodeRole() == app.SlaveNode {
173173
log.Info("setting up as slave")
174174
if app.ShouldBootstrapNode() {
175175
log.Info("doing bootstrap")
@@ -192,7 +192,7 @@ func configTopology(cfg *app.MysqlConfig) error {
192192
MASTER_CONNECT_RETRY=?;
193193
`
194194
if err := app.RunQuery(cfg, query,
195-
cfg.MasterHost, cfg.ReplicationUser, cfg.ReplicationPassword, connRetry,
195+
cfg.MasterFQDN(), cfg.ReplicationUser, cfg.ReplicationPassword, connRetry,
196196
); err != nil {
197197
return fmt.Errorf("failed to configure slave node, err: %s", err)
198198
}

pkg/sidecar/apphelper/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func newServer(cfg *app.MysqlConfig) *server {
4848

4949
// Shutdown gracefully the http server
5050
go func() {
51-
<-cfg.StopCh // wait for stop signal
51+
<-cfg.Stop // wait for stop signal
5252
if err := srv.Shutdown(context.Background()); err != nil {
5353
log.Error(err, "failed to stop http server")
5454

0 commit comments

Comments
 (0)