/
redis_cluster_controller.go
332 lines (294 loc) · 10.5 KB
/
redis_cluster_controller.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package rediscluster
import (
"fmt"
"time"
"github.com/golang/glog"
apps "k8s.io/api/apps/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
eventv1 "k8s.io/client-go/kubernetes/typed/core/v1"
appslisters "k8s.io/client-go/listers/apps/v1beta1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"github.com/anywhy/redis-operator/pkg/apis/redis/v1alpha1"
"github.com/anywhy/redis-operator/pkg/client/clientset/versioned"
informers "github.com/anywhy/redis-operator/pkg/client/informers/externalversions"
listers "github.com/anywhy/redis-operator/pkg/client/listers/redis/v1alpha1"
"github.com/anywhy/redis-operator/pkg/controller"
mm "github.com/anywhy/redis-operator/pkg/manager/member"
"github.com/anywhy/redis-operator/pkg/manager/meta"
)
// controllerKind contains the schema.GroupVersionKind for this controller type.
var controllerKind = v1alpha1.SchemeGroupVersion.WithKind("RedisCluster")
// Controller controls Rediss.
type Controller struct {
// kubernetes client interface
kubeClient kubernetes.Interface
// operator client interface
cli versioned.Interface
// control returns an interface capable of syncing a redis cluster.
// Abstracted out for testing.
control ControlInterface
// rcLister is able to list/get Rediss from a shared informer's store
rcLister listers.RedisClusterLister
// rcListerSynced returns true if the Rediss shared informer has synced at least once
rcListerSynced cache.InformerSynced
// setLister is able to list/get stateful sets from a shared informer's store
setLister appslisters.StatefulSetLister
// setListerSynced returns true if the statefulset shared informer has synced at least once
setListerSynced cache.InformerSynced
// Rediss that need to be synced.
queue workqueue.RateLimitingInterface
}
// NewController creates a Redis controller.
func NewController(
kubeCli kubernetes.Interface,
cli versioned.Interface,
informerFactory informers.SharedInformerFactory,
kubeInformerFactory kubeinformers.SharedInformerFactory,
autoFailover bool,
) *Controller {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(&eventv1.EventSinkImpl{
Interface: eventv1.New(kubeCli.CoreV1().RESTClient()).Events("")})
recorder := eventBroadcaster.NewRecorder(v1alpha1.Scheme, corev1.EventSource{Component: "rediscluster"})
rcInformer := informerFactory.Anywhy().V1alpha1().RedisClusters()
setInformer := kubeInformerFactory.Apps().V1beta1().StatefulSets()
svcInformer := kubeInformerFactory.Core().V1().Services()
pvcInformer := kubeInformerFactory.Core().V1().PersistentVolumeClaims()
pvInformer := kubeInformerFactory.Core().V1().PersistentVolumes()
podInformer := kubeInformerFactory.Core().V1().Pods()
// nodeInformer := kubeInformerFactory.Core().V1().Nodes()
rcControl := controller.NewRealRedisControl(cli, rcInformer.Lister(), recorder)
setControl := controller.NewRealStatefuSetControl(kubeCli, setInformer.Lister(), recorder)
svcControl := controller.NewRealServiceControl(kubeCli, svcInformer.Lister(), recorder)
podControl := controller.NewRealPodControl(kubeCli, podInformer.Lister(), recorder)
pvcControl := controller.NewRealPVCControl(kubeCli, recorder, pvcInformer.Lister())
pvControl := controller.NewRealPVControl(kubeCli, pvcInformer.Lister(), pvInformer.Lister(), recorder)
replicaScaler := mm.NewReplicaScaler(pvcInformer.Lister(), pvcControl)
replicaUpgrader := mm.NewReplicaUpgrader(podControl, podInformer.Lister())
rcc := &Controller{
kubeClient: kubeCli,
cli: cli,
control: NewDefaultRedisControl(
rcControl,
mm.NewReplicaMemberManager(
setControl,
svcControl,
svcInformer.Lister(),
podInformer.Lister(),
podControl,
setInformer.Lister(),
replicaScaler,
replicaUpgrader),
mm.NewSentinelMemberManager(
setControl,
svcControl,
svcInformer.Lister(),
podInformer.Lister(),
podControl,
setInformer.Lister()),
nil, // rediscluster controller
meta.NewReclaimPolicyManager(
pvcInformer.Lister(),
pvInformer.Lister(),
pvControl,
),
meta.NewMetaManager(
pvcInformer.Lister(),
pvcControl,
pvInformer.Lister(),
pvControl,
podInformer.Lister(),
podControl,
),
recorder,
),
queue: workqueue.NewNamedRateLimitingQueue(
workqueue.DefaultControllerRateLimiter(),
"redisclusters",
),
}
rcInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: rcc.enqueueRedis,
UpdateFunc: func(old, cur interface{}) {
rcc.enqueueRedis(cur)
},
DeleteFunc: rcc.enqueueRedis,
})
rcc.rcLister = rcInformer.Lister()
rcc.rcListerSynced = rcInformer.Informer().HasSynced
setInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: rcc.addStatefulSet,
UpdateFunc: func(old, cur interface{}) {
rcc.updateStatefuSet(old, cur)
},
DeleteFunc: rcc.deleteStatefulSet,
})
rcc.setLister = setInformer.Lister()
rcc.setListerSynced = setInformer.Informer().HasSynced
return rcc
}
// Run runs the Redis controller.
func (rcc *Controller) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer rcc.queue.ShutDown()
glog.Info("Starting Redis controller")
defer glog.Info("Shutting down Redis controller")
if !cache.WaitForCacheSync(stopCh, rcc.rcListerSynced, rcc.setListerSynced) {
return
}
for i := 0; i < workers; i++ {
go wait.Until(rcc.worker, time.Second, stopCh)
}
<-stopCh
}
// worker runs a worker goroutine that invokes processNextWorkItem until the the controller's queue is closed
func (rcc *Controller) worker() {
for rcc.processNextWorkItem() {
// revive:disable:empty-block
}
}
// processNextWorkItem dequeues items, processes them, and marks them done. It enforces that the syncHandler is never
// invoked concurrently with the same key.
func (rcc *Controller) processNextWorkItem() bool {
key, quit := rcc.queue.Get()
if quit {
return false
}
defer rcc.queue.Done(key)
if err := rcc.sync(key.(string)); err != nil {
utilruntime.HandleError(fmt.Errorf("Redis: %v, sync failed %v, requeuing", key.(string), err))
rcc.queue.AddRateLimited(key)
} else {
rcc.queue.Forget(key)
}
return true
}
// sync syncs the given Redis.
func (rcc *Controller) sync(key string) error {
startTime := time.Now()
defer func() {
glog.V(4).Infof("Finished syncing Redis %q (%v)", key, time.Since(startTime))
}()
ns, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
rc, err := rcc.rcLister.RedisClusters(ns).Get(name)
if errors.IsNotFound(err) {
glog.Infof("Redis has been deleted %v", key)
return nil
}
if err != nil {
return err
}
return rcc.syncRedis(rc.DeepCopy())
}
func (rcc *Controller) syncRedis(rc *v1alpha1.RedisCluster) error {
return rcc.control.UpdateRedisCluster(rc)
}
// enqueueRedis enqueues the given Redis in the work queue.
func (rcc *Controller) enqueueRedis(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
utilruntime.HandleError(fmt.Errorf("Cound't get key for object %+v: %v", obj, err))
return
}
rcc.queue.Add(key)
}
// addStatefulSet adds the redisbcluster for the statefulset to the sync queue
func (rcc *Controller) addStatefulSet(obj interface{}) {
set := obj.(*apps.StatefulSet)
ns, setName := set.GetNamespace(), set.GetName()
if set.DeletionTimestamp != nil {
// on a restart of the controller manager, it's possible a new statefulset shows up in a state that
// is already pending deletion. Prevent the statefulset from being a creation observation.
rcc.deleteStatefulSet(set)
return
}
// If it has a ControllerRef, that's all that matters.
rc := rcc.resolveRedisFromSet(ns, set)
if rc == nil {
return
}
glog.Infof("StatefuSet %s/%s created, Redis: %s/%s", ns, setName, ns, rc.Name)
rcc.enqueueRedis(rc)
}
// updateStatefuSet adds the Redis for the current and old statefulsets to the sync queue.
func (rcc *Controller) updateStatefuSet(old, cur interface{}) {
curSet := cur.(*apps.StatefulSet)
oldSet := old.(*apps.StatefulSet)
ns, setName := curSet.GetNamespace(), curSet.GetName()
if curSet.ResourceVersion == oldSet.ResourceVersion {
// Periodic resync will send update events for all known statefulsets.
// Two different versions of the same statefulset will always have different RVs.
return
}
// If it has a ControllerRef, that's all that matters.
rc := rcc.resolveRedisFromSet(ns, curSet)
if rc == nil {
return
}
glog.Infof("StatefulSet %s/%s updated, %+v -> %+v.", ns, setName, oldSet.Spec, curSet.Spec)
rcc.enqueueRedis(rc)
}
// deleteStatefulSet enqueues the Redis for the statefulset accounting for deletion tombstones.
func (rcc *Controller) deleteStatefulSet(obj interface{}) {
set, ok := obj.(*apps.StatefulSet)
ns := set.GetNamespace()
setName := set.GetName()
// When a delete is dropped, the relist will notice a statefuset in the store not
// in the list, leading to the insertion of a tombstone object which contains
// the deleted key/value.
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %+v", obj))
return
}
set, ok = tombstone.Obj.(*apps.StatefulSet)
if !ok {
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a statefuset %+v", obj))
return
}
}
// If it has a Redis, that's all that matters.
rc := rcc.resolveRedisFromSet(ns, set)
if rc == nil {
return
}
glog.Infof("StatefulSet %s/%s deleted through %v.", ns, setName, utilruntime.GetCaller())
rcc.enqueueRedis(rc)
}
// resolveRedisFromSet returns the Redis by a StatefulSet,
// or nil if the StatefulSet could not be resolved to a matching Redis
// of the correct Kind.
func (rcc *Controller) resolveRedisFromSet(namespace string, set *apps.StatefulSet) *v1alpha1.RedisCluster {
controllerRef := metav1.GetControllerOf(set)
if controllerRef == nil {
return nil
}
// We can't look up by UID, so look up by Name and then verify UID.
// Don't even try to look up by Name if it's the wrong Kind.
if controllerRef.Kind != controllerKind.Kind {
return nil
}
rc, err := rcc.rcLister.RedisClusters(namespace).Get(controllerRef.Name)
if err != nil {
return nil
}
if rc.UID != controllerRef.UID {
// The controller we found with this Name is not the same one that the
// ControllerRef points to.
return nil
}
return rc
}