Skip to content

Commit 004b104

Browse files
committed
separate out print functions acc. to driver to add the project column
1 parent 23b6ab5 commit 004b104

File tree

2 files changed

+75
-22
lines changed

2 files changed

+75
-22
lines changed

pkg/cli/list_helm.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
`github.com/loft-sh/vcluster/pkg/platform`
78
"strings"
89
"time"
910

1011
"github.com/loft-sh/log"
1112
"github.com/loft-sh/log/table"
1213
"github.com/loft-sh/vcluster/pkg/cli/find"
1314
"github.com/loft-sh/vcluster/pkg/cli/flags"
14-
"github.com/loft-sh/vcluster/pkg/platform"
1515
"github.com/sirupsen/logrus"
1616
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1717
"k8s.io/client-go/tools/clientcmd"
@@ -28,6 +28,8 @@ type ListVCluster struct {
2828
Connected bool
2929
}
3030

31+
type vClusterProjectMap map[string]string
32+
3133
type ListOptions struct {
3234
Driver string
3335

@@ -76,27 +78,15 @@ func printVClusters(ctx context.Context, options *ListOptions, output []ListVClu
7678
values := toValues(output)
7779
table.PrintTable(logger, header, values)
7880

79-
// show use driver command
80-
if showPlatform {
81-
platformClient, err := platform.InitClientFromConfig(ctx, globalFlags.LoadedConfig(logger))
82-
if err == nil {
83-
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
84-
defer cancel()
85-
86-
proVClusters, _ := platform.ListVClusters(ctx, platformClient, "", "", false)
87-
if len(proVClusters) > 0 {
88-
logger.Infof("You also have %d virtual clusters in your platform driver context.", len(proVClusters))
89-
logger.Info("If you want to see them, run: 'vcluster list --driver platform' or 'vcluster use driver platform' to change the default")
90-
}
91-
}
92-
} else {
81+
platformClient, err := platform.InitClientFromConfig(ctx, globalFlags.LoadedConfig(logger))
82+
if err == nil {
9383
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
9484
defer cancel()
9585

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

pkg/cli/list_platform.go

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ package cli
22

33
import (
44
"context"
5+
"encoding/json"
6+
"fmt"
57
"strings"
68
"time"
79

10+
"github.com/sirupsen/logrus"
11+
812
"github.com/loft-sh/log"
13+
"github.com/loft-sh/log/table"
14+
"github.com/loft-sh/vcluster/pkg/cli/find"
915
"github.com/loft-sh/vcluster/pkg/cli/flags"
1016
"github.com/loft-sh/vcluster/pkg/platform"
1117
"k8s.io/client-go/tools/clientcmd"
@@ -32,16 +38,18 @@ func ListPlatform(ctx context.Context, options *ListOptions, globalFlags *flags.
3238
return err
3339
}
3440

35-
err = printVClusters(ctx, options, proToVClusters(proVClusters, currentContext), globalFlags, false, logger)
41+
vClusters, vClusterProjectMapping := proToVClusters(proVClusters, currentContext)
42+
err = printProVClusters(ctx, options, vClusters, vClusterProjectMapping, globalFlags, logger)
3643
if err != nil {
3744
return err
3845
}
3946

4047
return nil
4148
}
4249

43-
func proToVClusters(vClusters []*platform.VirtualClusterInstanceProject, currentContext string) []ListVCluster {
50+
func proToVClusters(vClusters []*platform.VirtualClusterInstanceProject, currentContext string) ([]ListVCluster, vClusterProjectMap) {
4451
var output []ListVCluster
52+
vClusterProjectMapping := make(vClusterProjectMap)
4553
for _, vCluster := range vClusters {
4654
status := string(vCluster.VirtualCluster.Status.Phase)
4755
if vCluster.VirtualCluster.DeletionTimestamp != nil {
@@ -72,7 +80,62 @@ func proToVClusters(vClusters []*platform.VirtualClusterInstanceProject, current
7280
Status: status,
7381
Version: version,
7482
}
83+
uniqueVClusterIdentifier := vClusterOutput.Name + "_" + vClusterOutput.Namespace
84+
vClusterProjectMapping[uniqueVClusterIdentifier] = vCluster.Project.Name
7585
output = append(output, vClusterOutput)
7686
}
77-
return output
87+
return output, vClusterProjectMapping
88+
}
89+
90+
func printProVClusters(ctx context.Context, options *ListOptions, output []ListVCluster,
91+
vClusterProjectMapping vClusterProjectMap, globalFlags *flags.GlobalFlags, logger log.Logger) error {
92+
if options.Output == "json" {
93+
bytes, err := json.MarshalIndent(output, "", " ")
94+
if err != nil {
95+
return fmt.Errorf("json marshal vClusters: %w", err)
96+
}
97+
98+
logger.WriteString(logrus.InfoLevel, string(bytes)+"\n")
99+
} else {
100+
header := []string{"NAME", "NAMESPACE", "PROJECT", "STATUS", "VERSION", "CONNECTED", "AGE"}
101+
values := toTableValues(output, vClusterProjectMapping)
102+
table.PrintTable(logger, header, values)
103+
104+
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
105+
defer cancel()
106+
107+
vClusters, _ := find.ListVClusters(ctx, globalFlags.Context, "", "", log.Discard)
108+
if len(vClusters) > 0 {
109+
logger.Infof("You also have %d virtual clusters in your current kube-context.", len(vClusters))
110+
logger.Info("If you want to see them, run: 'vcluster list --driver helm' or 'vcluster use driver helm' to change the default")
111+
}
112+
113+
// show disconnect command
114+
if strings.HasPrefix(globalFlags.Context, "vcluster_") || strings.HasPrefix(globalFlags.Context, "vcluster-platform_") {
115+
logger.Infof("Run `vcluster disconnect` to switch back to the parent context")
116+
}
117+
}
118+
return nil
119+
}
120+
121+
func toTableValues(vClusters []ListVCluster, vClusterProjectMapping vClusterProjectMap) [][]string {
122+
var values [][]string
123+
for _, vCluster := range vClusters {
124+
isConnected := ""
125+
if vCluster.Connected {
126+
isConnected = "True"
127+
}
128+
129+
uniqueVClusterIdentifier := vCluster.Name + "_" + vCluster.Namespace
130+
values = append(values, []string{
131+
vCluster.Name,
132+
vCluster.Namespace,
133+
vClusterProjectMapping[uniqueVClusterIdentifier],
134+
vCluster.Status,
135+
vCluster.Version,
136+
isConnected,
137+
time.Since(vCluster.Created).Round(1 * time.Second).String(),
138+
})
139+
}
140+
return values
78141
}

0 commit comments

Comments
 (0)