Skip to content

Commit 099ca7d

Browse files
committed
Use CLUSTER_NAME.NAMESPACE as orchestrator cluster alias
1 parent f444261 commit 099ca7d

File tree

6 files changed

+70
-20
lines changed

6 files changed

+70
-20
lines changed

cmd/mysql-helper/util/util.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,22 @@ func GetHostname() string {
8282
}
8383

8484
func GetClusterName() string {
85-
hn := GetHostname()
86-
l := strings.Split(hn, "-")
85+
return getEnvValue("MY_CLUSTER_NAME")
86+
}
8787

88-
return strings.Join(l[:len(l)-2], "-")
88+
func GetNamespace() string {
89+
return getEnvValue("MY_NAMESPACE")
90+
}
91+
92+
func GetServiceName() string {
93+
return getEnvValue("MY_SERVICE_NAME")
8994
}
9095

9196
func NodeRole() string {
9297
if GetMasterHost() == GetHostFor(GetServerId()) {
9398
return "master"
9499
}
95100
return "slave"
96-
97101
}
98102

99103
func getOrdinal() int {
@@ -117,9 +121,10 @@ func GetServerId() int {
117121

118122
// GetHostFor returns the host for given server id
119123
func GetHostFor(id int) string {
120-
base := fmt.Sprintf("%s-%s", GetClusterName(), NameOfStatefulSet)
121-
govSVC := getEnvValue("TITANIUM_HEADLESS_SERVICE")
122-
return fmt.Sprintf("%s-%d.%s", base, id-100, govSVC)
124+
base := api.GetNameForResource(NameOfStatefulSet, GetClusterName())
125+
govSVC := GetServiceName()
126+
namespace := GetNamespace()
127+
return fmt.Sprintf("%s-%d.%s.%s", base, id-100, govSVC, namespace)
123128
}
124129

125130
func getEnvValue(key string) string {
@@ -169,8 +174,10 @@ func GetMasterHost() string {
169174
return GetHostFor(100)
170175
}
171176

177+
fqClusterName := fmt.Sprintf("%s.%s", GetClusterName(), GetNamespace())
178+
172179
client := orc.NewFromUri(orcUri)
173-
inst, err := client.Master(GetClusterName())
180+
inst, err := client.Master(fqClusterName)
174181
if err != nil {
175182
glog.Errorf("Failed to connect to orc for finding master, err: %s."+
176183
" Fallback to determine master by its id.", err)

hack/charts/mysql-operator/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ orchestrator:
3737
HostnameResolveMethod: "none"
3838
MySQLHostnameResolveMethod: "@@report_host"
3939
RemoveTextFromHostnameDisplay: ":3306"
40-
DetectClusterAliasQuery: "SELECT SUBSTRING(@@hostname, 1, LENGTH(@@hostname) - 1 - LENGTH(SUBSTRING_INDEX(@@hostname,'-',-2)))"
40+
DetectClusterAliasQuery: "SELECT CONCAT(SUBSTRING(@@hostname, 1, LENGTH(@@hostname) - 1 - LENGTH(SUBSTRING_INDEX(@@hostname,'-',-2))),'.',SUBSTRING_INDEX(@@report_host,'.',-1))"
4141
DetectInstanceAliasQuery: "SELECT @@hostname"
4242

4343
# Automated recovery (this is opt-in, so we need to set these)

pkg/apis/mysql/v1alpha1/cluster.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,10 @@ const (
232232
)
233233

234234
func (c *MysqlCluster) GetNameForResource(name ResourceName) string {
235-
return getNameForResource(name, c.Name)
235+
return GetNameForResource(name, c.Name)
236236
}
237237

238-
func getNameForResource(name ResourceName, clusterName string) string {
238+
func GetNameForResource(name ResourceName, clusterName string) string {
239239
return fmt.Sprintf("%s-mysql", clusterName)
240240
}
241241

@@ -273,12 +273,13 @@ func (c *MysqlCluster) GetMasterHost() string {
273273
// connect to orc and get the master host of the cluster.
274274
if len(c.Spec.GetOrcUri()) != 0 {
275275
client := orc.NewFromUri(c.Spec.GetOrcUri())
276-
if inst, err := client.Master(c.Name); err == nil {
276+
orcClusterName := fmt.Sprintf("%s.%s", c.Name, c.Namespace)
277+
if inst, err := client.Master(orcClusterName); err == nil {
277278
masterHost = inst.Key.Hostname
278279
} else {
279-
glog.Warning(
280-
"[GetMasterHost]: Failed to connect to orcheatratoro: %s, failback to default",
281-
err,
280+
glog.Warningf(
281+
"Failed getting master for %s: %s, falling back to default.",
282+
orcClusterName, err,
282283
)
283284
}
284285
}

pkg/mysqlcluster/cluster.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const (
6868
statusUpToDate = "up-to-date"
6969
statusCreated = "created"
7070
statusUpdated = "updated"
71-
statusFailed = "faild"
71+
statusFailed = "faild"
7272
statusOk = "ok"
7373
statusSkip = "skip"
7474
)
@@ -154,7 +154,7 @@ func (f *cFactory) Sync(ctx context.Context) error {
154154
for i := 0; i < int(f.cluster.Status.ReadyNodes); i++ {
155155
host := f.getHostForReplica(i)
156156
if err := client.Discover(host, MysqlPort); err != nil {
157-
glog.Infof("Failed to register into orchestrator host: %s", host)
157+
glog.Infof("Failed to register %s with orchestrator: %s", host, err.Error())
158158
}
159159
}
160160
}
@@ -174,6 +174,7 @@ func (f *cFactory) getOwnerReferences(ors ...[]metav1.OwnerReference) []metav1.O
174174
}
175175

176176
func (f *cFactory) getHostForReplica(no int) string {
177-
return fmt.Sprintf("%s-%d.%s", f.cluster.GetNameForResource(api.StatefulSet), no,
178-
f.cluster.GetNameForResource(api.HeadlessSVC))
177+
return fmt.Sprintf("%s-%d.%s.%s", f.cluster.GetNameForResource(api.StatefulSet), no,
178+
f.cluster.GetNameForResource(api.HeadlessSVC),
179+
f.cluster.Namespace)
179180
}

pkg/mysqlcluster/statefullset.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,52 @@ func (f *cFactory) ensureContainer(in core.Container, name, image string, args [
122122
in.ImagePullPolicy = f.cluster.Spec.PodSpec.ImagePullPolicy
123123
in.Args = args
124124
in.EnvFrom = f.getEnvSourcesFor(name)
125+
in.Env = f.getDefaultEnv()
125126
in.VolumeMounts = f.getVolumeMountsFor(name)
126127

127128
return in
128129
}
129130

131+
func (f *cFactory) getDefaultEnv() (env []core.EnvVar) {
132+
env = append(env, core.EnvVar{
133+
Name: "MY_NAMESPACE",
134+
ValueFrom: &core.EnvVarSource{
135+
FieldRef: &core.ObjectFieldSelector{
136+
FieldPath: "metadata.namespace",
137+
},
138+
},
139+
})
140+
env = append(env, core.EnvVar{
141+
Name: "MY_POD_NAME",
142+
ValueFrom: &core.EnvVarSource{
143+
FieldRef: &core.ObjectFieldSelector{
144+
FieldPath: "metadata.name",
145+
},
146+
},
147+
})
148+
env = append(env, core.EnvVar{
149+
Name: "MY_POD_IP",
150+
ValueFrom: &core.EnvVarSource{
151+
FieldRef: &core.ObjectFieldSelector{
152+
FieldPath: "status.podIP",
153+
},
154+
},
155+
})
156+
env = append(env, core.EnvVar{
157+
Name: "MY_SERVICE_NAME",
158+
Value: f.cluster.GetNameForResource(api.HeadlessSVC),
159+
})
160+
env = append(env, core.EnvVar{
161+
Name: "MY_CLUSTER_NAME",
162+
Value: f.cluster.Name,
163+
})
164+
env = append(env, core.EnvVar{
165+
Name: "MY_FQDN",
166+
Value: "$(MY_POD_NAME).$(MY_SERVICE_NAME).$(MY_NAMESPACE)",
167+
})
168+
return
169+
}
170+
130171
func (f *cFactory) ensureInitContainersSpec(in []core.Container) []core.Container {
131172
if len(in) == 0 {
132173
in = make([]core.Container, 2)

pkg/util/constants/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ const (
2727
DefaultBackupPodHTTPPort = 19999
2828

2929
EnvOperatorPodName = "MY_POD_NAME"
30-
EnvOperatorPodNamespace = "MY_POD_NAMESPACE"
30+
EnvOperatorPodNamespace = "MY_NAMESPACE"
3131
EnvRestoreOperatorServiceName = "SERVICE_ADDR"
3232
)

0 commit comments

Comments
 (0)