@@ -18,12 +18,12 @@ import (
18
18
"context"
19
19
"fmt"
20
20
"os"
21
+ "sort"
21
22
"strings"
22
23
23
24
"github.com/codefresh-io/cli-v2/pkg/log"
24
25
"github.com/codefresh-io/cli-v2/pkg/store"
25
26
"github.com/codefresh-io/cli-v2/pkg/util"
26
- cdutil "github.com/codefresh-io/cli-v2/pkg/util/cd"
27
27
kustutil "github.com/codefresh-io/cli-v2/pkg/util/kust"
28
28
29
29
"github.com/Masterminds/semver/v3"
@@ -43,8 +43,14 @@ type (
43
43
}
44
44
45
45
ClusterRemoveOptions struct {
46
+ runtimeName string
46
47
server string
48
+ }
49
+
50
+ ClusterCreateArgoRolloutsOptions struct {
47
51
runtimeName string
52
+ server string
53
+ namespace string
48
54
}
49
55
)
50
56
@@ -63,21 +69,19 @@ func NewClusterCommand() *cobra.Command {
63
69
},
64
70
}
65
71
66
- cmd .AddCommand (NewClusterAddCommand ())
67
- cmd .AddCommand (NewClusterRemoveCommand ())
68
- cmd .AddCommand (NewClusterListCommand ())
72
+ cmd .AddCommand (newClusterAddCommand ())
73
+ cmd .AddCommand (newClusterRemoveCommand ())
74
+ cmd .AddCommand (newClusterListCommand ())
75
+ cmd .AddCommand (newClusterCreateArgoRolloutsCommand ())
69
76
70
77
return cmd
71
78
}
72
79
73
- func NewClusterAddCommand () * cobra.Command {
74
- var (
75
- opts ClusterAddOptions
76
- err error
77
- )
80
+ func newClusterAddCommand () * cobra.Command {
81
+ var opts ClusterAddOptions
78
82
79
83
cmd := & cobra.Command {
80
- Use : "add RUNTIME_NAME" ,
84
+ Use : "add [ RUNTIME_NAME] " ,
81
85
Short : "Add a cluster to a given runtime" ,
82
86
Args : cobra .MaximumNArgs (1 ),
83
87
Example : util .Doc (`<BIN> cluster add my-runtime --context my-context` ),
@@ -101,7 +105,6 @@ func NewClusterAddCommand() *cobra.Command {
101
105
102
106
cmd .Flags ().BoolVar (& opts .dryRun , "dry-run" , false , "" )
103
107
opts .kubeFactory = kube .AddFlags (cmd .Flags ())
104
- die (err )
105
108
106
109
return cmd
107
110
}
@@ -193,16 +196,14 @@ func createAddClusterKustomization(ingressUrl, contextName, server, csdpToken, v
193
196
return k
194
197
}
195
198
196
- func NewClusterRemoveCommand () * cobra.Command {
197
- var (
198
- opts ClusterRemoveOptions
199
- )
199
+ func newClusterRemoveCommand () * cobra.Command {
200
+ var opts ClusterRemoveOptions
200
201
201
202
cmd := & cobra.Command {
202
- Use : "remove RUNTIME_NAME" ,
203
+ Use : "remove [ RUNTIME_NAME] " ,
203
204
Short : "Removes a cluster from a given runtime" ,
204
205
Args : cobra .MaximumNArgs (1 ),
205
- Example : util .Doc (`<BIN> cluster remove my-runtime --server-url my-server-url ` ),
206
+ Example : util .Doc (`<BIN> cluster remove my-runtime --server-url https://<some-hash>.gr7.us-east-1.eks.amazonaws.com ` ),
206
207
PreRunE : func (cmd * cobra.Command , args []string ) error {
207
208
var err error
208
209
@@ -232,7 +233,7 @@ func runClusterRemove(ctx context.Context, opts *ClusterRemoveOptions) error {
232
233
return err
233
234
}
234
235
235
- err = appProxy .AppProxyClusters ().RemoveCluster (ctx , opts .server , opts .runtimeName )
236
+ err = appProxy .AppProxyClusters ().Delete (ctx , opts .server , opts .runtimeName )
236
237
if err != nil {
237
238
return fmt .Errorf ("failed to remove cluster: %w" , err )
238
239
}
@@ -242,70 +243,94 @@ func runClusterRemove(ctx context.Context, opts *ClusterRemoveOptions) error {
242
243
return nil
243
244
}
244
245
245
- func NewClusterListCommand () * cobra.Command {
246
- var runtimeName string
247
- var kubeconfig string
246
+ func newClusterListCommand () * cobra.Command {
247
+ runtimeName := ""
248
248
249
249
cmd := & cobra.Command {
250
- Use : "list RUNTIME_NAME" ,
250
+ Use : "list [ RUNTIME_NAME] " ,
251
251
Short : "List all the clusters of a given runtime" ,
252
252
Args : cobra .MaximumNArgs (1 ),
253
253
Example : util .Doc (`<BIN> cluster list my-runtime` ),
254
- PreRunE : func (cmd * cobra.Command , args []string ) error {
255
- var err error
256
-
257
- runtimeName , err = ensureRuntimeName (cmd .Context (), args )
258
- return err
254
+ PreRun : func (_ * cobra.Command , args []string ) {
255
+ if len (args ) == 1 {
256
+ runtimeName = args [0 ]
257
+ }
259
258
},
260
- RunE : func (cmd * cobra.Command , _ []string ) error {
261
- return runClusterList (cmd .Context (), runtimeName , kubeconfig )
259
+ RunE : func (cmd * cobra.Command , args []string ) error {
260
+ return runClusterList (cmd .Context (), runtimeName )
262
261
},
263
262
}
264
263
265
- cmd .Flags ().StringVar (& kubeconfig , "kubeconfig" , "" , "Path to the kubeconfig file" )
266
-
267
264
return cmd
268
265
}
269
266
270
- func runClusterList (ctx context.Context , runtimeName , kubeconfig string ) error {
271
- runtime , err := cfConfig .NewClient ().V2 ().Runtime ().Get (ctx , runtimeName )
272
- if err != nil {
273
- return err
274
- }
275
-
276
- kubeContext , err := util .KubeContextNameByServer (* runtime .Cluster , kubeconfig )
277
- if err != nil {
278
- return fmt .Errorf ("failed getting context for \" %s\" : %w" , * runtime .Cluster , err )
279
- }
280
-
281
- clusters , err := cdutil .GetClusterList (ctx , kubeContext , * runtime .Metadata .Namespace , false )
267
+ func runClusterList (ctx context.Context , runtimeName string ) error {
268
+ clusters , err := cfConfig .NewClient ().V2 ().Cluster ().List (ctx , runtimeName )
282
269
if err != nil {
283
270
return err
284
271
}
285
272
286
- if len (clusters . Items ) == 0 {
273
+ if len (clusters ) == 0 {
287
274
log .G (ctx ).Info ("No clusters were found" )
288
275
return nil
289
276
}
290
277
278
+ sort .SliceStable (clusters , func (i , j int ) bool {
279
+ c1 := clusters [i ]
280
+ if c1 .Metadata .Name == "in-cluster" {
281
+ return true
282
+ }
283
+
284
+ c2 := clusters [j ]
285
+ if c2 .Metadata .Name == "in-cluster" {
286
+ return false
287
+ }
288
+
289
+ return c1 .Metadata .Name < c2 .Metadata .Name
290
+ })
291
+
291
292
tb := ansiterm .NewTabWriter (os .Stdout , 0 , 0 , 4 , ' ' , 0 )
293
+ if runtimeName == "" {
294
+ _ , err = fmt .Fprint (tb , "RUNTIME\t " )
295
+ if err != nil {
296
+ return err
297
+ }
298
+ }
299
+
292
300
_ , err = fmt .Fprintln (tb , "SERVER\t NAME\t VERSION\t STATUS\t MESSAGE" )
293
301
if err != nil {
294
302
return err
295
303
}
296
304
297
- for _ , c := range clusters . Items {
305
+ for _ , c := range clusters {
298
306
server := c .Server
299
307
if len (c .Namespaces ) > 0 {
300
308
server = fmt .Sprintf ("%s (%d namespaces)" , c .Server , len (c .Namespaces ))
301
309
}
302
310
311
+ version := ""
312
+ if c .Info .ServerVersion != nil {
313
+ version = * c .Info .ServerVersion
314
+ }
315
+
316
+ message := ""
317
+ if c .Info .ConnectionState .Message != nil {
318
+ message = * c .Info .ConnectionState .Message
319
+ }
320
+
321
+ if runtimeName == "" {
322
+ _ , err = fmt .Fprintf (tb , "%s\t " , c .Metadata .Runtime )
323
+ if err != nil {
324
+ return err
325
+ }
326
+ }
327
+
303
328
_ , err = fmt .Fprintf (tb , "%s\t %s\t %s\t %s\t %s\n " ,
304
329
server ,
305
- c .Name ,
306
- c . ServerVersion ,
307
- c .ConnectionState .Status ,
308
- c . ConnectionState . Message ,
330
+ c .Metadata . Name ,
331
+ version ,
332
+ c .Info . ConnectionState .Status ,
333
+ message ,
309
334
)
310
335
if err != nil {
311
336
return err
@@ -314,3 +339,46 @@ func runClusterList(ctx context.Context, runtimeName, kubeconfig string) error {
314
339
315
340
return tb .Flush ()
316
341
}
342
+
343
+ func newClusterCreateArgoRolloutsCommand () * cobra.Command {
344
+ var opts ClusterCreateArgoRolloutsOptions
345
+
346
+ cmd := & cobra.Command {
347
+ Use : "create-argo-rollouts [RUNTIME_NAME]" ,
348
+ Short : "creates argo-rollouts component on the target cluster" ,
349
+ Args : cobra .MaximumNArgs (1 ),
350
+ Example : util .Doc (`<BIN> cluster create-argo-rollouts my-runtime --server-url https://<some-hash>.gr7.us-east-1.eks.amazonaws.com --namespace managed-ns` ),
351
+ PreRunE : func (cmd * cobra.Command , args []string ) error {
352
+ var err error
353
+
354
+ opts .runtimeName , err = ensureRuntimeName (cmd .Context (), args )
355
+ return err
356
+ },
357
+ RunE : func (cmd * cobra.Command , _ []string ) error {
358
+ return runCreateArgoRollouts (cmd .Context (), & opts )
359
+ },
360
+ }
361
+
362
+ cmd .Flags ().StringVar (& opts .server , "server-url" , "" , "The cluster's server url" )
363
+ cmd .Flags ().StringVar (& opts .namespace , "namespace" , "" , "Path to the kubeconfig file" )
364
+ util .Die (cobra .MarkFlagRequired (cmd .Flags (), "server-url" ))
365
+ util .Die (cobra .MarkFlagRequired (cmd .Flags (), "namespace" ))
366
+
367
+ return cmd
368
+ }
369
+
370
+ func runCreateArgoRollouts (ctx context.Context , opts * ClusterCreateArgoRolloutsOptions ) error {
371
+ appProxy , err := cfConfig .NewClient ().AppProxy (ctx , opts .runtimeName , store .Get ().InsecureIngressHost )
372
+ if err != nil {
373
+ return err
374
+ }
375
+
376
+ err = appProxy .AppProxyClusters ().CreateArgoRollouts (ctx , opts .server , opts .namespace )
377
+ if err != nil {
378
+ return fmt .Errorf ("failed to create argo-rollouts on \" %s'\" : %w" , opts .server , err )
379
+ }
380
+
381
+ log .G (ctx ).Infof ("created argo-rollouts component on \" %s\" " , opts .server )
382
+
383
+ return nil
384
+ }
0 commit comments