@@ -23,7 +23,6 @@ import (
2323 "errors"
2424 "fmt"
2525 "hash"
26- "strings"
2726
2827 corev1 "k8s.io/api/core/v1"
2928 apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -33,6 +32,8 @@ import (
3332 operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
3433 "sigs.k8s.io/cluster-api-operator/internal/controller/genericprovider"
3534 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
35+ configclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
36+ "sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor"
3637 "sigs.k8s.io/cluster-api/util/conditions"
3738 "sigs.k8s.io/cluster-api/util/patch"
3839 ctrl "sigs.k8s.io/controller-runtime"
@@ -360,7 +361,7 @@ func calculateHash(ctx context.Context, k8sClient client.Client, provider generi
360361func applyFromCache (ctx context.Context , cl client.Client , provider genericprovider.GenericProvider ) (bool , error ) {
361362 log := log .FromContext (ctx )
362363
363- configMap , err := providerConfigMap (ctx , cl , provider )
364+ configMap , err := providerCacheConfigMap (ctx , cl , provider )
364365 if err != nil {
365366 log .Error (err , "failed to get provider config map" )
366367
@@ -393,37 +394,81 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
393394 return false , nil
394395 }
395396
396- components , err := getComponentsData (* configMap )
397- if err != nil {
398- log .Error (err , "failed to get provider components" )
397+ log .Info ("Applying provider configuration from cache" )
398+ errs := []error {}
399+
400+ mr := configclient .NewMemoryReader ()
399401
402+ if err := mr .Init (ctx , "" ); err != nil {
400403 return false , err
401404 }
402405
403- additionalManifests , err := fetchAdditionalManifests (ctx , cl , provider )
404- if err != nil {
405- log .Error (err , "failed to get additional manifests" )
406+ // Fetch configuration variables from the secret. See API field docs for more info.
407+ initReaderVariables (ctx , cl , mr , provider )
406408
407- return false , err
408- }
409+ processor := yamlprocessor .NewSimpleProcessor ()
410+ for _ , manifest := range configMap .Data {
411+ manifest , err := processor .Process ([]byte (manifest ), mr .Get )
412+ if err != nil {
413+ log .Error (err , "failed to process manifest" )
409414
410- if additionalManifests != "" {
411- components = components + "\n ---\n " + additionalManifests
412- }
415+ return false , err
416+ }
413417
414- for _ , manifests := range strings .Split (components , "---" ) {
415- manifests , err := utilyaml .ToUnstructured ([]byte (manifests ))
418+ manifests , err := utilyaml .ToUnstructured (manifest )
416419 if err != nil {
417420 log .Error (err , "failed to convert yaml to unstructured" )
418421
419422 return false , err
420423 }
421424
425+ if len (manifest ) > 1 {
426+ return false , fmt .Errorf ("multiple manifests found: %d" , len (manifests ))
427+ } else if len (manifests ) == 0 {
428+ continue
429+ }
430+
422431 if err := cl .Patch (ctx , & manifests [0 ], client .Apply , client .ForceOwnership , client .FieldOwner (cacheOwner )); err != nil {
423- log .Error (err , "failed to apply object from cache" )
432+ errs = append (errs , err )
433+ }
434+ }
424435
425- return false , nil
436+ for _ , binaryManifest := range configMap .BinaryData {
437+ manifest , err := decompressYaml (binaryManifest )
438+ if err != nil {
439+ log .Error (err , "failed to decompress yaml" )
440+
441+ return false , err
426442 }
443+
444+ manifest , err = processor .Process ([]byte (manifest ), mr .Get )
445+ if err != nil {
446+ log .Error (err , "failed to process manifest" )
447+
448+ return false , err
449+ }
450+ manifests , err := utilyaml .ToUnstructured ([]byte (manifest ))
451+ if err != nil {
452+ log .Error (err , "failed to convert yaml to unstructured" )
453+
454+ return false , err
455+ }
456+
457+ if len (manifest ) > 1 {
458+ return false , fmt .Errorf ("multiple manifests found: %d" , len (manifests ))
459+ } else if len (manifests ) == 0 {
460+ continue
461+ }
462+
463+ if err := cl .Patch (ctx , & manifests [0 ], client .Apply , client .ForceOwnership , client .FieldOwner (cacheOwner )); err != nil {
464+ errs = append (errs , err )
465+ }
466+ }
467+
468+ if err := kerrors .NewAggregate (errs ); err != nil {
469+ log .Error (err , "failed to apply objects from cache" )
470+
471+ return false , err
427472 }
428473
429474 log .Info ("Applied all objects from cache" )
@@ -433,7 +478,7 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
433478
434479// setCacheHash calculates current provider and configMap hash, and updates it on the configMap.
435480func setCacheHash (ctx context.Context , cl client.Client , provider genericprovider.GenericProvider ) error {
436- configMap , err := providerConfigMap (ctx , cl , provider )
481+ configMap , err := providerCacheConfigMap (ctx , cl , provider )
437482 if err != nil {
438483 return err
439484 }
0 commit comments