Skip to content

Commit c0a51ad

Browse files
Resolve files with duplicate content, add tests
Signed-off-by: Danil-Grigorev <[email protected]>
1 parent e79481c commit c0a51ad

31 files changed

+919
-99
lines changed

api/v1alpha1/zz_generated.conversion.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha2/provider_types.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,18 @@ type ContainerSpec struct {
210210
}
211211

212212
// FetchConfiguration determines the way to fetch the components and metadata for the provider.
213+
// +kubebuilder:validation:XValidation:rule="[has(self.oci), has(self.url), has(self.selector)].exists_one(x,x)", message="Must specify one and only one of {oci, url, selector}"
213214
type FetchConfiguration struct {
215+
// OCI configurations to be used for fetching the provider’s components and metadata from an OCI artifact.
216+
OCIConfiguration `json:",inline"`
217+
214218
// URL to be used for fetching the provider’s components and metadata from a remote Github repository.
215219
// For example, https://github.com/{owner}/{repository}/releases
216220
// You must set `providerSpec.Version` field for operator to pick up
217221
// desired version of the release from GitHub.
218222
// +optional
219223
URL string `json:"url,omitempty"`
220224

221-
// OCI to be used for fetching the provider’s components and metadata from an OCI artifact.
222-
// You must set `providerSpec.Version` field for operator to pick up desired version of the release from GitHub.
223-
// If the providerSpec.Version is missing, latest provider version from clusterctl defaults is used.
224-
// +optional
225-
OCI string `json:"oci,omitempty"`
226-
227225
// Selector to be used for fetching provider’s components and metadata from
228226
// ConfigMaps stored inside the cluster. Each ConfigMap is expected to contain
229227
// components and metadata for a specific version only.
@@ -233,6 +231,14 @@ type FetchConfiguration struct {
233231
Selector *metav1.LabelSelector `json:"selector,omitempty"`
234232
}
235233

234+
type OCIConfiguration struct {
235+
// OCI to be used for fetching the provider’s components and metadata from an OCI artifact.
236+
// You must set `providerSpec.Version` field for operator to pick up desired version of the release from GitHub.
237+
// If the providerSpec.Version is missing, latest provider version from clusterctl defaults is used.
238+
// +optional
239+
OCI string `json:"oci,omitempty"`
240+
}
241+
236242
// ProviderStatus defines the observed state of the Provider.
237243
type ProviderStatus struct {
238244
// Contract will contain the core provider contract that the provider is

api/v1alpha2/zz_generated.deepcopy.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/plugin/cmd/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ func deployCAPIOperator(ctx context.Context, opts *initOptions) error {
453453
return nil
454454
}
455455

456+
// templateGenericProvider prepares the provider manifest based on provided provider string
456457
func templateGenericProvider(providerType clusterctlv1.ProviderType, providerInput, defaultNamespace, configSecretName, configSecretNamespace string) (operatorv1.GenericProvider, error) {
457458
// Parse the provider string
458459
// Format is <provider-name>:<optional-namespace>:<optional-version>

cmd/plugin/cmd/init_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ func TestInitProviders(t *testing.T) {
265265
opts: &initOptions{
266266
coreProvider: "cluster-api:capi-system:v1.8.0",
267267
infrastructureProviders: []string{
268-
"cluster-api:capi-system:v1.8.0",
269268
"aws:capa-operator-system",
270269
"docker:capd-operator-system",
271270
},

cmd/plugin/cmd/preload.go

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424

2525
"github.com/spf13/cobra"
2626
corev1 "k8s.io/api/core/v1"
27-
apierrors "k8s.io/apimachinery/pkg/api/errors"
28-
"k8s.io/apimachinery/pkg/api/meta"
2927
kerrors "k8s.io/apimachinery/pkg/util/errors"
3028
"oras.land/oras-go/v2/registry/remote/auth"
3129
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
@@ -207,30 +205,18 @@ func runPreLoad() error {
207205
configMaps = append(configMaps, configMap)
208206
}
209207

210-
errors := []error{}
211-
212-
if !loadOpts.existing {
213-
for _, cm := range configMaps {
214-
out, err := yaml.Marshal(cm)
215-
if err != nil {
216-
return fmt.Errorf("cannot serialize provider config map: %w", err)
217-
}
218-
219-
fmt.Printf("---\n%s", string(out))
208+
if loadOpts.existing {
209+
client, err := CreateKubeClient(loadOpts.kubeconfig, "")
210+
if err != nil {
211+
return fmt.Errorf("cannot create a client: %w", err)
220212
}
221213

222-
return nil
223-
}
224-
225-
client, err := CreateKubeClient(loadOpts.kubeconfig, "")
226-
if err != nil {
227-
return fmt.Errorf("cannot create a client: %w", err)
228-
}
214+
existing, err := preloadExisting(ctx, client)
215+
if err != nil {
216+
return err
217+
}
229218

230-
for _, list := range operatorv1.ProviderLists {
231-
maps, err := fetchProviders(ctx, client, list.(genericProviderList))
232-
configMaps = append(configMaps, maps...)
233-
errors = append(errors, err)
219+
configMaps = append(configMaps, existing...)
234220
}
235221

236222
for _, cm := range configMaps {
@@ -242,15 +228,41 @@ func runPreLoad() error {
242228
fmt.Printf("---\n%s", string(out))
243229
}
244230

245-
return kerrors.NewAggregate(errors)
231+
return nil
232+
}
233+
234+
// preloadExisting uses existing cluster kubeconfig to list providers and create configmaps with components for each provider.
235+
func preloadExisting(ctx context.Context, cl client.Client) ([]*corev1.ConfigMap, error) {
236+
errors := []error{}
237+
configMaps := []*corev1.ConfigMap{}
238+
239+
for _, list := range operatorv1.ProviderLists {
240+
list, ok := list.(genericProviderList)
241+
if !ok {
242+
log.V(5).Info("Expected to get GenericProviderList")
243+
continue
244+
}
245+
246+
list, ok = list.DeepCopyObject().(genericProviderList)
247+
if !ok {
248+
log.V(5).Info("Expected to get GenericProviderList")
249+
continue
250+
}
251+
252+
maps, err := fetchProviders(ctx, cl, list)
253+
configMaps = append(configMaps, maps...)
254+
errors = append(errors, err)
255+
}
256+
257+
return configMaps, kerrors.NewAggregate(errors)
246258
}
247259

248260
func fetchProviders(ctx context.Context, cl client.Client, providerList genericProviderList) ([]*corev1.ConfigMap, error) {
249261
configMaps := []*corev1.ConfigMap{}
250262

251-
if err := cl.List(ctx, providerList, client.InNamespace("")); meta.IsNoMatchError(err) || apierrors.IsNotFound(err) {
252-
return configMaps, nil
253-
} else if err != nil {
263+
if err := retryWithExponentialBackoff(ctx, newReadBackoff(), func(ctx context.Context) error {
264+
return cl.List(ctx, providerList, client.InNamespace(""))
265+
}); err != nil {
254266
log.Error(err, fmt.Sprintf("Unable to list providers, %#v", err))
255267

256268
return configMaps, err
@@ -285,9 +297,10 @@ func templateConfigMap(ctx context.Context, providerType clusterctlv1.ProviderTy
285297

286298
spec := provider.GetSpec()
287299
spec.FetchConfig = &operatorv1.FetchConfiguration{
288-
OCI: url,
300+
OCIConfiguration: operatorv1.OCIConfiguration{
301+
OCI: url,
302+
},
289303
}
290-
291304
provider.SetSpec(spec)
292305

293306
if spec.Version != "" {

0 commit comments

Comments
 (0)