@@ -19,8 +19,11 @@ package imagelistpulljob
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "hash/fnv"
23
+ "reflect"
22
24
"time"
23
25
26
+ appsv1 "k8s.io/api/apps/v1"
24
27
corev1 "k8s.io/api/core/v1"
25
28
"k8s.io/apimachinery/pkg/api/errors"
26
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -29,9 +32,11 @@ import (
29
32
"k8s.io/apimachinery/pkg/types"
30
33
"k8s.io/apimachinery/pkg/util/clock"
31
34
utilerrors "k8s.io/apimachinery/pkg/util/errors"
35
+ "k8s.io/apimachinery/pkg/util/rand"
32
36
"k8s.io/client-go/tools/record"
33
37
"k8s.io/client-go/util/retry"
34
38
"k8s.io/klog/v2"
39
+ hashutil "k8s.io/kubernetes/pkg/util/hash"
35
40
"k8s.io/kubernetes/pkg/util/slice"
36
41
"sigs.k8s.io/controller-runtime/pkg/client"
37
42
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -40,8 +45,6 @@ import (
40
45
"sigs.k8s.io/controller-runtime/pkg/reconcile"
41
46
"sigs.k8s.io/controller-runtime/pkg/source"
42
47
43
- "reflect"
44
-
45
48
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
46
49
"github.com/openkruise/kruise/pkg/features"
47
50
"github.com/openkruise/kruise/pkg/util"
@@ -141,6 +144,11 @@ func (r *ReconcileImageListPullJob) Reconcile(_ context.Context, request reconci
141
144
return reconcile.Result {}, err
142
145
}
143
146
147
+ hash , err := r .refreshJobTemplateHash (job )
148
+ if err != nil {
149
+ return reconcile.Result {}, fmt .Errorf ("refresh job template hash error: %v" , err )
150
+ }
151
+
144
152
// The Job has been finished
145
153
if job .Status .CompletionTime != nil {
146
154
var leftTime time.Duration
@@ -189,7 +197,7 @@ func (r *ReconcileImageListPullJob) Reconcile(_ context.Context, request reconci
189
197
newStatus := r .calculateStatus (job , imagePullJobsMap )
190
198
191
199
// 4. Compute ImagePullJobActions
192
- needToCreate , needToDelete := r .computeImagePullJobActions (job , imagePullJobsMap )
200
+ needToCreate , needToDelete := r .computeImagePullJobActions (job , imagePullJobsMap , hash )
193
201
194
202
// 5. Sync ImagePullJob
195
203
err = r .syncImagePullJob (job , needToCreate , needToDelete )
@@ -207,6 +215,25 @@ func (r *ReconcileImageListPullJob) Reconcile(_ context.Context, request reconci
207
215
return reconcile.Result {}, nil
208
216
}
209
217
218
+ func (r * ReconcileImageListPullJob ) refreshJobTemplateHash (job * appsv1alpha1.ImageListPullJob ) (string , error ) {
219
+ newHash := func (job * appsv1alpha1.ImageListPullJob ) string {
220
+ jobTemplateHasher := fnv .New32a ()
221
+ hashutil .DeepHashObject (jobTemplateHasher , job .Spec .ImagePullJobTemplate )
222
+ return rand .SafeEncodeString (fmt .Sprint (jobTemplateHasher .Sum32 ()))
223
+ }(job )
224
+
225
+ oldHash := job .Labels [appsv1 .ControllerRevisionHashLabelKey ]
226
+ if newHash == oldHash {
227
+ return newHash , nil
228
+ }
229
+
230
+ emptyJob := & appsv1alpha1.ImageListPullJob {}
231
+ emptyJob .SetName (job .Name )
232
+ emptyJob .SetNamespace (job .Namespace )
233
+ body := fmt .Sprintf (`{"metadata":{"labels":{"%s":"%s"}}}` , appsv1 .ControllerRevisionHashLabelKey , newHash )
234
+ return newHash , r .Patch (context .TODO (), emptyJob , client .RawPatch (types .MergePatchType , []byte (body )))
235
+ }
236
+
210
237
func (r * ReconcileImageListPullJob ) updateStatus (job * appsv1alpha1.ImageListPullJob , newStatus * appsv1alpha1.ImageListPullJobStatus ) error {
211
238
return retry .RetryOnConflict (retry .DefaultBackoff , func () error {
212
239
imageListPullJob := & appsv1alpha1.ImageListPullJob {}
@@ -218,11 +245,11 @@ func (r *ReconcileImageListPullJob) updateStatus(job *appsv1alpha1.ImageListPull
218
245
})
219
246
}
220
247
221
- func (r * ReconcileImageListPullJob ) computeImagePullJobActions (job * appsv1alpha1.ImageListPullJob , imagePullJobs map [string ]* appsv1alpha1.ImagePullJob ) ([]* appsv1alpha1.ImagePullJob , []* appsv1alpha1.ImagePullJob ) {
248
+ func (r * ReconcileImageListPullJob ) computeImagePullJobActions (job * appsv1alpha1.ImageListPullJob , imagePullJobs map [string ]* appsv1alpha1.ImagePullJob , hash string ) ([]* appsv1alpha1.ImagePullJob , []* appsv1alpha1.ImagePullJob ) {
222
249
var needToDelete , needToCreate []* appsv1alpha1.ImagePullJob
223
250
//1. need to create
224
- images , needToDelete := r .filterImagesAndImagePullJobs (job , imagePullJobs )
225
- needToCreate = r .newImagePullJobs (job , images )
251
+ images , needToDelete := r .filterImagesAndImagePullJobs (job , imagePullJobs , hash )
252
+ needToCreate = r .newImagePullJobs (job , images , hash )
226
253
// some images delete from ImageListPullJob.Spec.Images
227
254
for image , imagePullJob := range imagePullJobs {
228
255
if ! slice .ContainsString (job .Spec .Images , image , nil ) {
@@ -333,7 +360,7 @@ func (r *ReconcileImageListPullJob) syncImagePullJob(job *appsv1alpha1.ImageList
333
360
return utilerrors .NewAggregate (errs )
334
361
}
335
362
336
- func (r * ReconcileImageListPullJob ) filterImagesAndImagePullJobs (job * appsv1alpha1.ImageListPullJob , imagePullJobs map [string ]* appsv1alpha1.ImagePullJob ) ([]string , []* appsv1alpha1.ImagePullJob ) {
363
+ func (r * ReconcileImageListPullJob ) filterImagesAndImagePullJobs (job * appsv1alpha1.ImageListPullJob , imagePullJobs map [string ]* appsv1alpha1.ImagePullJob , hash string ) ([]string , []* appsv1alpha1.ImagePullJob ) {
337
364
var images , imagesInCurrentImagePullJob []string
338
365
var needToDelete []* appsv1alpha1.ImagePullJob
339
366
@@ -354,7 +381,7 @@ func (r *ReconcileImageListPullJob) filterImagesAndImagePullJobs(job *appsv1alph
354
381
continue
355
382
}
356
383
// should create new imagePullJob if the template is changed.
357
- if imagePullJobSpecTemplateChanged (imagePullJob , job .Spec .ImagePullJobTemplate ) {
384
+ if ! isConsistentVersion (imagePullJob , & job .Spec .ImagePullJobTemplate , hash ) {
358
385
images = append (images , image )
359
386
// should delete old imagepulljob
360
387
needToDelete = append (needToDelete , imagePullJob )
@@ -364,7 +391,7 @@ func (r *ReconcileImageListPullJob) filterImagesAndImagePullJobs(job *appsv1alph
364
391
return images , needToDelete
365
392
}
366
393
367
- func (r * ReconcileImageListPullJob ) newImagePullJobs (job * appsv1alpha1.ImageListPullJob , images []string ) []* appsv1alpha1.ImagePullJob {
394
+ func (r * ReconcileImageListPullJob ) newImagePullJobs (job * appsv1alpha1.ImageListPullJob , images []string , hash string ) []* appsv1alpha1.ImagePullJob {
368
395
var needToCreate []* appsv1alpha1.ImagePullJob
369
396
if len (images ) <= 0 {
370
397
return needToCreate
@@ -374,8 +401,10 @@ func (r *ReconcileImageListPullJob) newImagePullJobs(job *appsv1alpha1.ImageList
374
401
ObjectMeta : metav1.ObjectMeta {
375
402
Namespace : job .Namespace ,
376
403
GenerateName : fmt .Sprintf ("%s-" , job .Name ),
377
- Labels : make (map [string ]string ),
378
- Annotations : make (map [string ]string ),
404
+ Labels : map [string ]string {
405
+ appsv1 .ControllerRevisionHashLabelKey : hash ,
406
+ },
407
+ Annotations : make (map [string ]string ),
379
408
OwnerReferences : []metav1.OwnerReference {
380
409
* metav1 .NewControllerRef (job , controllerKind ),
381
410
},
@@ -412,10 +441,15 @@ func (r *ReconcileImageListPullJob) getOwnedImagePullJob(job *appsv1alpha1.Image
412
441
return imagePullJobsMap , nil
413
442
}
414
443
415
- func imagePullJobSpecTemplateChanged (oldImagePullJob * appsv1alpha1.ImagePullJob , newImagePullJobTemplate appsv1alpha1.ImagePullJobTemplate ) bool {
416
- if ! reflect . DeepEqual ( oldImagePullJob .Spec . ImagePullJobTemplate , newImagePullJobTemplate ) {
417
- klog . V ( 3 ). Infof ( "imagePullJob(%s/%s) specification changed" , oldImagePullJob . Namespace , oldImagePullJob . Name )
444
+ func isConsistentVersion (oldImagePullJob * appsv1alpha1.ImagePullJob , newTemplate * appsv1alpha1.ImagePullJobTemplate , hash string ) bool {
445
+ oldHash , exists := oldImagePullJob .Labels [ appsv1 . ControllerRevisionHashLabelKey ]
446
+ if oldHash == hash {
418
447
return true
419
448
}
449
+ if ! exists && reflect .DeepEqual (oldImagePullJob .Spec .ImagePullJobTemplate , * newTemplate ) {
450
+ return true
451
+ }
452
+
453
+ klog .V (4 ).Infof ("imagePullJob(%s/%s) specification changed" , oldImagePullJob .Namespace , oldImagePullJob .Name )
420
454
return false
421
455
}
0 commit comments