-
Notifications
You must be signed in to change notification settings - Fork 20
/
sks_nodepool_show.go
155 lines (130 loc) · 4.97 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
155
package cmd
import (
"errors"
"fmt"
"strings"
egoscale "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"`
InstancePrefix string `json:"instance_prefix"`
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"`
PrivateNetworks []string `json:"private_networks"`
Version string `json:"version"`
Size int64 `json:"size"`
State string `json:"state"`
Labels map[string]string `json:"labels"`
}
func (o *sksNodepoolShowOutput) Type() string { return "SKS Nodepool" }
func (o *sksNodepoolShowOutput) toJSON() { outputJSON(o) }
func (o *sksNodepoolShowOutput) toText() { outputText(o) }
func (o *sksNodepoolShowOutput) toTable() { outputTable(o) }
type sksNodepoolShowCmd struct {
_ bool `cli-cmd:"show"`
Cluster string `cli-arg:"#" cli-usage:"CLUSTER-NAME|ID"`
Nodepool string `cli-arg:"#" cli-usage:"NODEPOOL-NAME|ID"`
Zone string `cli-short:"z" cli-usage:"SKS cluster zone"`
}
func (c *sksNodepoolShowCmd) cmdAliases() []string { return gShowAlias }
func (c *sksNodepoolShowCmd) cmdShort() string { return "Show an SKS cluster Nodepool details" }
func (c *sksNodepoolShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows an SKS cluster Nodepool details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&sksNodepoolShowOutput{}), ", "))
}
func (c *sksNodepoolShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *sksNodepoolShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
return output(showSKSNodepool(c.Zone, c.Cluster, c.Nodepool))
}
func showSKSNodepool(zone, c, np string) (outputter, error) {
var nodepool *egoscale.SKSNodepool
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zone))
cluster, err := cs.FindSKSCluster(ctx, zone, 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{
AntiAffinityGroups: make([]string, 0),
CreationDate: nodepool.CreatedAt.String(),
Description: defaultString(nodepool.Description, ""),
DiskSize: *nodepool.DiskSize,
ID: *nodepool.ID,
InstancePoolID: *nodepool.InstancePoolID,
InstancePrefix: defaultString(nodepool.InstancePrefix, ""),
Labels: func() (v map[string]string) {
if nodepool.Labels != nil {
v = *nodepool.Labels
}
return
}(),
Name: *nodepool.Name,
SecurityGroups: make([]string, 0),
PrivateNetworks: make([]string, 0),
Size: *nodepool.Size,
State: *nodepool.State,
Version: *nodepool.Version,
}
if nodepool.AntiAffinityGroupIDs != nil {
for _, id := range *nodepool.AntiAffinityGroupIDs {
antiAffinityGroup, err := cs.GetAntiAffinityGroup(ctx, zone, id)
if err != nil {
return nil, fmt.Errorf("error retrieving Anti-Affinity Group: %v", err)
}
out.AntiAffinityGroups = append(out.AntiAffinityGroups, *antiAffinityGroup.Name)
}
}
if nodepool.PrivateNetworkIDs != nil {
for _, id := range *nodepool.PrivateNetworkIDs {
privateNetwork, err := cs.GetPrivateNetwork(ctx, zone, id)
if err != nil {
return nil, fmt.Errorf("error retrieving Private Network: %v", err)
}
out.PrivateNetworks = append(out.PrivateNetworks, *privateNetwork.Name)
}
}
if nodepool.SecurityGroupIDs != nil {
for _, id := range *nodepool.SecurityGroupIDs {
securityGroup, err := cs.GetSecurityGroup(ctx, zone, id)
if err != nil {
return nil, fmt.Errorf("error retrieving Security Group: %v", err)
}
out.SecurityGroups = append(out.SecurityGroups, *securityGroup.Name)
}
}
serviceOffering, err := cs.GetInstanceType(ctx, zone, *nodepool.InstanceTypeID)
if err != nil {
return nil, fmt.Errorf("error retrieving service offering: %s", err)
}
out.InstanceType = *serviceOffering.Size
template, err := cs.GetTemplate(ctx, zone, *nodepool.TemplateID)
if err != nil {
return nil, fmt.Errorf("error retrieving template: %s", err)
}
out.Template = *template.Name
return &out, nil
}
func init() {
cobra.CheckErr(registerCLICommand(sksNodepoolCmd, &sksNodepoolShowCmd{}))
}