Skip to content

Commit 7b220c3

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

File tree

11 files changed

+49
-69
lines changed

11 files changed

+49
-69
lines changed

cmd/plugin/cmd/utils.go

-10
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,6 @@ func init() {
6060
utilruntime.Must(clusterctlv1.AddToScheme(scheme))
6161
}
6262

63-
type genericProvider interface {
64-
ctrlclient.Object
65-
operatorv1.GenericProvider
66-
}
67-
68-
type genericProviderList interface {
69-
ctrlclient.ObjectList
70-
operatorv1.GenericProviderList
71-
}
72-
7363
// CreateKubeClient creates a kubernetes client from provided kubeconfig and kubecontext.
7464
func CreateKubeClient(kubeconfigPath, kubeconfigContext string) (ctrlclient.Client, error) {
7565
// Use specified kubeconfig path and context

internal/controller/phases/manifests_downloader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (p *PhaseReconciler[P, G]) DownloadManifests(ctx context.Context, phase G)
6565

6666
log.Info("Downloading provider manifests")
6767

68-
repo, err := util.RepositoryFactory(ctx, p.providerConfig, p.configClient.Variables())
68+
repo, err := util.RepositoryFactory(ctx, p.ProviderConfig, p.ConfigClient.Variables())
6969
if err != nil {
7070
err = fmt.Errorf("failed to create repo from provider url for provider %q: %w", phase.GetProvider().GetName(), err)
7171

internal/controller/phases/phases.go

+30-30
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ const (
5757
// helps to iterate through provider reconciliation phases.
5858
type PhaseReconciler[P generic.Provider, G generic.Group[P]] struct {
5959
ctrlClient client.Client
60-
repo repository.Repository
61-
contract string
62-
options repository.ComponentsOptions
63-
providerConfig configclient.Provider
64-
configClient configclient.Client
65-
components repository.Components
60+
Repo repository.Repository
61+
Contract string
62+
Options repository.ComponentsOptions
63+
ProviderConfig configclient.Provider
64+
ConfigClient configclient.Client
65+
Components repository.Components
6666
}
6767

6868
type Phase[P generic.Provider] struct {
@@ -180,14 +180,14 @@ func (p *PhaseReconciler[P, G]) InitializePhaseReconciler(ctx context.Context, p
180180
}
181181

182182
// Load provider's secret and config url.
183-
p.configClient, err = configclient.New(ctx, "", configclient.InjectReader(reader))
183+
p.ConfigClient, err = configclient.New(ctx, "", configclient.InjectReader(reader))
184184
if err != nil {
185185
return reconcile.Result{}, wrapPhaseError(err, "failed to load the secret reader", operatorv1.ProviderInstalledCondition)
186186
}
187187

188188
// Get returns the configuration for the provider with a given name/type.
189189
// This is done using clusterctl internal API types.
190-
p.providerConfig, err = p.configClient.Providers().Get(phase.GetProvider().GetName(), phase.ClusterctlProviderType())
190+
p.ProviderConfig, err = p.ConfigClient.Providers().Get(phase.GetProvider().GetName(), phase.ClusterctlProviderType())
191191
if err != nil {
192192
return reconcile.Result{}, wrapPhaseError(err, operatorv1.UnknownProviderReason, operatorv1.ProviderInstalledCondition)
193193
}
@@ -219,14 +219,14 @@ func (p *PhaseReconciler[P, G]) Load(ctx context.Context, phase G) (reconcile.Re
219219
return reconcile.Result{}, wrapPhaseError(err, "failed to load additional manifests", operatorv1.ProviderInstalledCondition)
220220
}
221221

222-
p.repo, err = configmapRepository(ctx, phase.GetClient(), labelSelector, phase.GetProvider().GetNamespace(), additionalManifests)
222+
p.Repo, err = configmapRepository(ctx, phase.GetClient(), labelSelector, phase.GetProvider().GetNamespace(), additionalManifests)
223223
if err != nil {
224224
return reconcile.Result{}, wrapPhaseError(err, "failed to load the repository", operatorv1.ProviderInstalledCondition)
225225
}
226226

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

243243
// Store some provider specific inputs for passing it to clusterctl library
244-
p.options = repository.ComponentsOptions{
244+
p.Options = repository.ComponentsOptions{
245245
TargetNamespace: phase.GetProvider().GetNamespace(),
246246
SkipTemplateProcess: false,
247247
Version: spec.Version,
@@ -422,7 +422,7 @@ func getComponentsData(cm corev1.ConfigMap) (string, error) {
422422
func (p *PhaseReconciler[P, G]) validateRepoCAPIVersion(ctx context.Context, phase G) error {
423423
name := phase.GetProvider().GetName()
424424

425-
file, err := p.repo.GetFile(ctx, p.options.Version, metadataFile)
425+
file, err := p.Repo.GetFile(ctx, p.Options.Version, metadataFile)
426426
if err != nil {
427427
return fmt.Errorf("failed to read %q from the repository for provider %q: %w", metadataFile, name, err)
428428
}
@@ -436,21 +436,21 @@ func (p *PhaseReconciler[P, G]) validateRepoCAPIVersion(ctx context.Context, pha
436436
}
437437

438438
// Gets the contract for the target release.
439-
targetVersion, err := versionutil.ParseSemantic(p.options.Version)
439+
targetVersion, err := versionutil.ParseSemantic(p.Options.Version)
440440
if err != nil {
441441
return fmt.Errorf("failed to parse current version for the %s provider: %w", name, err)
442442
}
443443

444444
releaseSeries := latestMetadata.GetReleaseSeriesForVersion(targetVersion)
445445
if releaseSeries == nil {
446-
return fmt.Errorf("invalid provider metadata: version %s for the provider %s does not match any release series", p.options.Version, name)
446+
return fmt.Errorf("invalid provider metadata: version %s for the provider %s does not match any release series", p.Options.Version, name)
447447
}
448448

449449
if releaseSeries.Contract != "v1alpha4" && releaseSeries.Contract != "v1beta1" {
450450
return fmt.Errorf(capiVersionIncompatibilityMessage, clusterv1.GroupVersion.Version, releaseSeries.Contract, name)
451451
}
452452

453-
p.contract = releaseSeries.Contract
453+
p.Contract = releaseSeries.Contract
454454

455455
return nil
456456
}
@@ -461,35 +461,35 @@ func (p *PhaseReconciler[P, G]) Fetch(ctx context.Context, phase G) (reconcile.R
461461
log.Info("Fetching provider")
462462

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

468468
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason, operatorv1.ProviderInstalledCondition)
469469
}
470470

471471
// Generate a set of new objects using the clusterctl library. NewComponents() will do the yaml processing,
472472
// like ensure all the provider components are in proper namespace, replace variables, etc. See the clusterctl
473473
// documentation for more details.
474-
p.components, err = repository.NewComponents(repository.ComponentsInput{
475-
Provider: p.providerConfig,
476-
ConfigClient: p.configClient,
474+
p.Components, err = repository.NewComponents(repository.ComponentsInput{
475+
Provider: p.ProviderConfig,
476+
ConfigClient: p.ConfigClient,
477477
Processor: yamlprocessor.NewSimpleProcessor(),
478478
RawYaml: componentsFile,
479-
Options: p.options,
479+
Options: p.Options,
480480
})
481481
if err != nil {
482482
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason, operatorv1.ProviderInstalledCondition)
483483
}
484484

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

491491
// Apply patches to the provider components if specified.
492-
if err := repository.AlterComponents(p.components, applyPatches(ctx, phase.GetProvider())); err != nil {
492+
if err := repository.AlterComponents(p.Components, applyPatches(ctx, phase.GetProvider())); err != nil {
493493
return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason, operatorv1.ProviderInstalledCondition)
494494
}
495495

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

542542
log.Info("Installing provider")
543543

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

559559
func (p *PhaseReconciler[P, G]) ReportStatus(_ context.Context, phase G) (reconcile.Result, error) {
560560
status := phase.GetProvider().GetStatus()
561-
status.Contract = &p.contract
562-
installedVersion := p.components.Version()
561+
status.Contract = &p.Contract
562+
installedVersion := p.Components.Version()
563563
status.InstalledVersion = &installedVersion
564564
phase.GetProvider().SetStatus(status)
565565

@@ -589,9 +589,9 @@ func (p *PhaseReconciler[P, G]) Delete(ctx context.Context, phase G) (reconcile.
589589
}
590590

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

594-
if !provider.SameAs(p.providerConfig) {
594+
if !provider.SameAs(p.ProviderConfig) {
595595
genericProvider, err := GetGenericProvider(ctx, p.ctrlClient, provider)
596596
if err != nil {
597597
return nil, wrapPhaseError(err, "unable to find generic provider for configclient "+string(provider.Type())+": "+provider.Name(), operatorv1.ProviderUpgradedCondition)
@@ -619,7 +619,7 @@ func (p *PhaseReconciler[P, G]) repositoryProxy(ctx context.Context, provider co
619619
return nil, err
620620
}
621621

622-
return proxy.RepositoryProxy{Client: cl, RepositoryComponents: p.components}, nil
622+
return proxy.RepositoryProxy{Client: cl, RepositoryComponents: p.Components}, nil
623623
}
624624

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

646646
// newClusterClient returns a clusterctl client for interacting with management cluster.
647647
func newClusterClient[P generic.Provider, G generic.Group[P]](p *PhaseReconciler[P, G], phase G) cluster.Client {
648-
return cluster.New(cluster.Kubeconfig{}, p.configClient, cluster.InjectProxy(&proxy.ControllerProxy{
648+
return cluster.New(cluster.Kubeconfig{}, p.ConfigClient, cluster.InjectProxy(&proxy.ControllerProxy{
649649
CtrlClient: proxy.ClientProxy{
650650
Client: phase.GetClient(),
651651
ListProviders: listProviders,

internal/controller/phases/phases_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -586,16 +586,16 @@ releaseSeries:
586586
fakeclient := fake.NewClientBuilder().WithScheme(setupScheme()).WithObjects(tt.genericProviders...).Build()
587587
p := &PhaseReconciler[*operatorv1.CoreProvider, Phase[*operatorv1.CoreProvider]]{
588588
ctrlClient: fakeclient,
589-
providerConfig: coreProvider,
590-
repo: repository.NewMemoryRepository(),
589+
ProviderConfig: coreProvider,
590+
Repo: repository.NewMemoryRepository(),
591591
}
592592

593593
for i := range tt.configMaps {
594594
g.Expect(fakeclient.Create(ctx, &tt.configMaps[i])).To(Succeed())
595595
}
596596
if tt.defaultRepository {
597597
var err error
598-
p.repo, err = configmapRepository(ctx, fakeclient, &metav1.LabelSelector{
598+
p.Repo, err = configmapRepository(ctx, fakeclient, &metav1.LabelSelector{
599599
MatchLabels: map[string]string{
600600
operatorManagedLabel: "true",
601601
},

internal/controller/providers/addon.go

+2-2
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

+2-2
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

+2-2
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

+4-14
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

+1-1
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

+2-2
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

+2-2
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)