Skip to content

Commit

Permalink
chore: more changes to cloud commands
Browse files Browse the repository at this point in the history
Signed-off-by: nathan-nicholson <nathan@konstruct.io>
  • Loading branch information
nathan-nicholson committed Jan 28, 2025
1 parent 8eba418 commit e661a26
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 27 deletions.
6 changes: 4 additions & 2 deletions cmd/aws/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ func Create() *cobra.Command {
return fmt.Errorf("failed to get flags: %w", err)
}

stepper := step.NewStepFactory(cmd.ErrOrStderr())

// TODO: Handle for non-bubbletea
// progress.DisplayLogHints(40)
stepper.InfoStep(step.EMOJI_ALARM, fmt.Sprintf("Estimated time to complete: %s", "15-20 minutes"))

isValid, catalogApps, err := catalog.ValidateCatalogApps(cliFlags.InstallCatalogApps)
if !isValid {
Expand All @@ -94,7 +96,7 @@ func Create() *cobra.Command {
ctx := cmd.Context()

k1Client := KubefirstAWSClient{
stepper: step.NewStepFactory(cmd.ErrOrStderr()),
stepper: stepper,
cliFlags: cliFlags,
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/aws/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type KubefirstAWSClient struct {

func (c *KubefirstAWSClient) CreateManagementCluster(ctx context.Context, catalogApps []apiTypes.GitopsCatalogApp) error {

initializeConfigStep := c.stepper.NewStep("Initialize Config")
initializeConfigStep := c.stepper.NewProgressStep("Initialize Config")

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(c.cliFlags.CloudRegion))
if err != nil {
Expand Down Expand Up @@ -82,7 +82,7 @@ func (c *KubefirstAWSClient) CreateManagementCluster(ctx context.Context, catalo

initializeConfigStep.Complete(nil)

validateGitStep := c.stepper.NewStep("Setup Gitops Repository")
validateGitStep := c.stepper.NewProgressStep("Setup Gitops Repository")

gitAuth, err := gitShim.ValidateGitCredentials(c.cliFlags.GitProvider, c.cliFlags.GithubOrg, c.cliFlags.GitlabGroup)
if err != nil {
Expand Down Expand Up @@ -120,7 +120,7 @@ func (c *KubefirstAWSClient) CreateManagementCluster(ctx context.Context, catalo
}

validateGitStep.Complete(nil)
setupK3dClusterStep := c.stepper.NewStep("Setup k3d Cluster")
setupK3dClusterStep := c.stepper.NewProgressStep("Setup k3d Cluster")

k3dClusterCreationComplete := viper.GetBool("launch.deployed")
isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true"
Expand All @@ -143,7 +143,7 @@ func (c *KubefirstAWSClient) CreateManagementCluster(ctx context.Context, catalo
}

setupK3dClusterStep.Complete(nil)
createMgmtClusterStep := c.stepper.NewStep("Create Management Cluster")
createMgmtClusterStep := c.stepper.NewProgressStep("Create Management Cluster")

if err := provision.CreateMgmtCluster(gitAuth, c.cliFlags, catalogApps); err != nil {
wrerr := fmt.Errorf("failed to create management cluster: %w", err)
Expand Down
6 changes: 5 additions & 1 deletion cmd/civo/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func Create() *cobra.Command {
return fmt.Errorf("failed to get CLI flags: %w", err)
}

stepper := step.NewStepFactory(cmd.ErrOrStderr())

stepper.InfoStep(step.EMOJI_ALARM, fmt.Sprintf("Estimated time to complete: %s", "15-20 minutes"))

isValid, catalogApps, err := catalog.ValidateCatalogApps(cliFlags.InstallCatalogApps)
if !isValid {
return fmt.Errorf("catalog apps validation failed: %w", err)
Expand All @@ -74,7 +78,7 @@ func Create() *cobra.Command {
cliFlags: cliFlags,
}

return k1Client.CreateCivoManagementCluster(cmd.Context(), catalogApps)
return k1Client.CreateManagementCluster(cmd.Context(), catalogApps)
},
SilenceErrors: true,
SilenceUsage: true,
Expand Down
67 changes: 51 additions & 16 deletions cmd/civo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"os"
"strings"
"time"

internalssh "github.com/konstructio/kubefirst-api/pkg/ssh"
apiTypes "github.com/konstructio/kubefirst-api/pkg/types"
Expand All @@ -31,37 +32,42 @@ type KubefirstCivoClient struct {
cliFlags types.CliFlags
}

func (kc *KubefirstCivoClient) CreateCivoManagementCluster(ctx context.Context, catalogApps []apiTypes.GitopsCatalogApp) error {

initializeConfigStep := kc.stepper.NewStep("Initialize Config")
func (kc *KubefirstCivoClient) CreateManagementCluster(ctx context.Context, catalogApps []apiTypes.GitopsCatalogApp) error {

err := ValidateProvidedFlags(kc.cliFlags.GitProvider, kc.cliFlags.DNSProvider)

if err != nil {
wrerr := fmt.Errorf("failed to validate provided flags: %w", err)
initializeConfigStep.Complete(wrerr)
return wrerr
return fmt.Errorf("failed to validate provided flags: %w", err)
}

utilities.CreateK1ClusterDirectory(kc.cliFlags.ClusterName)
return CreateManagementCluster(kc, catalogApps)
}

func CreateManagementCluster(c *KubefirstCivoClient, catalogApps []apiTypes.GitopsCatalogApp) error {

initializeConfigStep := c.stepper.NewProgressStep("Initialize Config")

gitAuth, err := gitShim.ValidateGitCredentials(kc.cliFlags.GitProvider, kc.cliFlags.GithubOrg, kc.cliFlags.GitlabGroup)
utilities.CreateK1ClusterDirectory(c.cliFlags.ClusterName)

gitAuth, err := gitShim.ValidateGitCredentials(c.cliFlags.GitProvider, c.cliFlags.GithubOrg, c.cliFlags.GitlabGroup)
if err != nil {
wrerr := fmt.Errorf("failed to validate git credentials: %w", err)
initializeConfigStep.Complete(wrerr)
return wrerr
}

initializeConfigStep.Complete(nil)
validateGitStep := kc.stepper.NewStep("Setup Gitops Repository")
validateGitStep := c.stepper.NewProgressStep("Setup Gitops Repository")

// Validate git
executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", kc.cliFlags.GitProvider))
executionControl := viper.GetBool(fmt.Sprintf("kubefirst-checks.%s-credentials", c.cliFlags.GitProvider))

if !executionControl {
newRepositoryNames := []string{"gitops", "metaphor"}
newTeamNames := []string{"admins", "developers"}

initGitParameters := gitShim.GitInitParameters{
GitProvider: kc.cliFlags.GitProvider,
GitProvider: c.cliFlags.GitProvider,
GitToken: gitAuth.Token,
GitOwner: gitAuth.Owner,
Repositories: newRepositoryNames,
Expand All @@ -75,21 +81,26 @@ func (kc *KubefirstCivoClient) CreateCivoManagementCluster(ctx context.Context,
return wrerr
}
}
viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", kc.cliFlags.GitProvider), true)

viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", c.cliFlags.GitProvider), true)

viper.Set(fmt.Sprintf("kubefirst-checks.%s-credentials", c.cliFlags.GitProvider), true)

if err = viper.WriteConfig(); err != nil {
wrerr := fmt.Errorf("failed to write viper config: %w", err)
validateGitStep.Complete(wrerr)
return wrerr
}

validateGitStep.Complete(nil)
setupK3dClusterStep := kc.stepper.NewStep("Setup k3d Cluster")
setupK3dClusterStep := c.stepper.NewProgressStep("Setup k3d Cluster")

k3dClusterCreationComplete := viper.GetBool("launch.deployed")
isK1Debug := strings.ToLower(os.Getenv("K1_LOCAL_DEBUG")) == "true"

if !k3dClusterCreationComplete && !isK1Debug {
err = launch.Up(nil, true, kc.cliFlags.UseTelemetry)
err = launch.Up(nil, true, c.cliFlags.UseTelemetry)

if err != nil {
wrerr := fmt.Errorf("failed to setup k3d cluster: %w", err)
setupK3dClusterStep.Complete(wrerr)
Expand All @@ -105,16 +116,40 @@ func (kc *KubefirstCivoClient) CreateCivoManagementCluster(ctx context.Context,
}

setupK3dClusterStep.Complete(nil)
createMgmtClusterStep := kc.stepper.NewStep("Create Management Cluster")
createMgmtClusterStep := c.stepper.NewProgressStep("Create Management Cluster")

if err := provision.CreateMgmtCluster(gitAuth, kc.cliFlags, catalogApps); err != nil {
if err := provision.CreateMgmtCluster(gitAuth, c.cliFlags, catalogApps); err != nil {
wrerr := fmt.Errorf("failed to create management cluster: %w", err)
createMgmtClusterStep.Complete(wrerr)
return wrerr
}

createMgmtClusterStep.Complete(nil)

clusterClient := cluster.ClusterClient{}

clusterProvision := provision.NewClusterProvision(c.cliFlags.ClusterName, &clusterClient)

currentClusterStep := c.stepper.NewProgressStep(clusterProvision.GetCurrentStep())

for !clusterProvision.IsComplete() {

if currentClusterStep.GetName() != clusterProvision.GetCurrentStep() {
currentClusterStep.Complete(nil)
currentClusterStep = c.stepper.NewProgressStep(clusterProvision.GetCurrentStep())
}

err = clusterProvision.UpdateProvisionProgress()

if err != nil {
wrerr := fmt.Errorf("failure provisioning the management cluster: %w", err)
currentClusterStep.Complete(wrerr)
return wrerr
}

time.Sleep(5 * time.Second)
}

return nil
}

Expand Down
14 changes: 14 additions & 0 deletions internal/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ func GetConsoleIngressURL() string {
return "https://console.kubefirst.dev"
}

type ClusterClient struct{}

func (c *ClusterClient) CreateCluster(cluster apiTypes.ClusterDefinition) error {
return CreateCluster(cluster)
}

func (c *ClusterClient) GetCluster(clusterName string) (apiTypes.Cluster, error) {
return GetCluster(clusterName)
}

func (c *ClusterClient) ResetClusterProgress(clusterName string) error {
return ResetClusterProgress(clusterName)
}

func CreateCluster(cluster apiTypes.ClusterDefinition) error {
customTransport := http.DefaultTransport.(*http.Transport).Clone()
httpClient := http.Client{Transport: customTransport}
Expand Down
Loading

0 comments on commit e661a26

Please sign in to comment.