Skip to content

Commit 74196f4

Browse files
Lint, doc fixes and refactoring
Signed-off-by: Danil-Grigorev <[email protected]>
1 parent 304b67d commit 74196f4

File tree

3 files changed

+123
-145
lines changed

3 files changed

+123
-145
lines changed

cmd/plugin/cmd/init.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,6 @@ func templateGenericProvider(providerType clusterctlv1.ProviderType, providerInp
497497
spec := provider.GetSpec()
498498
spec.Version = version
499499
provider.SetSpec(spec)
500-
} else {
501-
version = latestVersion
502500
}
503501

504502
// Set config secret

cmd/plugin/cmd/preload.go

Lines changed: 50 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
"strings"
2323

2424
"github.com/spf13/cobra"
25-
v1 "k8s.io/api/core/v1"
25+
corev1 "k8s.io/api/core/v1"
2626
apierrors "k8s.io/apimachinery/pkg/api/errors"
2727
"k8s.io/apimachinery/pkg/api/meta"
2828
kerrors "k8s.io/apimachinery/pkg/util/errors"
2929
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
30-
"sigs.k8s.io/cluster-api-operator/internal/controller"
30+
providercontroller "sigs.k8s.io/cluster-api-operator/internal/controller"
3131
"sigs.k8s.io/cluster-api-operator/util"
3232
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
3333
configclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
@@ -56,32 +56,39 @@ var loadCmd = &cobra.Command{
5656
GroupID: groupManagement,
5757
Short: "Preload providers to a management cluster",
5858
Long: LongDesc(`
59-
Preload provider manifests from an OCI image to a management cluster.
59+
Preload provider manifests to a management cluster.
6060
61-
To prepare an image you can use oras CLI: https://oras.land/docs/installation
61+
To prepare an OCI image you can use oras CLI: https://oras.land/docs/installation
6262
6363
oras push ttl.sh/infrastructure-provider:v2.3.0 --artifact-type application/vnd.acme.config metadata.yaml:text/plain infrastructure-components.yaml:text/plain
64+
65+
Alternatively, for multi-provider OCI artifact, a fully specified name can be used for both metadata and components:
66+
67+
oras push ttl.sh/infrastructure-provider:tag --artifact-type application/vnd.acme.config infrastructure-docker-v1.9.3-metadata.yaml:text/plain infrastructure-docker-v1.9.3-components.yaml:text/plain
6468
`),
6569
Example: Examples(`
66-
# Load CAPI operator manifests from OCI source.
67-
# capioperator preload -u ttl.sh/infrastructure-provider
70+
# Load CAPI operator manifests from OCI source
71+
# capioperator preload --core cluster-api
6872
69-
# Prepare provider ConfigMap, from the given infrastructure provider.
73+
# Load CAPI operator manifests from any provider source in the cluster
74+
# capioperator preload -e
75+
76+
# Prepare provider ConfigMap from OCI, from the given infrastructure provider.
7077
capioperator preload --infrastructure=aws -u ttl.sh/infrastructure-provider
7178
72-
# Prepare provider ConfigMap with a specific version of the given infrastructure provider in the default namespace.
79+
# Prepare provider ConfigMap from OCI with a specific version of the given infrastructure provider in the default namespace.
7380
capioperator preload --infrastructure=aws::v2.3.0 -u ttl.sh/infrastructure-provider
7481
75-
# Prepare provider ConfigMap with a specific namespace and the latest version of the given infrastructure provider.
82+
# Prepare provider ConfigMap from OCI with a specific namespace and the latest version of the given infrastructure provider.
7683
capioperator preload --infrastructure=aws:custom-namespace -u ttl.sh/infrastructure-provider
7784
78-
# Prepare provider ConfigMap with a specific version and namespace of the given infrastructure provider.
85+
# Prepare provider ConfigMap from OCI with a specific version and namespace of the given infrastructure provider.
7986
capioperator preload --infrastructure=aws:custom-namespace:v2.3.0 -u ttl.sh/infrastructure-provider
8087
81-
# Prepare provider ConfigMap with multiple infrastructure providers.
88+
# Prepare provider ConfigMap from OCI with multiple infrastructure providers.
8289
capioperator preload --infrastructure=aws --infrastructure=vsphere -u ttl.sh/infrastructure-provider
8390
84-
# Prepare provider ConfigMap with a custom target namespace for the operator.
91+
# Prepare provider ConfigMap from OCI with a custom target namespace for the operator.
8592
capioperator preload --infrastructure aws --target-namespace foo -u ttl.sh/infrastructure-provider`),
8693
Args: cobra.NoArgs,
8794
RunE: func(cmd *cobra.Command, args []string) error {
@@ -123,7 +130,7 @@ func runPreLoad() error {
123130
return fmt.Errorf("missing configMap artifacts url")
124131
}
125132

126-
configMaps := []*v1.ConfigMap{}
133+
configMaps := []*corev1.ConfigMap{}
127134

128135
// Load Core Provider.
129136
if loadOpts.coreProvider != "" {
@@ -234,8 +241,8 @@ func runPreLoad() error {
234241
return kerrors.NewAggregate(errors)
235242
}
236243

237-
func fetchProviders(ctx context.Context, cl client.Client, providerList genericProviderList) ([]*v1.ConfigMap, error) {
238-
configMaps := []*v1.ConfigMap{}
244+
func fetchProviders(ctx context.Context, cl client.Client, providerList genericProviderList) ([]*corev1.ConfigMap, error) {
245+
configMaps := []*corev1.ConfigMap{}
239246

240247
if err := cl.List(ctx, providerList, client.InNamespace("")); meta.IsNoMatchError(err) || apierrors.IsNotFound(err) {
241248
return configMaps, nil
@@ -246,15 +253,15 @@ func fetchProviders(ctx context.Context, cl client.Client, providerList genericP
246253
}
247254

248255
for _, provider := range providerList.GetItems() {
249-
if provider.GetSpec().FetchConfig == nil || provider.GetSpec().FetchConfig.Selector == nil {
250-
cm, err := providerConfigMap(ctx, provider)
256+
if provider.GetSpec().FetchConfig != nil && provider.GetSpec().FetchConfig.OCI != "" {
257+
cm, err := providercontroller.OCIConfigMap(ctx, provider)
251258
if err != nil {
252259
return configMaps, err
253260
}
254261

255262
configMaps = append(configMaps, cm)
256-
} else if provider.GetSpec().FetchConfig != nil && provider.GetSpec().FetchConfig.OCI != "" {
257-
cm, err := ociConfigMap(ctx, provider)
263+
} else if provider.GetSpec().FetchConfig == nil || provider.GetSpec().FetchConfig.Selector == nil {
264+
cm, err := providerConfigMap(ctx, provider)
258265
if err != nil {
259266
return configMaps, err
260267
}
@@ -266,7 +273,7 @@ func fetchProviders(ctx context.Context, cl client.Client, providerList genericP
266273
return configMaps, nil
267274
}
268275

269-
func templateConfigMap(ctx context.Context, providerType clusterctlv1.ProviderType, url, providerInput, defaultNamespace string) (*v1.ConfigMap, error) {
276+
func templateConfigMap(ctx context.Context, providerType clusterctlv1.ProviderType, url, providerInput, defaultNamespace string) (*corev1.ConfigMap, error) {
270277
provider, err := templateGenericProvider(providerType, providerInput, defaultNamespace, "", "")
271278
if err != nil {
272279
return nil, err
@@ -277,34 +284,38 @@ func templateConfigMap(ctx context.Context, providerType clusterctlv1.ProviderTy
277284
OCI: url,
278285
}
279286

280-
// User didn't set the version, try to get repository default.
281-
if spec.Version == "" {
282-
configClient, err := configclient.New(ctx, "")
283-
if err != nil {
284-
return nil, fmt.Errorf("cannot create config client: %w", err)
285-
}
287+
provider.SetSpec(spec)
286288

287-
providerConfig, err := configClient.Providers().Get(provider.GetName(), util.ClusterctlProviderType(provider))
288-
if err != nil {
289-
if !strings.Contains(err.Error(), "failed to get configuration") {
290-
return nil, err
291-
}
292-
}
289+
if spec.Version != "" {
290+
return providercontroller.OCIConfigMap(ctx, provider)
291+
}
293292

294-
repo, err := util.RepositoryFactory(ctx, providerConfig, configClient.Variables())
295-
if err != nil {
296-
return nil, fmt.Errorf("cannot create repository: %w", err)
293+
// User didn't set the version, try to get repository default.
294+
configClient, err := configclient.New(ctx, "")
295+
if err != nil {
296+
return nil, fmt.Errorf("cannot create config client: %w", err)
297+
}
298+
299+
providerConfig, err := configClient.Providers().Get(provider.GetName(), util.ClusterctlProviderType(provider))
300+
if err != nil {
301+
if !strings.Contains(err.Error(), "failed to get configuration") {
302+
return nil, err
297303
}
304+
}
298305

299-
spec.Version = repo.DefaultVersion()
306+
repo, err := util.RepositoryFactory(ctx, providerConfig, configClient.Variables())
307+
if err != nil {
308+
return nil, fmt.Errorf("cannot create repository: %w", err)
300309
}
301310

311+
spec.Version = repo.DefaultVersion()
312+
302313
provider.SetSpec(spec)
303314

304-
return ociConfigMap(ctx, provider)
315+
return providercontroller.OCIConfigMap(ctx, provider)
305316
}
306317

307-
func providerConfigMap(ctx context.Context, provider operatorv1.GenericProvider) (*v1.ConfigMap, error) {
318+
func providerConfigMap(ctx context.Context, provider operatorv1.GenericProvider) (*corev1.ConfigMap, error) {
308319
mr := configclient.NewMemoryReader()
309320
if err := mr.Init(ctx, ""); err != nil {
310321
return nil, fmt.Errorf("unable to init memory reader: %w", err)
@@ -335,58 +346,5 @@ func providerConfigMap(ctx context.Context, provider operatorv1.GenericProvider)
335346
return nil, fmt.Errorf("cannot create repository: %w", err)
336347
}
337348

338-
metadata, err := repo.GetFile(ctx, provider.GetSpec().Version, "metadata.yaml")
339-
if err != nil {
340-
err = fmt.Errorf("failed to read metadata.yaml from the repository for provider %q: %w", provider.GetName(), err)
341-
342-
return nil, err
343-
}
344-
345-
components, err := repo.GetFile(ctx, provider.GetSpec().Version, repo.ComponentsPath())
346-
if err != nil {
347-
err = fmt.Errorf("failed to read %q from the repository for provider %q: %w", repo.ComponentsPath(), provider.GetName(), err)
348-
349-
return nil, err
350-
}
351-
352-
configMap, err := controller.TemplateManifestsConfigMap(provider, controller.ProviderLabels(provider), metadata, components, true)
353-
if err != nil {
354-
err = fmt.Errorf("failed to create config map for provider %q: %w", provider.GetName(), err)
355-
356-
return nil, err
357-
}
358-
359-
// Unset owner references due to lack of existing provider owner object
360-
configMap.OwnerReferences = nil
361-
362-
return configMap, nil
363-
}
364-
365-
func ociConfigMap(ctx context.Context, provider operatorv1.GenericProvider) (*v1.ConfigMap, error) {
366-
store, err := controller.FetchOCI(ctx, provider, nil)
367-
if err != nil {
368-
return nil, err
369-
}
370-
371-
metadata, err := store.GetMetadata(provider)
372-
if err != nil {
373-
return nil, err
374-
}
375-
376-
components, err := store.GetComponents(provider)
377-
if err != nil {
378-
return nil, err
379-
}
380-
381-
configMap, err := controller.TemplateManifestsConfigMap(provider, controller.OCILabels(provider), metadata, components, true)
382-
if err != nil {
383-
err = fmt.Errorf("failed to create config map for provider %q: %w", provider.GetName(), err)
384-
385-
return nil, err
386-
}
387-
388-
// Unset owner references due to lack of existing provider owner object
389-
configMap.OwnerReferences = nil
390-
391-
return configMap, nil
349+
return providercontroller.RepositoryConfigMap(ctx, provider, repo)
392350
}

0 commit comments

Comments
 (0)