diff --git a/cmd/vclusterctl/cmd/connect.go b/cmd/vclusterctl/cmd/connect.go index 7252c5d88e..08f2ea049b 100644 --- a/cmd/vclusterctl/cmd/connect.go +++ b/cmd/vclusterctl/cmd/connect.go @@ -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? diff --git a/cmd/vclusterctl/cmd/create.go b/cmd/vclusterctl/cmd/create.go index 459f181280..3452ed5970 100644 --- a/cmd/vclusterctl/cmd/create.go +++ b/cmd/vclusterctl/cmd/create.go @@ -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 diff --git a/cmd/vclusterctl/cmd/find/find.go b/cmd/vclusterctl/cmd/find/find.go index d953cef36d..dab378f9e7 100644 --- a/cmd/vclusterctl/cmd/find/find.go +++ b/cmd/vclusterctl/cmd/find/find.go @@ -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) @@ -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 != "" { @@ -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 { diff --git a/cmd/vclusterctl/cmd/list.go b/cmd/vclusterctl/cmd/list.go index ae1c09de8c..f425ecfa3d 100644 --- a/cmd/vclusterctl/cmd/list.go +++ b/cmd/vclusterctl/cmd/list.go @@ -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" @@ -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") } } @@ -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,