Skip to content

Commit ede0217

Browse files
smanpathakAMecea
authored andcommitted
Set Pod labels according to Orc status
1 parent 5aacd27 commit ede0217

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

pkg/mysqlcluster/endpoints.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@ const (
2929
)
3030

3131
func (f *cFactory) updateMasterServiceEndpoints() error {
32-
masterHost := f.cluster.GetPodHostname(0)
33-
34-
for _, ns := range f.cluster.Status.Nodes {
35-
if cond := ns.GetCondition(api.NodeConditionMaster); cond != nil &&
36-
cond.Status == core.ConditionTrue {
37-
masterHost = ns.Name
38-
}
32+
masterHost := f.getMasterHost()
33+
if err := f.updatePodLabels(masterHost); err != nil {
34+
return err
3935
}
4036

4137
return f.addNodesToService(f.cluster.GetNameForResource(api.MasterService), masterHost)

pkg/mysqlcluster/pods.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2018 Pressinfra SRL
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mysqlcluster
18+
19+
import (
20+
"strings"
21+
22+
kcore "github.com/appscode/kutil/core/v1"
23+
"github.com/golang/glog"
24+
core "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
)
27+
28+
// Set K8S labels indicating role pods: Master or Replica
29+
func (f *cFactory) updatePodLabels(masterHost string) error {
30+
for _, ns := range f.cluster.Status.Nodes {
31+
pod, err := getPodForHostname(f.client, f.namespace, f.getLabels(map[string]string{}), ns.Name)
32+
if err != nil {
33+
glog.Errorf("Failed to update pod labels: %s", err)
34+
continue
35+
}
36+
37+
labels := pod.GetLabels()
38+
val, desiredVal := "replica", "replica"
39+
exists := false
40+
val, exists = labels["role"]
41+
42+
if strings.Contains(masterHost, pod.Name) {
43+
desiredVal = "master"
44+
}
45+
46+
if !exists || val != desiredVal {
47+
labels["role"] = desiredVal
48+
glog.Infof("Updating labels for Pod: %s", pod.Name)
49+
50+
meta := metav1.ObjectMeta{
51+
Name: pod.Name,
52+
Labels: labels,
53+
OwnerReferences: pod.GetOwnerReferences(),
54+
Namespace: pod.GetNamespace(),
55+
}
56+
kcore.CreateOrPatchPod(f.client, meta,
57+
func(in *core.Pod) *core.Pod {
58+
in.Labels = labels
59+
return in
60+
})
61+
}
62+
}
63+
64+
return nil
65+
}

pkg/mysqlcluster/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,17 @@ func (f *cFactory) addNodesToService(serviceName string, hosts ...string) error
167167

168168
return err
169169
}
170+
171+
// Returns name of current master host in a cluster
172+
func (f *cFactory) getMasterHost() string {
173+
masterHost := f.cluster.GetPodHostname(0)
174+
175+
for _, ns := range f.cluster.Status.Nodes {
176+
if cond := ns.GetCondition(api.NodeConditionMaster); cond != nil &&
177+
cond.Status == core.ConditionTrue {
178+
masterHost = ns.Name
179+
}
180+
}
181+
182+
return masterHost
183+
}

0 commit comments

Comments
 (0)