-
Notifications
You must be signed in to change notification settings - Fork 18
/
sks_nodepool_show.go
154 lines (130 loc) · 4.45 KB
/
sks_nodepool_show.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/exoscale/egoscale"
exov2 "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type sksNodepoolShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CreationDate string `json:"creation_date"`
InstancePoolID string `json:"instance_pool_id"`
InstanceType string `json:"instance_type"`
Template string `json:"template"`
DiskSize int64 `json:"disk_size"`
AntiAffinityGroups []string `json:"anti_affinity_groups"`
SecurityGroups []string `json:"security_groups"`
Version string `json:"version"`
Size int64 `json:"size"`
State string `json:"state"`
}
func (o *sksNodepoolShowOutput) toJSON() { outputJSON(o) }
func (o *sksNodepoolShowOutput) toText() { outputText(o) }
func (o *sksNodepoolShowOutput) toTable() { outputTable(o) }
func (o *sksNodepoolShowOutput) Type() string { return "SKS Nodepool" }
var sksNodepoolShowCmd = &cobra.Command{
Use: "show CLUSTER-NAME|ID NODEPOOL-NAME|ID",
Short: "Show a SKS cluster Nodepool details",
Long: fmt.Sprintf(`This command shows a SKS cluster Nodepool details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&sksNodepoolShowOutput{}), ", ")),
Aliases: gShowAlias,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
z, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
zone, err := getZoneByNameOrID(z)
if err != nil {
return fmt.Errorf("error retrieving zone: %s", err)
}
return output(showSKSNodepool(zone, args[0], args[1]))
},
}
func showSKSNodepool(zone *egoscale.Zone, c, np string) (outputter, error) {
var nodepool *exov2.SKSNodepool
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zone.Name))
cluster, err := lookupSKSCluster(ctx, zone.Name, c)
if err != nil {
return nil, err
}
for _, n := range cluster.Nodepools {
if n.ID == np || n.Name == np {
nodepool = n
break
}
}
if nodepool == nil {
return nil, errors.New("Nodepool not found") // nolint:golint
}
out := sksNodepoolShowOutput{
ID: nodepool.ID,
Name: nodepool.Name,
Description: nodepool.Description,
CreationDate: nodepool.CreatedAt.String(),
InstancePoolID: nodepool.InstancePoolID,
DiskSize: nodepool.DiskSize,
AntiAffinityGroups: make([]string, 0),
SecurityGroups: make([]string, 0),
Version: nodepool.Version,
Size: nodepool.Size,
State: nodepool.State,
}
serviceOffering, err := getServiceOfferingByNameOrID(nodepool.InstanceTypeID)
if err != nil {
return nil, fmt.Errorf("error retrieving service offering: %s", err)
}
out.InstanceType = serviceOffering.Name
template, err := getTemplateByNameOrID(zone.ID,
egoscale.MustParseUUID(nodepool.TemplateID).String(),
"featured")
if err != nil {
return nil, fmt.Errorf("error retrieving template: %s", err)
}
out.Template = template.Name
if len(nodepool.AntiAffinityGroupIDs) > 0 {
allAntiAffinityGroups, err := cs.ListWithContext(gContext, &egoscale.AffinityGroup{})
if err != nil {
return nil, fmt.Errorf("error listing Anti-Affinity Groups: %s", err)
}
for _, s := range allAntiAffinityGroups {
sg := s.(*egoscale.AffinityGroup)
for _, id := range nodepool.AntiAffinityGroupIDs {
if sg.ID.String() == id {
out.AntiAffinityGroups = append(out.AntiAffinityGroups, sg.Name)
}
}
}
}
if len(nodepool.SecurityGroupIDs) > 0 {
allSecurityGroups, err := cs.ListWithContext(gContext, &egoscale.SecurityGroup{})
if err != nil {
return nil, fmt.Errorf("error listing Security Groups: %s", err)
}
for _, s := range allSecurityGroups {
sg := s.(*egoscale.SecurityGroup)
for _, id := range nodepool.SecurityGroupIDs {
if sg.ID.String() == id {
out.SecurityGroups = append(out.SecurityGroups, sg.Name)
}
}
}
}
return &out, nil
}
func init() {
sksNodepoolShowCmd.Flags().StringP("zone", "z", "", "SKS cluster zone")
sksNodepoolCmd.AddCommand(sksNodepoolShowCmd)
}