Skip to content

Commit

Permalink
refactor: move legacy config to /config
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Apr 22, 2024
1 parent deff887 commit ef8174c
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 57 deletions.
28 changes: 2 additions & 26 deletions cmd/vclusterctl/cmd/activate.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
package cmd

import (
"context"
"os"

loftctlUtil "github.com/loft-sh/loftctl/v3/pkg/util"
"github.com/loft-sh/log"
platformcmd "github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/platform"
"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/cli"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
)

type ActivateCmd struct {
*flags.GlobalFlags
cli.ActivateOptions

Log log.Logger
}

func NewActivateCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
cmd := &ActivateCmd{
cmd := &platformcmd.ImportCmd{
GlobalFlags: globalFlags,
Log: log.GetInstance(),
}
Expand Down Expand Up @@ -61,18 +52,3 @@ vcluster activate my-vcluster --cluster connected-cluster \

return importCmd, nil
}

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

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

return cli.ActivateHelm()
}
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/convert/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"

"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/config/legacyconfig"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/config/legacyconfig"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/yaml"
Expand Down
77 changes: 77 additions & 0 deletions cmd/vclusterctl/cmd/platform/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package platform

import (
"context"
"os"

loftctlUtil "github.com/loft-sh/loftctl/v3/pkg/util"
"github.com/loft-sh/log"
"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/cli"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"github.com/spf13/cobra"
)

type ImportCmd struct {
*flags.GlobalFlags
cli.ActivateOptions

Log log.Logger
}

func NewImportCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &ImportCmd{
GlobalFlags: globalFlags,
Log: log.GetInstance(),
}

description := `########################################################
############### vcluster platform import ###############
########################################################
Imports a vCluster into a vCluster platform project.
Example:
vcluster platform import my-vcluster --cluster connected-cluster \
--namespace vcluster-my-vcluster --project my-project --import-name my-vcluster
#######################################################
`

importCmd := &cobra.Command{
Use: "import" + loftctlUtil.VClusterNameOnlyUseLine,
Short: "Imports a vCluster into a vCluster platform project",
Long: description,
Args: loftctlUtil.VClusterNameOnlyValidator,
RunE: func(cobraCmd *cobra.Command, args []string) error {
if config.ShouldCheckForProFeatures() {
cmd.Log.Warnf("In order to use a Pro feature, please contact us at https://www.vcluster.com/pro-demo or downgrade by running `vcluster upgrade --version v0.19.5`")
os.Exit(1)
}

return cmd.Run(cobraCmd.Context(), args)
},
}

importCmd.Flags().StringVar(&cmd.Manager, "manager", "", "The manager to use for managing the virtual cluster, can be either helm or platform.")

importCmd.Flags().StringVar(&cmd.ClusterName, "cluster", "", "Cluster name of the cluster the virtual cluster is running on")
importCmd.Flags().StringVar(&cmd.Project, "project", "", "The project to import the vCluster into")
importCmd.Flags().StringVar(&cmd.ImportName, "import-name", "", "The name of the vCluster under projects. If unspecified, will use the vcluster name")

return importCmd
}

// 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
}

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

return cli.ActivateHelm()
}
1 change: 1 addition & 0 deletions cmd/vclusterctl/cmd/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Deprecated, please use vcluster platform instead
platformCmd.AddCommand(NewResetCmd(loftctlGlobalFlags))
platformCmd.AddCommand(connect.NewConnectCmd(loftctlGlobalFlags))
platformCmd.AddCommand(NewAccessKeyCmd(loftctlGlobalFlags))
platformCmd.AddCommand(NewImportCmd(globalFlags))

return platformCmd, nil
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 12 additions & 13 deletions pkg/cli/connect_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ type ConnectOptions struct {

type connectHelm struct {
*flags.GlobalFlags

ConnectOptions
*ConnectOptions

portForwarding bool
rawConfig clientcmdapi.Config
Expand All @@ -75,7 +74,7 @@ type connectHelm struct {
func ConnectHelm(ctx context.Context, options *ConnectOptions, globalFlags *flags.GlobalFlags, vClusterName string, command []string, log log.Logger) error {
cmd := &connectHelm{
GlobalFlags: globalFlags,
ConnectOptions: *options,
ConnectOptions: options,
Log: log,
}

Expand Down Expand Up @@ -125,11 +124,11 @@ func (cmd *connectHelm) connect(ctx context.Context, vCluster *find.VCluster, co
}

// build vKubeConfig
return executeCommand(getLocalVClusterConfig(*kubeConfig, &cmd.ConnectOptions), command, cmd.errorChan, cmd.Log)
return executeCommand(getLocalVClusterConfig(*kubeConfig, cmd.ConnectOptions), command, cmd.errorChan, cmd.Log)
}

// write kube config
err = writeKubeConfig(kubeConfig, vCluster.Name, &cmd.ConnectOptions, cmd.GlobalFlags, cmd.portForwarding, cmd.Log)
err = writeKubeConfig(kubeConfig, vCluster.Name, cmd.ConnectOptions, cmd.GlobalFlags, cmd.portForwarding, cmd.Log)
if err != nil {
return err
}
Expand All @@ -155,7 +154,12 @@ func writeKubeConfig(kubeConfig *clientcmdapi.Config, vClusterName string, optio
}

// write kube config to file
if options.UpdateCurrent {
if options.Print {
_, err = os.Stdout.Write(out)
if err != nil {
return err
}
} else if options.UpdateCurrent {
var clusterConfig *clientcmdapi.Cluster
for _, c := range kubeConfig.Clusters {
clusterConfig = c
Expand Down Expand Up @@ -199,11 +203,6 @@ func writeKubeConfig(kubeConfig *clientcmdapi.Config, vClusterName string, optio
log.WriteString(logrus.InfoLevel, "- Use `vcluster disconnect` to return to your previous kube context\n")
log.WriteString(logrus.InfoLevel, "- Use `kubectl get namespaces` to access the vcluster\n")
}
} else if options.Print {
_, err = os.Stdout.Write(out)
if err != nil {
return err
}
} else {
err = os.WriteFile(options.KubeConfig, out, 0666)
if err != nil {
Expand Down Expand Up @@ -377,7 +376,7 @@ func (cmd *connectHelm) getVClusterKubeConfig(ctx context.Context, vclusterName

// we want to use a service account token in the kube config
if cmd.ServiceAccount != "" {
token, err := createServiceAccountToken(ctx, *kubeConfig, &cmd.ConnectOptions, cmd.Log)
token, err := createServiceAccountToken(ctx, *kubeConfig, cmd.ConnectOptions, cmd.Log)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -595,7 +594,7 @@ func getLocalVClusterClient(vKubeConfig clientcmdapi.Config, options *ConnectOpt
}

func (cmd *connectHelm) waitForVCluster(ctx context.Context, vKubeConfig clientcmdapi.Config, errorChan chan error) error {
vKubeClient, err := getLocalVClusterClient(vKubeConfig, &cmd.ConnectOptions)
vKubeClient, err := getLocalVClusterClient(vKubeConfig, cmd.ConnectOptions)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/connect_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestExchangeContextName(t *testing.T) {
GlobalFlags: &flags.GlobalFlags{
Namespace: namespace,
},
ConnectOptions: ConnectOptions{
ConnectOptions: &ConnectOptions{
KubeConfigContextName: testCase.newContextName,
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cli/connect_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

type connectPlatform struct {
*flags.GlobalFlags
ConnectOptions
*ConnectOptions

log log.Logger
}
Expand All @@ -38,7 +38,7 @@ func ConnectPlatform(ctx context.Context, options *ConnectOptions, globalFlags *
// create connect platform command
cmd := connectPlatform{
GlobalFlags: globalFlags,
ConnectOptions: *options,
ConnectOptions: options,

log: log,
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func (cmd *connectPlatform) getVClusterKubeConfig(ctx context.Context, platformC
}

// create service account token
token, err := createServiceAccountToken(ctx, *kubeConfig, &cmd.ConnectOptions, cmd.log)
token, err := createServiceAccountToken(ctx, *kubeConfig, cmd.ConnectOptions, cmd.log)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cli/create_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"github.com/loft-sh/log/survey"
"github.com/loft-sh/log/terminal"
"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/config/legacyconfig"
"github.com/loft-sh/vcluster/pkg/cli/find"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/cli/localkubernetes"
"github.com/loft-sh/vcluster/pkg/config/legacyconfig"
"github.com/loft-sh/vcluster/pkg/constants"
"github.com/loft-sh/vcluster/pkg/embed"
"github.com/loft-sh/vcluster/pkg/helm"
Expand Down Expand Up @@ -82,7 +82,7 @@ var AllowedDistros = []string{"k8s", "k3s", "k0s", "eks"}

type createHelm struct {
*flags.GlobalFlags
CreateOptions
*CreateOptions

rawConfig clientcmdapi.Config
log log.Logger
Expand All @@ -94,7 +94,7 @@ type createHelm struct {
func CreateHelm(ctx context.Context, options *CreateOptions, globalFlags *flags.GlobalFlags, vClusterName string, log log.Logger) error {
cmd := &createHelm{
GlobalFlags: globalFlags,
CreateOptions: *options,
CreateOptions: options,

log: log,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/delete_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type DeleteOptions struct {

type deleteHelm struct {
*flags.GlobalFlags
DeleteOptions
*DeleteOptions

rawConfig *clientcmdapi.Config
restConfig *rest.Config
Expand All @@ -50,7 +50,7 @@ type deleteHelm struct {
func DeleteHelm(ctx context.Context, options *DeleteOptions, globalFlags *flags.GlobalFlags, vClusterName string, log log.Logger) error {
cmd := deleteHelm{
GlobalFlags: globalFlags,
DeleteOptions: *options,
DeleteOptions: options,
log: log,
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/list_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ func ListHelm(ctx context.Context, options *ListOptions, globalFlags *flags.Glob
return err
}

err = printVClusters(ctx, options, globalFlags, ossToVClusters(vClusters, currentContext), true, log)
err = printVClusters(ctx, options, ossToVClusters(vClusters, currentContext), globalFlags, true, log)
if err != nil {
return err
}

return nil
}

func printVClusters(ctx context.Context, options *ListOptions, globalFlags *flags.GlobalFlags, output []ListVCluster, showPlatform bool, logger log.Logger) error {
func printVClusters(ctx context.Context, options *ListOptions, output []ListVCluster, globalFlags *flags.GlobalFlags, showPlatform bool, logger log.Logger) error {
if options.Output == "json" {
bytes, err := json.MarshalIndent(output, "", " ")
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion pkg/cli/list_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func ListPlatform(ctx context.Context, options *ListOptions, globalFlags *flags.
}
currentContext := rawConfig.CurrentContext

if globalFlags.Context == "" {
globalFlags.Context = currentContext
}

platformClient, err := platform.CreatePlatformClient()
if err != nil {
return err
Expand All @@ -28,7 +32,7 @@ func ListPlatform(ctx context.Context, options *ListOptions, globalFlags *flags.
return err
}

err = printVClusters(ctx, options, globalFlags, proToVClusters(proVClusters, currentContext), false, logger)
err = printVClusters(ctx, options, proToVClusters(proVClusters, currentContext), globalFlags, false, logger)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"

"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/config/legacyconfig"
"github.com/loft-sh/vcluster/config/legacyconfig"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
Expand Down
4 changes: 2 additions & 2 deletions pkg/platform/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func PrintManagerInfo(verb string, manager ManagerType, log log.Logger) {
if err == nil {
if manager == ManagerHelm {
log.Infof("Using vCluster manager 'helm' to %s your vClusters, which means the vCluster CLI is running helm commands directly", verb)
log.Infof("If you prefer to use the vCluster platform API instead, use the flag '--manager platform' or run 'vcluster use manager platform' to change the default")
log.Info("If you prefer to use the vCluster platform API instead, use the flag '--manager platform' or run 'vcluster use manager platform' to change the default")
} else {
log.Info("Using vCluster manager 'platform' to %s your vClusters, which means the CLI is using the vCluster platform API instead of helm", verb)
log.Infof("Using vCluster manager 'platform' to %s your vClusters, which means the CLI is using the vCluster platform API instead of helm", verb)
log.Info("If you prefer to use helm instead, use the flag '--manager helm' or run 'vcluster use manager helm' to change the default")
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sync"
"time"

"github.com/loft-sh/vcluster/pkg/config/legacyconfig"
"github.com/loft-sh/vcluster/config/legacyconfig"
plugintypes "github.com/loft-sh/vcluster/pkg/plugin/types"
"github.com/loft-sh/vcluster/pkg/util/kubeconfig"
"github.com/loft-sh/vcluster/pkg/util/loghelper"
Expand Down
2 changes: 1 addition & 1 deletion pkg/setup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"os"

vclusterconfig "github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/config/legacyconfig"
"github.com/loft-sh/vcluster/pkg/config"
"github.com/loft-sh/vcluster/pkg/config/legacyconfig"
"github.com/loft-sh/vcluster/pkg/helm"
"github.com/loft-sh/vcluster/pkg/k3s"
"github.com/loft-sh/vcluster/pkg/util/translate"
Expand Down

0 comments on commit ef8174c

Please sign in to comment.