Skip to content

Commit f4cbe27

Browse files
Convert apply from secret to phase
Signed-off-by: Danil-Grigorev <[email protected]>
1 parent 7ef782c commit f4cbe27

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

internal/controller/genericprovider_controller.go

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,17 @@ func (r *GenericProviderReconciler) BuildWithManager(ctx context.Context, mgr ct
9292
reconciler := NewPhaseReconciler(*r, r.Provider, r.ProviderList)
9393

9494
r.ReconcilePhases = []PhaseFn{
95+
reconciler.ApplyFromCache,
9596
reconciler.PreflightChecks,
9697
reconciler.InitializePhaseReconciler,
9798
reconciler.DownloadManifests,
9899
reconciler.Load,
99100
reconciler.Fetch,
101+
reconciler.Store,
100102
reconciler.Upgrade,
101103
reconciler.Install,
102104
reconciler.ReportStatus,
105+
reconciler.Finalize,
103106
}
104107

105108
r.DeletePhases = []PhaseFn{
@@ -177,13 +180,7 @@ func (r *GenericProviderReconciler) Reconcile(ctx context.Context, req reconcile
177180
return ctrl.Result{}, err
178181
}
179182

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 {
187184
log.Info("No changes detected, skipping further steps")
188185

189186
return ctrl.Result{}, nil
@@ -355,39 +352,39 @@ func calculateHash(ctx context.Context, k8sClient client.Client, provider generi
355352
return fmt.Sprintf("%x", hash.Sum(nil)), err
356353
}
357354

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) {
360357
log := log.FromContext(ctx)
361358

362359
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) {
364361
// secret does not exist, nothing to apply
365-
return false, nil
362+
return &Result{}, nil
366363
} else if err != nil {
367364
log.Error(err, "failed to get provider cache")
368365

369-
return false, fmt.Errorf("failed to get provider cache: %w", err)
366+
return &Result{}, fmt.Errorf("failed to get provider cache: %w", err)
370367
}
371368

372369
// calculate combined hash for provider and config map cache
373370
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 {
375372
log.Error(err, "failed to calculate provider hash")
376373

377-
return false, err
374+
return &Result{}, err
378375
}
379376

380377
if err := addObjectToHash(hash, secret.Data); err != nil {
381378
log.Error(err, "failed to calculate config map hash")
382379

383-
return false, err
380+
return &Result{}, err
384381
}
385382

386383
cacheHash := fmt.Sprintf("%x", hash.Sum(nil))
387384
if secret.GetAnnotations()[appliedSpecHashAnnotation] != cacheHash {
388385
log.Info("Provider or cache state has changed", "cacheHash", cacheHash, "providerHash", secret.GetAnnotations()[appliedSpecHashAnnotation])
389386

390-
return false, nil
387+
return &Result{}, nil
391388
}
392389

393390
log.Info("Applying provider configuration from cache")
@@ -397,12 +394,12 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
397394
mr := configclient.NewMemoryReader()
398395

399396
if err := mr.Init(ctx, ""); err != nil {
400-
return false, err
397+
return &Result{}, err
401398
}
402399

403400
// 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
406403
}
407404

408405
for _, manifest := range secret.Data {
@@ -416,11 +413,11 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
416413
if err != nil {
417414
log.Error(err, "failed to convert yaml to unstructured")
418415

419-
return false, err
416+
return &Result{}, err
420417
}
421418

422419
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 {
424421
errs = append(errs, err)
425422
}
426423
}
@@ -435,7 +432,7 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
435432
if err != nil {
436433
log.Error(err, "failed to decompress yaml")
437434

438-
return false, err
435+
return &Result{}, err
439436
}
440437

441438
manifests := []unstructured.Unstructured{}
@@ -444,11 +441,11 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
444441
if err != nil {
445442
log.Error(err, "failed to convert yaml to unstructured")
446443

447-
return false, err
444+
return &Result{}, err
448445
}
449446

450447
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 {
452449
errs = append(errs, err)
453450
}
454451
}
@@ -457,12 +454,12 @@ func applyFromCache(ctx context.Context, cl client.Client, provider genericprovi
457454
if err := kerrors.NewAggregate(errs); err != nil {
458455
log.Error(err, "failed to apply objects from cache")
459456

460-
return false, err
457+
return &Result{}, err
461458
}
462459

463460
log.Info("Applied all objects from cache")
464461

465-
return true, nil
462+
return &Result{Completed: true}, nil
466463
}
467464

468465
// setCacheHash calculates current provider and secret hash, and updates it on the secret.

internal/controller/manifests_downloader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ func (p *PhaseReconciler) checkConfigMapExists(ctx context.Context, labelSelecto
140140
return len(configMapList.Items) == 1, nil
141141
}
142142

143-
// finalize applies combined hash to a configMap, in order to mark provider provisioning completed.
144-
func (p *phaseReconciler) finalize(ctx context.Context) (reconcile.Result, error) {
143+
// Finalize applies combined hash to a configMap, in order to mark provider provisioning completed.
144+
func (p *PhaseReconciler) Finalize(ctx context.Context) (*Result, error) {
145145
err := setCacheHash(ctx, p.ctrlClient, p.provider)
146146
if err != nil {
147147
ctrl.LoggerFrom(ctx).V(5).Error(err, "Failed to update providers ConfigMap hash")
148148
}
149149

150-
return reconcile.Result{}, wrapPhaseError(err, "failed to update providers ConfigMap hash", operatorv1.ProviderInstalledCondition)
150+
return &Result{}, wrapPhaseError(err, "failed to update providers ConfigMap hash", operatorv1.ProviderInstalledCondition)
151151
}
152152

153153
// prepareConfigMapLabels returns labels that identify a config map with downloaded manifests.

internal/controller/phases.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import (
4848
ctrl "sigs.k8s.io/controller-runtime"
4949
"sigs.k8s.io/controller-runtime/pkg/client"
5050
"sigs.k8s.io/controller-runtime/pkg/log"
51-
"sigs.k8s.io/controller-runtime/pkg/reconcile"
5251
)
5352

5453
// fakeURL is the stub url for custom providers, missing from clusterctl repository.
@@ -566,8 +565,8 @@ func (p *PhaseReconciler) Fetch(ctx context.Context) (*Result, error) {
566565
return &Result{}, nil
567566
}
568567

569-
// store stores the provider components in the cache.
570-
func (p *phaseReconciler) store(ctx context.Context) (reconcile.Result, error) {
568+
// Store stores the provider components in the cache.
569+
func (p *PhaseReconciler) Store(ctx context.Context) (*Result, error) {
571570
log := ctrl.LoggerFrom(ctx)
572571
log.Info("Storing provider in cache")
573572

@@ -576,7 +575,7 @@ func (p *phaseReconciler) store(ctx context.Context) (reconcile.Result, error) {
576575
log.Error(err, "cannot fetch kind of the Secret resource")
577576
err = fmt.Errorf("cannot fetch kind of the Secret resource: %w", err)
578577

579-
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsCustomizationErrorReason, operatorv1.ProviderInstalledCondition)
578+
return &Result{}, wrapPhaseError(err, operatorv1.ComponentsCustomizationErrorReason, operatorv1.ProviderInstalledCondition)
580579
}
581580

582581
secret := &corev1.Secret{
@@ -610,13 +609,13 @@ func (p *phaseReconciler) store(ctx context.Context) (reconcile.Result, error) {
610609

611610
manifests, err := apijson.Marshal(addNamespaceIfMissing(p.components.Objs(), p.provider.GetNamespace()))
612611
if err != nil {
613-
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsCustomizationErrorReason, operatorv1.ProviderInstalledCondition)
612+
return &Result{}, wrapPhaseError(err, operatorv1.ComponentsCustomizationErrorReason, operatorv1.ProviderInstalledCondition)
614613
}
615614

616615
if p.needsCompression {
617616
var buf bytes.Buffer
618617
if err := compressData(&buf, manifests); err != nil {
619-
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsCustomizationErrorReason, operatorv1.ProviderInstalledCondition)
618+
return &Result{}, wrapPhaseError(err, operatorv1.ComponentsCustomizationErrorReason, operatorv1.ProviderInstalledCondition)
620619
}
621620

622621
secret.Data["cache"] = buf.Bytes()
@@ -627,10 +626,10 @@ func (p *phaseReconciler) store(ctx context.Context) (reconcile.Result, error) {
627626
if err := p.ctrlClient.Patch(ctx, secret, client.Apply, client.ForceOwnership, client.FieldOwner(cacheOwner)); err != nil {
628627
log.Error(err, "failed to apply cache config map")
629628

630-
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsCustomizationErrorReason, operatorv1.ProviderInstalledCondition)
629+
return &Result{}, wrapPhaseError(err, operatorv1.ComponentsCustomizationErrorReason, operatorv1.ProviderInstalledCondition)
631630
}
632631

633-
return reconcile.Result{}, nil
632+
return &Result{}, nil
634633
}
635634

636635
// addNamespaceIfMissing adda a Namespace object if missing (this ensure the targetNamespace will be created).

0 commit comments

Comments
 (0)