Skip to content

Commit

Permalink
feat: allow for specifying namespace in infra providers
Browse files Browse the repository at this point in the history
This PR adds the ability to the pkgs as well as to ./capi to specify a
namespace for an infra provider. It also busts the bootstrap command
into two subcommands, core and infra. This makes it easier to handle
infra providers in a separate namespace.

UX wise, it'll look like:
```
$ ./capi bootstrap core

$ ./capi bootstrap infra --providers aws:v0.6.8 --target-ns my-ns --watching-ns my-test-ns
```
with the ns flags being optional of course.

Signed-off-by: Spencer Smith <[email protected]>
  • Loading branch information
rsmitty committed Sep 15, 2021
1 parent e5fdc2a commit 2e0c2fe
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 116 deletions.
44 changes: 2 additions & 42 deletions cmd/capi/cmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
package cmd

import (
"context"

"github.com/spf13/cobra"

"github.com/talos-systems/capi-utils/pkg/capi"
"github.com/talos-systems/capi-utils/pkg/capi/infrastructure"
)

var setupOptions = map[string]interface{}{}
Expand All @@ -24,50 +21,13 @@ var manager *capi.Manager

var bootstrapCmd = &cobra.Command{
Use: "bootstrap",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()

providers := make([]infrastructure.Provider, len(options.InfrastructureProviders))
for i, name := range options.InfrastructureProviders {
provider, err := infrastructure.NewProvider(name)
if err != nil {
return err
}

if opts, ok := setupOptions[provider.Name()]; ok {
if err = provider.Configure(opts); err != nil {
return err
}
}

providers[i] = provider
}

var err error

manager, err = capi.NewManager(ctx, capi.Options{
ClusterctlConfigPath: options.ClusterctlConfigPath,
CoreProvider: options.CoreProvider,
BootstrapProviders: options.BootstrapProviders,
InfrastructureProviders: providers,
ControlPlaneProviders: options.ControlPlaneProviders,
})
if err != nil {
return err
}

return manager.Install(ctx)
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}

func init() {
rootCmd.AddCommand(bootstrapCmd)

bootstrapCmd.PersistentFlags().StringVar(&options.BootstrapClusterName, "bootstrap-cluster-name", options.BootstrapClusterName, "bootstrap cluster name")
bootstrapCmd.PersistentFlags().StringSliceVar(&options.InfrastructureProviders, "infrastructure-providers", options.InfrastructureProviders, "infrastructure providers to set up")
bootstrapCmd.PersistentFlags().StringVar(&options.ClusterctlConfigPath, "clusterctl-config", options.ClusterctlConfigPath, "path to the clusterctl config file")
// AWS provider flags
opts := infrastructure.NewAWSSetupOptions()
bootstrapCmd.PersistentFlags().StringVar(&opts.AWSCredentials, "aws-base64-encoded-credentials", awsOptions.b64EncodedCredentials, "AWS_B64ENCODED_CREDENTIALS")
setupOptions[infrastructure.AWSProviderName] = opts
}
22 changes: 0 additions & 22 deletions cmd/capi/cmd/bootstrap_capi.go

This file was deleted.

49 changes: 49 additions & 0 deletions cmd/capi/cmd/bootstrap_core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package cmd

import (
"context"

"github.com/spf13/cobra"

"github.com/talos-systems/capi-utils/pkg/capi"
"github.com/talos-systems/capi-utils/pkg/capi/infrastructure"
)

var capiCoreCmd = &cobra.Command{
Use: "core",
Short: "Install and patch core CAPI.",
Long: `
This command installs everything for CAPI *but* infrastructure providers.
It is assumed that these core components are global watchers and can handle
reconciling CAPI resources across any namespace.
`,
Example: `
capi bootstrap core
`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()

var err error

manager, err = capi.NewManager(ctx, capi.Options{
ClusterctlConfigPath: options.ClusterctlConfigPath,
CoreProvider: options.CoreProvider,
BootstrapProviders: options.BootstrapProviders,
InfrastructureProviders: []infrastructure.Provider{},
ControlPlaneProviders: options.ControlPlaneProviders,
})
if err != nil {
return err
}

return manager.Install(ctx)
},
}

func init() {
bootstrapCmd.AddCommand(capiCoreCmd)
}
83 changes: 83 additions & 0 deletions cmd/capi/cmd/bootstrap_infra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package cmd

import (
"context"

"github.com/spf13/cobra"

"github.com/talos-systems/capi-utils/pkg/capi"
"github.com/talos-systems/capi-utils/pkg/capi/infrastructure"
"github.com/talos-systems/capi-utils/pkg/constants"
)

var (
targetNS string
watchingNS string
)

var capiInfraCmd = &cobra.Command{
Use: "infra",
Short: "Install and patch CAPI infra providers.",
Long: ``,
Example: `
## Specifying multiple infra providers
capi bootstrap infra --providers aws,sidero
## Specifying namespaces and version for infra provider
capi bootstrap infra --providers aws:v0.6.8 --target-ns my-ns --watching-ns my-ns-to-watch
`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()

providers := make([]infrastructure.Provider, len(options.InfrastructureProviders))
for i, name := range options.InfrastructureProviders {
provider, err := infrastructure.NewProvider(
name,
infrastructure.WithProviderNS(targetNS),
infrastructure.WithWatchingNS(watchingNS),
)
if err != nil {
return err
}

if opts, ok := setupOptions[provider.Name()]; ok {
if err = provider.Configure(opts); err != nil {
return err
}
}

providers[i] = provider
}

var err error

manager, err = capi.NewManager(ctx, capi.Options{
ClusterctlConfigPath: options.ClusterctlConfigPath,
CoreProvider: "",
BootstrapProviders: []string{},
InfrastructureProviders: providers,
ControlPlaneProviders: []string{},
})
if err != nil {
return err
}

return manager.Install(ctx)
},
}

func init() {
bootstrapCmd.AddCommand(capiInfraCmd)
capiInfraCmd.PersistentFlags().StringSliceVar(&options.InfrastructureProviders, "providers", []string{"aws"}, "Name(s) of infra provider(s) to init")
capiInfraCmd.PersistentFlags().StringVar(&targetNS, "target-ns", "", "Namespace to install proivder in")
capiInfraCmd.PersistentFlags().StringVar(&watchingNS, "watching-ns", "", "Namespace for provider to watch")

// AWS provider flags
opts := infrastructure.NewAWSSetupOptions()
capiInfraCmd.PersistentFlags().StringVar(&opts.AWSCredentials, "aws-base64-encoded-credentials", awsOptions.b64EncodedCredentials, "AWS_B64ENCODED_CREDENTIALS")
setupOptions[constants.AWSProviderName] = opts
}
3 changes: 2 additions & 1 deletion cmd/capi/cmd/cluster_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/talos-systems/capi-utils/pkg/capi"
"github.com/talos-systems/capi-utils/pkg/capi/infrastructure"
"github.com/talos-systems/capi-utils/pkg/constants"
)

var clusterCreateCmdFlags struct {
Expand All @@ -33,7 +34,7 @@ var clusterCreateCmd = &cobra.Command{
capi.WithClusterNamespace(clusterCmdFlags.clusterNamespace),
}

if deployOptions.Provider == "" || deployOptions.Provider == infrastructure.AWSProviderName {
if deployOptions.Provider == "" || deployOptions.Provider == constants.AWSProviderName {
if clusterCreateCmdFlags.templatePath == "" {
deployOptions.Template = infrastructure.AWSTalosTemplate
}
Expand Down
1 change: 0 additions & 1 deletion cmd/capi/cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd

// Options control the sidero testing.
type Options struct {
BootstrapClusterName string
ClusterctlConfigPath string
CoreProvider string

Expand Down
Loading

0 comments on commit 2e0c2fe

Please sign in to comment.