-
Notifications
You must be signed in to change notification settings - Fork 19
/
sks_nodepool_update.go
174 lines (146 loc) · 5.53 KB
/
sks_nodepool_update.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
package cmd
import (
"errors"
"fmt"
"strings"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type sksNodepoolUpdateCmd struct {
_ bool `cli-cmd:"update"`
Cluster string `cli-arg:"#" cli-usage:"CLUSTER-NAME|ID"`
Nodepool string `cli-arg:"#" cli-usage:"NODEPOOL-NAME|ID"`
AntiAffinityGroups []string `cli-flag:"anti-affinity-group" cli-usage:"Nodepool Anti-Affinity Group NAME|ID (can be specified multiple times)"`
DeployTarget string `cli-usage:"Nodepool Deploy Target NAME|ID"`
Description string `cli-usage:"Nodepool description"`
DiskSize int64 `cli-usage:"Nodepool Compute instances disk size"`
InstancePrefix string `cli-usage:"string to prefix Nodepool member names with"`
InstanceType string `cli-usage:"Nodepool Compute instances type"`
Labels map[string]string `cli-flag:"label" cli-usage:"Nodepool label (format: key=value)"`
Name string `cli-usage:"Nodepool name"`
PrivateNetworks []string `cli-flag:"private-network" cli-usage:"Nodepool Private Network NAME|ID (can be specified multiple times)"`
SecurityGroups []string `cli-flag:"security-group" cli-usage:"Nodepool Security Group NAME|ID (can be specified multiple times)"`
Zone string `cli-short:"z" cli-usage:"SKS cluster zone"`
}
func (c *sksNodepoolUpdateCmd) cmdAliases() []string { return nil }
func (c *sksNodepoolUpdateCmd) cmdShort() string { return "Update an SKS cluster Nodepool" }
func (c *sksNodepoolUpdateCmd) cmdLong() string {
return fmt.Sprintf(`This command updates an SKS Nodepool.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&sksNodepoolShowOutput{}), ", "),
)
}
func (c *sksNodepoolUpdateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *sksNodepoolUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
var (
nodepool *egoscale.SKSNodepool
updated bool
)
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
cluster, err := cs.FindSKSCluster(ctx, c.Zone, c.Cluster)
if err != nil {
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
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.AntiAffinityGroups)) {
nodepoolAntiAffinityGroupIDs := make([]string, len(c.AntiAffinityGroups))
for i, v := range c.AntiAffinityGroups {
antiAffinityGroup, err := cs.FindAntiAffinityGroup(ctx, c.Zone, v)
if err != nil {
return fmt.Errorf("error retrieving Anti-Affinity Group: %s", err)
}
nodepoolAntiAffinityGroupIDs[i] = *antiAffinityGroup.ID
}
nodepool.AntiAffinityGroupIDs = &nodepoolAntiAffinityGroupIDs
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.DeployTarget)) {
deployTarget, err := cs.FindDeployTarget(ctx, c.Zone, c.DeployTarget)
if err != nil {
return fmt.Errorf("error retrieving Deploy Target: %s", err)
}
nodepool.DeployTargetID = deployTarget.ID
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Description)) {
nodepool.Description = &c.Description
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.DiskSize)) {
nodepool.DiskSize = &c.DiskSize
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.InstancePrefix)) {
nodepool.InstancePrefix = &c.InstancePrefix
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.InstanceType)) {
nodepoolInstanceType, err := cs.FindInstanceType(ctx, c.Zone, c.InstanceType)
if err != nil {
return fmt.Errorf("error retrieving instance type: %s", err)
}
nodepool.InstanceTypeID = nodepoolInstanceType.ID
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Labels)) {
nodepool.Labels = &c.Labels
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Name)) {
nodepool.Name = &c.Name
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.PrivateNetworks)) {
nodepoolPrivateNetworkIDs := make([]string, len(c.PrivateNetworks))
for i, v := range c.PrivateNetworks {
privateNetwork, err := cs.FindPrivateNetwork(ctx, c.Zone, v)
if err != nil {
return fmt.Errorf("error retrieving Private Network: %s", err)
}
nodepoolPrivateNetworkIDs[i] = *privateNetwork.ID
}
nodepool.PrivateNetworkIDs = &nodepoolPrivateNetworkIDs
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.SecurityGroups)) {
nodepoolSecurityGroupIDs := make([]string, len(c.SecurityGroups))
for i, v := range c.SecurityGroups {
securityGroup, err := cs.FindSecurityGroup(ctx, c.Zone, v)
if err != nil {
return fmt.Errorf("error retrieving Security Group: %s", err)
}
nodepoolSecurityGroupIDs[i] = *securityGroup.ID
}
nodepool.SecurityGroupIDs = &nodepoolSecurityGroupIDs
updated = true
}
if updated {
decorateAsyncOperation(fmt.Sprintf("Updating Nodepool %q...", c.Nodepool), func() {
if err = cluster.UpdateNodepool(ctx, nodepool); err != nil {
return
}
})
if err != nil {
return err
}
}
if !gQuiet {
return output(showSKSNodepool(c.Zone, *cluster.ID, *nodepool.ID))
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(sksNodepoolCmd, &sksNodepoolUpdateCmd{}))
}