@@ -27,13 +27,13 @@ import (
27
27
corev1 "k8s.io/api/core/v1"
28
28
apierrors "k8s.io/apimachinery/pkg/api/errors"
29
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
30
31
kerrors "k8s.io/apimachinery/pkg/util/errors"
31
32
"k8s.io/client-go/rest"
32
33
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
33
34
"sigs.k8s.io/cluster-api-operator/internal/controller/genericprovider"
34
35
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
35
36
configclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
36
- "sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor"
37
37
"sigs.k8s.io/cluster-api/util/conditions"
38
38
"sigs.k8s.io/cluster-api/util/patch"
39
39
ctrl "sigs.k8s.io/controller-runtime"
@@ -43,8 +43,6 @@ import (
43
43
"sigs.k8s.io/controller-runtime/pkg/handler"
44
44
"sigs.k8s.io/controller-runtime/pkg/log"
45
45
"sigs.k8s.io/controller-runtime/pkg/reconcile"
46
-
47
- utilyaml "sigs.k8s.io/cluster-api/util/yaml"
48
46
)
49
47
50
48
type GenericProviderReconciler struct {
@@ -361,14 +359,14 @@ func calculateHash(ctx context.Context, k8sClient client.Client, provider generi
361
359
func applyFromCache (ctx context.Context , cl client.Client , provider genericprovider.GenericProvider ) (bool , error ) {
362
360
log := log .FromContext (ctx )
363
361
364
- configMap := & corev1.ConfigMap {}
365
- if err := cl .Get (ctx , client.ObjectKey {Name : ProviderCacheName (provider ), Namespace : provider .GetNamespace ()}, configMap ); apierrors .IsNotFound (err ) {
366
- // config map does not exist, nothing to apply
362
+ secret := & corev1.Secret {}
363
+ if err := cl .Get (ctx , client.ObjectKey {Name : ProviderCacheName (provider ), Namespace : provider .GetNamespace ()}, secret ); apierrors .IsNotFound (err ) {
364
+ // secret does not exist, nothing to apply
367
365
return false , nil
368
366
} else if err != nil {
369
- log .Error (err , "failed to get provider config map " )
367
+ log .Error (err , "failed to get provider cache " )
370
368
371
- return false , fmt .Errorf ("failed to get cache ConfigMap : %w" , err )
369
+ return false , fmt .Errorf ("failed to get provider cache : %w" , err )
372
370
}
373
371
374
372
// calculate combined hash for provider and config map cache
@@ -379,15 +377,15 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
379
377
return false , err
380
378
}
381
379
382
- if err := addObjectToHash (hash , configMap .Data ); err != nil {
380
+ if err := addObjectToHash (hash , secret .Data ); err != nil {
383
381
log .Error (err , "failed to calculate config map hash" )
384
382
385
383
return false , err
386
384
}
387
385
388
386
cacheHash := fmt .Sprintf ("%x" , hash .Sum (nil ))
389
- if configMap .GetAnnotations ()[appliedSpecHashAnnotation ] != cacheHash {
390
- log .Info ("Provider or cache state has changed" , "cacheHash" , cacheHash , "providerHash" , configMap .GetAnnotations ()[appliedSpecHashAnnotation ])
387
+ if secret .GetAnnotations ()[appliedSpecHashAnnotation ] != cacheHash {
388
+ log .Info ("Provider or cache state has changed" , "cacheHash" , cacheHash , "providerHash" , secret .GetAnnotations ()[appliedSpecHashAnnotation ])
391
389
392
390
return false , nil
393
391
}
@@ -407,63 +405,52 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
407
405
return false , err
408
406
}
409
407
410
- processor := yamlprocessor .NewSimpleProcessor ()
411
- for _ , manifest := range configMap .Data {
412
- manifest , err := processor .Process ([]byte (manifest ), mr .Get )
413
- if err != nil {
414
- log .Error (err , "failed to process manifest" )
415
-
416
- return false , err
408
+ for _ , manifest := range secret .Data {
409
+ if secret .GetAnnotations ()[operatorv1 .CompressedAnnotation ] == operatorv1 .TrueValue {
410
+ break
417
411
}
418
412
419
- manifests , err := utilyaml .ToUnstructured (manifest )
413
+ manifests := []unstructured.Unstructured {}
414
+
415
+ err := json .Unmarshal (manifest , & manifests )
420
416
if err != nil {
421
417
log .Error (err , "failed to convert yaml to unstructured" )
422
418
423
419
return false , err
424
420
}
425
421
426
- if len ( manifests ) > 1 {
427
- return false , fmt . Errorf ( "multiple manifests found: %d" , len ( manifests ))
428
- } else if len ( manifests ) == 0 {
429
- continue
422
+ for _ , manifest := range manifests {
423
+ if err := cl . Patch ( ctx , & manifest , client . Apply , client . ForceOwnership , client . FieldOwner ( cacheOwner )); err != nil {
424
+ errs = append ( errs , err )
425
+ }
430
426
}
427
+ }
431
428
432
- if err := cl .Patch (ctx , & manifests [0 ], client .Apply , client .ForceOwnership , client .FieldOwner (cacheOwner )); err != nil {
433
- errs = append (errs , err )
429
+ for _ , binaryManifest := range secret .Data {
430
+ if secret .GetAnnotations ()[operatorv1 .CompressedAnnotation ] != operatorv1 .TrueValue {
431
+ break
434
432
}
435
- }
436
433
437
- for _ , binaryManifest := range configMap .BinaryData {
438
- manifest , err := decompressYaml (binaryManifest )
434
+ manifest , err := decompressData (binaryManifest )
439
435
if err != nil {
440
436
log .Error (err , "failed to decompress yaml" )
441
437
442
438
return false , err
443
439
}
444
440
445
- manifest , err = processor .Process (manifest , mr .Get )
446
- if err != nil {
447
- log .Error (err , "failed to process manifest" )
448
-
449
- return false , err
450
- }
441
+ manifests := []unstructured.Unstructured {}
451
442
452
- manifests , err := utilyaml . ToUnstructured (manifest )
443
+ err = json . Unmarshal (manifest , & manifests )
453
444
if err != nil {
454
445
log .Error (err , "failed to convert yaml to unstructured" )
455
446
456
447
return false , err
457
448
}
458
449
459
- if len (manifests ) > 1 {
460
- return false , fmt .Errorf ("multiple manifests found: %d" , len (manifests ))
461
- } else if len (manifests ) == 0 {
462
- continue
463
- }
464
-
465
- if err := cl .Patch (ctx , & manifests [0 ], client .Apply , client .ForceOwnership , client .FieldOwner (cacheOwner )); err != nil {
466
- errs = append (errs , err )
450
+ for _ , manifest := range manifests {
451
+ if err := cl .Patch (ctx , & manifest , client .Apply , client .ForceOwnership , client .FieldOwner (cacheOwner )); err != nil {
452
+ errs = append (errs , err )
453
+ }
467
454
}
468
455
}
469
456
@@ -478,14 +465,14 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
478
465
return true , nil
479
466
}
480
467
481
- // setCacheHash calculates current provider and configMap hash, and updates it on the configMap .
468
+ // setCacheHash calculates current provider and secret hash, and updates it on the secret .
482
469
func setCacheHash (ctx context.Context , cl client.Client , provider genericprovider.GenericProvider ) error {
483
- configMap := & corev1.ConfigMap {}
484
- if err := cl .Get (ctx , client.ObjectKey {Name : ProviderCacheName (provider ), Namespace : provider .GetNamespace ()}, configMap ); err != nil {
485
- return fmt .Errorf ("failed to get cache ConfigMaps : %w" , err )
470
+ secret := & corev1.Secret {}
471
+ if err := cl .Get (ctx , client.ObjectKey {Name : ProviderCacheName (provider ), Namespace : provider .GetNamespace ()}, secret ); err != nil {
472
+ return fmt .Errorf ("failed to get cache secret : %w" , err )
486
473
}
487
474
488
- helper , err := patch .NewHelper (configMap , cl )
475
+ helper , err := patch .NewHelper (secret , cl )
489
476
if err != nil {
490
477
return err
491
478
}
@@ -496,19 +483,19 @@ func setCacheHash(ctx context.Context, cl client.Client, provider genericprovide
496
483
return err
497
484
}
498
485
499
- if err := addObjectToHash (hash , configMap .Data ); err != nil {
486
+ if err := addObjectToHash (hash , secret .Data ); err != nil {
500
487
return err
501
488
}
502
489
503
490
cacheHash := fmt .Sprintf ("%x" , hash .Sum (nil ))
504
491
505
- annotations := configMap .GetAnnotations ()
492
+ annotations := secret .GetAnnotations ()
506
493
if annotations == nil {
507
494
annotations = map [string ]string {}
508
495
}
509
496
510
497
annotations [appliedSpecHashAnnotation ] = cacheHash
511
- configMap .SetAnnotations (annotations )
498
+ secret .SetAnnotations (annotations )
512
499
513
- return helper .Patch (ctx , configMap )
500
+ return helper .Patch (ctx , secret )
514
501
}
0 commit comments