@@ -43,7 +43,8 @@ const (
4343
4444// BaseConfig contains information related with the pod.
4545type 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
108116func 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-
155147func 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}
0 commit comments