@@ -92,14 +92,17 @@ func (r *GenericProviderReconciler) BuildWithManager(ctx context.Context, mgr ct
92
92
reconciler := NewPhaseReconciler (* r , r .Provider , r .ProviderList )
93
93
94
94
r .ReconcilePhases = []PhaseFn {
95
+ reconciler .ApplyFromCache ,
95
96
reconciler .PreflightChecks ,
96
97
reconciler .InitializePhaseReconciler ,
97
98
reconciler .DownloadManifests ,
98
99
reconciler .Load ,
99
100
reconciler .Fetch ,
101
+ reconciler .Store ,
100
102
reconciler .Upgrade ,
101
103
reconciler .Install ,
102
104
reconciler .ReportStatus ,
105
+ reconciler .Finalize ,
103
106
}
104
107
105
108
r .DeletePhases = []PhaseFn {
@@ -177,13 +180,7 @@ func (r *GenericProviderReconciler) Reconcile(ctx context.Context, req reconcile
177
180
return ctrl.Result {}, err
178
181
}
179
182
180
- // Check provider config map for changes
181
- cacheUsed , err := applyFromCache (ctx , r .Client , r .Provider )
182
- if err != nil {
183
- return ctrl.Result {}, err
184
- }
185
-
186
- if r .Provider .GetAnnotations ()[appliedSpecHashAnnotation ] == specHash || cacheUsed {
183
+ if r .Provider .GetAnnotations ()[appliedSpecHashAnnotation ] == specHash {
187
184
log .Info ("No changes detected, skipping further steps" )
188
185
189
186
return ctrl.Result {}, nil
@@ -355,39 +352,39 @@ func calculateHash(ctx context.Context, k8sClient client.Client, provider generi
355
352
return fmt .Sprintf ("%x" , hash .Sum (nil )), err
356
353
}
357
354
358
- // applyFromCache applies provider configuration from cache and returns true if the cache did not change.
359
- func applyFromCache ( ctx context.Context , cl client. Client , provider genericprovider. GenericProvider ) (bool , error ) {
355
+ // ApplyFromCache applies provider configuration from cache and returns true if the cache did not change.
356
+ func ( p * PhaseReconciler ) ApplyFromCache ( ctx context.Context ) (* Result , error ) {
360
357
log := log .FromContext (ctx )
361
358
362
359
secret := & corev1.Secret {}
363
- if err := cl . Get (ctx , client.ObjectKey {Name : ProviderCacheName (provider ), Namespace : provider .GetNamespace ()}, secret ); apierrors .IsNotFound (err ) {
360
+ if err := p . ctrlClient . Get (ctx , client.ObjectKey {Name : ProviderCacheName (p . provider ), Namespace : p . provider .GetNamespace ()}, secret ); apierrors .IsNotFound (err ) {
364
361
// secret does not exist, nothing to apply
365
- return false , nil
362
+ return & Result {} , nil
366
363
} else if err != nil {
367
364
log .Error (err , "failed to get provider cache" )
368
365
369
- return false , fmt .Errorf ("failed to get provider cache: %w" , err )
366
+ return & Result {} , fmt .Errorf ("failed to get provider cache: %w" , err )
370
367
}
371
368
372
369
// calculate combined hash for provider and config map cache
373
370
hash := sha256 .New ()
374
- if err := providerHash (ctx , cl , hash , provider ); err != nil {
371
+ if err := providerHash (ctx , p . ctrlClient , hash , p . provider ); err != nil {
375
372
log .Error (err , "failed to calculate provider hash" )
376
373
377
- return false , err
374
+ return & Result {} , err
378
375
}
379
376
380
377
if err := addObjectToHash (hash , secret .Data ); err != nil {
381
378
log .Error (err , "failed to calculate config map hash" )
382
379
383
- return false , err
380
+ return & Result {} , err
384
381
}
385
382
386
383
cacheHash := fmt .Sprintf ("%x" , hash .Sum (nil ))
387
384
if secret .GetAnnotations ()[appliedSpecHashAnnotation ] != cacheHash {
388
385
log .Info ("Provider or cache state has changed" , "cacheHash" , cacheHash , "providerHash" , secret .GetAnnotations ()[appliedSpecHashAnnotation ])
389
386
390
- return false , nil
387
+ return & Result {} , nil
391
388
}
392
389
393
390
log .Info ("Applying provider configuration from cache" )
@@ -397,12 +394,12 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
397
394
mr := configclient .NewMemoryReader ()
398
395
399
396
if err := mr .Init (ctx , "" ); err != nil {
400
- return false , err
397
+ return & Result {} , err
401
398
}
402
399
403
400
// Fetch configuration variables from the secret. See API field docs for more info.
404
- if err := initReaderVariables (ctx , cl , mr , provider ); err != nil {
405
- return false , err
401
+ if err := initReaderVariables (ctx , p . ctrlClient , mr , p . provider ); err != nil {
402
+ return & Result {} , err
406
403
}
407
404
408
405
for _ , manifest := range secret .Data {
@@ -416,11 +413,11 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
416
413
if err != nil {
417
414
log .Error (err , "failed to convert yaml to unstructured" )
418
415
419
- return false , err
416
+ return & Result {} , err
420
417
}
421
418
422
419
for _ , manifest := range manifests {
423
- if err := cl .Patch (ctx , & manifest , client .Apply , client .ForceOwnership , client .FieldOwner (cacheOwner )); err != nil {
420
+ if err := p . ctrlClient .Patch (ctx , & manifest , client .Apply , client .ForceOwnership , client .FieldOwner (cacheOwner )); err != nil {
424
421
errs = append (errs , err )
425
422
}
426
423
}
@@ -435,7 +432,7 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
435
432
if err != nil {
436
433
log .Error (err , "failed to decompress yaml" )
437
434
438
- return false , err
435
+ return & Result {} , err
439
436
}
440
437
441
438
manifests := []unstructured.Unstructured {}
@@ -444,11 +441,11 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
444
441
if err != nil {
445
442
log .Error (err , "failed to convert yaml to unstructured" )
446
443
447
- return false , err
444
+ return & Result {} , err
448
445
}
449
446
450
447
for _ , manifest := range manifests {
451
- if err := cl .Patch (ctx , & manifest , client .Apply , client .ForceOwnership , client .FieldOwner (cacheOwner )); err != nil {
448
+ if err := p . ctrlClient .Patch (ctx , & manifest , client .Apply , client .ForceOwnership , client .FieldOwner (cacheOwner )); err != nil {
452
449
errs = append (errs , err )
453
450
}
454
451
}
@@ -457,12 +454,12 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
457
454
if err := kerrors .NewAggregate (errs ); err != nil {
458
455
log .Error (err , "failed to apply objects from cache" )
459
456
460
- return false , err
457
+ return & Result {} , err
461
458
}
462
459
463
460
log .Info ("Applied all objects from cache" )
464
461
465
- return true , nil
462
+ return & Result { Completed : true } , nil
466
463
}
467
464
468
465
// setCacheHash calculates current provider and secret hash, and updates it on the secret.
0 commit comments