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 22, 2024
1 parent 82b07ec commit b21e860
Show file tree
Hide file tree
Showing 43 changed files with 522 additions and 494 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)
c := config.Read(cmd.Config, cmd.Log)
if c.Manager.Type == manager.Platform {
platformClient, err := platform.CreatePlatformClient(ctx, c.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
26 changes: 18 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,24 @@ 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
}
c := config.Read(cmd.Config, cmd.log)

// 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)
config.PrintManagerInfo("create", c.Manager.Type, cmd.log)
if c.Manager.Type == manager.Platform {
platformClient, err := platform.CreatePlatformClient(ctx, c.Platform.Config)
if err != nil {
return err
}
return cli.CreatePlatform(ctx, &cmd.CreateOptions, platformClient, cmd.GlobalFlags, args[0], cmd.log)
}

if err := cli.CreateHelm(ctx, &cmd.CreateOptions, &c.Platform.Config, cmd.GlobalFlags, args[0], cmd.log); err != nil {
return err
}
if err := config.Write(cmd.Config, c); 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
}
c := config.Read(cmd.Config, cmd.log)
config.PrintManagerInfo("delete", c.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 c.Manager.Type == manager.Platform {
platformClient, err := platform.CreatePlatformClient(cobraCmd.Context(), c.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, c.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()
c := config.Read(globalFlags.Config, logger)
platformClient, err := platform.CreatePlatformClient(cobraCmd.Context(), c.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
}

c := 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 c.Manager.Type == manager.Platform {
return cli.ListPlatform(cobraCmd.Context(), &cmd.ListOptions, c.Platform.Config, cmd.GlobalFlags, cmd.log)
}

return cli.ListHelm(cobraCmd.Context(), &cmd.ListOptions, cmd.GlobalFlags, cmd.log)
return cli.ListHelm(cobraCmd.Context(), &cmd.ListOptions, c.Platform.Config, cmd.GlobalFlags, cmd.log)
}
5 changes: 2 additions & 3 deletions cmd/vclusterctl/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/use"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
)

Expand All @@ -21,7 +20,7 @@ type LoginOptions struct {
}

func NewLoginCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftGlobalFlags, err := platform.GlobalFlags(globalFlags)
loftGlobalFlags, err := flags.LoftctlGlobalFlags(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to parse pro flags: %w", err)
}
Expand Down Expand Up @@ -61,7 +60,7 @@ vcluster login https://my-vcluster-platform.com --access-key myaccesskey

// should switch manager
if options.Manager != "" {
err = use.SwitchManager(options.Manager, log.GetInstance())
err = use.SwitchManager(cobraCmd.Context(), globalFlags.Config, options.Manager, log.GetInstance())
if err != nil {
return fmt.Errorf("switch manager failed: %w", err)
}
Expand Down
10 changes: 7 additions & 3 deletions cmd/vclusterctl/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
loftctl "github.com/loft-sh/loftctl/v4/cmd/loftctl/cmd"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/use"
"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"
)

func NewLogoutCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftctlGlobalFlags, err := platform.GlobalFlags(globalFlags)
loftctlGlobalFlags, err := flags.LoftctlGlobalFlags(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to parse pro flags: %w", err)
}
Expand All @@ -38,7 +40,9 @@ vcluster logout
Long: description,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
_, err := platform.CreatePlatformClient()
logger := log.GetInstance()
c := config.Read(cmd.Config, logger)
_, err := platform.CreatePlatformClient(cobraCmd.Context(), c.Platform.Config)
if err != nil {
return err
}
Expand All @@ -48,7 +52,7 @@ vcluster logout
return err
}

err = use.SwitchManager(string(platform.ManagerHelm), log.GetInstance())
err = use.SwitchManager(cobraCmd.Context(), globalFlags.Config, string(manager.Helm), logger)
if err != nil {
return err
}
Expand Down
15 changes: 9 additions & 6 deletions cmd/vclusterctl/cmd/pause.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/spf13/cobra"
)
Expand Down Expand Up @@ -65,14 +67,15 @@ vcluster pause test --namespace test

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

// check if we should create a platform vCluster
if manager == platform.ManagerPlatform {
return cli.PausePlatform(ctx, &cmd.PauseOptions, args[0], cmd.Log)
if c.Manager.Type == manager.Platform {
platformClient, err := platform.CreatePlatformClient(ctx, c.Platform.Config)
if err != nil {
return err
}
return cli.PausePlatform(ctx, &cmd.PauseOptions, platformClient, args[0], cmd.Log)
}

return cli.PauseHelm(ctx, cmd.GlobalFlags, args[0], cmd.Log)
Expand Down
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/platform/access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func getToken(cmd *AccessKeyCmd, baseClient client.Client) error {
if config == nil {
return ErrNoConfigLoaded
} else if config.Host == "" || config.AccessKey == "" {
return fmt.Errorf("%w: please make sure you have run '%s [%s]'", ErrNotLoggedIn, product.LoginCmd(), product.Url())
return fmt.Errorf("%w: please make sure you have run '%s' to create one or '%s [%s]' if one already exists", ErrNotLoggedIn, product.StartCmd(), product.LoginCmd(), product.Url())
}

// by default we print the access key as token
Expand Down
26 changes: 19 additions & 7 deletions cmd/vclusterctl/cmd/platform/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package platform

import (
"context"
"fmt"

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 @@ -56,15 +59,24 @@ vcluster platform import my-vcluster --cluster connected-cluster \

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

// check if we should create a platform vCluster
if manager == platform.ManagerPlatform {
return cli.ActivatePlatform(ctx, &cmd.ActivateOptions, cmd.GlobalFlags, args[0], cmd.Log)
if c.Manager.Type == manager.Platform {
platformClient, err := platform.CreatePlatformClient(ctx, c.Platform.Config)
if err != nil {
return err
}

return cli.ActivatePlatform(ctx, &cmd.ActivateOptions, platformClient, cmd.GlobalFlags, args[0], cmd.Log)
}

if err := cli.ActivateHelm(ctx, &cmd.ActivateOptions, &c.Platform.Config, cmd.GlobalFlags, args[0], cmd.Log); err != nil {
return err
}
if err := config.Write(cmd.Config, c); err != nil {
return fmt.Errorf("save vCluster config: %w", err)
}

return cli.ActivateHelm(ctx, &cmd.ActivateOptions, cmd.GlobalFlags, args[0], cmd.Log)
return nil
}
3 changes: 1 addition & 2 deletions cmd/vclusterctl/cmd/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/platform/connect"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
)

Expand All @@ -20,7 +19,7 @@ func NewPlatformCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
Args: cobra.NoArgs,
}

loftctlGlobalFlags, err := platform.GlobalFlags(globalFlags)
loftctlGlobalFlags, err := flags.LoftctlGlobalFlags(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to parse pro flags: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/vclusterctl/cmd/platform/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/platform/connect"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
)

Expand All @@ -22,7 +21,7 @@ Deprecated, please use vcluster platform instead
Args: cobra.NoArgs,
}

loftctlGlobalFlags, err := platform.GlobalFlags(globalFlags)
loftctlGlobalFlags, err := flags.LoftctlGlobalFlags(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to parse pro flags: %w", err)
}
Expand Down
16 changes: 9 additions & 7 deletions cmd/vclusterctl/cmd/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 @@ -60,14 +62,14 @@ vcluster resume test --namespace test

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

c := config.Read(cmd.Config, cmd.Log)
// check if we should resume a platform backed virtual cluster
if manager == platform.ManagerPlatform {
return cli.ResumePlatform(ctx, &cmd.ResumeOptions, args[0], cmd.Log)
if c.Manager.Type == manager.Platform {
platformClient, err := platform.CreatePlatformClient(ctx, c.Platform.Config)
if err != nil {
return err
}
return cli.ResumePlatform(ctx, &cmd.ResumeOptions, platformClient, args[0], cmd.Log)
}

return cli.ResumeHelm(ctx, cmd.GlobalFlags, args[0], cmd.Log)
Expand Down
Loading

0 comments on commit b21e860

Please sign in to comment.