|
| 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 | +} |
0 commit comments