@@ -2,10 +2,16 @@ package cli
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
6
+ "fmt"
5
7
"strings"
6
8
"time"
7
9
10
+ "github.com/sirupsen/logrus"
11
+
8
12
"github.com/loft-sh/log"
13
+ "github.com/loft-sh/log/table"
14
+ "github.com/loft-sh/vcluster/pkg/cli/find"
9
15
"github.com/loft-sh/vcluster/pkg/cli/flags"
10
16
"github.com/loft-sh/vcluster/pkg/platform"
11
17
"k8s.io/client-go/tools/clientcmd"
@@ -32,16 +38,18 @@ func ListPlatform(ctx context.Context, options *ListOptions, globalFlags *flags.
32
38
return err
33
39
}
34
40
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 )
36
43
if err != nil {
37
44
return err
38
45
}
39
46
40
47
return nil
41
48
}
42
49
43
- func proToVClusters (vClusters []* platform.VirtualClusterInstanceProject , currentContext string ) []ListVCluster {
50
+ func proToVClusters (vClusters []* platform.VirtualClusterInstanceProject , currentContext string ) ( []ListVCluster , vClusterProjectMap ) {
44
51
var output []ListVCluster
52
+ vClusterProjectMapping := make (vClusterProjectMap )
45
53
for _ , vCluster := range vClusters {
46
54
status := string (vCluster .VirtualCluster .Status .Phase )
47
55
if vCluster .VirtualCluster .DeletionTimestamp != nil {
@@ -72,7 +80,62 @@ func proToVClusters(vClusters []*platform.VirtualClusterInstanceProject, current
72
80
Status : status ,
73
81
Version : version ,
74
82
}
83
+ uniqueVClusterIdentifier := vClusterOutput .Name + "_" + vClusterOutput .Namespace
84
+ vClusterProjectMapping [uniqueVClusterIdentifier ] = vCluster .Project .Name
75
85
output = append (output , vClusterOutput )
76
86
}
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
78
141
}
0 commit comments