Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesfrey committed May 17, 2024
1 parent 9cdaba6 commit f3eb913
Show file tree
Hide file tree
Showing 30 changed files with 436 additions and 257 deletions.
6 changes: 3 additions & 3 deletions cmd/vclusterctl/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ 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/platform"
"github.com/loft-sh/vcluster/pkg/upgrade"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -92,13 +92,13 @@ func (cmd *ConnectCmd) Run(ctx context.Context, args []string) error {
}

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

// is platform manager?
if manager == platform.ManagerPlatform {
if manager == config.ManagerPlatform {
return cli.ConnectPlatform(ctx, &cmd.ConnectOptions, cmd.GlobalFlags, vClusterName, args[1:], cmd.Log)
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/vclusterctl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,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/constants"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/loft-sh/vcluster/pkg/upgrade"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -94,14 +94,14 @@ 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)
manager, err := config.GetManager(cmd.Config, cmd.Manager)
if err != nil {
return err
}

// check if we should create a platform vCluster
platform.PrintManagerInfo("create", manager, cmd.log)
if manager == platform.ManagerPlatform {
config.PrintManagerInfo("create", manager, cmd.log)
if manager == config.ManagerPlatform {
return cli.CreatePlatform(ctx, &cmd.CreateOptions, cmd.GlobalFlags, args[0], cmd.log)
}

Expand Down
13 changes: 9 additions & 4 deletions cmd/vclusterctl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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/platform"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -62,14 +63,18 @@ 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)
manager, err := config.GetManager(cmd.Config, cmd.Manager)
if err != nil {
return err
}

// check if we should delete a platform vCluster
platform.PrintManagerInfo("delete", manager, cmd.log)
if manager == platform.ManagerPlatform {
// check if we should create a platform vCluster
// if there is a platform client we print an info message
_, err = platform.CreatePlatformClient()
if err == nil {
config.PrintManagerInfo("delete", manager, cmd.log)
}
if manager == config.ManagerPlatform {
// deploy platform cluster
return cli.DeletePlatform(cobraCmd.Context(), &cmd.DeleteOptions, args[0], cmd.log)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/vclusterctl/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"runtime"

"github.com/loft-sh/log"
"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 +21,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 @@ -41,7 +42,7 @@ vcluster info
Version: cobraCmd.Root().Version,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
MachineID: telemetry.GetMachineID(log.GetInstance()),
MachineID: telemetry.GetMachineID(globalFlags.Config, log.GetInstance()),
}

platformClient, err := platform.CreatePlatformClient()
Expand Down
6 changes: 3 additions & 3 deletions cmd/vclusterctl/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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/spf13/cobra"
)

Expand Down Expand Up @@ -53,13 +53,13 @@ vcluster list --namespace test

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

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

Expand Down
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(globalFlags.Config, options.Manager, log.GetInstance())
if err != nil {
return fmt.Errorf("switch manager failed: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/vclusterctl/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ 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/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 Down Expand Up @@ -48,7 +49,7 @@ vcluster logout
return err
}

err = use.SwitchManager(string(platform.ManagerHelm), log.GetInstance())
err = use.SwitchManager(globalFlags.Config, string(config.ManagerHelm), log.GetInstance())
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/vclusterctl/cmd/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ 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/platform"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -65,13 +65,13 @@ 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)
manager, err := config.GetManager(cmd.Config, cmd.Manager)
if err != nil {
return err
}

// check if we should create a platform vCluster
if manager == platform.ManagerPlatform {
if manager == config.ManagerPlatform {
return cli.PausePlatform(ctx, &cmd.PauseOptions, args[0], cmd.Log)
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/vclusterctl/cmd/platform/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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/platform"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -56,13 +56,13 @@ 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)
manager, err := config.GetManager(cmd.Config, cmd.Manager)
if err != nil {
return err
}

// check if we should create a platform vCluster
if manager == platform.ManagerPlatform {
if manager == config.ManagerPlatform {
return cli.ActivatePlatform(ctx, &cmd.ActivateOptions, cmd.GlobalFlags, args[0], cmd.Log)
}

Expand Down
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
6 changes: 3 additions & 3 deletions cmd/vclusterctl/cmd/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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/platform"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -60,13 +60,13 @@ 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)
manager, err := config.GetManager(cmd.Config, cmd.Manager)
if err != nil {
return err
}

// check if we should resume a platform backed virtual cluster
if manager == platform.ManagerPlatform {
if manager == config.ManagerPlatform {
return cli.ResumePlatform(ctx, &cmd.ResumeOptions, args[0], cmd.Log)
}

Expand Down
23 changes: 16 additions & 7 deletions cmd/vclusterctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
cmdpro "github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/platform"
cmdtelemetry "github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/telemetry"
"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/platform"
"github.com/loft-sh/vcluster/pkg/telemetry"
Expand All @@ -27,6 +28,14 @@ func NewRootCmd(log log.Logger) *cobra.Command {
SilenceErrors: true,
Short: "Welcome to vcluster!",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
if globalFlags.Config == "" {
var err error
globalFlags.Config, err = config.DefaultConfigFilePath()
if err != nil {
log.Fatalf("failed to get vcluster configuration file path: %w", err)
}
}

if globalFlags.Silent {
log.SetLevel(logrus.FatalLevel)
} else if globalFlags.Debug {
Expand All @@ -49,9 +58,6 @@ func Execute() {
panic(err)
}

// start telemetry
telemetry.StartCLI()

// start command
log := log.GetInstance()
rootCmd, err := BuildRoot(log)
Expand All @@ -60,6 +66,9 @@ func Execute() {
log.Fatalf("error building root: %+v\n", err)
}

// start telemetry
telemetry.StartCLI(globalFlags.Config)

// Execute command
err = rootCmd.ExecuteContext(context.Background())
recordAndFlush(err)
Expand All @@ -76,7 +85,7 @@ func Execute() {
func BuildRoot(log log.Logger) (*cobra.Command, error) {
rootCmd := NewRootCmd(log)
persistentFlags := rootCmd.PersistentFlags()
globalFlags = flags.SetGlobalFlags(persistentFlags)
globalFlags = flags.SetGlobalFlags(persistentFlags, log)

// Set version for --version flag
rootCmd.Version = upgrade.GetVersion()
Expand All @@ -93,9 +102,9 @@ func BuildRoot(log log.Logger) (*cobra.Command, error) {
rootCmd.AddCommand(get.NewGetCmd(globalFlags))
rootCmd.AddCommand(use.NewUseCmd(globalFlags))
rootCmd.AddCommand(convert.NewConvertCmd(globalFlags))
rootCmd.AddCommand(cmdtelemetry.NewTelemetryCmd())
rootCmd.AddCommand(cmdtelemetry.NewTelemetryCmd(globalFlags))
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(NewInfoCmd())
rootCmd.AddCommand(NewInfoCmd(globalFlags))

// add pro commands
proCmd, err := cmdpro.NewProCmd(globalFlags)
Expand Down Expand Up @@ -143,6 +152,6 @@ func BuildRoot(log log.Logger) (*cobra.Command, error) {
}

func recordAndFlush(err error) {
telemetry.CollectorCLI.RecordCLI(platform.Self, err)
telemetry.CollectorCLI.RecordCLI(globalFlags.Config, platform.Self, err)
telemetry.CollectorCLI.Flush()
}
Loading

0 comments on commit f3eb913

Please sign in to comment.