Skip to content

Add the "project" column for vcluster list command when driver flag is set to platform #2777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions pkg/cli/list_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ type ListVCluster struct {
Connected bool
}

// ListProVCluster holds information about a vCluster along with the associated project name
type ListProVCluster struct {
ListVCluster
Project string
}

type ListOptions struct {
Driver string

Expand Down Expand Up @@ -55,15 +61,15 @@ func ListHelm(ctx context.Context, options *ListOptions, globalFlags *flags.Glob
return err
}

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

return nil
}

func printVClusters(ctx context.Context, options *ListOptions, output []ListVCluster, globalFlags *flags.GlobalFlags, showPlatform bool, logger log.Logger) error {
func printVClusters(ctx context.Context, options *ListOptions, output []ListVCluster, globalFlags *flags.GlobalFlags, logger log.Logger) error {
if options.Output == "json" {
bytes, err := json.MarshalIndent(output, "", " ")
if err != nil {
Expand All @@ -76,27 +82,15 @@ func printVClusters(ctx context.Context, options *ListOptions, output []ListVClu
values := toValues(output)
table.PrintTable(logger, header, values)

// show use driver command
if showPlatform {
platformClient, err := platform.InitClientFromConfig(ctx, globalFlags.LoadedConfig(logger))
if err == nil {
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

proVClusters, _ := platform.ListVClusters(ctx, platformClient, "", "", false)
if len(proVClusters) > 0 {
logger.Infof("You also have %d virtual clusters in your platform driver context.", len(proVClusters))
logger.Info("If you want to see them, run: 'vcluster list --driver platform' or 'vcluster use driver platform' to change the default")
}
}
} else {
platformClient, err := platform.InitClientFromConfig(ctx, globalFlags.LoadedConfig(logger))
if err == nil {
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

vClusters, _ := find.ListVClusters(ctx, globalFlags.Context, "", "", log.Discard)
if len(vClusters) > 0 {
logger.Infof("You also have %d virtual clusters in your current kube-context.", len(vClusters))
logger.Info("If you want to see them, run: 'vcluster list --driver helm' or 'vcluster use driver helm' to change the default")
proVClusters, _ := platform.ListVClusters(ctx, platformClient, "", "", false)
if len(proVClusters) > 0 {
logger.Infof("You also have %d virtual clusters in your platform driver context.", len(proVClusters))
logger.Info("If you want to see them, run: 'vcluster list --driver platform' or 'vcluster use driver platform' to change the default")
}
}

Expand Down
82 changes: 71 additions & 11 deletions pkg/cli/list_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package cli

import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/sirupsen/logrus"

"github.com/loft-sh/log"
"github.com/loft-sh/log/table"
"github.com/loft-sh/vcluster/pkg/cli/find"
"github.com/loft-sh/vcluster/pkg/cli/flags"
"github.com/loft-sh/vcluster/pkg/platform"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -32,16 +38,16 @@ func ListPlatform(ctx context.Context, options *ListOptions, globalFlags *flags.
return err
}

err = printVClusters(ctx, options, proToVClusters(proVClusters, currentContext), globalFlags, false, logger)
err = printProVClusters(ctx, options, proToVClusters(proVClusters, currentContext), globalFlags, logger)
if err != nil {
return err
}

return nil
}

func proToVClusters(vClusters []*platform.VirtualClusterInstanceProject, currentContext string) []ListVCluster {
var output []ListVCluster
func proToVClusters(vClusters []*platform.VirtualClusterInstanceProject, currentContext string) []ListProVCluster {
var output []ListProVCluster
for _, vCluster := range vClusters {
status := string(vCluster.VirtualCluster.Status.Phase)
if vCluster.VirtualCluster.DeletionTimestamp != nil {
Expand All @@ -63,16 +69,70 @@ func proToVClusters(vClusters []*platform.VirtualClusterInstanceProject, current
}

connected := strings.HasPrefix(currentContext, "vcluster-platform_"+vCluster.VirtualCluster.Name+"_"+vCluster.Project.Name)
vClusterOutput := ListVCluster{
Name: name,
Namespace: vCluster.VirtualCluster.Spec.ClusterRef.Namespace,
Connected: connected,
Created: vCluster.VirtualCluster.CreationTimestamp.Time,
AgeSeconds: int(time.Since(vCluster.VirtualCluster.CreationTimestamp.Time).Round(time.Second).Seconds()),
Status: status,
Version: version,
vClusterOutput := ListProVCluster{
ListVCluster{
Name: name,
Namespace: vCluster.VirtualCluster.Spec.ClusterRef.Namespace,
Connected: connected,
Created: vCluster.VirtualCluster.CreationTimestamp.Time,
AgeSeconds: int(time.Since(vCluster.VirtualCluster.CreationTimestamp.Time).Round(time.Second).Seconds()),
Status: status,
Version: version,
},
vCluster.Project.Name,
}
output = append(output, vClusterOutput)
}
return output
}

func printProVClusters(ctx context.Context, options *ListOptions, output []ListProVCluster, globalFlags *flags.GlobalFlags, logger log.Logger) error {
if options.Output == "json" {
bytes, err := json.MarshalIndent(output, "", " ")
if err != nil {
return fmt.Errorf("json marshal vClusters: %w", err)
}

logger.WriteString(logrus.InfoLevel, string(bytes)+"\n")
} else {
header := []string{"NAME", "NAMESPACE", "PROJECT", "STATUS", "VERSION", "CONNECTED", "AGE"}
values := toTableValues(output)
table.PrintTable(logger, header, values)

ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

vClusters, _ := find.ListVClusters(ctx, globalFlags.Context, "", "", log.Discard)
if len(vClusters) > 0 {
logger.Infof("You also have %d virtual clusters in your current kube-context.", len(vClusters))
logger.Info("If you want to see them, run: 'vcluster list --driver helm' or 'vcluster use driver helm' to change the default")
}

// show disconnect command
if strings.HasPrefix(globalFlags.Context, "vcluster_") || strings.HasPrefix(globalFlags.Context, "vcluster-platform_") {
logger.Infof("Run `vcluster disconnect` to switch back to the parent context")
}
}
return nil
}

func toTableValues(vClusters []ListProVCluster) [][]string {
var values [][]string
for _, vCluster := range vClusters {
isConnected := ""
if vCluster.Connected {
isConnected = "True"
}

values = append(values, []string{
vCluster.Name,
vCluster.Namespace,
vCluster.Project,
vCluster.Status,
vCluster.Version,
isConnected,
time.Since(vCluster.Created).Round(1 * time.Second).String(),
})
}
return values
}
Loading