Skip to content

Commit

Permalink
refactor: extract provisioning logic into external function to reduce…
Browse files Browse the repository at this point in the history
… duplication

Signed-off-by: nathan-nicholson <nathan@konstruct.io>
  • Loading branch information
nathan-nicholson committed Jan 29, 2025
1 parent d718281 commit 92af82a
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 497 deletions.
58 changes: 1 addition & 57 deletions cmd/akamai/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Check failure on line 42 in cmd/akamai/create.go

View workflow job for this annotation

GitHub Actions / build

error returned from external package is unwrapped: sig: func github.com/konstructio/kubefirst/internal/provision.ManagementCluster(cliFlags github.com/konstructio/kubefirst/internal/types.CliFlags, catalogApps []github.com/konstructio/kubefirst-api/pkg/types.GitopsCatalogApp) error (wrapcheck)
}

func ValidateProvidedFlags(gitProvider, dnsProvider string) error {
Expand Down
66 changes: 1 addition & 65 deletions cmd/aws/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ import (
"fmt"
"os"
"slices"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
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"
Expand Down Expand Up @@ -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())
Expand All @@ -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)

Check failure on line 70 in cmd/aws/create.go

View workflow job for this annotation

GitHub Actions / build

error returned from external package is unwrapped: sig: func github.com/konstructio/kubefirst/internal/provision.ManagementCluster(cliFlags github.com/konstructio/kubefirst/internal/types.CliFlags, catalogApps []github.com/konstructio/kubefirst-api/pkg/types.GitopsCatalogApp) error (wrapcheck)
}

func ValidateProvidedFlags(ctx context.Context, cfg aws.Config, gitProvider, amiType, nodeType string) error {
Expand Down
60 changes: 1 addition & 59 deletions cmd/azure/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Check failure on line 54 in cmd/azure/create.go

View workflow job for this annotation

GitHub Actions / build

error returned from external package is unwrapped: sig: func github.com/konstructio/kubefirst/internal/provision.ManagementCluster(cliFlags github.com/konstructio/kubefirst/internal/types.CliFlags, catalogApps []github.com/konstructio/kubefirst-api/pkg/types.GitopsCatalogApp) error (wrapcheck)
}

func ValidateProvidedFlags(gitProvider string) error {
Expand Down
61 changes: 1 addition & 60 deletions cmd/civo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 92af82a

Please sign in to comment.