Skip to content

Commit b15d18b

Browse files
Expose PhaseReconciler in generic phase reconcile functions
Signed-off-by: Danil Grigorev <[email protected]>
1 parent b68088f commit b15d18b

File tree

10 files changed

+42
-62
lines changed

10 files changed

+42
-62
lines changed

cmd/plugin/cmd/utils.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ func init() {
6868
utilruntime.Must(clusterctlv1.AddToScheme(scheme))
6969
}
7070

71-
type genericProvider interface {
72-
ctrlclient.Object
73-
operatorv1.GenericProvider
74-
}
75-
76-
type genericProviderList interface {
77-
ctrlclient.ObjectList
78-
operatorv1.GenericProviderList
79-
}
80-
8171
// CreateKubeClient creates a kubernetes client from provided kubeconfig and kubecontext.
8272
func CreateKubeClient(kubeconfigPath, kubeconfigContext string) (ctrlclient.Client, error) {
8373
// Use specified kubeconfig path and context

internal/controller/phases/phases.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,14 @@ func (p *PhaseReconciler[P, G]) InitializePhaseReconciler(ctx context.Context, p
206206
}
207207

208208
// Load provider's secret and config url.
209-
p.configClient, err = configclient.New(ctx, "", configclient.InjectReader(reader))
209+
p.ConfigClient, err = configclient.New(ctx, "", configclient.InjectReader(reader))
210210
if err != nil {
211211
return reconcile.Result{}, wrapPhaseError(err, "failed to load the secret reader", operatorv1.ProviderInstalledCondition)
212212
}
213213

214214
// Get returns the configuration for the provider with a given name/type.
215215
// This is done using clusterctl internal API types.
216-
p.providerConfig, err = p.configClient.Providers().Get(phase.GetProvider().GetName(), phase.ClusterctlProviderType())
216+
p.ProviderConfig, err = p.ConfigClient.Providers().Get(phase.GetProvider().GetName(), phase.ClusterctlProviderType())
217217
if err != nil {
218218
return reconcile.Result{}, wrapPhaseError(err, operatorv1.UnknownProviderReason, operatorv1.ProviderInstalledCondition)
219219
}
@@ -245,14 +245,14 @@ func (p *PhaseReconciler[P, G]) Load(ctx context.Context, phase G) (reconcile.Re
245245
return reconcile.Result{}, wrapPhaseError(err, "failed to load additional manifests", operatorv1.ProviderInstalledCondition)
246246
}
247247

248-
p.repo, err = configmapRepository(ctx, phase.GetClient(), labelSelector, phase.GetProvider().GetNamespace(), additionalManifests)
248+
p.Repo, err = configmapRepository(ctx, phase.GetClient(), labelSelector, phase.GetProvider().GetNamespace(), additionalManifests)
249249
if err != nil {
250250
return reconcile.Result{}, wrapPhaseError(err, "failed to load the repository", operatorv1.ProviderInstalledCondition)
251251
}
252252

253253
if spec.Version == "" {
254254
// User didn't set the version, so we need to find the latest one from the matching config maps.
255-
repoVersions, err := p.repo.GetVersions(ctx)
255+
repoVersions, err := p.Repo.GetVersions(ctx)
256256
if err != nil {
257257
return reconcile.Result{}, wrapPhaseError(err, fmt.Sprintf("failed to get a list of available versions for provider %q", phase.GetProvider().GetName()), operatorv1.ProviderInstalledCondition)
258258
}
@@ -267,7 +267,7 @@ func (p *PhaseReconciler[P, G]) Load(ctx context.Context, phase G) (reconcile.Re
267267
}
268268

269269
// Store some provider specific inputs for passing it to clusterctl library
270-
p.options = repository.ComponentsOptions{
270+
p.Options = repository.ComponentsOptions{
271271
TargetNamespace: phase.GetProvider().GetNamespace(),
272272
SkipTemplateProcess: false,
273273
Version: spec.Version,
@@ -455,7 +455,7 @@ func getComponentsData(cm corev1.ConfigMap) (string, error) {
455455
func (p *PhaseReconciler[P, G]) validateRepoCAPIVersion(ctx context.Context, phase G) error {
456456
name := phase.GetProvider().GetName()
457457

458-
file, err := p.repo.GetFile(ctx, p.options.Version, metadataFile)
458+
file, err := p.Repo.GetFile(ctx, p.Options.Version, metadataFile)
459459
if err != nil {
460460
return fmt.Errorf("failed to read %q from the repository for provider %q: %w", metadataFile, name, err)
461461
}
@@ -469,21 +469,21 @@ func (p *PhaseReconciler[P, G]) validateRepoCAPIVersion(ctx context.Context, pha
469469
}
470470

471471
// Gets the contract for the target release.
472-
targetVersion, err := versionutil.ParseSemantic(p.options.Version)
472+
targetVersion, err := versionutil.ParseSemantic(p.Options.Version)
473473
if err != nil {
474474
return fmt.Errorf("failed to parse current version for the %s provider: %w", name, err)
475475
}
476476

477477
releaseSeries := latestMetadata.GetReleaseSeriesForVersion(targetVersion)
478478
if releaseSeries == nil {
479-
return fmt.Errorf("invalid provider metadata: version %s for the provider %s does not match any release series", p.options.Version, name)
479+
return fmt.Errorf("invalid provider metadata: version %s for the provider %s does not match any release series", p.Options.Version, name)
480480
}
481481

482482
if releaseSeries.Contract != "v1alpha4" && releaseSeries.Contract != "v1beta1" {
483483
return fmt.Errorf(capiVersionIncompatibilityMessage, clusterv1.GroupVersion.Version, releaseSeries.Contract, name)
484484
}
485485

486-
p.contract = releaseSeries.Contract
486+
p.Contract = releaseSeries.Contract
487487

488488
return nil
489489
}
@@ -494,35 +494,35 @@ func (p *PhaseReconciler[P, G]) Fetch(ctx context.Context, phase G) (reconcile.R
494494
log.Info("Fetching provider")
495495

496496
// Fetch the provider components yaml file from the provided repository GitHub/GitLab/ConfigMap.
497-
componentsFile, err := p.repo.GetFile(ctx, p.options.Version, p.repo.ComponentsPath())
497+
componentsFile, err := p.Repo.GetFile(ctx, p.Options.Version, p.Repo.ComponentsPath())
498498
if err != nil {
499-
err = fmt.Errorf("failed to read %q from provider's repository %q: %w", p.repo.ComponentsPath(), p.providerConfig.ManifestLabel(), err)
499+
err = fmt.Errorf("failed to read %q from provider's repository %q: %w", p.Repo.ComponentsPath(), p.ProviderConfig.ManifestLabel(), err)
500500

501501
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason, operatorv1.ProviderInstalledCondition)
502502
}
503503

504504
// Generate a set of new objects using the clusterctl library. NewComponents() will do the yaml processing,
505505
// like ensure all the provider components are in proper namespace, replace variables, etc. See the clusterctl
506506
// documentation for more details.
507-
p.components, err = repository.NewComponents(repository.ComponentsInput{
508-
Provider: p.providerConfig,
509-
ConfigClient: p.configClient,
507+
p.Components, err = repository.NewComponents(repository.ComponentsInput{
508+
Provider: p.ProviderConfig,
509+
ConfigClient: p.ConfigClient,
510510
Processor: yamlprocessor.NewSimpleProcessor(),
511511
RawYaml: componentsFile,
512-
Options: p.options,
512+
Options: p.Options,
513513
})
514514
if err != nil {
515515
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason, operatorv1.ProviderInstalledCondition)
516516
}
517517

518518
// ProviderSpec provides fields for customizing the provider deployment options.
519519
// We can use clusterctl library to apply this customizations.
520-
if err := repository.AlterComponents(p.components, customizeObjectsFn(phase.GetProvider())); err != nil {
520+
if err := repository.AlterComponents(p.Components, customizeObjectsFn(phase.GetProvider())); err != nil {
521521
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason, operatorv1.ProviderInstalledCondition)
522522
}
523523

524524
// Apply patches to the provider components if specified.
525-
if err := repository.AlterComponents(p.components, applyPatches(ctx, phase.GetProvider())); err != nil {
525+
if err := repository.AlterComponents(p.Components, applyPatches(ctx, phase.GetProvider())); err != nil {
526526
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason, operatorv1.ProviderInstalledCondition)
527527
}
528528

@@ -579,7 +579,7 @@ func (p *PhaseReconciler[P, G]) Install(ctx context.Context, phase G) (reconcile
579579

580580
log.Info("Installing provider")
581581

582-
if err := clusterClient.ProviderComponents().Create(ctx, p.components.Objs()); err != nil {
582+
if err := clusterClient.ProviderComponents().Create(ctx, p.Components.Objs()); err != nil {
583583
reason := "Install failed"
584584
if wait.Interrupted(err) {
585585
reason = "Timed out waiting for deployment to become ready"
@@ -596,8 +596,8 @@ func (p *PhaseReconciler[P, G]) Install(ctx context.Context, phase G) (reconcile
596596

597597
func (p *PhaseReconciler[P, G]) ReportStatus(_ context.Context, phase G) (reconcile.Result, error) {
598598
status := phase.GetProvider().GetStatus()
599-
status.Contract = &p.contract
600-
installedVersion := p.components.Version()
599+
status.Contract = &p.Contract
600+
installedVersion := p.Components.Version()
601601
status.InstalledVersion = &installedVersion
602602
phase.GetProvider().SetStatus(status)
603603

@@ -649,9 +649,9 @@ func (p *PhaseReconciler[P, G]) Delete(ctx context.Context, phase G) (reconcile.
649649
}
650650

651651
func (p *PhaseReconciler[P, G]) repositoryProxy(ctx context.Context, provider configclient.Provider, configClient configclient.Client, options ...repository.Option) (repository.Client, error) {
652-
injectRepo := p.repo
652+
injectRepo := p.Repo
653653

654-
if !provider.SameAs(p.providerConfig) {
654+
if !provider.SameAs(p.ProviderConfig) {
655655
genericProvider, err := GetGenericProvider(ctx, p.ctrlClient, provider)
656656
if err != nil {
657657
return nil, wrapPhaseError(err, "unable to find generic provider for configclient "+string(provider.Type())+": "+provider.Name(), operatorv1.ProviderUpgradedCondition)
@@ -679,7 +679,7 @@ func (p *PhaseReconciler[P, G]) repositoryProxy(ctx context.Context, provider co
679679
return nil, err
680680
}
681681

682-
return proxy.RepositoryProxy{Client: cl, RepositoryComponents: p.components}, nil
682+
return proxy.RepositoryProxy{Client: cl, RepositoryComponents: p.Components}, nil
683683
}
684684

685685
// GetGenericProvider returns the first of generic providers matching the type and the name from the configclient.Provider.
@@ -705,7 +705,7 @@ func GetGenericProvider(ctx context.Context, cl client.Client, provider configcl
705705

706706
// newClusterClient returns a clusterctl client for interacting with management cluster.
707707
func newClusterClient[P generic.Provider, G generic.Group[P]](p *PhaseReconciler[P, G], phase G) cluster.Client {
708-
return cluster.New(cluster.Kubeconfig{}, p.configClient, cluster.InjectProxy(&proxy.ControllerProxy{
708+
return cluster.New(cluster.Kubeconfig{}, p.ConfigClient, cluster.InjectProxy(&proxy.ControllerProxy{
709709
CtrlClient: proxy.ClientProxy{
710710
Client: phase.GetClient(),
711711
ListProviders: listProviders,

internal/controller/phases/phases_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,8 @@ releaseSeries:
587587
fakeclient := fake.NewClientBuilder().WithScheme(setupScheme()).WithObjects(tt.genericProviders...).Build()
588588
p := &PhaseReconciler[*operatorv1.CoreProvider, Phase[*operatorv1.CoreProvider]]{
589589
ctrlClient: fakeclient,
590-
providerConfig: coreProvider,
591-
repo: repository.NewMemoryRepository(),
590+
ProviderConfig: coreProvider,
591+
Repo: repository.NewMemoryRepository(),
592592
}
593593

594594
for i := range tt.configMaps {
@@ -597,7 +597,7 @@ releaseSeries:
597597

598598
if tt.defaultRepository {
599599
var err error
600-
p.repo, err = configmapRepository(ctx, fakeclient, &metav1.LabelSelector{
600+
p.Repo, err = configmapRepository(ctx, fakeclient, &metav1.LabelSelector{
601601
MatchLabels: map[string]string{
602602
operatorManagedLabel: "true",
603603
},

internal/controller/providers/addon.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ import (
2727
)
2828

2929
type AddonProviderReconciler struct {
30-
generic.ProviderReconciler[*operatorv1.AddonProvider]
30+
*CommonProviderReconciler[*operatorv1.AddonProvider]
3131
}
3232

3333
func NewAddonProviderReconciler(conn generic.Connector) generic.ProviderReconciler[*operatorv1.AddonProvider] {
3434
return &AddonProviderReconciler{
35-
ProviderReconciler: NewCommonProviderReconciler[*operatorv1.AddonProvider](conn),
35+
CommonProviderReconciler: NewCommonProviderReconciler[*operatorv1.AddonProvider](conn),
3636
}
3737
}
3838

internal/controller/providers/bootstrap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ import (
2727
)
2828

2929
type BootstrapProviderReconciler struct {
30-
generic.ProviderReconciler[*operatorv1.BootstrapProvider]
30+
*CommonProviderReconciler[*operatorv1.BootstrapProvider]
3131
}
3232

3333
func NewBootstrapProviderReconciler(conn generic.Connector) generic.ProviderReconciler[*operatorv1.BootstrapProvider] {
3434
return &BootstrapProviderReconciler{
35-
ProviderReconciler: NewCommonProviderReconciler[*operatorv1.BootstrapProvider](conn),
35+
CommonProviderReconciler: NewCommonProviderReconciler[*operatorv1.BootstrapProvider](conn),
3636
}
3737
}
3838

internal/controller/providers/control_plane.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import (
2626
)
2727

2828
type ControlPlaneProviderReconciler struct {
29-
generic.ProviderReconciler[*operatorv1.ControlPlaneProvider]
29+
*CommonProviderReconciler[*operatorv1.ControlPlaneProvider]
3030
}
3131

3232
func NewControlPlaneProviderReconciler(conn generic.Connector) generic.ProviderReconciler[*operatorv1.ControlPlaneProvider] {
3333
return &ControlPlaneProviderReconciler{
34-
ProviderReconciler: NewCommonProviderReconciler[*operatorv1.ControlPlaneProvider](conn),
34+
CommonProviderReconciler: NewCommonProviderReconciler[*operatorv1.ControlPlaneProvider](conn),
3535
}
3636
}
3737

internal/controller/providers/core.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,15 @@ import (
3030
configclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
3131
"sigs.k8s.io/cluster-api/util/conditions"
3232
ctrl "sigs.k8s.io/controller-runtime"
33-
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3433
)
3534

3635
type CoreProviderReconciler struct {
37-
generic.ProviderReconciler[*operatorv1.CoreProvider]
38-
r *GenericProviderReconciler[*operatorv1.CoreProvider]
36+
*GenericProviderReconciler[*operatorv1.CoreProvider]
3937
}
4038

4139
func NewCoreProviderReconciler(conn generic.Connector) generic.ProviderReconciler[*operatorv1.CoreProvider] {
42-
r := NewGenericProviderReconciler[*operatorv1.CoreProvider](conn)
4340
return &CoreProviderReconciler{
44-
ProviderReconciler: r,
45-
r: r,
41+
GenericProviderReconciler: NewGenericProviderReconciler[*operatorv1.CoreProvider](conn),
4642
}
4743
}
4844

@@ -52,8 +48,8 @@ func (r *CoreProviderReconciler) PreflightChecks(
5248
provider *operatorv1.CoreProvider,
5349
) []generic.ReconcileFn[*operatorv1.CoreProvider, generic.Group[*operatorv1.CoreProvider]] {
5450
return append(
55-
generic.NewReconcileFnList(r.corePreflightChecks, r.testMyStuff),
56-
r.ProviderReconciler.PreflightChecks(ctx, provider)...,
51+
generic.NewReconcileFnList(r.corePreflightChecks),
52+
r.GenericProviderReconciler.PreflightChecks(ctx, provider)...,
5753
)
5854
}
5955

@@ -119,9 +115,3 @@ func (r *CoreProviderReconciler) corePreflightChecks(ctx context.Context, phase
119115

120116
return ctrl.Result{}, nil
121117
}
122-
123-
func (r *CoreProviderReconciler) testMyStuff(ctx context.Context, phase generic.Group[*operatorv1.CoreProvider]) (reconcile.Result, error) {
124-
// r.ProviderReconciler.
125-
// x := phase.GetProvider()
126-
return ctrl.Result{}, nil
127-
}

internal/controller/providers/generic_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ type CommonProviderReconciler[P generic.Provider] struct {
121121
generic.ProviderReconciler[P]
122122
}
123123

124-
func NewCommonProviderReconciler[P generic.Provider](conn generic.Connector) generic.ProviderReconciler[P] {
124+
func NewCommonProviderReconciler[P generic.Provider](conn generic.Connector) *CommonProviderReconciler[P] {
125125
return &CommonProviderReconciler[P]{
126126
ProviderReconciler: NewGenericProviderReconciler[P](conn),
127127
}

internal/controller/providers/infrastructure.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import (
2626
)
2727

2828
type InfrastructureProviderReconciler struct {
29-
generic.ProviderReconciler[*operatorv1.InfrastructureProvider]
29+
*CommonProviderReconciler[*operatorv1.InfrastructureProvider]
3030
}
3131

3232
func NewInfrastructureProviderReconciler(conn generic.Connector) generic.ProviderReconciler[*operatorv1.InfrastructureProvider] {
3333
return &InfrastructureProviderReconciler{
34-
ProviderReconciler: NewCommonProviderReconciler[*operatorv1.InfrastructureProvider](conn),
34+
CommonProviderReconciler: NewCommonProviderReconciler[*operatorv1.InfrastructureProvider](conn),
3535
}
3636
}
3737

internal/controller/providers/ipam.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ import (
2727
)
2828

2929
type IPAMProviderReconciler struct {
30-
generic.ProviderReconciler[*operatorv1.IPAMProvider]
30+
*CommonProviderReconciler[*operatorv1.IPAMProvider]
3131
}
3232

3333
func NewIPAMProviderReconciler(conn generic.Connector) generic.ProviderReconciler[*operatorv1.IPAMProvider] {
3434
return &IPAMProviderReconciler{
35-
ProviderReconciler: NewCommonProviderReconciler[*operatorv1.IPAMProvider](conn),
35+
CommonProviderReconciler: NewCommonProviderReconciler[*operatorv1.IPAMProvider](conn),
3636
}
3737
}
3838

0 commit comments

Comments
 (0)