Skip to content

Commit 4cdd1d0

Browse files
committed
add a new response object for proVCluster list
sort imports
1 parent 004b104 commit 4cdd1d0

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

pkg/cli/list_helm.go

Lines changed: 6 additions & 2 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`
87
"strings"
98
"time"
109

1110
"github.com/loft-sh/log"
1211
"github.com/loft-sh/log/table"
1312
"github.com/loft-sh/vcluster/pkg/cli/find"
1413
"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,7 +28,11 @@ type ListVCluster struct {
2828
Connected bool
2929
}
3030

31-
type vClusterProjectMap map[string]string
31+
// ListProVCluster holds information about a vCluster along with the associated project name
32+
type ListProVCluster struct {
33+
ListVCluster
34+
Project string
35+
}
3236

3337
type ListOptions struct {
3438
Driver string

pkg/cli/list_platform.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,16 @@ func ListPlatform(ctx context.Context, options *ListOptions, globalFlags *flags.
3838
return err
3939
}
4040

41-
vClusters, vClusterProjectMapping := proToVClusters(proVClusters, currentContext)
42-
err = printProVClusters(ctx, options, vClusters, vClusterProjectMapping, globalFlags, logger)
41+
err = printProVClusters(ctx, options, proToVClusters(proVClusters, currentContext), globalFlags, logger)
4342
if err != nil {
4443
return err
4544
}
4645

4746
return nil
4847
}
4948

50-
func proToVClusters(vClusters []*platform.VirtualClusterInstanceProject, currentContext string) ([]ListVCluster, vClusterProjectMap) {
51-
var output []ListVCluster
52-
vClusterProjectMapping := make(vClusterProjectMap)
49+
func proToVClusters(vClusters []*platform.VirtualClusterInstanceProject, currentContext string) []ListProVCluster {
50+
var output []ListProVCluster
5351
for _, vCluster := range vClusters {
5452
status := string(vCluster.VirtualCluster.Status.Phase)
5553
if vCluster.VirtualCluster.DeletionTimestamp != nil {
@@ -71,24 +69,24 @@ func proToVClusters(vClusters []*platform.VirtualClusterInstanceProject, current
7169
}
7270

7371
connected := strings.HasPrefix(currentContext, "vcluster-platform_"+vCluster.VirtualCluster.Name+"_"+vCluster.Project.Name)
74-
vClusterOutput := ListVCluster{
75-
Name: name,
76-
Namespace: vCluster.VirtualCluster.Spec.ClusterRef.Namespace,
77-
Connected: connected,
78-
Created: vCluster.VirtualCluster.CreationTimestamp.Time,
79-
AgeSeconds: int(time.Since(vCluster.VirtualCluster.CreationTimestamp.Time).Round(time.Second).Seconds()),
80-
Status: status,
81-
Version: version,
72+
vClusterOutput := ListProVCluster{
73+
ListVCluster{
74+
Name: name,
75+
Namespace: vCluster.VirtualCluster.Spec.ClusterRef.Namespace,
76+
Connected: connected,
77+
Created: vCluster.VirtualCluster.CreationTimestamp.Time,
78+
AgeSeconds: int(time.Since(vCluster.VirtualCluster.CreationTimestamp.Time).Round(time.Second).Seconds()),
79+
Status: status,
80+
Version: version,
81+
},
82+
vCluster.Project.Name,
8283
}
83-
uniqueVClusterIdentifier := vClusterOutput.Name + "_" + vClusterOutput.Namespace
84-
vClusterProjectMapping[uniqueVClusterIdentifier] = vCluster.Project.Name
8584
output = append(output, vClusterOutput)
8685
}
87-
return output, vClusterProjectMapping
86+
return output
8887
}
8988

90-
func printProVClusters(ctx context.Context, options *ListOptions, output []ListVCluster,
91-
vClusterProjectMapping vClusterProjectMap, globalFlags *flags.GlobalFlags, logger log.Logger) error {
89+
func printProVClusters(ctx context.Context, options *ListOptions, output []ListProVCluster, globalFlags *flags.GlobalFlags, logger log.Logger) error {
9290
if options.Output == "json" {
9391
bytes, err := json.MarshalIndent(output, "", " ")
9492
if err != nil {
@@ -98,7 +96,7 @@ func printProVClusters(ctx context.Context, options *ListOptions, output []ListV
9896
logger.WriteString(logrus.InfoLevel, string(bytes)+"\n")
9997
} else {
10098
header := []string{"NAME", "NAMESPACE", "PROJECT", "STATUS", "VERSION", "CONNECTED", "AGE"}
101-
values := toTableValues(output, vClusterProjectMapping)
99+
values := toTableValues(output)
102100
table.PrintTable(logger, header, values)
103101

104102
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
@@ -118,19 +116,18 @@ func printProVClusters(ctx context.Context, options *ListOptions, output []ListV
118116
return nil
119117
}
120118

121-
func toTableValues(vClusters []ListVCluster, vClusterProjectMapping vClusterProjectMap) [][]string {
119+
func toTableValues(vClusters []ListProVCluster) [][]string {
122120
var values [][]string
123121
for _, vCluster := range vClusters {
124122
isConnected := ""
125123
if vCluster.Connected {
126124
isConnected = "True"
127125
}
128126

129-
uniqueVClusterIdentifier := vCluster.Name + "_" + vCluster.Namespace
130127
values = append(values, []string{
131128
vCluster.Name,
132129
vCluster.Namespace,
133-
vClusterProjectMapping[uniqueVClusterIdentifier],
130+
vCluster.Project,
134131
vCluster.Status,
135132
vCluster.Version,
136133
isConnected,

0 commit comments

Comments
 (0)