-
Notifications
You must be signed in to change notification settings - Fork 80
/
controller.go
435 lines (392 loc) · 12.5 KB
/
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package localdiskactioncontroller
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
jsonpatch "github.com/evanphx/json-patch"
"github.com/google/uuid"
"github.com/hwameistor/hwameistor/pkg/apis/hwameistor/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/gobwas/glob"
clientset "github.com/hwameistor/hwameistor/pkg/apis/client/clientset/versioned"
informers "github.com/hwameistor/hwameistor/pkg/apis/client/informers/externalversions/hwameistor/v1alpha1"
listers "github.com/hwameistor/hwameistor/pkg/apis/client/listers/hwameistor/v1alpha1"
"github.com/sirupsen/logrus"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
const (
LatestMatchedLength = 10
DefaultLogKey = "lda-controller"
)
func NewLocalDiskActionController(
clientSet clientset.Interface,
localDiskInformer informers.LocalDiskInformer,
localDiskActionInformer informers.LocalDiskActionInformer) *LocalDiskActionController {
c := &LocalDiskActionController{
clientSet: clientSet,
localDisksLister: localDiskInformer.Lister(),
localDisksSynced: localDiskInformer.Informer().HasSynced,
localDiskActionsLister: localDiskActionInformer.Lister(),
localDiskActionsSynced: localDiskActionInformer.Informer().HasSynced,
localDiskWorkqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "localDisks"),
localDiskActionWorkqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "localDiskActions"),
}
localDiskInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.enqueueLocalDisk,
UpdateFunc: func(old, new interface{}) {
c.enqueueLocalDisk(new)
},
})
localDiskActionInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.enqueueLocalDiskAction,
UpdateFunc: func(old, new interface{}) {
c.enqueueLocalDiskAction(new)
},
})
return c
}
type LocalDiskActionController struct {
clientSet clientset.Interface
localDisksLister listers.LocalDiskLister
localDisksSynced cache.InformerSynced
localDiskActionsLister listers.LocalDiskActionLister
localDiskActionsSynced cache.InformerSynced
localDiskWorkqueue workqueue.RateLimitingInterface
localDiskActionWorkqueue workqueue.RateLimitingInterface
}
func (c *LocalDiskActionController) Run(ctx context.Context) error {
var wg sync.WaitGroup
defer utilruntime.HandleCrash()
defer func() {
if waitTimeout(&wg, 30*time.Second) {
logrus.Info("timeout: processors exit")
} else {
logrus.Info("all processors exit")
}
}()
defer c.localDiskWorkqueue.ShutDown()
defer c.localDiskActionWorkqueue.ShutDown()
logrus.Info("starting localdiskaction controller")
// Wait for the caches to be synced before starting processors
logrus.Info("waiting for informer caches to sync")
if ok := cache.WaitForCacheSync(ctx.Done(), c.localDisksSynced, c.localDiskActionsSynced); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}
wg.Add(2)
go func() {
defer wg.Done()
logrus.Info("start LDA processors")
wait.UntilWithContext(ctx, c.runLDAProcessor, time.Second)
logrus.Info("shutdown LDA processors")
}()
go func() {
defer wg.Done()
logrus.Info("start LD processors")
wait.UntilWithContext(ctx, c.runLDProcessor, time.Second)
logrus.Info("shutdown LD processors")
}()
<-ctx.Done()
return nil
}
func (c *LocalDiskActionController) enqueueLocalDisk(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
logrus.Error(err)
return
}
if ld, ok := obj.(*v1alpha1.LocalDisk); !ok {
logrus.Error("error decoding object, invalid type")
return
} else if ld.Spec.Reserved {
return
}
c.localDiskWorkqueue.Add(key)
}
func (c *LocalDiskActionController) enqueueLocalDiskAction(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
logrus.Error(err)
return
}
c.localDiskActionWorkqueue.Add(key)
}
func (c *LocalDiskActionController) runLDProcessor(ctx context.Context) {
for c.processNextLDWorkItem(ctx) {
}
}
func (c *LocalDiskActionController) runLDAProcessor(ctx context.Context) {
for c.processNextLDAWorkItem(ctx) {
}
}
func (c *LocalDiskActionController) processNextLDWorkItem(ctx context.Context) bool {
obj, shutdown := c.localDiskWorkqueue.Get()
if shutdown {
return false
}
log := logrus.WithFields(logrus.Fields{
"processor": "localdisk",
"traceId": uuid.New().String(),
})
// We wrap this block in a func so we can defer c.workqueue.Done.
err := func(obj interface{}) error {
defer c.localDiskWorkqueue.Done(obj)
var key string
var ok bool
if key, ok = obj.(string); !ok {
// As the item in the workqueue is actually invalid, we call
// Forget here else we'd go into a loop of attempting to
// process a work item that is invalid.
c.localDiskWorkqueue.Forget(obj)
log.Errorf("expected string in workqueue but got %#v", obj)
return nil
}
log = log.WithField("name", key)
ctx = logIntoContext(ctx, log)
if err := c.syncLDHandler(ctx, key); err != nil {
c.localDiskWorkqueue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
}
c.localDiskWorkqueue.Forget(obj)
log.Info("successfully synced")
return nil
}(obj)
if err != nil {
log.Error(err)
return true
}
return true
}
func (c *LocalDiskActionController) processNextLDAWorkItem(ctx context.Context) bool {
obj, shutdown := c.localDiskActionWorkqueue.Get()
if shutdown {
return false
}
log := logrus.WithFields(logrus.Fields{
"processor": "localdiskaction",
"traceId": uuid.New().String(),
})
// We wrap this block in a func so we can defer c.workqueue.Done.
err := func(obj interface{}) error {
defer c.localDiskActionWorkqueue.Done(obj)
var key string
var ok bool
if key, ok = obj.(string); !ok {
// As the item in the workqueue is actually invalid, we call
// Forget here else we'd go into a loop of attempting to
// process a work item that is invalid.
c.localDiskActionWorkqueue.Forget(obj)
log.Errorf("expected string in workqueue but got %#v", obj)
return nil
}
log = log.WithField("name", key)
ctx = logIntoContext(ctx, log)
if err := c.syncLDAHandler(ctx, key); err != nil {
c.localDiskActionWorkqueue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
}
c.localDiskActionWorkqueue.Forget(obj)
log.Info("successfully synced")
return nil
}(obj)
if err != nil {
log.Error(err)
return true
}
return true
}
func (c *LocalDiskActionController) syncLDHandler(ctx context.Context, key string) error {
log := logFromContext(ctx)
log.Info("start sync localdisk")
_, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
log.Errorf("invalid resource key: %s", key)
return nil
}
ld, err := c.localDisksLister.Get(name)
if err != nil {
if errors.IsNotFound(err) {
log.Error("localdisk in work queue no longer exists")
return nil
}
return err
}
ldas, err := c.localDiskActionsLister.List(labels.Everything())
if err != nil {
return err
}
if !ld.Spec.Reserved {
_, err = c.doReserveByLDAs(ctx, ld, ldas)
if err != nil {
return err
}
}
return nil
}
func (c *LocalDiskActionController) syncLDAHandler(ctx context.Context, key string) error {
log := logFromContext(ctx)
log.Info("start sync localdiskaction")
_, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
log.Errorf("invalid resource key: %s", key)
return nil
}
lda, err := c.localDiskActionsLister.Get(name)
if err != nil {
if errors.IsNotFound(err) {
log.Error("localdiskaction in work queue no longer exists")
return nil
}
return err
}
localDisks, err := c.localDisksLister.List(labels.Everything())
if err != nil {
return err
}
if lda.Spec.Action == v1alpha1.LocalDiskActionReserve {
_, err = c.doReserveByLDs(ctx, lda, localDisks)
if err != nil {
return err
}
} else {
return fmt.Errorf("unknown action type")
}
return nil
}
func (c *LocalDiskActionController) doReserveByLDs(ctx context.Context, lda *v1alpha1.LocalDiskAction, localDisks []*v1alpha1.LocalDisk) (*v1alpha1.LocalDiskAction, error) {
log := logFromContext(ctx)
log.WithField("rule", lda.Spec.Rule).Info("do reserve localdisks by localdiskaction")
for _, ld := range localDisks {
if c.shouldFilterLDByRule(ctx, &lda.Spec.Rule, ld) && !ld.Spec.Reserved {
log.WithField("localdiskName", ld.Name).Info("localdisk should be reserved")
_, err := c.patchReserve(ctx, ld)
if err != nil {
return nil, err
}
log.Info("refresh localdiskaction")
lda, err = c.refreshLatestLd(ctx, lda, ld.Name)
if err != nil {
return nil, err
}
}
}
return lda, nil
}
func (c *LocalDiskActionController) doReserveByLDAs(ctx context.Context, ld *v1alpha1.LocalDisk, ldas []*v1alpha1.LocalDiskAction) (*v1alpha1.LocalDisk, error) {
var err error
for _, lda := range ldas {
if lda.Spec.Action == v1alpha1.LocalDiskActionReserve && c.shouldFilterLDByRule(ctx, &lda.Spec.Rule, ld) {
log := logFromContext(ctx).WithField("localdiskactionName", lda.Name)
log.WithField("rule", lda.Spec.Rule).Info("localdisk should be reserved")
ld, err = c.patchReserve(ctx, ld)
if err != nil {
return nil, err
}
log.Info("refresh localdiskaction")
_, err = c.refreshLatestLd(ctx, lda, ld.Name)
if err != nil {
return nil, err
}
// should return if ld has been patched
return ld, nil
}
}
return ld, nil
}
func (c *LocalDiskActionController) shouldFilterLDByRule(ctx context.Context, rule *v1alpha1.LocalDiskActionRule, ld *v1alpha1.LocalDisk) bool {
// should not filter if rule is empty
if rule.MinCapacity == 0 && rule.MaxCapacity == 0 && rule.DevicePath == "" {
return false
}
if rule.MinCapacity != 0 && ld.Spec.Capacity >= rule.MinCapacity {
return false
}
if rule.MaxCapacity != 0 && ld.Spec.Capacity <= rule.MaxCapacity {
return false
}
if rule.DevicePath != "" {
g, err := glob.Compile(rule.DevicePath)
if err != nil {
logFromContext(ctx).WithField("devicePath", rule.DevicePath).Error("can't compile rule DevicePath")
return false
}
if !g.Match(ld.Spec.DevicePath) {
return false
}
}
return true
}
func (c *LocalDiskActionController) patchReserve(ctx context.Context, oldLocalDisk *v1alpha1.LocalDisk) (*v1alpha1.LocalDisk, error) {
if oldLocalDisk.Spec.Reserved {
return oldLocalDisk, nil
}
newLocalDisk := oldLocalDisk.DeepCopy()
newLocalDisk.Spec.Reserved = true
oldData, _ := json.Marshal(oldLocalDisk)
newData, _ := json.Marshal(newLocalDisk)
data, err := jsonpatch.CreateMergePatch(oldData, newData)
if err != nil {
return nil, fmt.Errorf("create patch data err: %s", err)
}
if res, err := c.clientSet.HwameistorV1alpha1().LocalDisks().Patch(ctx, newLocalDisk.Name, types.MergePatchType, data, metav1.PatchOptions{}); err != nil {
return nil, err
} else {
return res, nil
}
}
func (c *LocalDiskActionController) refreshLatestLd(ctx context.Context, oldLda *v1alpha1.LocalDiskAction, ldName string) (*v1alpha1.LocalDiskAction, error) {
// check if ldName has already exist
for _, n := range oldLda.Status.LatestMatchedLds {
if n == ldName {
return oldLda, nil
}
}
newLda := oldLda.DeepCopy()
latestMatchedLds := append([]string{ldName}, newLda.Status.LatestMatchedLds...)
if len(latestMatchedLds) > LatestMatchedLength {
latestMatchedLds = latestMatchedLds[:LatestMatchedLength]
}
newLda.Status.LatestMatchedLds = latestMatchedLds
oldData, _ := json.Marshal(oldLda)
newData, _ := json.Marshal(newLda)
data, err := jsonpatch.CreateMergePatch(oldData, newData)
if err != nil {
return nil, fmt.Errorf("create patch data err: %s", err)
}
if res, err := c.clientSet.HwameistorV1alpha1().LocalDiskActions().Patch(ctx, newLda.Name, types.MergePatchType, data, metav1.PatchOptions{}, "status"); err != nil {
return nil, err
} else {
return res, nil
}
}
func logIntoContext(ctx context.Context, log *logrus.Entry) context.Context {
return context.WithValue(ctx, DefaultLogKey, log)
}
func logFromContext(ctx context.Context) *logrus.Entry {
if v, ok := ctx.Value(DefaultLogKey).(*logrus.Entry); ok {
return v
}
return logrus.WithFields(nil)
}
func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return false // completed normally
case <-time.After(timeout):
return true // timed out
}
}