Skip to content

Commit

Permalink
Refactored pro commands
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Kosiewski <[email protected]>
  • Loading branch information
Thomas Kosiewski committed Sep 22, 2023
1 parent 3bacd5d commit 5af9ef0
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 234 deletions.
42 changes: 25 additions & 17 deletions cmd/vclusterctl/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,28 @@ package cmd
import (
"fmt"

"github.com/loft-sh/api/v3/pkg/product"
loftctl "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd"
loftctlflags "github.com/loft-sh/loftctl/v3/cmd/loftctl/flags"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/spf13/cobra"
)

func NewLoginCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftctlGlobalFlags := &loftctlflags.GlobalFlags{
Silent: globalFlags.Silent,
Debug: globalFlags.Debug,
LogOutput: globalFlags.LogOutput,
loftctlGlobalFlags, err := pro.GlobalFlags(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to parse pro flags: %w", err)
}

if globalFlags.Config != "" {
loftctlGlobalFlags.Config = globalFlags.Config
} else {
var err error
loftctlGlobalFlags.Config, err = pro.LoftctlConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
cmd := &loftctl.LoginCmd{
GlobalFlags: loftctlGlobalFlags,
Log: log.GetInstance(),
}

loginCmd := loftctl.NewLoginCmd(loftctlGlobalFlags)

loginCmd.Use = "login [VCLUSTER_PRO_HOST]"
loginCmd.Long = `########################################################
description := `########################################################
#################### vcluster login ####################
########################################################
Login into vCluster.Pro
Example:
Expand All @@ -39,5 +33,19 @@ vcluster login https://my-vcluster-pro.com --access-key myaccesskey
########################################################
`

loginCmd := &cobra.Command{
Use: "login [VCLUSTER_PRO_HOST]",
Short: "Login to a vCluster.Pro instance",
Long: description,
Args: cobra.MaximumNArgs(1),
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.RunLogin(cobraCmd.Context(), args)
},
}

loginCmd.Flags().StringVar(&cmd.AccessKey, "access-key", "", "The access key to use")
loginCmd.Flags().BoolVar(&cmd.Insecure, "insecure", false, product.Replace("Allow login into an insecure Loft instance"))
loginCmd.Flags().BoolVar(&cmd.DockerLogin, "docker-login", true, "If true, will log into the docker image registries the user has image pull secrets for")

return loginCmd, nil
}
38 changes: 21 additions & 17 deletions cmd/vclusterctl/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,43 @@ import (
"fmt"

loftctl "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd"
loftctlflags "github.com/loft-sh/loftctl/v3/cmd/loftctl/flags"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/spf13/cobra"
)

func NewLogoutCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftctlGlobalFlags := &loftctlflags.GlobalFlags{
Silent: globalFlags.Silent,
Debug: globalFlags.Debug,
LogOutput: globalFlags.LogOutput,
loftctlGlobalFlags, err := pro.GlobalFlags(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to parse pro flags: %w", err)
}

if globalFlags.Config != "" {
loftctlGlobalFlags.Config = globalFlags.Config
} else {
var err error
loftctlGlobalFlags.Config, err = pro.LoftctlConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
cmd := &loftctl.LogoutCmd{
GlobalFlags: loftctlGlobalFlags,
Log: log.GetInstance(),
}

logoutCmd := loftctl.NewLogoutCmd(loftctlGlobalFlags)

logoutCmd.Use = "logout"
logoutCmd.Long = `########################################################
description := `########################################################
################### vcluster logout ####################
########################################################
Log out of vCluster.Pro
Example:
vcluster logout
########################################################
`

logoutCmd := &cobra.Command{
Use: "logout",
Short: "Log out of a vCluster.Pro instance",
Long: description,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.RunLogout(cobraCmd.Context(), args)
},
}

return logoutCmd, nil

}
57 changes: 4 additions & 53 deletions cmd/vclusterctl/cmd/pro/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package pro
import (
"fmt"

loftctl "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd"
loftctlreset "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd/reset"
loftctlflags "github.com/loft-sh/loftctl/v3/cmd/loftctl/flags"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/spf13/cobra"
Expand All @@ -23,20 +19,9 @@ func NewProCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
Args: cobra.NoArgs,
}

loftctlGlobalFlags := &loftctlflags.GlobalFlags{
Silent: globalFlags.Silent,
Debug: globalFlags.Debug,
LogOutput: globalFlags.LogOutput,
}

if globalFlags.Config != "" {
loftctlGlobalFlags.Config = globalFlags.Config
} else {
var err error
loftctlGlobalFlags.Config, err = pro.LoftctlConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
loftctlGlobalFlags, err := pro.GlobalFlags(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to parse pro flags: %w", err)
}

startCmd, err := NewStartCmd(loftctlGlobalFlags)
Expand All @@ -45,41 +30,7 @@ func NewProCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
}

proCmd.AddCommand(startCmd)
proCmd.AddCommand(loftctlreset.NewResetCmd(loftctlGlobalFlags))
proCmd.AddCommand(NewResetCmd(loftctlGlobalFlags))

return proCmd, nil
}

func NewStartCmd(loftctlGlobalFlags *loftctlflags.GlobalFlags) (*cobra.Command, error) {
starCmd := loftctl.NewStartCmd(loftctlGlobalFlags)

err := starCmd.Flags().Set("product", "vcluster-pro")
if err != nil {
return nil, fmt.Errorf("failed to set product flag: %w", err)
}

err = starCmd.Flags().Set("chart-name", "vcluster-control-plane")
if err != nil {
return nil, fmt.Errorf("failed to set chart-name flag: %w", err)
}

starCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
version := pro.MinimumVersionTag

latestVersion, err := pro.LatestCompatibleVersion(cmd.Context())
if err != nil {
log.GetInstance().Warnf("failed to get latest compatible version: %v", err)
} else {
version = latestVersion
}

err = starCmd.Flags().Set("version", version)
if err != nil {
return fmt.Errorf("failed to set version flag: %w", err)
}

return nil
}

return starCmd, nil
}
60 changes: 60 additions & 0 deletions cmd/vclusterctl/cmd/pro/reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package pro

import (
"github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd/reset"
loftctlflags "github.com/loft-sh/loftctl/v3/cmd/loftctl/flags"
"github.com/loft-sh/log"
"github.com/spf13/cobra"
)

func NewResetCmd(loftctlGlobalFlags *loftctlflags.GlobalFlags) *cobra.Command {
description := `########################################################
################## vcluster pro reset ##################
########################################################
`
cmd := &cobra.Command{
Use: "reset",
Short: "Reset configuration",
Long: description,
Args: cobra.NoArgs,
}

cmd.AddCommand(NewPasswordCmd(loftctlGlobalFlags))

return cmd
}

func NewPasswordCmd(globalFlags *loftctlflags.GlobalFlags) *cobra.Command {
cmd := &reset.PasswordCmd{
GlobalFlags: globalFlags,
Log: log.GetInstance(),
}

description := `########################################################
############## vcluster pro reset password #############
########################################################
Resets the password of a user.
Example:
vcluster pro reset password
vcluster pro reset password --user admin
#######################################################
`

c := &cobra.Command{
Use: "password",
Short: "Resets the password of a user",
Long: description,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.Run()
},
}

c.Flags().StringVar(&cmd.User, "user", "admin", "The name of the user to reset the password")
c.Flags().StringVar(&cmd.Password, "password", "", "The new password to use")
c.Flags().BoolVar(&cmd.Create, "create", false, "Creates the user if it does not exist")
c.Flags().BoolVar(&cmd.Force, "force", false, "If user had no password will create one")

return c
}
75 changes: 75 additions & 0 deletions cmd/vclusterctl/cmd/pro/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package pro

import (
"context"

loftctl "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd"
loftctlflags "github.com/loft-sh/loftctl/v3/cmd/loftctl/flags"
"github.com/loft-sh/loftctl/v3/pkg/start"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/spf13/cobra"
)

func NewStartCmd(loftctlGlobalFlags *loftctlflags.GlobalFlags) (*cobra.Command, error) {
cmd := &loftctl.StartCmd{
Options: start.Options{
GlobalFlags: loftctlGlobalFlags,
Log: log.GetInstance(),
Product: "vcluster-pro",
},
}

version := pro.MinimumVersionTag

latestVersion, err := pro.LatestCompatibleVersion(context.TODO())
if err == nil {
version = latestVersion
}

startCmd := &cobra.Command{
Use: "start",
Short: "Start a vCluster.Pro instance and connect via port-forwarding",
Long: `########################################################
################## vcluster pro start ##################
########################################################
Starts a vCluster.Pro instance in your Kubernetes cluster
and then establishes a port-forwarding connection.
Please make sure you meet the following requirements
before running this command:
1. Current kube-context has admin access to the cluster
2. Helm v3 must be installed
3. kubectl must be installed
########################################################
`,
Args: cobra.NoArgs,
RunE: func(cobraCmd *cobra.Command, args []string) error {
return start.NewLoftStarter(cmd.Options).Start(cobraCmd.Context())
},
}

startCmd.Flags().StringVar(&cmd.Context, "context", "", "The kube context to use for installation")
startCmd.Flags().StringVar(&cmd.Namespace, "namespace", "vcluster-pro", "The namespace to install vCluster.Pro into")
startCmd.Flags().StringVar(&cmd.LocalPort, "local-port", "", "The local port to bind to if using port-forwarding")
startCmd.Flags().StringVar(&cmd.Host, "host", "", "Provide a hostname to enable ingress and configure its hostname")
startCmd.Flags().StringVar(&cmd.Password, "password", "", "The password to use for the admin account. (If empty this will be the namespace UID)")
startCmd.Flags().StringVar(&cmd.Version, "version", version, "The vCluster.Pro version to install")
startCmd.Flags().StringVar(&cmd.Values, "values", "", "Path to a file for extra vCluster.Pro helm chart values")
startCmd.Flags().BoolVar(&cmd.ReuseValues, "reuse-values", true, "Reuse previous vCluster.Pro helm values on upgrade")
startCmd.Flags().BoolVar(&cmd.Upgrade, "upgrade", false, "If true, vCluster.Pro will try to upgrade the release")
startCmd.Flags().StringVar(&cmd.Email, "email", "", "The email to use for the installation")
startCmd.Flags().BoolVar(&cmd.Reset, "reset", false, "If true, an existing loft instance will be deleted before installing vCluster.Pro")
startCmd.Flags().BoolVar(&cmd.NoWait, "no-wait", false, "If true, vCluster.Pro will not wait after installing it")
startCmd.Flags().BoolVar(&cmd.NoPortForwarding, "no-port-forwarding", false, "If true, vCluster.Pro will not do port forwarding after installing it")
startCmd.Flags().BoolVar(&cmd.NoTunnel, "no-tunnel", false, "If true, vCluster.Pro will not create a loft.host tunnel for this installation")
startCmd.Flags().BoolVar(&cmd.NoLogin, "no-login", false, "If true, vCluster.Pro will not login to a vCluster.Pro instance on start")
startCmd.Flags().StringVar(&cmd.ChartPath, "chart-path", "", "The vCluster.Pro chart path to deploy vCluster.Pro")
startCmd.Flags().StringVar(&cmd.ChartRepo, "chart-repo", "https://charts.loft.sh/", "The chart repo to deploy vCluster.Pro")
startCmd.Flags().StringVar(&cmd.ChartName, "chart-name", "vcluster-control-plane", "The chart name to deploy vCluster.Pro")

return startCmd, nil
}
12 changes: 3 additions & 9 deletions cmd/vclusterctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,19 @@ func BuildRoot(log log.Logger) (*cobra.Command, error) {
if err != nil {
return nil, fmt.Errorf("failed to create pro command: %w", err)
}
if proCmd != nil {
rootCmd.AddCommand(proCmd)
}
rootCmd.AddCommand(proCmd)

loginCmd, err := NewLoginCmd(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to create login command: %w", err)
}
if loginCmd != nil {
rootCmd.AddCommand(loginCmd)
}
rootCmd.AddCommand(loginCmd)

logoutCmd, err := NewLogoutCmd(globalFlags)
if err != nil {
return nil, fmt.Errorf("failed to create logout command: %w", err)
}
if logoutCmd != nil {
rootCmd.AddCommand(logoutCmd)
}
rootCmd.AddCommand(logoutCmd)

// add completion command
err = rootCmd.RegisterFlagCompletionFunc("namespace", newNamespaceCompletionFunc(rootCmd.Context()))
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/golang-lru/v2 v2.0.2
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0
github.com/loft-sh/api/v3 v3.0.0-20230921143328-114580f85fdd
github.com/loft-sh/loftctl/v3 v3.0.0-20230921143437-669b265e3ecf
github.com/loft-sh/api/v3 v3.0.0-20230922094800-6d0c1cbf0fa6
github.com/loft-sh/loftctl/v3 v3.0.0-20230922094952-2a6aef29f31e
github.com/loft-sh/utils v0.0.25
github.com/mitchellh/go-homedir v1.1.0
github.com/moby/term v0.5.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ github.com/loft-sh/agentapi/v3 v3.3.0-ci.1.0.20230921083523-e1d74f6f8fd1 h1:XE+J
github.com/loft-sh/agentapi/v3 v3.3.0-ci.1.0.20230921083523-e1d74f6f8fd1/go.mod h1:8jmVWFBcLjwh6Tv2KPVxkIjUJpVxUMROcbYYVYxR1pI=
github.com/loft-sh/api/v3 v3.0.0-20230921143328-114580f85fdd h1:8UQwV6TK1QAHlsdrL8we59IKrPH1z8nSO6+mI2lziXs=
github.com/loft-sh/api/v3 v3.0.0-20230921143328-114580f85fdd/go.mod h1:eIbt+Ze6F2fW651JE3VTlFsool+S/hX603aFG5s/AZg=
github.com/loft-sh/api/v3 v3.0.0-20230922094800-6d0c1cbf0fa6 h1:AGmtKdjldgxN4nj6WRSpGe0PKswASDqxnMy1r2ScmVc=
github.com/loft-sh/api/v3 v3.0.0-20230922094800-6d0c1cbf0fa6/go.mod h1:eIbt+Ze6F2fW651JE3VTlFsool+S/hX603aFG5s/AZg=
github.com/loft-sh/apiserver v0.0.0-20230628051307-f26967fbb40f h1:eeLBYlrGOvzJK3h7JYdBGpRvdjzD/sTCVPL2lIQsXlg=
github.com/loft-sh/apiserver v0.0.0-20230628051307-f26967fbb40f/go.mod h1:Mlbl/QTA48cbKpQcmYMA56TQ+Z6Nna7t7ocbCoCmf5w=
github.com/loft-sh/external-types v0.0.2-0.20230301201552-ec939da949b4 h1:eSuBR+6j7MWmab6AU8WGd4EYQiRCZKupxThUxqf38C8=
Expand All @@ -560,6 +562,8 @@ github.com/loft-sh/jspolicy v0.1.0 h1:FNAWR6tRX5NRWxAf9RI/86NRH83NXYbobLHSZyMND7
github.com/loft-sh/jspolicy v0.1.0/go.mod h1:4Zi38iEB0JvhnkrNHPpoueSUWQ1OlHMNB9JHTGEsPO0=
github.com/loft-sh/loftctl/v3 v3.0.0-20230921143437-669b265e3ecf h1:SwIal5P1coM01bw3OYz0PLokpu4iTaYOMlPH1WfWBjk=
github.com/loft-sh/loftctl/v3 v3.0.0-20230921143437-669b265e3ecf/go.mod h1:I+dMG4zKZWOn+CcteN0cyPGOclHlqqmXBC6THcUwj60=
github.com/loft-sh/loftctl/v3 v3.0.0-20230922094952-2a6aef29f31e h1:OhQRp9uH7U14lYg8yOrnOiVhhxxC9tFdwWSyxoSUJ/I=
github.com/loft-sh/loftctl/v3 v3.0.0-20230922094952-2a6aef29f31e/go.mod h1:g2HZHmpkqtiQGqM8QVRuaCgNDI6IMW6DZQP0cOUgxp8=
github.com/loft-sh/log v0.0.0-20230824104949-bd516c25712a h1:/gqqjKpcHEdFXIX41lx1Y/FBqT/72gbPpf7sa20tyM8=
github.com/loft-sh/log v0.0.0-20230824104949-bd516c25712a/go.mod h1:YImeRjXH34Yf5E79T7UHBQpDZl9fIaaFRgyZ/bkY+UQ=
github.com/loft-sh/utils v0.0.25 h1:JbbRJfXO1Rd34fQcaoDSmwyPBEzsrLwBSR21C90hHuk=
Expand Down
Loading

0 comments on commit 5af9ef0

Please sign in to comment.