Skip to content

Commit

Permalink
fix: nested vcluster commands
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Sep 28, 2023
1 parent 65f7c43 commit 762c6dc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
11 changes: 10 additions & 1 deletion cmd/vclusterctl/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,16 @@ func (cmd *ConnectCmd) getVClusterProKubeConfig(ctx context.Context, proClient p

// make sure kube context name is set
if cmd.KubeConfigContextName == "" {
cmd.KubeConfigContextName = find.VClusterProContextName(vCluster.VirtualCluster.Name, vCluster.Project.Name, rawConfig.CurrentContext)
// 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?
Expand Down
3 changes: 3 additions & 0 deletions cmd/vclusterctl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ func (cmd *CreateCmd) prepare(ctx context.Context, vClusterName string) error {

// check if vcluster in vcluster
_, _, previousContext := find.VClusterFromContext(rawConfig.CurrentContext)
if previousContext == "" {
_, _, previousContext = find.VClusterProFromContext(rawConfig.CurrentContext)
}
if previousContext != "" {
if terminal.IsTerminalIn {
switchBackOption := "No, switch back to context " + previousContext
Expand Down
22 changes: 11 additions & 11 deletions cmd/vclusterctl/cmd/find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func ListVClusters(ctx context.Context, proClient proclient.Client, context, nam
}

var proVClusters []pro.VirtualClusterInstanceProject
if namespace == "" && proClient != nil {
if proClient != nil {
proVClusters, err = pro.ListVClusters(ctx, proClient, name, project)
if err != nil {
log.Warnf("Error retrieving pro vclusters: %v", err)
Expand All @@ -176,20 +176,20 @@ func ListVClusters(ctx context.Context, proClient proclient.Client, context, nam
func ListOSSVClusters(ctx context.Context, context, name, namespace string) ([]VCluster, error) {
var err error

_, _, proParentContext := VClusterProFromContext(context)
if proParentContext != "" {
return nil, nil
}

vClusterName, _, vClusterContext := VClusterFromContext(context)
timeout := time.Minute
if vClusterName != "" {
timeout = time.Second * 5
}

vclusters := []VCluster{}
isPro := strings.HasPrefix(context, "vcluster-pro_")
if !isPro {
// In case of error in vcluster listing in vcluster context, the below check will skip the error and try searching for parent context vclusters.
vclusters, err = findInContext(ctx, context, name, namespace, timeout, false)
if err != nil && vClusterName == "" {
return nil, errors.Wrap(err, "find vcluster")
}
vclusters, err := findInContext(ctx, context, name, namespace, timeout, false)
if err != nil && vClusterName == "" {
return nil, errors.Wrap(err, "find vcluster")
}

if vClusterName != "" {
Expand All @@ -204,8 +204,8 @@ func ListOSSVClusters(ctx context.Context, context, name, namespace string) ([]V
return vclusters, nil
}

func VClusterProContextName(vClusterName string, vClusterNamespace string, currentContext string) string {
return "vcluster-pro_" + vClusterName + "_" + vClusterNamespace + "_" + currentContext
func VClusterProContextName(vClusterName string, projectName string, currentContext string) string {
return "vcluster-pro_" + vClusterName + "_" + projectName + "_" + currentContext
}

func VClusterContextName(vClusterName string, vClusterNamespace string, currentContext string) string {
Expand Down
9 changes: 3 additions & 6 deletions cmd/vclusterctl/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"
"time"

"github.com/loft-sh/loftctl/v3/pkg/kubeconfig"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/find"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -115,7 +114,7 @@ func (cmd *ListCmd) Run(cobraCmd *cobra.Command, _ []string) error {
header := []string{"NAME", "NAMESPACE", "STATUS", "VERSION", "CONNECTED", "CREATED", "AGE", "PRO"}
values := toValues(output)
table.PrintTable(cmd.log, header, values)
if strings.HasPrefix(cmd.Context, "vcluster_") {
if strings.HasPrefix(cmd.Context, "vcluster_") || strings.HasPrefix(cmd.Context, "vcluster-pro_") {
cmd.log.Infof("Run `vcluster disconnect` to switch back to the parent context")
}
}
Expand Down Expand Up @@ -156,13 +155,11 @@ func proToVClusters(vClusters []pro.VirtualClusterInstanceProject, currentContex
status = "Pending"
}

context := kubeconfig.VirtualClusterInstanceContextName(vCluster.Project.Name, vCluster.VirtualCluster.Name)

connected := strings.HasPrefix(currentContext, "vcluster-pro_"+vCluster.VirtualCluster.Name+"_"+vCluster.Project.Name)
vClusterOutput := VCluster{
Name: vCluster.VirtualCluster.Spec.ClusterRef.VirtualCluster,
Namespace: vCluster.VirtualCluster.Spec.ClusterRef.Namespace,
Context: context,
Connected: currentContext == context,
Connected: connected,
Created: vCluster.VirtualCluster.CreationTimestamp.Time,
AgeSeconds: int(time.Since(vCluster.VirtualCluster.CreationTimestamp.Time).Round(time.Second).Seconds()),
Status: status,
Expand Down

0 comments on commit 762c6dc

Please sign in to comment.