Skip to content

Commit

Permalink
refactor: remove cloud specific services KRA-72 (#2405)
Browse files Browse the repository at this point in the history
* refactor: remove cloud specific services

Signed-off-by: nathan-nicholson <nathan@konstruct.io>

* chore: addressing linting issues

Signed-off-by: nathan-nicholson <nathan@konstruct.io>

* chore: more linting fixes

Signed-off-by: nathan-nicholson <nathan@konstruct.io>

* chore: extra newline

Signed-off-by: nathan-nicholson <nathan@konstruct.io>

---------

Signed-off-by: nathan-nicholson <nathan@konstruct.io>
  • Loading branch information
nathan-nicholson authored Jan 30, 2025
1 parent 3bcabce commit 32badfa
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 363 deletions.
20 changes: 17 additions & 3 deletions cmd/akamai/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"fmt"

"github.com/konstructio/kubefirst-api/pkg/constants"
"github.com/konstructio/kubefirst/internal/catalog"
"github.com/konstructio/kubefirst/internal/common"
"github.com/konstructio/kubefirst/internal/progress"
"github.com/konstructio/kubefirst/internal/provision"
"github.com/konstructio/kubefirst/internal/utilities"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -51,17 +53,29 @@ func Create() *cobra.Command {
Short: "create the kubefirst platform running on akamai kubernetes",
TraverseChildren: true,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
cliFlags, err := utilities.GetFlags(cmd, "akamai")
if err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to get flags: %w", err)
}

akamaiService := Service{
cliFlags: &cliFlags,
progress.DisplayLogHints(25)

isValid, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps)
if !isValid {
return fmt.Errorf("catalog validation failed: %w", err)
}

err = ValidateProvidedFlags(cliFlags.GitProvider, cliFlags.DNSProvider)
if err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to validate flags: %w", err)
}

if err := akamaiService.CreateCluster(cmd.Context()); err != nil {
provision := provision.Provisioner{}

if err := provision.ProvisionManagementCluster(ctx, &cliFlags, catalogApps); err != nil {
return fmt.Errorf("failed to create cluster: %w", err)
}

Expand Down
29 changes: 0 additions & 29 deletions cmd/akamai/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,14 @@ See the LICENSE file for more details.
package akamai

import (
"context"
"fmt"
"os"

internalssh "github.com/konstructio/kubefirst-api/pkg/ssh"
"github.com/konstructio/kubefirst/internal/catalog"
"github.com/konstructio/kubefirst/internal/progress"
"github.com/konstructio/kubefirst/internal/provision"
"github.com/konstructio/kubefirst/internal/types"
"github.com/rs/zerolog/log"
)

type Service struct {
cliFlags *types.CliFlags
}

func (s *Service) CreateCluster(ctx context.Context) error {
progress.DisplayLogHints(25)

isValid, catalogApps, err := catalog.ValidateCatalogApps(ctx, s.cliFlags.InstallCatalogApps)
if !isValid {
return fmt.Errorf("catalog validation failed: %w", err)
}

err = ValidateProvidedFlags(s.cliFlags.GitProvider, s.cliFlags.DNSProvider)
if err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to validate flags: %w", err)
}

if err := provision.ManagementCluster(s.cliFlags, catalogApps); err != nil {
return fmt.Errorf("failed to provision management cluster: %w", err)
}

return nil
}

func ValidateProvidedFlags(gitProvider, dnsProvider string) error {
progress.AddStep("Validate provided flags")

Expand Down
43 changes: 38 additions & 5 deletions cmd/aws/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ package aws
import (
"fmt"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/konstructio/kubefirst-api/pkg/constants"
"github.com/konstructio/kubefirst/internal/catalog"
"github.com/konstructio/kubefirst/internal/common"
"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"
)

var (
Expand Down Expand Up @@ -81,23 +85,52 @@ func Create() *cobra.Command {
Short: "create the kubefirst platform running in aws",
TraverseChildren: true,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
cliFlags, err := utilities.GetFlags(cmd, "aws")
if err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to get flags: %w", err)
}

awsService := Service{
cliFlags: &cliFlags,
progress.DisplayLogHints(40)

isValid, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps)
if !isValid {
return fmt.Errorf("invalid catalog apps: %w", err)
}

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(cliFlags.CloudRegion))
if err != nil {
return fmt.Errorf("unable to load AWS SDK config: %w", err)
}

err = ValidateProvidedFlags(ctx, cfg, cliFlags.GitProvider, cliFlags.AMIType, cliFlags.NodeType)
if err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to validate provided flags: %w", err)
}

if err := awsService.CreateCluster(cmd.Context()); err != nil {
return fmt.Errorf("failed to create cluster: %w", err)
creds, err := getSessionCredentials(ctx, cfg.Credentials)
if err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to retrieve AWS credentials: %w", err)
}

viper.Set("kubefirst.state-store-creds.access-key-id", creds.AccessKeyID)
viper.Set("kubefirst.state-store-creds.secret-access-key-id", creds.SecretAccessKey)
viper.Set("kubefirst.state-store-creds.token", creds.SessionToken)
if err := viper.WriteConfig(); err != nil {
return fmt.Errorf("failed to write config: %w", err)
}

provision := provision.Provisioner{}

if err := provision.ProvisionManagementCluster(ctx, &cliFlags, catalogApps); err != nil {
return fmt.Errorf("failed to provision aws management cluster: %w", err)
}

return nil
},
// PreRun: common.CheckDocker,
}

awsDefaults := constants.GetCloudDefaults().Aws
Expand Down
48 changes: 0 additions & 48 deletions cmd/aws/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,14 @@ import (
"slices"

"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"
"github.com/konstructio/kubefirst/internal/catalog"
"github.com/konstructio/kubefirst/internal/progress"
"github.com/konstructio/kubefirst/internal/provision"
"github.com/konstructio/kubefirst/internal/types"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

type Service struct {
cliFlags *types.CliFlags
}

func (s *Service) CreateCluster(ctx context.Context) error {
progress.DisplayLogHints(40)

isValid, catalogApps, err := catalog.ValidateCatalogApps(ctx, s.cliFlags.InstallCatalogApps)
if !isValid {
return fmt.Errorf("invalid catalog apps: %w", err)
}

cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(s.cliFlags.CloudRegion))
if err != nil {
return fmt.Errorf("unable to load AWS SDK config: %w", err)
}

err = ValidateProvidedFlags(ctx, cfg, s.cliFlags.GitProvider, s.cliFlags.AMIType, s.cliFlags.NodeType)
if err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to validate provided flags: %w", err)
}

creds, err := getSessionCredentials(ctx, cfg.Credentials)
if err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to retrieve AWS credentials: %w", err)
}

viper.Set("kubefirst.state-store-creds.access-key-id", creds.AccessKeyID)
viper.Set("kubefirst.state-store-creds.secret-access-key-id", creds.SecretAccessKey)
viper.Set("kubefirst.state-store-creds.token", creds.SessionToken)
if err := viper.WriteConfig(); err != nil {
return fmt.Errorf("failed to write config: %w", err)
}

if err := provision.ManagementCluster(s.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 {
progress.AddStep("Validate provided flags")

Expand Down
21 changes: 18 additions & 3 deletions cmd/azure/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"fmt"

"github.com/konstructio/kubefirst-api/pkg/constants"
"github.com/konstructio/kubefirst/internal/catalog"
"github.com/konstructio/kubefirst/internal/common"
"github.com/konstructio/kubefirst/internal/progress"
"github.com/konstructio/kubefirst/internal/provision"
"github.com/konstructio/kubefirst/internal/utilities"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -56,17 +58,30 @@ func Create() *cobra.Command {
Short: "create the kubefirst platform running on Azure kubernetes",
TraverseChildren: true,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
cliFlags, err := utilities.GetFlags(cmd, "azure")
if err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to get flags: %w", err)
}

azureService := Service{
cliFlags: &cliFlags,
progress.DisplayLogHints(20)

isValid, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps)
if !isValid {
progress.Error(err.Error())
return nil
}

if err := azureService.CreateCluster(cmd.Context()); err != nil {
err = ValidateProvidedFlags(cliFlags.GitProvider)
if err != nil {
progress.Error(err.Error())
return nil
}

provision := provision.Provisioner{}

if err := provision.ProvisionManagementCluster(ctx, &cliFlags, catalogApps); err != nil {
return fmt.Errorf("failed to create Azure management cluster: %w", err)
}

Expand Down
30 changes: 0 additions & 30 deletions cmd/azure/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ See the LICENSE file for more details.
package azure

import (
"context"
"fmt"
"os"

internalssh "github.com/konstructio/kubefirst-api/pkg/ssh"
"github.com/konstructio/kubefirst/internal/catalog"
"github.com/konstructio/kubefirst/internal/progress"
"github.com/konstructio/kubefirst/internal/provision"
"github.com/konstructio/kubefirst/internal/types"
"github.com/rs/zerolog/log"
)

Expand All @@ -30,32 +26,6 @@ var envvarSecrets = []string{
"ARM_SUBSCRIPTION_ID",
}

type Service struct {
cliFlags *types.CliFlags
}

func (s *Service) CreateCluster(ctx context.Context) error {
progress.DisplayLogHints(20)

isValid, catalogApps, err := catalog.ValidateCatalogApps(ctx, s.cliFlags.InstallCatalogApps)
if !isValid {
progress.Error(err.Error())
return nil
}

err = ValidateProvidedFlags(s.cliFlags.GitProvider)
if err != nil {
progress.Error(err.Error())
return nil
}

if err := provision.ManagementCluster(s.cliFlags, catalogApps); err != nil {
return fmt.Errorf("failed to provision management cluster: %w", err)
}

return nil
}

func ValidateProvidedFlags(gitProvider string) error {
progress.AddStep("Validate provided flags")

Expand Down
19 changes: 16 additions & 3 deletions cmd/civo/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"fmt"

"github.com/konstructio/kubefirst-api/pkg/constants"
"github.com/konstructio/kubefirst/internal/catalog"
"github.com/konstructio/kubefirst/internal/common"
"github.com/konstructio/kubefirst/internal/progress"
"github.com/konstructio/kubefirst/internal/provision"
"github.com/konstructio/kubefirst/internal/utilities"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -62,18 +64,29 @@ func Create() *cobra.Command {
Short: "Create the Kubefirst platform running on Civo Kubernetes",
TraverseChildren: true,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
cliFlags, err := utilities.GetFlags(cmd, "civo")
if err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to get CLI flags: %w", err)
}

service := &Service{
cliFlags: &cliFlags,
progress.DisplayLogHints(15)

isValid, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps)
if !isValid {
return fmt.Errorf("catalog apps validation failed: %w", err)
}

err = service.CreateCluster(cmd.Context())
err = ValidateProvidedFlags(cliFlags.GitProvider, cliFlags.DNSProvider)
if err != nil {
progress.Error(err.Error())
return fmt.Errorf("failed to validate provided flags: %w", err)
}

provision := provision.Provisioner{}

if err := provision.ProvisionManagementCluster(ctx, &cliFlags, catalogApps); err != nil {
return fmt.Errorf("failed to create Civo management cluster: %w", err)
}

Expand Down
Loading

0 comments on commit 32badfa

Please sign in to comment.