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