/
cluster_labeler.go
265 lines (229 loc) · 8.93 KB
/
cluster_labeler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2018 Oracle and/or its affiliates. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package labeler
import (
"context"
"fmt"
"time"
"github.com/golang/glog"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
labels "k8s.io/apimachinery/pkg/labels"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
wait "k8s.io/apimachinery/pkg/util/wait"
corev1informers "k8s.io/client-go/informers/core/v1"
kubernetes "k8s.io/client-go/kubernetes"
corev1listers "k8s.io/client-go/listers/core/v1"
cache "k8s.io/client-go/tools/cache"
workqueue "k8s.io/client-go/util/workqueue"
cluster "github.com/oracle/mysql-operator/pkg/cluster"
innodb "github.com/oracle/mysql-operator/pkg/cluster/innodb"
constants "github.com/oracle/mysql-operator/pkg/constants"
clusterctrl "github.com/oracle/mysql-operator/pkg/controllers/cluster"
controllerutils "github.com/oracle/mysql-operator/pkg/controllers/util"
)
const controllerAgentName = "innodb-cluster-labeler"
// ClusterLabelerController adds annotations about the InnoDB cluster state
// to the Cluster's Pods. This controller should only be run iff the the
// local MySQL instance believes that it is the primary of the MySQL cluster.
type ClusterLabelerController struct {
// localInstance represents the local MySQL instance.
localInstance *cluster.Instance
// podLister is able to list/get Pods from a shared informer's store.
podLister corev1listers.PodLister
// podListerSynced returns true if the Pod shared informer has synced at
// least once.
podListerSynced cache.InformerSynced
// podControl enables control of cluster Pods.
podControl clusterctrl.PodControlInterface
queue workqueue.RateLimitingInterface
store cache.Store
}
func keyFunc(obj interface{}) (string, error) {
status, ok := obj.(*innodb.ClusterStatus)
if !ok {
return "", fmt.Errorf("expected *innodb.ClusterStatus got %T", obj)
}
return status.ClusterName, nil
}
// NewClusterLabelerController creates a new ClusterLabelerController.
func NewClusterLabelerController(
localInstance *cluster.Instance,
kubeClient kubernetes.Interface,
podInformer corev1informers.PodInformer,
) *ClusterLabelerController {
controller := &ClusterLabelerController{
localInstance: localInstance,
podLister: podInformer.Lister(),
podListerSynced: podInformer.Informer().HasSynced,
podControl: clusterctrl.NewRealPodControl(kubeClient, podInformer.Lister()),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), controllerAgentName),
store: cache.NewStore(keyFunc),
}
return controller
}
func (clc *ClusterLabelerController) updateClusterRoleLabel(pod *corev1.Pod, val string) error {
new := pod.DeepCopy()
if val == "" {
delete(new.Labels, constants.LabelClusterRole)
} else {
new.Labels[constants.LabelClusterRole] = val
}
return clc.podControl.PatchPod(pod, new)
}
// syncHandler labels the Pods in a Cluster as being either a primary or
// secondary based on the given innodb.ClusterStatus.
func (clc *ClusterLabelerController) syncHandler(key string) error {
obj, exists, err := clc.store.GetByKey(key)
if err != nil {
return errors.Wrapf(err, "getting key %q out of store", key)
}
if !exists {
utilruntime.HandleError(fmt.Errorf("key %q in work queue no longer exists", key))
return nil
}
status := obj.(*innodb.ClusterStatus)
namespace := clc.localInstance.Namespace
clusterName := clc.localInstance.ClusterName
// Get any Pods already labeled as primaries for this cluster.
primaries, err := clc.podLister.Pods(namespace).List(PrimarySelector(clusterName))
if err != nil {
return errors.Wrap(err, "failed to list primaries")
}
// Remove the mysql.oracle.com/role=primary label from any Pods that aren't
// the local primary.
primaryLabeled := false
for _, pod := range primaries {
if pod.Name == clc.localInstance.PodName() {
primaryLabeled = true
continue
}
var role string
if !inCluster(status, pod.Name, clc.localInstance.Port) {
glog.Infof("Removing %q label from previously labeled primary %s/%s",
constants.LabelClusterRole, pod.Namespace, pod.Name)
role = ""
} else {
glog.Infof("Labeling previously labeled primary %s/%s as secondary", pod.Namespace, pod.Name)
role = constants.ClusterRoleSecondary
}
if err := clc.updateClusterRoleLabel(pod, role); err != nil {
return errors.Wrap(err, "relabeling primary")
}
}
// If the local primary is not yet labeled mysql.oracle.com/role=primary
// label it.
if !primaryLabeled {
primary, err := clc.podLister.Pods(namespace).Get(clc.localInstance.PodName())
if err != nil {
return errors.Wrap(err, "failed to get primary Pod")
}
glog.Infof("Labeling %s/%s as primary", primary.Namespace, primary.Name)
if err := clc.updateClusterRoleLabel(primary, constants.ClusterRolePrimary); err != nil {
return errors.Wrapf(err, "labeling %s/%s as primary", primary.Namespace, primary.Name)
}
}
// Get all non-primary Pods.
pods, err := clc.podLister.Pods(namespace).List(NonPrimarySelector(clusterName))
if err != nil {
return errors.Wrap(err, "failed to list non-primary Cluster pods")
}
// Ensure they are labeled as secondary or not at all.
for _, pod := range pods {
if !inCluster(status, pod.Name, clc.localInstance.Port) {
if HasRoleSelector(clusterName).Matches(labels.Set(pod.Labels)) {
glog.Infof("Removing %q label from %s/%s as it's no longer in an ONLINE state",
constants.LabelClusterRole, pod.Namespace, pod.Name)
if err := clc.updateClusterRoleLabel(pod, ""); err != nil {
return errors.Wrapf(err, "removing %q label from %s/%s", constants.LabelClusterRole, pod.Namespace, pod.Name)
}
}
continue
}
if pod.Name != clc.localInstance.PodName() && !SecondarySelector(clusterName).Matches(labels.Set(pod.Labels)) {
glog.Infof("Labeling %s/%s as secondary", pod.Namespace, pod.Name)
if err := clc.updateClusterRoleLabel(pod, constants.ClusterRoleSecondary); err != nil {
return errors.Wrapf(err, "labeling %s/%s as secondary", pod.Namespace, pod.Name)
}
}
}
return nil
}
// processNextWorkItem will read a single work item off the workqueue and
// attempt to process it, by calling the syncHandler.
func (clc *ClusterLabelerController) processNextWorkItem() bool {
obj, shutdown := clc.queue.Get()
if shutdown {
return false
}
// We wrap this block in a func so we can defer clc.queue.Done().
err := func(obj interface{}) error {
defer clc.queue.Done(obj)
key := obj.(string)
if err := clc.syncHandler(key); err != nil {
return errors.Wrapf(err, "error syncing %q", key)
}
clc.queue.Forget(obj)
return nil
}(obj)
if err != nil {
utilruntime.HandleError(fmt.Errorf("Error syncing cluster status: %+v", err))
clc.queue.AddRateLimited(obj)
}
return true
}
// runWorker runs a worker goroutine that invokes processNextWorkItem until the
// controller's queue is closed.
func (clc *ClusterLabelerController) runWorker() {
for clc.processNextWorkItem() {
}
}
// EnqueueClusterStatus takes an *innodb.ClusterStatus, stores it in the
// cache.Store, and then enqueues its key.
func (clc *ClusterLabelerController) EnqueueClusterStatus(obj interface{}) error {
key, err := keyFunc(obj)
if err != nil {
return err
}
if err := clc.store.Add(obj); err != nil {
return errors.Wrap(err, "adding cluster status to store")
}
clc.queue.Add(key)
return nil
}
// Run runs the ClusterLabelerController.
func (clc *ClusterLabelerController) Run(ctx context.Context) {
defer utilruntime.HandleCrash()
defer clc.queue.ShutDown()
glog.Infof("Starting the ClusterLabelerController")
// Wait for the caches to be synced before starting worker
glog.Info("Waiting for ClusterLabelerController informer caches to sync")
if !controllerutils.WaitForCacheSync(controllerAgentName, ctx.Done(), clc.podListerSynced) {
return
}
glog.Info("Starting ClusterLabelerController controller worker")
go wait.Until(clc.runWorker, time.Second, ctx.Done())
glog.Info("Started ClusterLabelerController controller worker")
defer glog.Info("Shutting down ClusterLabelerController controller worker")
<-ctx.Done()
}
// inCluster returns true if an instance is a functioning member of the InnoDB
// cluster.
func inCluster(status *innodb.ClusterStatus, podName string, port int) bool {
statefuSetName, _ := cluster.GetParentNameAndOrdinal(podName)
address := fmt.Sprintf("%s.%s:%d", podName, statefuSetName, port)
inst, ok := status.DefaultReplicaSet.Topology[address]
r := ok && (inst.Status == innodb.InstanceStatusOnline)
return r
}