Skip to content

Commit

Permalink
added integration test, refactored configuration and design (#3458)
Browse files Browse the repository at this point in the history
* Changes after the review to enhance the following aspects

Testing:

- Added integration test so we could test the functionality e2e: it uses some local configuration that we need to test but already provides the acceptance layer that we were missing:

Design:

- Bootstrap workflow moved to the domain layer within `pkg` so it could be presented in different forms.
- Integrated configuration chain of responsibility into a single a builder pattern, so we have configurability in this layer. As a result:
  - we dont need to pass the flags to the steps
  - we config the stepsbefore the workflow is executed which seems the right moment.

Other refactors:
- Moved steps to package `steps` from `command`  for consistency

* integrated ssh key management
  • Loading branch information
enekofb authored Oct 10, 2023
1 parent 5aa5270 commit b94e36f
Show file tree
Hide file tree
Showing 23 changed files with 1,119 additions and 509 deletions.
94 changes: 36 additions & 58 deletions cmd/gitops/app/bootstrap/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,38 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/weaveworks/weave-gitops-enterprise/pkg/bootstrap/commands"
"github.com/weaveworks/weave-gitops-enterprise/pkg/bootstrap/utils"
. "github.com/weaveworks/weave-gitops-enterprise/pkg/bootstrap"
"github.com/weaveworks/weave-gitops-enterprise/pkg/bootstrap/steps"
"github.com/weaveworks/weave-gitops/cmd/gitops/config"
"github.com/weaveworks/weave-gitops/pkg/logger"
)

const (
cmdName = "bootstrap"
cmdShortDescription = `gitops bootstrap will help getting started with Weave GitOps Enterprise through simple steps in bootstrap by performing the following tasks:
- Verify the entitlement file exist on the cluster and valid.
- Verify Flux installation is valid.
- Allow option to bootstrap Flux in the generic git server way if not installed.
- Allow selecting the version of WGE to be installed from the latest 3 versions.
- Set the admin password for WGE Dashboard.
- Easy steps to make OIDC flow
cmdShortDescription = `gitops bootstrap installs Weave GitOps Enterprise in simple steps:
- Entitlements: check that you have valid entitlements.
- Flux: check or bootstrap Flux.
- Weave Gitops: check or install a supported Weave GitOps version with default configuration.
- Authentication: check or setup cluster user authentication to access the dashboard.
`
cmdExamples = `
# Start WGE installation from the current kubeconfig
gitops bootstrap
# Start WGE installation from a specific kubeconfig
gitops bootstrap --kubeconfig <your-kubeconfig-location>
# Start WGE installation with given 'username' and 'password'
gitops bootstrap --username wego-admin --password=hell0!
`
)

type bootstrapFlags struct {
username string
password string
version string
domainType string
domain string
privateKeyPath string
privateKeyPassword string
}
Expand All @@ -45,69 +48,44 @@ func Command(opts *config.Options) *cobra.Command {
Use: cmdName,
Short: cmdShortDescription,
Example: cmdExamples,
Run: getBootstrapCmdRun(opts),
RunE: getBootstrapCmdRun(opts),
}

cmd.Flags().StringVarP(&flags.username, "username", "u", "", "Dashboard admin username")
cmd.Flags().StringVarP(&flags.password, "password", "p", "", "Dashboard admin password")
cmd.Flags().StringVarP(&flags.version, "version", "v", "", "Weave GitOps Enterprise version")
cmd.Flags().StringVarP(&flags.version, "version", "v", "", "Weave GitOps Enterprise version to install")
cmd.Flags().StringVarP(&flags.domainType, "domain-type", "t", "", "dashboard domain type: could be 'localhost' or 'externaldns'")
cmd.Flags().StringVarP(&flags.domain, "domain", "d", "", "indicate the domain to use in case of using `externaldns`")
cmd.Flags().StringVarP(&flags.privateKeyPath, "private-key", "k", "", "Private key path. This key will be used to push the Weave GitOps Enterprise's resources to the default cluster repository")
cmd.Flags().StringVarP(&flags.privateKeyPassword, "private-key-password", "c", "", "Private key password. If the private key is encrypted using password")
return cmd
}

func getBootstrapCmdRun(opts *config.Options) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
logger := logger.NewCLILogger(os.Stdout)

if err := bootstrap(opts, logger); err != nil {
logger.Failuref(err.Error())
}
}
}

// Bootstrap initiated by the command runs the WGE bootstrap steps
func bootstrap(opts *config.Options, logger logger.Logger) error {
kubernetesClient, err := utils.GetKubernetesClient(opts.Kubeconfig)
if err != nil {
return fmt.Errorf("failed to get kubernetes client. error: %s", err)
}

installedVersion, err := utils.GetHelmRelease(kubernetesClient, commands.WgeHelmReleaseName, commands.WGEDefaultNamespace)
if err == nil {
logger.Successf("WGE version: %s is already installed on your cluster!", installedVersion)
return nil
}
func getBootstrapCmdRun(opts *config.Options) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {

config := commands.Config{}
config.KubernetesClient = kubernetesClient
config.Logger = logger
cliLogger := logger.NewCLILogger(os.Stdout)

flagsMap := map[string]string{
commands.UserName: flags.username,
commands.Password: flags.password,
commands.WGEVersion: flags.version,
commands.PrivateKeyPath: flags.privateKeyPath,
commands.PrivateKeyPassword: flags.privateKeyPassword,
}
// create config from flags
c, err := steps.NewConfigBuilder().
WithLogWriter(cliLogger).
WithKubeconfig(opts.Kubeconfig).
WithUsername(flags.username).
WithPassword(flags.password).
WithVersion(flags.version).
WithDomainType(flags.domainType).
WithDomain(flags.domain).
WithPrivateKey(flags.privateKeyPath, flags.privateKeyPassword).
Build()

var steps = []commands.BootstrapStep{
commands.CheckEntitlementSecretStep,
commands.VerifyFluxInstallationStep,
commands.AskPrivateKeyStep,
commands.SelectWgeVersionStep,
commands.AskAdminCredsSecretStep,
commands.SelectDomainType,
commands.InstallWGEStep,
commands.CheckUIDomainStep,
}
if err != nil {
return fmt.Errorf("cannot config bootstrap: %v", err)
}

for _, step := range steps {
err := step.Execute(&config, flagsMap)
err = Bootstrap(c)
if err != nil {
return err
return fmt.Errorf("cannot execute bootstrap: %v", err)
}
return nil
}

return nil
}
Loading

0 comments on commit b94e36f

Please sign in to comment.