Skip to content

Commit

Permalink
chore(vclusterctl): consolidate vcluster cli and platform config
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesfrey committed May 23, 2024
1 parent fbbbc52 commit a94a3a1
Show file tree
Hide file tree
Showing 126 changed files with 1,874 additions and 9,216 deletions.
18 changes: 9 additions & 9 deletions cmd/vclusterctl/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
loftctlUtil "github.com/loft-sh/loftctl/v4/pkg/util"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/manager"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/upgrade"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -91,15 +93,13 @@ func (cmd *ConnectCmd) Run(ctx context.Context, args []string) error {
return err
}

// get manager
manager, err := platform.GetManager(cmd.Manager)
if err != nil {
return err
}

// is platform manager?
if manager == platform.ManagerPlatform {
return cli.ConnectPlatform(ctx, &cmd.ConnectOptions, cmd.GlobalFlags, vClusterName, args[1:], cmd.Log)
cfg := config.Read(cmd.Config, cmd.Log)
if cfg.Manager.Type == manager.Platform {
platformClient, err := platform.CreateClientFromConfig(ctx, cfg.Platform.Config)
if err != nil {
return err
}
return cli.ConnectPlatform(ctx, &cmd.ConnectOptions, platformClient, cmd.GlobalFlags, vClusterName, args[1:], cmd.Log)
}

return cli.ConnectHelm(ctx, &cmd.ConnectOptions, cmd.GlobalFlags, vClusterName, args[1:], cmd.Log)
Expand Down
35 changes: 27 additions & 8 deletions cmd/vclusterctl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
loftctlUtil "github.com/loft-sh/loftctl/v4/pkg/util"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/constants"
"github.com/loft-sh/vcluster/pkg/manager"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/upgrade"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -94,16 +96,33 @@ vcluster create test --namespace test

// Run executes the functionality
func (cmd *CreateCmd) Run(ctx context.Context, args []string) error {
manager, err := platform.GetManager(cmd.Manager)
if err != nil {
return err
}
cfg := config.Read(cmd.Config, cmd.log)

config.PrintManagerInfo("create", cfg.Manager.Type, cmd.log)
platformClient, err := platform.CreateClientFromConfig(ctx, cfg.Platform.Config)

// check if we should create a platform vCluster
platform.PrintManagerInfo("create", manager, cmd.log)
if manager == platform.ManagerPlatform {
return cli.CreatePlatform(ctx, &cmd.CreateOptions, cmd.GlobalFlags, args[0], cmd.log)
if cfg.Manager.Type == manager.Platform {
// when using the platform manager it is necessary to have a working platformClient.
if err != nil {
return err
}
return cli.CreatePlatform(ctx, &cmd.CreateOptions, platformClient, cmd.GlobalFlags, args[0], cmd.log)
}

// when using helm the platformClient is not strictly necessary, only when the given vcluster values config of the user has platform features enabled.
if err := cli.CreateHelm(ctx, &cmd.CreateOptions, platformClient, cmd.GlobalFlags, args[0], cmd.log); err != nil {
return err
}
// if we have a platformClient we need to write the latest config to file because CreateHelm updates the VirtualClusterAccessKey in the platform config.
if platformClient != nil {
platformConfig := platformClient.Config()
cfg.Platform.Config = platformConfig

if err := config.Write(cmd.Config, cfg); err != nil {
return fmt.Errorf("save vCluster config: %w", err)
}
}

return cli.CreateHelm(ctx, &cmd.CreateOptions, cmd.GlobalFlags, args[0], cmd.log)
return nil
}
21 changes: 11 additions & 10 deletions cmd/vclusterctl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
loftctlUtil "github.com/loft-sh/loftctl/v4/pkg/util"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/manager"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -62,17 +64,16 @@ vcluster delete test --namespace test

// Run executes the functionality
func (cmd *DeleteCmd) Run(cobraCmd *cobra.Command, args []string) error {
manager, err := platform.GetManager(cmd.Manager)
if err != nil {
return err
}
cfg := config.Read(cmd.Config, cmd.log)
config.PrintManagerInfo("delete", cfg.Manager.Type, cmd.log)

// check if we should delete a platform vCluster
platform.PrintManagerInfo("delete", manager, cmd.log)
if manager == platform.ManagerPlatform {
// deploy platform cluster
return cli.DeletePlatform(cobraCmd.Context(), &cmd.DeleteOptions, args[0], cmd.log)
if cfg.Manager.Type == manager.Platform {
platformClient, err := platform.CreateClientFromConfig(cobraCmd.Context(), cfg.Platform.Config)
if err != nil {
return err
}
return cli.DeletePlatform(cobraCmd.Context(), &cmd.DeleteOptions, platformClient, args[0], cmd.log)
}

return cli.DeleteHelm(cobraCmd.Context(), &cmd.DeleteOptions, cmd.GlobalFlags, args[0], cmd.log)
return cli.DeleteHelm(cobraCmd.Context(), &cmd.DeleteOptions, cfg.Platform.Config, cmd.GlobalFlags, args[0], cmd.log)
}
10 changes: 7 additions & 3 deletions cmd/vclusterctl/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"runtime"

"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/telemetry"
"github.com/spf13/cobra"
Expand All @@ -20,7 +22,7 @@ type cliInfo struct {
}

// NewInfoCmd creates a new info command
func NewInfoCmd() *cobra.Command {
func NewInfoCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cobraCmd := &cobra.Command{
Use: "info",
Short: "Displays informations about the cli and platform",
Expand All @@ -37,14 +39,16 @@ vcluster info
Args: cobra.NoArgs,
Hidden: true,
RunE: func(cobraCmd *cobra.Command, _ []string) error {
logger := log.GetInstance()
infos := cliInfo{
Version: cobraCmd.Root().Version,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
MachineID: telemetry.GetMachineID(log.GetInstance()),
MachineID: telemetry.GetMachineID(globalFlags.Config, logger),
}

platformClient, err := platform.CreatePlatformClient()
cfg := config.Read(globalFlags.Config, logger)
platformClient, err := platform.CreateClientFromConfig(cobraCmd.Context(), cfg.Platform.Config)
if err == nil {
infos.InstanceID = platformClient.Self().Status.InstanceID
}
Expand Down
15 changes: 6 additions & 9 deletions cmd/vclusterctl/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cmd
import (
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/cli"
"github.com/loft-sh/vcluster/pkg/cli/config"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/manager"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -53,15 +54,11 @@ vcluster list --namespace test

// Run executes the functionality
func (cmd *ListCmd) Run(cobraCmd *cobra.Command, _ []string) error {
manager, err := platform.GetManager(cmd.Manager)
if err != nil {
return err
}

cfg := config.Read(cmd.Config, cmd.log)
// check if we should create a platform vCluster
if manager == platform.ManagerPlatform {
return cli.ListPlatform(cobraCmd.Context(), &cmd.ListOptions, cmd.GlobalFlags, cmd.log)
if cfg.Manager.Type == manager.Platform {
return cli.ListPlatform(cobraCmd.Context(), &cmd.ListOptions, cfg.Platform.Config, cmd.GlobalFlags, cmd.log)
}

return cli.ListHelm(cobraCmd.Context(), &cmd.ListOptions, cmd.GlobalFlags, cmd.log)
return cli.ListHelm(cobraCmd.Context(), &cmd.ListOptions, cfg.Platform.Config, cmd.GlobalFlags, cmd.log)
}
Loading

0 comments on commit a94a3a1

Please sign in to comment.