Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedosin committed Nov 25, 2023
1 parent 5839bb1 commit a1a0087
Show file tree
Hide file tree
Showing 3 changed files with 328 additions and 52 deletions.
55 changes: 35 additions & 20 deletions cmd/plugin/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ func init() {
func runInit() error {
ctx := context.Background()

return initProviders(ctx, initOpts)
}

func initProviders(ctx context.Context, opts *initOptions) error {
client, err := CreateKubeClient(initOpts.kubeconfig, initOpts.kubeconfigContext)
if err != nil {
return fmt.Errorf("cannot create a client: %w", err)
Expand All @@ -173,46 +169,52 @@ func initProviders(ctx context.Context, opts *initOptions) error {
}

// Ensure that cert manager is installed.
if err := ensureCertManager(opts); err != nil {
if err := ensureCertManager(initOpts); err != nil {
return fmt.Errorf("cannot ensure that cert manager is installed: %w", err)
}

// Deploy CAPI operator if it doesn't exist.
if !deploymentExists {
if err := deployCAPIOperator(opts); err != nil {
if err := deployCAPIOperator(initOpts); err != nil {
return fmt.Errorf("cannot deploy CAPI operator: %w", err)
}
}

return initProviders(ctx, client, initOpts)
}

func initProviders(ctx context.Context, client ctrlclient.Client, initOpts *initOptions) error {
// Deploy Core Provider.
if err := createGenericProvider(ctx, client, newGenericProvider(clusterctlv1.CoreProviderType), opts.coreProvider, opts.targetNamespace); err != nil {
return fmt.Errorf("cannot create core provider: %w", err)
if initOpts.coreProvider != "" {
if err := createGenericProvider(ctx, client, newGenericProvider(clusterctlv1.CoreProviderType), initOpts.coreProvider, initOpts.targetNamespace); err != nil {
return fmt.Errorf("cannot create core provider: %w", err)
}
}

// Deploy Bootstrap Providers.
for _, bootstrapProvider := range opts.bootstrapProviders {
if err := createGenericProvider(ctx, client, newGenericProvider(clusterctlv1.BootstrapProviderType), bootstrapProvider, opts.targetNamespace); err != nil {
for _, bootstrapProvider := range initOpts.bootstrapProviders {
if err := createGenericProvider(ctx, client, newGenericProvider(clusterctlv1.BootstrapProviderType), bootstrapProvider, initOpts.targetNamespace); err != nil {
return fmt.Errorf("cannot create bootstrap provider: %w", err)
}
}

// Deploy Infrastructure Providers.
for _, infrastructureProvider := range opts.infrastructureProviders {
if err := createGenericProvider(ctx, client, newGenericProvider(clusterctlv1.InfrastructureProviderType), infrastructureProvider, opts.targetNamespace); err != nil {
for _, infrastructureProvider := range initOpts.infrastructureProviders {
if err := createGenericProvider(ctx, client, newGenericProvider(clusterctlv1.InfrastructureProviderType), infrastructureProvider, initOpts.targetNamespace); err != nil {
return fmt.Errorf("cannot create infrastructure provider: %w", err)
}
}

// Deploy Control Plane Providers.
for _, controlPlaneProvider := range opts.controlPlaneProviders {
if err := createGenericProvider(ctx, client, newGenericProvider(clusterctlv1.ControlPlaneProviderType), controlPlaneProvider, opts.targetNamespace); err != nil {
for _, controlPlaneProvider := range initOpts.controlPlaneProviders {
if err := createGenericProvider(ctx, client, newGenericProvider(clusterctlv1.ControlPlaneProviderType), controlPlaneProvider, initOpts.targetNamespace); err != nil {
return fmt.Errorf("cannot create control plane provider: %w", err)
}
}

// Deploy Add-on Providers.
for _, addonProvider := range opts.addonProviders {
if err := createGenericProvider(ctx, client, newGenericProvider(clusterctlv1.AddonProviderType), addonProvider, opts.targetNamespace); err != nil {
for _, addonProvider := range initOpts.addonProviders {
if err := createGenericProvider(ctx, client, newGenericProvider(clusterctlv1.AddonProviderType), addonProvider, initOpts.targetNamespace); err != nil {
return fmt.Errorf("cannot create addon provider: %w", err)
}
}
Expand Down Expand Up @@ -344,7 +346,11 @@ func newGenericProvider(providerType clusterctlv1.ProviderType) genericprovider.
// createGenericProvider creates a generic provider.
func createGenericProvider(ctx context.Context, client ctrlclient.Client, provider genericprovider.GenericProvider, providerInput string, defaultNamespace string) error {
// Parse the provider string
// Format is <optional-namespace>:<provider-name>:<optional-version>
// Format is <provider-name>:<optional-namespace>:<optional-version>
// Example: aws:capa-system:v2.1.5 -> name: aws, namespace: capa-system, version: v2.1.5
// Example: aws -> name: aws, namespace: <defaultNamespace>, version: <latestVersion>
// Example: aws::v2.1.5 -> name: aws, namespace: <defaultNamespace>, version: v2.1.5
// Example: aws:capa-system -> name: aws, namespace: capa-system, version: <latestVersion>
var name, namespace, version string

parts := strings.Split(providerInput, ":")
Expand All @@ -353,15 +359,19 @@ func createGenericProvider(ctx context.Context, client ctrlclient.Client, provid
name = parts[0]
case 2:
name = parts[0]
version = parts[1]
namespace = parts[1]
case 3:
namespace = parts[0]
name = parts[1]
name = parts[0]
namespace = parts[1]
version = parts[2]
default:
return fmt.Errorf("invalid provider format: %s", provider)
}

if name == "" {
return fmt.Errorf("provider name can't be empty")
}

// Set name and namespace
provider.SetName(name)

Expand All @@ -378,6 +388,11 @@ func createGenericProvider(ctx context.Context, client ctrlclient.Client, provid
provider.SetSpec(spec)
}

// Ensure that desired namespace exists
if err := EnsureNamespaceExists(ctx, client, namespace); err != nil {
return fmt.Errorf("cannot ensure that namespace exists: %w", err)
}

// Create the provider
if err := client.Create(ctx, provider.GetObject()); err != nil && !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("cannot create provider: %w", err)
Expand Down
Loading

0 comments on commit a1a0087

Please sign in to comment.