@@ -22,12 +22,12 @@ import (
22
22
"strings"
23
23
24
24
"github.com/spf13/cobra"
25
- v1 "k8s.io/api/core/v1"
25
+ corev1 "k8s.io/api/core/v1"
26
26
apierrors "k8s.io/apimachinery/pkg/api/errors"
27
27
"k8s.io/apimachinery/pkg/api/meta"
28
28
kerrors "k8s.io/apimachinery/pkg/util/errors"
29
29
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"
31
31
"sigs.k8s.io/cluster-api-operator/util"
32
32
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
33
33
configclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
@@ -56,32 +56,39 @@ var loadCmd = &cobra.Command{
56
56
GroupID : groupManagement ,
57
57
Short : "Preload providers to a management cluster" ,
58
58
Long : LongDesc (`
59
- Preload provider manifests from an OCI image to a management cluster.
59
+ Preload provider manifests to a management cluster.
60
60
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
62
62
63
63
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
64
68
` ),
65
69
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
68
72
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.
70
77
capioperator preload --infrastructure=aws -u ttl.sh/infrastructure-provider
71
78
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.
73
80
capioperator preload --infrastructure=aws::v2.3.0 -u ttl.sh/infrastructure-provider
74
81
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.
76
83
capioperator preload --infrastructure=aws:custom-namespace -u ttl.sh/infrastructure-provider
77
84
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.
79
86
capioperator preload --infrastructure=aws:custom-namespace:v2.3.0 -u ttl.sh/infrastructure-provider
80
87
81
- # Prepare provider ConfigMap with multiple infrastructure providers.
88
+ # Prepare provider ConfigMap from OCI with multiple infrastructure providers.
82
89
capioperator preload --infrastructure=aws --infrastructure=vsphere -u ttl.sh/infrastructure-provider
83
90
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.
85
92
capioperator preload --infrastructure aws --target-namespace foo -u ttl.sh/infrastructure-provider` ),
86
93
Args : cobra .NoArgs ,
87
94
RunE : func (cmd * cobra.Command , args []string ) error {
@@ -123,7 +130,7 @@ func runPreLoad() error {
123
130
return fmt .Errorf ("missing configMap artifacts url" )
124
131
}
125
132
126
- configMaps := []* v1 .ConfigMap {}
133
+ configMaps := []* corev1 .ConfigMap {}
127
134
128
135
// Load Core Provider.
129
136
if loadOpts .coreProvider != "" {
@@ -234,8 +241,8 @@ func runPreLoad() error {
234
241
return kerrors .NewAggregate (errors )
235
242
}
236
243
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 {}
239
246
240
247
if err := cl .List (ctx , providerList , client .InNamespace ("" )); meta .IsNoMatchError (err ) || apierrors .IsNotFound (err ) {
241
248
return configMaps , nil
@@ -246,15 +253,15 @@ func fetchProviders(ctx context.Context, cl client.Client, providerList genericP
246
253
}
247
254
248
255
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 )
251
258
if err != nil {
252
259
return configMaps , err
253
260
}
254
261
255
262
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 )
258
265
if err != nil {
259
266
return configMaps , err
260
267
}
@@ -266,7 +273,7 @@ func fetchProviders(ctx context.Context, cl client.Client, providerList genericP
266
273
return configMaps , nil
267
274
}
268
275
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 ) {
270
277
provider , err := templateGenericProvider (providerType , providerInput , defaultNamespace , "" , "" )
271
278
if err != nil {
272
279
return nil , err
@@ -277,34 +284,38 @@ func templateConfigMap(ctx context.Context, providerType clusterctlv1.ProviderTy
277
284
OCI : url ,
278
285
}
279
286
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 )
286
288
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
+ }
293
292
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
297
303
}
304
+ }
298
305
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 )
300
309
}
301
310
311
+ spec .Version = repo .DefaultVersion ()
312
+
302
313
provider .SetSpec (spec )
303
314
304
- return ociConfigMap (ctx , provider )
315
+ return providercontroller . OCIConfigMap (ctx , provider )
305
316
}
306
317
307
- func providerConfigMap (ctx context.Context , provider operatorv1.GenericProvider ) (* v1 .ConfigMap , error ) {
318
+ func providerConfigMap (ctx context.Context , provider operatorv1.GenericProvider ) (* corev1 .ConfigMap , error ) {
308
319
mr := configclient .NewMemoryReader ()
309
320
if err := mr .Init (ctx , "" ); err != nil {
310
321
return nil , fmt .Errorf ("unable to init memory reader: %w" , err )
@@ -335,58 +346,5 @@ func providerConfigMap(ctx context.Context, provider operatorv1.GenericProvider)
335
346
return nil , fmt .Errorf ("cannot create repository: %w" , err )
336
347
}
337
348
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 )
392
350
}
0 commit comments