-
Notifications
You must be signed in to change notification settings - Fork 18
/
sks_nodepool_list.go
97 lines (81 loc) · 2.31 KB
/
sks_nodepool_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package cmd
import (
"fmt"
"os"
"strings"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type sksNodepoolListItemOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Cluster string `json:"cluster"`
Size int64 `json:"size"`
State string `json:"state"`
Zone string `json:"zone"`
}
type sksNodepoolListOutput []sksNodepoolListItemOutput
func (o *sksNodepoolListOutput) toJSON() { outputJSON(o) }
func (o *sksNodepoolListOutput) toText() { outputText(o) }
func (o *sksNodepoolListOutput) toTable() { outputTable(o) }
var sksNodepoolListCmd = &cobra.Command{
Use: "list",
Short: "List SKS cluster Nodepools",
Long: fmt.Sprintf(`This command lists SKS cluster Nodepools.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&sksNodepoolListItemOutput{}), ", ")),
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
zone = strings.ToLower(zone)
return output(listSKSNodepools(zone), nil)
},
}
func listSKSNodepools(zone string) outputter {
var sksClusterZones []string
if zone != "" {
sksClusterZones = []string{zone}
} else {
sksClusterZones = zones
}
out := make(sksNodepoolListOutput, 0)
res := make(chan sksNodepoolListItemOutput)
defer close(res)
go func() {
for cluster := range res {
out = append(out, cluster)
}
}()
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zone))
err := forEachZone(sksClusterZones, func(zone string) error {
list, err := cs.ListSKSClusters(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list SKS clusters in zone %s: %v", zone, err)
}
for _, cluster := range list {
for _, np := range cluster.Nodepools {
res <- sksNodepoolListItemOutput{
ID: np.ID,
Name: np.Name,
Cluster: cluster.Name,
Size: np.Size,
State: np.State,
Zone: zone,
}
}
}
return nil
})
if err != nil {
_, _ = fmt.Fprintf(os.Stderr,
"warning: errors during listing, results might be incomplete.\n%s\n", err) // nolint:golint
}
return &out
}
func init() {
sksNodepoolListCmd.Flags().StringP("zone", "z", "", "Zone to filter results to")
sksNodepoolCmd.AddCommand(sksNodepoolListCmd)
}