@@ -27,13 +27,13 @@ import (
2727 corev1 "k8s.io/api/core/v1"
2828 apierrors "k8s.io/apimachinery/pkg/api/errors"
2929 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3031 kerrors "k8s.io/apimachinery/pkg/util/errors"
3132 "k8s.io/client-go/rest"
3233 operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
3334 "sigs.k8s.io/cluster-api-operator/internal/controller/genericprovider"
3435 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3536 configclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
36- "sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor"
3737 "sigs.k8s.io/cluster-api/util/conditions"
3838 "sigs.k8s.io/cluster-api/util/patch"
3939 ctrl "sigs.k8s.io/controller-runtime"
@@ -43,8 +43,6 @@ import (
4343 "sigs.k8s.io/controller-runtime/pkg/handler"
4444 "sigs.k8s.io/controller-runtime/pkg/log"
4545 "sigs.k8s.io/controller-runtime/pkg/reconcile"
46-
47- utilyaml "sigs.k8s.io/cluster-api/util/yaml"
4846)
4947
5048type GenericProviderReconciler struct {
@@ -361,14 +359,14 @@ func calculateHash(ctx context.Context, k8sClient client.Client, provider generi
361359func applyFromCache (ctx context.Context , cl client.Client , provider genericprovider.GenericProvider ) (bool , error ) {
362360 log := log .FromContext (ctx )
363361
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
367365 return false , nil
368366 } else if err != nil {
369- log .Error (err , "failed to get provider config map " )
367+ log .Error (err , "failed to get provider cache " )
370368
371- return false , fmt .Errorf ("failed to get cache ConfigMap : %w" , err )
369+ return false , fmt .Errorf ("failed to get provider cache : %w" , err )
372370 }
373371
374372 // calculate combined hash for provider and config map cache
@@ -379,15 +377,15 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
379377 return false , err
380378 }
381379
382- if err := addObjectToHash (hash , configMap .Data ); err != nil {
380+ if err := addObjectToHash (hash , secret .Data ); err != nil {
383381 log .Error (err , "failed to calculate config map hash" )
384382
385383 return false , err
386384 }
387385
388386 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 ])
391389
392390 return false , nil
393391 }
@@ -407,63 +405,52 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
407405 return false , err
408406 }
409407
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
417411 }
418412
419- manifests , err := utilyaml .ToUnstructured (manifest )
413+ manifests := []unstructured.Unstructured {}
414+
415+ err := json .Unmarshal (manifest , & manifests )
420416 if err != nil {
421417 log .Error (err , "failed to convert yaml to unstructured" )
422418
423419 return false , err
424420 }
425421
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+ }
430426 }
427+ }
431428
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
434432 }
435- }
436433
437- for _ , binaryManifest := range configMap .BinaryData {
438- manifest , err := decompressYaml (binaryManifest )
434+ manifest , err := decompressData (binaryManifest )
439435 if err != nil {
440436 log .Error (err , "failed to decompress yaml" )
441437
442438 return false , err
443439 }
444440
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 {}
451442
452- manifests , err := utilyaml . ToUnstructured (manifest )
443+ err = json . Unmarshal (manifest , & manifests )
453444 if err != nil {
454445 log .Error (err , "failed to convert yaml to unstructured" )
455446
456447 return false , err
457448 }
458449
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+ }
467454 }
468455 }
469456
@@ -478,14 +465,14 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
478465 return true , nil
479466}
480467
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 .
482469func 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 )
486473 }
487474
488- helper , err := patch .NewHelper (configMap , cl )
475+ helper , err := patch .NewHelper (secret , cl )
489476 if err != nil {
490477 return err
491478 }
@@ -496,19 +483,19 @@ func setCacheHash(ctx context.Context, cl client.Client, provider genericprovide
496483 return err
497484 }
498485
499- if err := addObjectToHash (hash , configMap .Data ); err != nil {
486+ if err := addObjectToHash (hash , secret .Data ); err != nil {
500487 return err
501488 }
502489
503490 cacheHash := fmt .Sprintf ("%x" , hash .Sum (nil ))
504491
505- annotations := configMap .GetAnnotations ()
492+ annotations := secret .GetAnnotations ()
506493 if annotations == nil {
507494 annotations = map [string ]string {}
508495 }
509496
510497 annotations [appliedSpecHashAnnotation ] = cacheHash
511- configMap .SetAnnotations (annotations )
498+ secret .SetAnnotations (annotations )
512499
513- return helper .Patch (ctx , configMap )
500+ return helper .Patch (ctx , secret )
514501}
0 commit comments