Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extract provisioning logic into external function to reduce duplication #2403

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 2 additions & 54 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,54 +39,8 @@ 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)
if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil {
return fmt.Errorf("failed to provision management cluster: %w", err)
}

return nil
Expand Down
64 changes: 2 additions & 62 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,53 +67,8 @@ 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)
if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil {
return fmt.Errorf("failed to provision management cluster: %w", err)
}

return nil
Expand Down
58 changes: 2 additions & 56 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,58 +51,10 @@ 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
}
if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil {
return fmt.Errorf("failed to provision management cluster: %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")
}

provision.CreateMgmtCluster(gitAuth, cliFlags, catalogApps)

return nil
}

Expand Down
59 changes: 2 additions & 57 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,57 +39,8 @@ 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)
if err := provision.ManagementCluster(cliFlags, catalogApps); err != nil {
return fmt.Errorf("failed to provision management cluster: %w", err)
}

return nil
Expand Down
Loading