From 92af82a789f814dce4fe645a88b12bfb4e63a0e1 Mon Sep 17 00:00:00 2001 From: nathan-nicholson Date: Wed, 29 Jan 2025 12:13:31 -0600 Subject: [PATCH 1/3] refactor: extract provisioning logic into external function to reduce duplication Signed-off-by: nathan-nicholson --- cmd/akamai/create.go | 58 +-------------------------- cmd/aws/create.go | 66 +----------------------------- cmd/azure/create.go | 60 +--------------------------- cmd/civo/create.go | 61 +--------------------------- cmd/digitalocean/create.go | 65 +----------------------------- cmd/google/create.go | 62 +--------------------------- cmd/k3s/create.go | 67 +------------------------------ cmd/vultr/create.go | 65 +----------------------------- internal/provision/provision.go | 71 ++++++++++++++++++++++++++++++++- 9 files changed, 78 insertions(+), 497 deletions(-) diff --git a/cmd/akamai/create.go b/cmd/akamai/create.go index 67ea6d756..bc138d209 100644 --- a/cmd/akamai/create.go +++ b/cmd/akamai/create.go @@ -9,20 +9,14 @@ package akamai import ( "fmt" "os" - "strings" internalssh "github.com/konstructio/kubefirst-api/pkg/ssh" - pkg "github.com/konstructio/kubefirst-api/pkg/utils" "github.com/konstructio/kubefirst/internal/catalog" - "github.com/konstructio/kubefirst/internal/cluster" - "github.com/konstructio/kubefirst/internal/gitShim" - "github.com/konstructio/kubefirst/internal/launch" "github.com/konstructio/kubefirst/internal/progress" "github.com/konstructio/kubefirst/internal/provision" "github.com/konstructio/kubefirst/internal/utilities" "github.com/rs/zerolog/log" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func createAkamai(cmd *cobra.Command, _ []string) error { @@ -45,57 +39,7 @@ func createAkamai(cmd *cobra.Command, _ []string) error { return fmt.Errorf("failed to validate flags: %w", err) } - utilities.CreateK1ClusterDirectory(cliFlags.ClusterName) - - gitAuth, err := gitShim.ValidateGitCredentials(cliFlags.GitProvider, cliFlags.GithubOrg, cliFlags.GitlabGroup) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to validate git credentials: %w", err) - } - - executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider)) - if !executionControl { - newRepositoryNames := []string{"gitops", "metaphor"} - newTeamNames := []string{"admins", "developers"} - - initGitParameters := gitShim.GitInitParameters{ - GitProvider: cliFlags.GitProvider, - GitToken: gitAuth.Token, - GitOwner: gitAuth.Owner, - Repositories: newRepositoryNames, - Teams: newTeamNames, - } - - err = gitShim.InitializeGitProvider(&initGitParameters) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to initialize git provider: %w", err) - } - } - viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider), true) - if err := viper.WriteConfig(); err != nil { - return fmt.Errorf("failed to write viper config: %w", err) - } - - k3dClusterCreationComplete := viper.GetBool("launch.deployed") - isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true" - - if !k3dClusterCreationComplete && !isK1Debug { - launch.Up(nil, true, cliFlags.UseTelemetry) - } - - err = pkg.IsAppAvailable(fmt.Sprintf("%s/api/proxyHealth", cluster.GetConsoleIngressURL()), "kubefirst api") - if err != nil { - progress.Error("unable to start kubefirst api") - return fmt.Errorf("failed to check kubefirst api availability: %w", err) - } - - if err := provision.CreateMgmtCluster(gitAuth, cliFlags, catalogApps); err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to create management cluster: %w", err) - } - - return nil + return provision.ManagementCluster(cliFlags, catalogApps) } func ValidateProvidedFlags(gitProvider, dnsProvider string) error { diff --git a/cmd/aws/create.go b/cmd/aws/create.go index d39a8e440..455a06f58 100644 --- a/cmd/aws/create.go +++ b/cmd/aws/create.go @@ -11,7 +11,6 @@ import ( "fmt" "os" "slices" - "strings" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" @@ -19,11 +18,7 @@ import ( ec2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/aws/aws-sdk-go-v2/service/ssm" internalssh "github.com/konstructio/kubefirst-api/pkg/ssh" - pkg "github.com/konstructio/kubefirst-api/pkg/utils" "github.com/konstructio/kubefirst/internal/catalog" - "github.com/konstructio/kubefirst/internal/cluster" - "github.com/konstructio/kubefirst/internal/gitShim" - "github.com/konstructio/kubefirst/internal/launch" "github.com/konstructio/kubefirst/internal/progress" "github.com/konstructio/kubefirst/internal/provision" "github.com/konstructio/kubefirst/internal/utilities" @@ -59,16 +54,6 @@ func createAws(cmd *cobra.Command, _ []string) error { return fmt.Errorf("failed to validate provided flags: %w", err) } - utilities.CreateK1ClusterDirectory(cliFlags.ClusterName) - - // If cluster setup is complete, return - clusterSetupComplete := viper.GetBool("kubefirst-checks.cluster-install-complete") - if clusterSetupComplete { - err = fmt.Errorf("this cluster install process has already completed successfully") - progress.Error(err.Error()) - return nil - } - creds, err := getSessionCredentials(ctx, cfg.Credentials) if err != nil { progress.Error(err.Error()) @@ -82,56 +67,7 @@ func createAws(cmd *cobra.Command, _ []string) error { return fmt.Errorf("failed to write config: %w", err) } - gitAuth, err := gitShim.ValidateGitCredentials(cliFlags.GitProvider, cliFlags.GithubOrg, cliFlags.GitlabGroup) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to validate Git credentials: %w", err) - } - - executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider)) - if !executionControl { - newRepositoryNames := []string{"gitops", "metaphor"} - newTeamNames := []string{"admins", "developers"} - - initGitParameters := gitShim.GitInitParameters{ - GitProvider: cliFlags.GitProvider, - GitToken: gitAuth.Token, - GitOwner: gitAuth.Owner, - Repositories: newRepositoryNames, - Teams: newTeamNames, - } - - err = gitShim.InitializeGitProvider(&initGitParameters) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to initialize Git provider: %w", err) - } - } - - viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider), true) - if err := viper.WriteConfig(); err != nil { - return fmt.Errorf("failed to write config: %w", err) - } - - k3dClusterCreationComplete := viper.GetBool("launch.deployed") - isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true" - - if !k3dClusterCreationComplete && !isK1Debug { - launch.Up(nil, true, cliFlags.UseTelemetry) - } - - err = pkg.IsAppAvailable(fmt.Sprintf("%s/api/proxyHealth", cluster.GetConsoleIngressURL()), "kubefirst api") - if err != nil { - progress.Error("unable to start kubefirst api") - return fmt.Errorf("failed to check kubefirst API availability: %w", err) - } - - if err := provision.CreateMgmtCluster(gitAuth, cliFlags, catalogApps); err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to create management cluster: %w", err) - } - - return nil + return provision.ManagementCluster(cliFlags, catalogApps) } func ValidateProvidedFlags(ctx context.Context, cfg aws.Config, gitProvider, amiType, nodeType string) error { diff --git a/cmd/azure/create.go b/cmd/azure/create.go index 696dacda7..e3c26bc4c 100644 --- a/cmd/azure/create.go +++ b/cmd/azure/create.go @@ -9,20 +9,14 @@ package azure import ( "fmt" "os" - "strings" internalssh "github.com/konstructio/kubefirst-api/pkg/ssh" - utils "github.com/konstructio/kubefirst-api/pkg/utils" "github.com/konstructio/kubefirst/internal/catalog" - "github.com/konstructio/kubefirst/internal/cluster" - "github.com/konstructio/kubefirst/internal/gitShim" - "github.com/konstructio/kubefirst/internal/launch" "github.com/konstructio/kubefirst/internal/progress" "github.com/konstructio/kubefirst/internal/provision" "github.com/konstructio/kubefirst/internal/utilities" "github.com/rs/zerolog/log" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // Environment variables required for authentication. This should be a @@ -57,59 +51,7 @@ func createAzure(cmd *cobra.Command, _ []string) error { return nil } - // If cluster setup is complete, return - clusterSetupComplete := viper.GetBool("kubefirst-checks.cluster-install-complete") - if clusterSetupComplete { - err = fmt.Errorf("this cluster install process has already completed successfully") - progress.Error(err.Error()) - return nil - } - - utilities.CreateK1ClusterDirectory(cliFlags.ClusterName) - - gitAuth, err := gitShim.ValidateGitCredentials(cliFlags.GitProvider, cliFlags.GithubOrg, cliFlags.GitlabGroup) - if err != nil { - progress.Error(err.Error()) - return nil - } - - executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider)) - if !executionControl { - newRepositoryNames := []string{"gitops", "metaphor"} - newTeamNames := []string{"admins", "developers"} - - initGitParameters := gitShim.GitInitParameters{ - GitProvider: cliFlags.GitProvider, - GitToken: gitAuth.Token, - GitOwner: gitAuth.Owner, - Repositories: newRepositoryNames, - Teams: newTeamNames, - } - err = gitShim.InitializeGitProvider(&initGitParameters) - if err != nil { - progress.Error(err.Error()) - return nil - } - } - - viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider), true) - viper.WriteConfig() - - k3dClusterCreationComplete := viper.GetBool("launch.deployed") - isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true" - - if !k3dClusterCreationComplete && !isK1Debug { - launch.Up(nil, true, cliFlags.UseTelemetry) - } - - err = utils.IsAppAvailable(fmt.Sprintf("%s/api/proxyHealth", cluster.GetConsoleIngressURL()), "kubefirst api") - if err != nil { - progress.Error("unable to start kubefirst api") - } - - provision.CreateMgmtCluster(gitAuth, cliFlags, catalogApps) - - return nil + return provision.ManagementCluster(cliFlags, catalogApps) } func ValidateProvidedFlags(gitProvider string) error { diff --git a/cmd/civo/create.go b/cmd/civo/create.go index a8e42aa1d..76fa33c79 100644 --- a/cmd/civo/create.go +++ b/cmd/civo/create.go @@ -9,20 +9,14 @@ package civo import ( "fmt" "os" - "strings" internalssh "github.com/konstructio/kubefirst-api/pkg/ssh" - utils "github.com/konstructio/kubefirst-api/pkg/utils" "github.com/konstructio/kubefirst/internal/catalog" - "github.com/konstructio/kubefirst/internal/cluster" - "github.com/konstructio/kubefirst/internal/gitShim" - "github.com/konstructio/kubefirst/internal/launch" "github.com/konstructio/kubefirst/internal/progress" "github.com/konstructio/kubefirst/internal/provision" "github.com/konstructio/kubefirst/internal/utilities" "github.com/rs/zerolog/log" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func createCivo(cmd *cobra.Command, _ []string) error { @@ -45,60 +39,7 @@ func createCivo(cmd *cobra.Command, _ []string) error { return fmt.Errorf("failed to validate provided flags: %w", err) } - // If cluster setup is complete, return - - utilities.CreateK1ClusterDirectory(cliFlags.ClusterName) - - gitAuth, err := gitShim.ValidateGitCredentials(cliFlags.GitProvider, cliFlags.GithubOrg, cliFlags.GitlabGroup) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to validate git credentials: %w", err) - } - - // Validate git - executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider)) - if !executionControl { - newRepositoryNames := []string{"gitops", "metaphor"} - newTeamNames := []string{"admins", "developers"} - - initGitParameters := gitShim.GitInitParameters{ - GitProvider: cliFlags.GitProvider, - GitToken: gitAuth.Token, - GitOwner: gitAuth.Owner, - Repositories: newRepositoryNames, - Teams: newTeamNames, - } - - err = gitShim.InitializeGitProvider(&initGitParameters) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to initialize Git provider: %w", err) - } - } - viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider), true) - if err = viper.WriteConfig(); err != nil { - return fmt.Errorf("failed to write viper config: %w", err) - } - - k3dClusterCreationComplete := viper.GetBool("launch.deployed") - isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true" - - if !k3dClusterCreationComplete && !isK1Debug { - launch.Up(nil, true, cliFlags.UseTelemetry) - } - - err = utils.IsAppAvailable(fmt.Sprintf("%s/api/proxyHealth", cluster.GetConsoleIngressURL()), "kubefirst api") - if err != nil { - progress.Error("unable to start kubefirst api") - return fmt.Errorf("API availability check failed: %w", err) - } - - if err := provision.CreateMgmtCluster(gitAuth, cliFlags, catalogApps); err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to create management cluster: %w", err) - } - - return nil + return provision.ManagementCluster(cliFlags, catalogApps) } func ValidateProvidedFlags(gitProvider, dnsProvider string) error { diff --git a/cmd/digitalocean/create.go b/cmd/digitalocean/create.go index 8453d100b..2560658eb 100644 --- a/cmd/digitalocean/create.go +++ b/cmd/digitalocean/create.go @@ -10,20 +10,14 @@ import ( "errors" "fmt" "os" - "strings" internalssh "github.com/konstructio/kubefirst-api/pkg/ssh" - utils "github.com/konstructio/kubefirst-api/pkg/utils" "github.com/konstructio/kubefirst/internal/catalog" - "github.com/konstructio/kubefirst/internal/cluster" - "github.com/konstructio/kubefirst/internal/gitShim" - "github.com/konstructio/kubefirst/internal/launch" "github.com/konstructio/kubefirst/internal/progress" "github.com/konstructio/kubefirst/internal/provision" "github.com/konstructio/kubefirst/internal/utilities" "github.com/rs/zerolog/log" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func createDigitalocean(cmd *cobra.Command, _ []string) error { @@ -50,64 +44,7 @@ func createDigitalocean(cmd *cobra.Command, _ []string) error { return fmt.Errorf("failed to validate provided flags: %w", err) } - // If cluster setup is complete, return - clusterSetupComplete := viper.GetBool("kubefirst-checks.cluster-install-complete") - if clusterSetupComplete { - err = fmt.Errorf("this cluster install process has already completed successfully") - progress.Error(err.Error()) - return err - } - - utilities.CreateK1ClusterDirectory(cliFlags.ClusterName) - - gitAuth, err := gitShim.ValidateGitCredentials(cliFlags.GitProvider, cliFlags.GithubOrg, cliFlags.GitlabGroup) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to validate git credentials: %w", err) - } - - // Validate git - executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider)) - if !executionControl { - newRepositoryNames := []string{"gitops", "metaphor"} - newTeamNames := []string{"admins", "developers"} - - initGitParameters := gitShim.GitInitParameters{ - GitProvider: cliFlags.GitProvider, - GitToken: gitAuth.Token, - GitOwner: gitAuth.Owner, - Repositories: newRepositoryNames, - Teams: newTeamNames, - } - - err = gitShim.InitializeGitProvider(&initGitParameters) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to initialize git provider: %w", err) - } - } - viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider), true) - viper.WriteConfig() - - k3dClusterCreationComplete := viper.GetBool("launch.deployed") - isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true" - - if !k3dClusterCreationComplete && !isK1Debug { - launch.Up(nil, true, cliFlags.UseTelemetry) - } - - err = utils.IsAppAvailable(fmt.Sprintf("%s/api/proxyHealth", cluster.GetConsoleIngressURL()), "kubefirst api") - if err != nil { - progress.Error("unable to start kubefirst api") - return fmt.Errorf("failed to check app availability for Kubefirst API: %w", err) - } - - if err := provision.CreateMgmtCluster(gitAuth, cliFlags, catalogApps); err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to create management cluster: %w", err) - } - - return nil + return provision.ManagementCluster(cliFlags, catalogApps) } func ValidateProvidedFlags(gitProvider, dnsProvider string) error { diff --git a/cmd/google/create.go b/cmd/google/create.go index 1c19b19f9..017a48f2e 100644 --- a/cmd/google/create.go +++ b/cmd/google/create.go @@ -9,20 +9,14 @@ package google import ( "fmt" "os" - "strings" internalssh "github.com/konstructio/kubefirst-api/pkg/ssh" - utils "github.com/konstructio/kubefirst-api/pkg/utils" "github.com/konstructio/kubefirst/internal/catalog" - "github.com/konstructio/kubefirst/internal/cluster" - "github.com/konstructio/kubefirst/internal/gitShim" - "github.com/konstructio/kubefirst/internal/launch" "github.com/konstructio/kubefirst/internal/progress" "github.com/konstructio/kubefirst/internal/provision" "github.com/konstructio/kubefirst/internal/utilities" "github.com/rs/zerolog/log" "github.com/spf13/cobra" - "github.com/spf13/viper" _ "k8s.io/client-go/plugin/pkg/client/auth" // required for authentication ) @@ -46,61 +40,7 @@ func createGoogle(cmd *cobra.Command, _ []string) error { return fmt.Errorf("validation of provided flags failed: %w", err) } - clusterSetupComplete := viper.GetBool("kubefirst-checks.cluster-install-complete") - if clusterSetupComplete { - err = fmt.Errorf("this cluster install process has already completed successfully") - progress.Error(err.Error()) - return fmt.Errorf("cluster setup is complete: %w", err) - } - - utilities.CreateK1ClusterDirectory(cliFlags.ClusterName) - - gitAuth, err := gitShim.ValidateGitCredentials(cliFlags.GitProvider, cliFlags.GithubOrg, cliFlags.GitlabGroup) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("git credentials validation failed: %w", err) - } - - executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider)) - if !executionControl { - newRepositoryNames := []string{"gitops", "metaphor"} - newTeamNames := []string{"admins", "developers"} - - initGitParameters := gitShim.GitInitParameters{ - GitProvider: cliFlags.GitProvider, - GitToken: gitAuth.Token, - GitOwner: gitAuth.Owner, - Repositories: newRepositoryNames, - Teams: newTeamNames, - } - err = gitShim.InitializeGitProvider(&initGitParameters) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("initialization of git provider failed: %w", err) - } - } - viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider), true) - viper.WriteConfig() - - k3dClusterCreationComplete := viper.GetBool("launch.deployed") - isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true" - - if !k3dClusterCreationComplete && !isK1Debug { - launch.Up(nil, true, cliFlags.UseTelemetry) - } - - err = utils.IsAppAvailable(fmt.Sprintf("%s/api/proxyHealth", cluster.GetConsoleIngressURL()), "kubefirst api") - if err != nil { - progress.Error("unable to start kubefirst api") - return fmt.Errorf("kubefirst api availability check failed: %w", err) - } - - if err := provision.CreateMgmtCluster(gitAuth, cliFlags, catalogApps); err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to create management cluster: %w", err) - } - - return nil + return provision.ManagementCluster(cliFlags, catalogApps) } func ValidateProvidedFlags(gitProvider string) error { diff --git a/cmd/k3s/create.go b/cmd/k3s/create.go index 027bcf0c2..3331e0d89 100644 --- a/cmd/k3s/create.go +++ b/cmd/k3s/create.go @@ -9,22 +9,15 @@ package k3s import ( "errors" "fmt" - "os" - "strings" "github.com/rs/zerolog/log" internalssh "github.com/konstructio/kubefirst-api/pkg/ssh" - utils "github.com/konstructio/kubefirst-api/pkg/utils" "github.com/konstructio/kubefirst/internal/catalog" - "github.com/konstructio/kubefirst/internal/cluster" - "github.com/konstructio/kubefirst/internal/gitShim" - "github.com/konstructio/kubefirst/internal/launch" "github.com/konstructio/kubefirst/internal/progress" "github.com/konstructio/kubefirst/internal/provision" "github.com/konstructio/kubefirst/internal/utilities" "github.com/spf13/cobra" - "github.com/spf13/viper" _ "k8s.io/client-go/plugin/pkg/client/auth" // required for k8s authentication ) @@ -52,65 +45,7 @@ func createK3s(cmd *cobra.Command, _ []string) error { return fmt.Errorf("provided flags validation failed: %w", err) } - // If cluster setup is complete, return - clusterSetupComplete := viper.GetBool("kubefirst-checks.cluster-install-complete") - if clusterSetupComplete { - err = errors.New("this cluster install process has already completed successfully") - progress.Error(err.Error()) - return nil - } - - utilities.CreateK1ClusterDirectory(cliFlags.ClusterName) - - gitAuth, err := gitShim.ValidateGitCredentials(cliFlags.GitProvider, cliFlags.GithubOrg, cliFlags.GitlabGroup) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("git credentials validation failed: %w", err) - } - - executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider)) - if !executionControl { - newRepositoryNames := []string{"gitops", "metaphor"} - newTeamNames := []string{"admins", "developers"} - - initGitParameters := gitShim.GitInitParameters{ - GitProvider: cliFlags.GitProvider, - GitToken: gitAuth.Token, - GitOwner: gitAuth.Owner, - Repositories: newRepositoryNames, - Teams: newTeamNames, - } - err = gitShim.InitializeGitProvider(&initGitParameters) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("initialization of git provider failed: %w", err) - } - } - viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider), true) - - if err := viper.WriteConfig(); err != nil { - return fmt.Errorf("writing config failed: %w", err) - } - - k3dClusterCreationComplete := viper.GetBool("launch.deployed") - isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true" - - if !k3dClusterCreationComplete && !isK1Debug { - launch.Up(nil, true, cliFlags.UseTelemetry) - } - - err = utils.IsAppAvailable(fmt.Sprintf("%s/api/proxyHealth", cluster.GetConsoleIngressURL()), "kubefirst api") - if err != nil { - progress.Error("unable to start kubefirst api") - return fmt.Errorf("app availability check failed: %w", err) - } - - if err := provision.CreateMgmtCluster(gitAuth, cliFlags, catalogApps); err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to create management cluster: %w", err) - } - - return nil + return provision.ManagementCluster(cliFlags, catalogApps) } func ValidateProvidedFlags(gitProvider string) error { diff --git a/cmd/vultr/create.go b/cmd/vultr/create.go index 7f27f1897..da173bca1 100644 --- a/cmd/vultr/create.go +++ b/cmd/vultr/create.go @@ -10,20 +10,14 @@ import ( "errors" "fmt" "os" - "strings" internalssh "github.com/konstructio/kubefirst-api/pkg/ssh" - utils "github.com/konstructio/kubefirst-api/pkg/utils" "github.com/konstructio/kubefirst/internal/catalog" - "github.com/konstructio/kubefirst/internal/cluster" - "github.com/konstructio/kubefirst/internal/gitShim" - "github.com/konstructio/kubefirst/internal/launch" "github.com/konstructio/kubefirst/internal/progress" "github.com/konstructio/kubefirst/internal/provision" "github.com/konstructio/kubefirst/internal/utilities" "github.com/rs/zerolog/log" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func createVultr(cmd *cobra.Command, _ []string) error { @@ -50,64 +44,7 @@ func createVultr(cmd *cobra.Command, _ []string) error { return fmt.Errorf("invalid provided flags: %w", err) } - clusterSetupComplete := viper.GetBool("kubefirst-checks.cluster-install-complete") - if clusterSetupComplete { - err = fmt.Errorf("this cluster install process has already completed successfully") - progress.Error(err.Error()) - return nil - } - - utilities.CreateK1ClusterDirectory(cliFlags.ClusterName) - - gitAuth, err := gitShim.ValidateGitCredentials(cliFlags.GitProvider, cliFlags.GithubOrg, cliFlags.GitlabGroup) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to validate git credentials: %w", err) - } - - executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider)) - if !executionControl { - newRepositoryNames := []string{"gitops", "metaphor"} - newTeamNames := []string{"admins", "developers"} - - initGitParameters := gitShim.GitInitParameters{ - GitProvider: cliFlags.GitProvider, - GitToken: gitAuth.Token, - GitOwner: gitAuth.Owner, - Repositories: newRepositoryNames, - Teams: newTeamNames, - } - - err = gitShim.InitializeGitProvider(&initGitParameters) - if err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to initialize git provider: %w", err) - } - } - viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider), true) - if err := viper.WriteConfig(); err != nil { - return fmt.Errorf("failed to write config: %w", err) - } - - k3dClusterCreationComplete := viper.GetBool("launch.deployed") - isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true" - - if !k3dClusterCreationComplete && !isK1Debug { - launch.Up(nil, true, cliFlags.UseTelemetry) - } - - err = utils.IsAppAvailable(fmt.Sprintf("%s/api/proxyHealth", cluster.GetConsoleIngressURL()), "kubefirst api") - if err != nil { - progress.Error("unable to start kubefirst api") - return fmt.Errorf("kubefirst api availability check failed: %w", err) - } - - if err := provision.CreateMgmtCluster(gitAuth, cliFlags, catalogApps); err != nil { - progress.Error(err.Error()) - return fmt.Errorf("failed to create management cluster: %w", err) - } - - return nil + return provision.ManagementCluster(cliFlags, catalogApps) } func ValidateProvidedFlags(gitProvider, dnsProvider string) error { diff --git a/internal/provision/provision.go b/internal/provision/provision.go index 4be889488..eaca735a3 100644 --- a/internal/provision/provision.go +++ b/internal/provision/provision.go @@ -9,16 +9,22 @@ package provision import ( "errors" "fmt" + "os" + "strings" apiTypes "github.com/konstructio/kubefirst-api/pkg/types" + utils "github.com/konstructio/kubefirst-api/pkg/utils" "github.com/konstructio/kubefirst/internal/cluster" + "github.com/konstructio/kubefirst/internal/gitShim" + "github.com/konstructio/kubefirst/internal/launch" "github.com/konstructio/kubefirst/internal/progress" "github.com/konstructio/kubefirst/internal/types" "github.com/konstructio/kubefirst/internal/utilities" "github.com/rs/zerolog/log" + "github.com/spf13/viper" ) -func CreateMgmtCluster(gitAuth apiTypes.GitAuth, cliFlags types.CliFlags, catalogApps []apiTypes.GitopsCatalogApp) error { +func CreateMgmtClusterRequest(gitAuth apiTypes.GitAuth, cliFlags types.CliFlags, catalogApps []apiTypes.GitopsCatalogApp) error { clusterRecord := utilities.CreateClusterDefinitionRecordFromRaw( gitAuth, cliFlags, @@ -49,3 +55,66 @@ func CreateMgmtCluster(gitAuth apiTypes.GitAuth, cliFlags types.CliFlags, catalo progress.StartProvisioning(clusterRecord.ClusterName) return nil } + +func ManagementCluster(cliFlags types.CliFlags, catalogApps []apiTypes.GitopsCatalogApp) error { + + clusterSetupComplete := viper.GetBool("kubefirst-checks.cluster-install-complete") + if clusterSetupComplete { + err := fmt.Errorf("this cluster install process has already completed successfully") + progress.Error(err.Error()) + return nil + } + + utilities.CreateK1ClusterDirectory(cliFlags.ClusterName) + + gitAuth, err := gitShim.ValidateGitCredentials(cliFlags.GitProvider, cliFlags.GithubOrg, cliFlags.GitlabGroup) + if err != nil { + progress.Error(err.Error()) + return fmt.Errorf("failed to validate git credentials: %w", err) + } + + // Validate git + executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider)) + if !executionControl { + newRepositoryNames := []string{"gitops", "metaphor"} + newTeamNames := []string{"admins", "developers"} + + initGitParameters := gitShim.GitInitParameters{ + GitProvider: cliFlags.GitProvider, + GitToken: gitAuth.Token, + GitOwner: gitAuth.Owner, + Repositories: newRepositoryNames, + Teams: newTeamNames, + } + + err = gitShim.InitializeGitProvider(&initGitParameters) + if err != nil { + progress.Error(err.Error()) + return fmt.Errorf("failed to initialize Git provider: %w", err) + } + } + viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", cliFlags.GitProvider), true) + if err = viper.WriteConfig(); err != nil { + return fmt.Errorf("failed to write viper config: %w", err) + } + + k3dClusterCreationComplete := viper.GetBool("launch.deployed") + isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true" + + if !k3dClusterCreationComplete && !isK1Debug { + launch.Up(nil, true, cliFlags.UseTelemetry) + } + + err = utils.IsAppAvailable(fmt.Sprintf("%s/api/proxyHealth", cluster.GetConsoleIngressURL()), "kubefirst api") + if err != nil { + progress.Error("unable to start kubefirst api") + return fmt.Errorf("API availability check failed: %w", err) + } + + if err := CreateMgmtClusterRequest(gitAuth, cliFlags, catalogApps); err != nil { + progress.Error(err.Error()) + return fmt.Errorf("failed to create management cluster: %w", err) + } + + return nil +} From 5761fb69df20ca482f58018f105538c130587126 Mon Sep 17 00:00:00 2001 From: nathan-nicholson Date: Wed, 29 Jan 2025 12:21:24 -0600 Subject: [PATCH 2/3] chore: wrapper errors before returning Signed-off-by: nathan-nicholson --- cmd/akamai/create.go | 6 +++++- cmd/aws/create.go | 6 +++++- cmd/azure/create.go | 6 +++++- cmd/civo/create.go | 6 +++++- cmd/digitalocean/create.go | 6 +++++- cmd/google/create.go | 6 +++++- cmd/k3s/create.go | 6 +++++- cmd/vultr/create.go | 6 +++++- 8 files changed, 40 insertions(+), 8 deletions(-) diff --git a/cmd/akamai/create.go b/cmd/akamai/create.go index bc138d209..bee90b928 100644 --- a/cmd/akamai/create.go +++ b/cmd/akamai/create.go @@ -39,7 +39,11 @@ func createAkamai(cmd *cobra.Command, _ []string) error { return fmt.Errorf("failed to validate flags: %w", err) } - return provision.ManagementCluster(cliFlags, catalogApps) + if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil { + return fmt.Errorf("failed to provision management cluster: %w", err) + } + + return nil } func ValidateProvidedFlags(gitProvider, dnsProvider string) error { diff --git a/cmd/aws/create.go b/cmd/aws/create.go index 455a06f58..c55d1e370 100644 --- a/cmd/aws/create.go +++ b/cmd/aws/create.go @@ -67,7 +67,11 @@ func createAws(cmd *cobra.Command, _ []string) error { return fmt.Errorf("failed to write config: %w", err) } - return provision.ManagementCluster(cliFlags, catalogApps) + if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil { + return fmt.Errorf("failed to provision management cluster: %w", err) + } + + return nil } func ValidateProvidedFlags(ctx context.Context, cfg aws.Config, gitProvider, amiType, nodeType string) error { diff --git a/cmd/azure/create.go b/cmd/azure/create.go index e3c26bc4c..b45e8574c 100644 --- a/cmd/azure/create.go +++ b/cmd/azure/create.go @@ -51,7 +51,11 @@ func createAzure(cmd *cobra.Command, _ []string) error { return nil } - return provision.ManagementCluster(cliFlags, catalogApps) + if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil { + return fmt.Errorf("failed to provision management cluster: %w", err) + } + + return nil } func ValidateProvidedFlags(gitProvider string) error { diff --git a/cmd/civo/create.go b/cmd/civo/create.go index 76fa33c79..3637577a9 100644 --- a/cmd/civo/create.go +++ b/cmd/civo/create.go @@ -39,7 +39,11 @@ func createCivo(cmd *cobra.Command, _ []string) error { return fmt.Errorf("failed to validate provided flags: %w", err) } - return provision.ManagementCluster(cliFlags, catalogApps) + if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil { + return fmt.Errorf("failed to provision management cluster: %w", err) + } + + return nil } func ValidateProvidedFlags(gitProvider, dnsProvider string) error { diff --git a/cmd/digitalocean/create.go b/cmd/digitalocean/create.go index 2560658eb..e064fadce 100644 --- a/cmd/digitalocean/create.go +++ b/cmd/digitalocean/create.go @@ -44,7 +44,11 @@ func createDigitalocean(cmd *cobra.Command, _ []string) error { return fmt.Errorf("failed to validate provided flags: %w", err) } - return provision.ManagementCluster(cliFlags, catalogApps) + if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil { + return fmt.Errorf("failed to provision management cluster: %w", err) + } + + return nil } func ValidateProvidedFlags(gitProvider, dnsProvider string) error { diff --git a/cmd/google/create.go b/cmd/google/create.go index 017a48f2e..fa9056810 100644 --- a/cmd/google/create.go +++ b/cmd/google/create.go @@ -40,7 +40,11 @@ func createGoogle(cmd *cobra.Command, _ []string) error { return fmt.Errorf("validation of provided flags failed: %w", err) } - return provision.ManagementCluster(cliFlags, catalogApps) + if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil { + return fmt.Errorf("failed to provision management cluster: %w", err) + } + + return nil } func ValidateProvidedFlags(gitProvider string) error { diff --git a/cmd/k3s/create.go b/cmd/k3s/create.go index 3331e0d89..aec7b61fb 100644 --- a/cmd/k3s/create.go +++ b/cmd/k3s/create.go @@ -45,7 +45,11 @@ func createK3s(cmd *cobra.Command, _ []string) error { return fmt.Errorf("provided flags validation failed: %w", err) } - return provision.ManagementCluster(cliFlags, catalogApps) + if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil { + return fmt.Errorf("failed to provision management cluster: %w", err) + } + + return nil } func ValidateProvidedFlags(gitProvider string) error { diff --git a/cmd/vultr/create.go b/cmd/vultr/create.go index da173bca1..a4294f72b 100644 --- a/cmd/vultr/create.go +++ b/cmd/vultr/create.go @@ -44,7 +44,11 @@ func createVultr(cmd *cobra.Command, _ []string) error { return fmt.Errorf("invalid provided flags: %w", err) } - return provision.ManagementCluster(cliFlags, catalogApps) + if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil { + return fmt.Errorf("failed to provision management cluster: %w", err) + } + + return nil } func ValidateProvidedFlags(gitProvider, dnsProvider string) error { From 9183016693500c5c7106880031fef228e1ca3562 Mon Sep 17 00:00:00 2001 From: nathan-nicholson Date: Wed, 29 Jan 2025 12:31:23 -0600 Subject: [PATCH 3/3] chore: remove newline Signed-off-by: nathan-nicholson --- internal/provision/provision.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/provision/provision.go b/internal/provision/provision.go index eaca735a3..54173c944 100644 --- a/internal/provision/provision.go +++ b/internal/provision/provision.go @@ -57,7 +57,6 @@ func CreateMgmtClusterRequest(gitAuth apiTypes.GitAuth, cliFlags types.CliFlags, } func ManagementCluster(cliFlags types.CliFlags, catalogApps []apiTypes.GitopsCatalogApp) error { - clusterSetupComplete := viper.GetBool("kubefirst-checks.cluster-install-complete") if clusterSetupComplete { err := fmt.Errorf("this cluster install process has already completed successfully")