Skip to content

Commit

Permalink
refactor: remove special pro functionality from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Apr 9, 2024
1 parent d1ebef2 commit 20f57c9
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 1,313 deletions.
619 changes: 0 additions & 619 deletions cmd/vclusterctl/cmd/app/create/pro.go

This file was deleted.

59 changes: 0 additions & 59 deletions cmd/vclusterctl/cmd/app/create/types.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func wrapCompletionFuncWithTimeout(defaultDirective cobra.ShellCompDirective, co
// It takes into account the namespace if specified by the --namespace flag.
func newValidVClusterNameFunc(globalFlags *flags.GlobalFlags) completionFunc {
fn := func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
vclusters, _, err := find.ListVClusters(cmd.Context(), nil, globalFlags.Context, "", globalFlags.Namespace, "", log.Default.ErrorStreamOnly())
vclusters, err := find.ListVClusters(cmd.Context(), globalFlags.Context, "", globalFlags.Namespace, log.Default.ErrorStreamOnly())
if err != nil {
return []string{}, cobra.ShellCompDirectiveError | cobra.ShellCompDirectiveNoFileComp
}
Expand Down
143 changes: 3 additions & 140 deletions cmd/vclusterctl/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import (
"syscall"
"time"

"github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd/use"
proclient "github.com/loft-sh/loftctl/v3/pkg/client"
"github.com/loft-sh/loftctl/v3/pkg/vcluster"
"github.com/loft-sh/vcluster/pkg/procli"
"github.com/loft-sh/vcluster/pkg/util/clihelper"
"github.com/pkg/errors"
"github.com/samber/lo"
Expand Down Expand Up @@ -61,7 +57,6 @@ type ConnectCmd struct {
KubeConfigContextName string
Server string
KubeConfig string
Project string
ServiceAccount string
LocalPort int
ServiceAccountExpiration int
Expand Down Expand Up @@ -121,9 +116,6 @@ vcluster connect test -n test -- kubectl get ns
cobraCmd.Flags().BoolVar(&cmd.Insecure, "insecure", false, "If specified, vcluster will create the kube config with insecure-skip-tls-verify")
cobraCmd.Flags().BoolVar(&cmd.BackgroundProxy, "background-proxy", false, "If specified, vcluster will create the background proxy in docker [its mainly used for vclusters with no nodeport service.]")

// pro
cobraCmd.Flags().StringVar(&cmd.Project, "project", "", "[PRO] The pro project the vcluster is in")

return cobraCmd
}

Expand All @@ -134,27 +126,20 @@ func (cmd *ConnectCmd) Run(ctx context.Context, args []string) error {
vClusterName = args[0]
}

proClient, err := procli.CreateProClient()
if err != nil {
cmd.Log.Debugf("Error creating pro client: %v", err)
}

return cmd.Connect(ctx, proClient, vClusterName, args[1:])
return cmd.Connect(ctx, vClusterName, args[1:])
}

func (cmd *ConnectCmd) Connect(ctx context.Context, proClient procli.Client, vClusterName string, command []string) error {
func (cmd *ConnectCmd) Connect(ctx context.Context, vClusterName string, command []string) error {
// validate flags
err := cmd.validateFlags()
if err != nil {
return err
}

// retrieve the vcluster
vCluster, proVCluster, err := find.GetVCluster(ctx, proClient, cmd.Context, vClusterName, cmd.Namespace, cmd.Project, cmd.Log)
vCluster, err := find.GetVCluster(ctx, cmd.Context, vClusterName, cmd.Namespace, cmd.Log)
if err != nil {
return err
} else if proVCluster != nil {
return cmd.connectPro(ctx, proClient, proVCluster, command)
}

return cmd.connectOss(ctx, vCluster, command)
Expand All @@ -168,58 +153,6 @@ func (cmd *ConnectCmd) validateFlags() error {
return nil
}

func (cmd *ConnectCmd) connectPro(ctx context.Context, proClient proclient.Client, vCluster *procli.VirtualClusterInstanceProject, command []string) error {
err := cmd.validateProFlags()
if err != nil {
return err
}

// create management client
managementClient, err := proClient.Management()
if err != nil {
return err
}

// wait for vCluster to become ready
vCluster.VirtualCluster, err = vcluster.WaitForVirtualClusterInstance(ctx, managementClient, vCluster.VirtualCluster.Namespace, vCluster.VirtualCluster.Name, true, cmd.Log)
if err != nil {
return err
}

// retrieve vCluster kube config
kubeConfig, err := cmd.getVClusterProKubeConfig(ctx, proClient, vCluster)
if err != nil {
return err
}

// check if we should execute command
if len(command) > 0 {
return cmd.executeCommand(*kubeConfig, command)
}

return cmd.writeKubeConfig(kubeConfig, vCluster.VirtualCluster.Name)
}

func (cmd *ConnectCmd) validateProFlags() error {
if cmd.PodName != "" {
return fmt.Errorf("cannot use --pod with a pro vCluster")
}
if cmd.Server != "" {
return fmt.Errorf("cannot use --server with a pro vCluster")
}
if cmd.BackgroundProxy {
return fmt.Errorf("cannot use --background-proxy with a pro vCluster")
}
if cmd.LocalPort != 0 {
return fmt.Errorf("cannot use --local-port with a pro vCluster")
}
if cmd.Address != "" {
return fmt.Errorf("cannot use --address with a pro vCluster")
}

return nil
}

func (cmd *ConnectCmd) connectOss(ctx context.Context, vCluster *find.VCluster, command []string) error {
// prepare clients and find vcluster
err := cmd.prepare(ctx, vCluster)
Expand Down Expand Up @@ -397,76 +330,6 @@ func (cmd *ConnectCmd) prepare(ctx context.Context, vCluster *find.VCluster) err
return nil
}

func (cmd *ConnectCmd) getVClusterProKubeConfig(ctx context.Context, proClient proclient.Client, vCluster *procli.VirtualClusterInstanceProject) (*clientcmdapi.Config, error) {
contextOptions, err := use.CreateVirtualClusterInstanceOptions(ctx, proClient, "", vCluster.Project.Name, vCluster.VirtualCluster, false, false, cmd.Log)
if err != nil {
return nil, fmt.Errorf("prepare vCluster kube config: %w", err)
}

// make sure access key is set
if contextOptions.Token == "" && len(contextOptions.ClientCertificateData) == 0 && len(contextOptions.ClientKeyData) == 0 {
contextOptions.Token = proClient.Config().AccessKey
}

// get current context
rawConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{
CurrentContext: cmd.Context,
}).RawConfig()
if err != nil {
return nil, err
}

// make sure kube context name is set
if cmd.KubeConfigContextName == "" {
// use parent context if this is a vcluster context
kubeContext := rawConfig.CurrentContext
_, _, parentContext := find.VClusterProFromContext(kubeContext)
if parentContext == "" {
_, _, parentContext = find.VClusterFromContext(kubeContext)
}
if parentContext != "" {
kubeContext = parentContext
}
cmd.KubeConfigContextName = find.VClusterProContextName(vCluster.VirtualCluster.Name, vCluster.Project.Name, kubeContext)
}

// set insecure true?
if cmd.Insecure {
contextOptions.InsecureSkipTLSVerify = true
}

// build kube config
kubeConfig, err := clihelper.GetProKubeConfig(contextOptions)
if err != nil {
return nil, err
}

// we want to use a service account token in the kube config
if cmd.ServiceAccount != "" {
// check if its enabled on the pro vcluster
if !vCluster.VirtualCluster.Status.VirtualCluster.ForwardToken {
return nil, fmt.Errorf("forward token is not enabled on the vCluster and hence you cannot authenticate with a service account token")
}

// create service account token
token, err := cmd.createServiceAccountToken(ctx, *kubeConfig)
if err != nil {
return nil, err
}

// set service account token
for k := range kubeConfig.AuthInfos {
kubeConfig.AuthInfos[k] = &clientcmdapi.AuthInfo{
Token: token,
Extensions: make(map[string]runtime.Object),
ImpersonateUserExtra: make(map[string][]string),
}
}
}

return kubeConfig, nil
}

func (cmd *ConnectCmd) getVClusterKubeConfig(ctx context.Context, vclusterName string, command []string) (*clientcmdapi.Config, error) {
var err error
podName := cmd.PodName
Expand Down
Loading

0 comments on commit 20f57c9

Please sign in to comment.