-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathcontroller.go
683 lines (610 loc) · 26 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
Copyright 2018 The kube-fledged authors.
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 app
import (
"context"
"fmt"
"reflect"
"time"
"github.com/golang/glog"
v1alpha2 "github.com/senthilrch/kube-fledged/pkg/apis/kubefledged/v1alpha2"
clientset "github.com/senthilrch/kube-fledged/pkg/client/clientset/versioned"
fledgedscheme "github.com/senthilrch/kube-fledged/pkg/client/clientset/versioned/scheme"
informers "github.com/senthilrch/kube-fledged/pkg/client/informers/externalversions/kubefledged/v1alpha2"
listers "github.com/senthilrch/kube-fledged/pkg/client/listers/kubefledged/v1alpha2"
"github.com/senthilrch/kube-fledged/pkg/images"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
)
const controllerAgentName = "kubefledged-controller"
const imageCachePurgeAnnotationKey = "kubefledged.io/purge-imagecache"
const imageCacheRefreshAnnotationKey = "kubefledged.io/refresh-imagecache"
const (
// SuccessSynced is used as part of the Event 'reason' when a ImageCache is synced
SuccessSynced = "Synced"
// MessageResourceSynced is the message used for an Event fired when a ImageCache
// is synced successfully
MessageResourceSynced = "ImageCache synced successfully"
)
// Controller is the controller for ImageCache resources
type Controller struct {
// kubeclientset is a standard kubernetes clientset
kubeclientset kubernetes.Interface
// kubefledgedclientset is a clientset for kubefledged.io API group
kubefledgedclientset clientset.Interface
fledgedNameSpace string
nodesLister corelisters.NodeLister
nodesSynced cache.InformerSynced
imageCachesLister listers.ImageCacheLister
imageCachesSynced cache.InformerSynced
// workqueue is a rate limited work queue. This is used to queue work to be
// processed instead of performing it as soon as a change happens. This
// means we can ensure we only process a fixed amount of resources at a
// time, and makes it easy to ensure we are never processing the same item
// simultaneously in two different workers.
workqueue workqueue.RateLimitingInterface
imageworkqueue workqueue.RateLimitingInterface
imageManager *images.ImageManager
// recorder is an event recorder for recording Event resources to the
// Kubernetes API.
recorder record.EventRecorder
imageCacheRefreshFrequency time.Duration
}
// NewController returns a new fledged controller
func NewController(
kubeclientset kubernetes.Interface,
kubefledgedclientset clientset.Interface,
namespace string,
nodeInformer coreinformers.NodeInformer,
imageCacheInformer informers.ImageCacheInformer,
imageCacheRefreshFrequency time.Duration,
imagePullDeadlineDuration time.Duration,
criClientImage string,
busyboxImage string,
imagePullPolicy string,
serviceAccountName string,
imageDeleteJobHostNetwork bool,
jobPriorityClassName string,
canDeleteJob bool,
criSocketPath string) *Controller {
runtime.Must(fledgedscheme.AddToScheme(scheme.Scheme))
glog.V(4).Info("Creating event broadcaster")
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeclientset.CoreV1().Events("")})
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: controllerAgentName})
controller := &Controller{
kubeclientset: kubeclientset,
kubefledgedclientset: kubefledgedclientset,
fledgedNameSpace: namespace,
nodesLister: nodeInformer.Lister(),
nodesSynced: nodeInformer.Informer().HasSynced,
imageCachesLister: imageCacheInformer.Lister(),
imageCachesSynced: imageCacheInformer.Informer().HasSynced,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ImageCaches"),
imageworkqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ImagePullerStatus"),
recorder: recorder,
imageCacheRefreshFrequency: imageCacheRefreshFrequency,
}
imageManager, _ := images.NewImageManager(controller.workqueue, controller.imageworkqueue,
controller.kubeclientset, controller.fledgedNameSpace, imagePullDeadlineDuration,
criClientImage, busyboxImage, imagePullPolicy, serviceAccountName, imageDeleteJobHostNetwork,
jobPriorityClassName, canDeleteJob, criSocketPath)
controller.imageManager = imageManager
glog.Info("Setting up event handlers")
// Set up an event handler for when ImageCache resources change
imageCacheInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
controller.enqueueImageCache(images.ImageCacheCreate, nil, obj)
},
UpdateFunc: func(old, new interface{}) {
controller.enqueueImageCache(images.ImageCacheUpdate, old, new)
},
DeleteFunc: func(obj interface{}) {
controller.enqueueImageCache(images.ImageCacheDelete, obj, nil)
},
})
return controller
}
// PreFlightChecks performs pre-flight checks and actions before the controller is started
func (c *Controller) PreFlightChecks() error {
if err := c.danglingJobs(); err != nil {
return err
}
if err := c.danglingImageCaches(); err != nil {
return err
}
return nil
}
// danglingJobs finds and removes dangling or stuck jobs
func (c *Controller) danglingJobs() error {
appEqKubefledged, _ := labels.NewRequirement("app", selection.Equals, []string{"kubefledged"})
kubefledgedEqImagemanager, _ := labels.NewRequirement("kubefledged", selection.Equals, []string{"kubefledged-image-manager"})
labelSelector := labels.NewSelector()
labelSelector = labelSelector.Add(*appEqKubefledged, *kubefledgedEqImagemanager)
joblist, err := c.kubeclientset.BatchV1().Jobs("").List(context.TODO(), metav1.ListOptions{
LabelSelector: labelSelector.String(),
})
if err != nil {
glog.Errorf("Error listing jobs: %v", err)
return err
}
if joblist == nil || len(joblist.Items) == 0 {
glog.Info("No dangling or stuck jobs found...")
return nil
}
deletePropagation := metav1.DeletePropagationBackground
for _, job := range joblist.Items {
err := c.kubeclientset.BatchV1().Jobs(job.Namespace).
Delete(context.TODO(), job.Name, metav1.DeleteOptions{PropagationPolicy: &deletePropagation})
if err != nil {
glog.Errorf("Error deleting job(%s): %v", job.Name, err)
return err
}
glog.Infof("Dangling Job(%s) deleted", job.Name)
}
return nil
}
// danglingImageCaches finds dangling or stuck image cache and marks them as abhorted. Such
// image caches will get refreshed in the next cycle
func (c *Controller) danglingImageCaches() error {
dangling := false
imagecachelist, err := c.kubefledgedclientset.KubefledgedV1alpha2().ImageCaches("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
glog.Errorf("Error listing imagecaches: %v", err)
return err
}
if imagecachelist == nil || len(imagecachelist.Items) == 0 {
glog.Info("No dangling or stuck imagecaches found...")
return nil
}
status := &v1alpha2.ImageCacheStatus{
Failures: map[string]v1alpha2.NodeReasonMessageList{},
Status: v1alpha2.ImageCacheActionStatusAborted,
Reason: v1alpha2.ImageCacheReasonImagePullAborted,
Message: v1alpha2.ImageCacheMessageImagePullAborted,
}
for _, imagecache := range imagecachelist.Items {
if imagecache.Status.Status == v1alpha2.ImageCacheActionStatusProcessing {
status.StartTime = imagecache.Status.StartTime
err := c.updateImageCacheStatus(&imagecache, status)
if err != nil {
glog.Errorf("Error updating ImageCache(%s) status to '%s': %v", imagecache.Name, v1alpha2.ImageCacheActionStatusAborted, err)
return err
}
dangling = true
glog.Infof("Dangling Image cache(%s) status changed to '%s'", imagecache.Name, v1alpha2.ImageCacheActionStatusAborted)
}
}
if !dangling {
glog.Info("No dangling or stuck imagecaches found...")
}
return nil
}
// Run will set up the event handlers for types we are interested in, as well
// as syncing informer caches and starting workers. It will block until stopCh
// is closed, at which point it will shutdown the workqueue and wait for
// workers to finish processing their current work items.
func (c *Controller) Run(threadiness int, stopCh <-chan struct{}) error {
defer runtime.HandleCrash()
defer c.workqueue.ShutDown()
defer c.imageworkqueue.ShutDown()
// Start the informer factories to begin populating the informer caches
glog.Info("Starting kubefledged-controller")
// Wait for the caches to be synced before starting workers
if ok := cache.WaitForCacheSync(stopCh, c.nodesSynced, c.imageCachesSynced); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}
glog.Info("Informer caches synched successfull")
// Launch workers to process ImageCache resources
for i := 0; i < threadiness; i++ {
go wait.Until(c.runWorker, time.Second, stopCh)
}
glog.Info("Image cache worker started")
if c.imageCacheRefreshFrequency.Nanoseconds() != int64(0) {
go wait.Until(c.runRefreshWorker, c.imageCacheRefreshFrequency, stopCh)
glog.Info("Image cache refresh worker started")
}
c.imageManager.Run(stopCh)
if err := c.imageManager.Run(stopCh); err != nil {
glog.Fatalf("Error running image manager: %s", err.Error())
}
glog.Info("Image manager started")
<-stopCh
glog.Info("Shutting down workers")
return nil
}
// enqueueImageCache takes a ImageCache resource and converts it into a namespace/name
// string which is then put onto the work queue. This method should *not* be
// passed resources of any type other than ImageCache.
func (c *Controller) enqueueImageCache(workType images.WorkType, old, new interface{}) bool {
var key string
var err error
var obj interface{}
wqKey := images.WorkQueueKey{}
switch workType {
case images.ImageCacheCreate:
obj = new
newImageCache := new.(*v1alpha2.ImageCache)
// If the ImageCache resource already has a status field, it means it's already
// synced, so do not queue it for processing
if !reflect.DeepEqual(newImageCache.Status, v1alpha2.ImageCacheStatus{}) {
return false
}
case images.ImageCacheUpdate:
obj = new
oldImageCache := old.(*v1alpha2.ImageCache)
newImageCache := new.(*v1alpha2.ImageCache)
if oldImageCache.Status.Status == v1alpha2.ImageCacheActionStatusProcessing {
if !reflect.DeepEqual(newImageCache.Spec, oldImageCache.Spec) {
glog.Warningf("Received image cache update/purge/delete for '%s' while it is under processing, so ignoring.", oldImageCache.Name)
return false
}
}
if _, exists := newImageCache.Annotations[imageCachePurgeAnnotationKey]; exists {
if _, exists := oldImageCache.Annotations[imageCachePurgeAnnotationKey]; !exists {
workType = images.ImageCachePurge
break
}
}
if _, exists := newImageCache.Annotations[imageCacheRefreshAnnotationKey]; exists {
if _, exists := oldImageCache.Annotations[imageCacheRefreshAnnotationKey]; !exists {
workType = images.ImageCacheRefresh
break
}
}
if reflect.DeepEqual(newImageCache.Spec, oldImageCache.Spec) {
return false
}
case images.ImageCacheDelete:
return false
case images.ImageCacheRefresh:
obj = old
}
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return false
}
wqKey.WorkType = workType
wqKey.ObjKey = key
if workType == images.ImageCacheUpdate {
oldImageCache := old.(*v1alpha2.ImageCache)
wqKey.OldImageCache = oldImageCache
}
c.workqueue.AddRateLimited(wqKey)
glog.V(4).Infof("enqueueImageCache::ImageCache resource queued for work type %s", workType)
return true
}
// runWorker is a long-running function that will continually call the
// processNextWorkItem function in order to read and process a message on the
// workqueue.
func (c *Controller) runWorker() {
for c.processNextWorkItem() {
}
}
// processNextWorkItem will read a single work item off the workqueue and
// attempt to process it, by calling the syncHandler.
func (c *Controller) processNextWorkItem() bool {
//glog.Info("processNextWorkItem::Beginning...")
obj, shutdown := c.workqueue.Get()
if shutdown {
return false
}
// We wrap this block in a func so we can defer c.workqueue.Done.
err := func(obj interface{}) error {
// We call Done here so the workqueue knows we have finished
// processing this item. We also must remember to call Forget if we
// do not want this work item being re-queued. For example, we do
// not call Forget if a transient error occurs, instead the item is
// put back on the workqueue and attempted again after a back-off
// period.
defer c.workqueue.Done(obj)
var key images.WorkQueueKey
var ok bool
// We expect strings to come off the workqueue. These are of the
// form namespace/name. We do this as the delayed nature of the
// workqueue means the items in the informer cache may actually be
// more up to date that when the item was initially put onto the
// workqueue.
if key, ok = obj.(images.WorkQueueKey); !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.workqueue.Forget(obj)
runtime.HandleError(fmt.Errorf("unexpected type in workqueue: %#v", obj))
return nil
}
// Run the syncHandler, passing it the namespace/name string of the
// ImageCache resource to be synced.
if err := c.syncHandler(key); err != nil {
glog.Errorf("error syncing imagecache: %v", err.Error())
return fmt.Errorf("error syncing imagecache: %v", err.Error())
}
// Finally, if no error occurs we Forget this item so it does not
// get queued again until another change happens.
c.workqueue.Forget(obj)
//glog.Infof("Successfully synced '%s' for event '%s'", key.ObjKey, key.WorkType)
return nil
}(obj)
if err != nil {
runtime.HandleError(err)
return true
}
return true
}
// runRefreshWorker is resposible of refreshing the image cache
func (c *Controller) runRefreshWorker() {
// List the ImageCache resources
imageCaches, err := c.imageCachesLister.ImageCaches("").List(labels.Everything())
if err != nil {
glog.Errorf("Error in listing image caches: %v", err)
return
}
for i := range imageCaches {
// Do not refresh if status is not yet updated
if reflect.DeepEqual(imageCaches[i].Status, v1alpha2.ImageCacheStatus{}) {
continue
}
// Do not refresh if image cache is already under processing
if imageCaches[i].Status.Status == v1alpha2.ImageCacheActionStatusProcessing {
continue
}
// Do not refresh image cache if cache spec validation failed
if imageCaches[i].Status.Status == v1alpha2.ImageCacheActionStatusFailed &&
imageCaches[i].Status.Reason == v1alpha2.ImageCacheReasonCacheSpecValidationFailed {
continue
}
// Do not refresh if image cache has been purged
if imageCaches[i].Status.Reason == v1alpha2.ImageCacheReasonImageCachePurge {
continue
}
c.enqueueImageCache(images.ImageCacheRefresh, imageCaches[i], nil)
}
}
// syncHandler compares the actual state with the desired, and attempts to
// converge the two. It then updates the Status block of the ImageCache resource
// with the current status of the resource.
func (c *Controller) syncHandler(wqKey images.WorkQueueKey) error {
status := &v1alpha2.ImageCacheStatus{
Failures: map[string]v1alpha2.NodeReasonMessageList{},
}
// Convert the namespace/name string into a distinct namespace and name
namespace, name, err := cache.SplitMetaNamespaceKey(wqKey.ObjKey)
if err != nil {
glog.Errorf("Error from cache.SplitMetaNamespaceKey(): %v", err)
return err
}
glog.Infof("Starting to sync image cache %s(%s)", name, wqKey.WorkType)
switch wqKey.WorkType {
case images.ImageCacheCreate, images.ImageCacheUpdate, images.ImageCacheRefresh, images.ImageCachePurge:
startTime := metav1.Now()
status.StartTime = &startTime
// Get the ImageCache resource with this namespace/name
imageCache, err := c.imageCachesLister.ImageCaches(namespace).Get(name)
if err != nil {
// The ImageCache resource may no longer exist, in which case we stop
// processing.
glog.Errorf("Error getting imagecache(%s): %v", name, err)
return err
}
if wqKey.WorkType == images.ImageCacheUpdate && wqKey.OldImageCache == nil {
status.Status = v1alpha2.ImageCacheActionStatusFailed
status.Reason = v1alpha2.ImageCacheReasonOldImageCacheNotFound
status.Message = v1alpha2.ImageCacheMessageOldImageCacheNotFound
if err := c.updateImageCacheStatus(imageCache, status); err != nil {
glog.Errorf("Error updating imagecache status to %s: %v", status.Status, err)
return err
}
glog.Errorf("%s: %s", v1alpha2.ImageCacheReasonOldImageCacheNotFound, v1alpha2.ImageCacheMessageOldImageCacheNotFound)
return fmt.Errorf("%s: %s", v1alpha2.ImageCacheReasonOldImageCacheNotFound, v1alpha2.ImageCacheMessageOldImageCacheNotFound)
}
cacheSpec := imageCache.Spec.CacheSpec
glog.V(4).Infof("cacheSpec: %+v", cacheSpec)
var nodes []*corev1.Node
status.Status = v1alpha2.ImageCacheActionStatusProcessing
if wqKey.WorkType == images.ImageCacheCreate {
status.Reason = v1alpha2.ImageCacheReasonImageCacheCreate
status.Message = v1alpha2.ImageCacheMessagePullingImages
}
if wqKey.WorkType == images.ImageCacheUpdate {
status.Reason = v1alpha2.ImageCacheReasonImageCacheUpdate
status.Message = v1alpha2.ImageCacheMessageUpdatingCache
}
if wqKey.WorkType == images.ImageCacheRefresh {
status.Reason = v1alpha2.ImageCacheReasonImageCacheRefresh
status.Message = v1alpha2.ImageCacheMessageRefreshingCache
}
if wqKey.WorkType == images.ImageCachePurge {
status.Reason = v1alpha2.ImageCacheReasonImageCachePurge
status.Message = v1alpha2.ImageCacheMessagePurgeCache
}
imageCache, err = c.kubefledgedclientset.KubefledgedV1alpha2().ImageCaches(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
glog.Errorf("Error getting imagecache(%s) from api server: %v", name, err)
return err
}
if err = c.updateImageCacheStatus(imageCache, status); err != nil {
glog.Errorf("Error updating imagecache status to %s: %v", status.Status, err)
return err
}
for k, i := range cacheSpec {
if len(i.NodeSelector) > 0 {
if nodes, err = c.nodesLister.List(labels.Set(i.NodeSelector).AsSelector()); err != nil {
glog.Errorf("Error listing nodes using nodeselector %+v: %v", i.NodeSelector, err)
return err
}
} else {
if nodes, err = c.nodesLister.List(labels.Everything()); err != nil {
glog.Errorf("Error listing nodes using nodeselector labels.Everything(): %v", err)
return err
}
}
glog.V(4).Infof("No. of nodes in %+v is %d", i.NodeSelector, len(nodes))
for _, n := range nodes {
for m := range i.Images {
ipr := images.ImageWorkRequest{
Image: i.Images[m],
Node: n,
ContainerRuntimeVersion: n.Status.NodeInfo.ContainerRuntimeVersion,
WorkType: wqKey.WorkType,
Imagecache: imageCache,
}
c.imageworkqueue.AddRateLimited(ipr)
}
if wqKey.WorkType == images.ImageCacheUpdate {
for _, oldimage := range wqKey.OldImageCache.Spec.CacheSpec[k].Images {
matched := false
for _, newimage := range i.Images {
if oldimage == newimage {
matched = true
break
}
}
if !matched {
ipr := images.ImageWorkRequest{
Image: oldimage,
Node: n,
ContainerRuntimeVersion: n.Status.NodeInfo.ContainerRuntimeVersion,
WorkType: images.ImageCachePurge,
Imagecache: imageCache,
}
c.imageworkqueue.AddRateLimited(ipr)
}
}
}
}
}
// We add an empty image pull request to signal the image manager that all
// requests for this sync action have been placed in the imageworkqueue
c.imageworkqueue.AddRateLimited(images.ImageWorkRequest{WorkType: wqKey.WorkType, Imagecache: imageCache})
case images.ImageCacheStatusUpdate:
glog.V(4).Infof("wqKey.Status = %+v", wqKey.Status)
// Finally, we update the status block of the ImageCache resource to reflect the
// current state of the world
// Get the ImageCache resource with this namespace/name
imageCache, err := c.kubefledgedclientset.KubefledgedV1alpha2().ImageCaches(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
glog.Errorf("Error getting image cache %s: %v", name, err)
return err
}
if imageCache.Status.StartTime != nil {
status.StartTime = imageCache.Status.StartTime
}
status.Status = v1alpha2.ImageCacheActioneNoImagesPulledOrDeleted
status.Reason = imageCache.Status.Reason
status.Message = v1alpha2.ImageCacheMessageNoImagesPulledOrDeleted
failures := false
for _, v := range *wqKey.Status {
if (v.Status == images.ImageWorkResultStatusSucceeded || v.Status == images.ImageWorkResultStatusAlreadyPulled) && !failures {
status.Status = v1alpha2.ImageCacheActionStatusSucceeded
if v.ImageWorkRequest.WorkType == images.ImageCachePurge {
status.Message = v1alpha2.ImageCacheMessageImagesDeletedSuccessfully
} else {
status.Message = v1alpha2.ImageCacheMessageImagesPulledSuccessfully
}
}
if (v.Status == images.ImageWorkResultStatusFailed || v.Status == images.ImageWorkResultStatusUnknown) && !failures {
failures = true
status.Status = v1alpha2.ImageCacheActionStatusFailed
if v.ImageWorkRequest.WorkType == images.ImageCachePurge {
status.Message = v1alpha2.ImageCacheMessageImageDeleteFailedForSomeImages
} else {
status.Message = v1alpha2.ImageCacheMessageImagePullFailedForSomeImages
}
}
if v.Status == images.ImageWorkResultStatusFailed || v.Status == images.ImageWorkResultStatusUnknown {
status.Failures[v.ImageWorkRequest.Image] = append(
status.Failures[v.ImageWorkRequest.Image], v1alpha2.NodeReasonMessage{
Node: v.ImageWorkRequest.Node.Labels["kubernetes.io/hostname"],
Reason: v.Reason,
Message: v.Message,
})
}
}
err = c.updateImageCacheStatus(imageCache, status)
if err != nil {
glog.Errorf("Error updating ImageCache status: %v", err)
return err
}
if imageCache.Status.Reason == v1alpha2.ImageCacheReasonImageCachePurge || imageCache.Status.Reason == v1alpha2.ImageCacheReasonImageCacheRefresh {
imageCache, err := c.kubefledgedclientset.KubefledgedV1alpha2().ImageCaches(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
glog.Errorf("Error getting image cache %s: %v", name, err)
return err
}
if imageCache.Status.Reason == v1alpha2.ImageCacheReasonImageCachePurge {
if err := c.removeAnnotation(imageCache, imageCachePurgeAnnotationKey); err != nil {
glog.Errorf("Error removing Annotation %s from imagecache(%s): %v", imageCachePurgeAnnotationKey, imageCache.Name, err)
return err
}
}
if imageCache.Status.Reason == v1alpha2.ImageCacheReasonImageCacheRefresh {
if _, ok := imageCache.Annotations[imageCacheRefreshAnnotationKey]; ok {
if err := c.removeAnnotation(imageCache, imageCacheRefreshAnnotationKey); err != nil {
glog.Errorf("Error removing Annotation %s from imagecache(%s): %v", imageCacheRefreshAnnotationKey, imageCache.Name, err)
return err
}
}
}
}
if status.Status == v1alpha2.ImageCacheActionStatusSucceeded || status.Status == v1alpha2.ImageCacheActioneNoImagesPulledOrDeleted {
c.recorder.Event(imageCache, corev1.EventTypeNormal, status.Reason, status.Message)
}
if status.Status == v1alpha2.ImageCacheActionStatusFailed {
c.recorder.Event(imageCache, corev1.EventTypeWarning, status.Reason, status.Message)
}
}
glog.Infof("Completed sync actions for image cache %s(%s)", name, wqKey.WorkType)
return nil
}
func (c *Controller) updateImageCacheStatus(imageCache *v1alpha2.ImageCache, status *v1alpha2.ImageCacheStatus) error {
imageCacheCopy, err := c.kubefledgedclientset.KubefledgedV1alpha2().ImageCaches(imageCache.Namespace).Get(context.TODO(), imageCache.Name, metav1.GetOptions{})
if err != nil {
return err
}
// NEVER modify objects from the store. It's a read-only, local cache.
// You can use DeepCopy() to make a deep copy of original object and modify this copy
// Or create a copy manually for better performance
imageCacheCopy.Status = *status
if imageCacheCopy.Status.Status != v1alpha2.ImageCacheActionStatusProcessing {
completionTime := metav1.Now()
imageCacheCopy.Status.CompletionTime = &completionTime
}
// If the CustomResourceSubresources feature gate is not enabled,
// we must use Update instead of UpdateStatus to update the Status block of the ImageCache resource.
// UpdateStatus will not allow changes to the Spec of the resource,
// which is ideal for ensuring nothing other than resource status has been updated.
_, err = c.kubefledgedclientset.KubefledgedV1alpha2().ImageCaches(imageCache.Namespace).Update(context.TODO(), imageCacheCopy, metav1.UpdateOptions{})
return err
}
func (c *Controller) removeAnnotation(imageCache *v1alpha2.ImageCache, annotationKey string) error {
imageCacheCopy := imageCache.DeepCopy()
delete(imageCacheCopy.Annotations, annotationKey)
_, err := c.kubefledgedclientset.KubefledgedV1alpha2().ImageCaches(imageCache.Namespace).Update(context.TODO(), imageCacheCopy, metav1.UpdateOptions{})
if err == nil {
glog.Infof("Annotation %s removed from imagecache(%s)", annotationKey, imageCache.Name)
}
return err
}