-
Notifications
You must be signed in to change notification settings - Fork 19
/
sks_nodepool_show.go
201 lines (175 loc) · 6.67 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/utils"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
)
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"`
Taints []string `json:"taints"`
Labels map[string]string `json:"labels"`
AddOns []string `json:"addons"`
ImageGCMin string `json:"image_gc_min_age"`
ImageGcLowThreshold int64 `json:"image_gc_low_threshold"`
ImageGcHighThreshold int64 `json:"image_gc_high_threshold"`
}
func (o *sksNodepoolShowOutput) Type() string { return "SKS Nodepool" }
func (o *sksNodepoolShowOutput) ToJSON() { output.JSON(o) }
func (o *sksNodepoolShowOutput) ToText() { output.Text(o) }
func (o *sksNodepoolShowOutput) ToTable() { output.Table(o) }
type sksNodepoolShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ 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(output.TemplateAnnotations(&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 {
var nodepool *egoscale.SKSNodepool
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, c.Zone))
cluster, err := globalstate.EgoscaleClient.FindSKSCluster(ctx, c.Zone, c.Cluster)
if err != nil {
if errors.Is(err, exoapi.ErrNotFound) {
return fmt.Errorf("resource not found in zone %q", c.Zone)
}
return err
}
for _, n := range cluster.Nodepools {
if *n.ID == c.Nodepool || *n.Name == c.Nodepool {
nodepool = n
break
}
}
if nodepool == nil {
return errors.New("Nodepool not found") // nolint:golint
}
out := sksNodepoolShowOutput{
AddOns: func() (v []string) {
if nodepool.AddOns != nil {
v = *nodepool.AddOns
}
return
}(),
AntiAffinityGroups: make([]string, 0),
CreationDate: nodepool.CreatedAt.String(),
Description: utils.DefaultString(nodepool.Description, ""),
DiskSize: *nodepool.DiskSize,
ID: *nodepool.ID,
InstancePoolID: *nodepool.InstancePoolID,
InstancePrefix: utils.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,
Taints: func() (v []string) {
if nodepool.Taints != nil {
v = make([]string, 0)
for k, t := range *nodepool.Taints {
v = append(v, fmt.Sprintf("%s=%s:%s", k, t.Value, t.Effect))
}
}
return
}(),
ImageGCMin: func() (s string) {
if nodepool.KubeletImageGc != nil && nodepool.KubeletImageGc.MinAge != nil {
return *nodepool.KubeletImageGc.MinAge
}
return kubeletImageGcMinAge
}(),
ImageGcLowThreshold: func() (s int64) {
if nodepool.KubeletImageGc != nil && nodepool.KubeletImageGc.LowThreshold != nil {
return *nodepool.KubeletImageGc.LowThreshold
}
return kubeletImageGcLowThreshold
}(),
ImageGcHighThreshold: func() (s int64) {
if nodepool.KubeletImageGc != nil && nodepool.KubeletImageGc.HighThreshold != nil {
return *nodepool.KubeletImageGc.HighThreshold
}
return kubeletImageGcHighThreshold
}(),
Version: *nodepool.Version,
}
if nodepool.AntiAffinityGroupIDs != nil {
for _, id := range *nodepool.AntiAffinityGroupIDs {
antiAffinityGroup, err := globalstate.EgoscaleClient.GetAntiAffinityGroup(ctx, c.Zone, id)
if err != nil {
return fmt.Errorf("error retrieving Anti-Affinity Group: %w", err)
}
out.AntiAffinityGroups = append(out.AntiAffinityGroups, *antiAffinityGroup.Name)
}
}
if nodepool.PrivateNetworkIDs != nil {
for _, id := range *nodepool.PrivateNetworkIDs {
privateNetwork, err := globalstate.EgoscaleClient.GetPrivateNetwork(ctx, c.Zone, id)
if err != nil {
return fmt.Errorf("error retrieving Private Network: %w", err)
}
out.PrivateNetworks = append(out.PrivateNetworks, *privateNetwork.Name)
}
}
if nodepool.SecurityGroupIDs != nil {
for _, id := range *nodepool.SecurityGroupIDs {
securityGroup, err := globalstate.EgoscaleClient.GetSecurityGroup(ctx, c.Zone, id)
if err != nil {
return fmt.Errorf("error retrieving Security Group: %w", err)
}
out.SecurityGroups = append(out.SecurityGroups, *securityGroup.Name)
}
}
serviceOffering, err := globalstate.EgoscaleClient.GetInstanceType(ctx, c.Zone, *nodepool.InstanceTypeID)
if err != nil {
return fmt.Errorf("error retrieving service offering: %w", err)
}
out.InstanceType = *serviceOffering.Size
template, err := globalstate.EgoscaleClient.GetTemplate(ctx, c.Zone, *nodepool.TemplateID)
if err != nil {
return fmt.Errorf("error retrieving template: %w", err)
}
out.Template = *template.Name
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(sksNodepoolCmd, &sksNodepoolShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}