This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 558
/
scale.go
415 lines (355 loc) · 12.4 KB
/
scale.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"sort"
"strings"
"time"
"github.com/Azure/acs-engine/pkg/acsengine"
"github.com/Azure/acs-engine/pkg/acsengine/transform"
"github.com/Azure/acs-engine/pkg/api"
"github.com/Azure/acs-engine/pkg/armhelpers"
"github.com/Azure/acs-engine/pkg/armhelpers/utils"
"github.com/Azure/acs-engine/pkg/i18n"
"github.com/Azure/acs-engine/pkg/operations"
"github.com/leonelquinteros/gotext"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type scaleCmd struct {
authArgs
// user input
resourceGroupName string
deploymentDirectory string
newDesiredAgentCount int
containerService *api.ContainerService
apiVersion string
location string
agentPoolToScale string
classicMode bool
// derived
apiModelPath string
agentPool *api.AgentPoolProfile
client armhelpers.ACSEngineClient
locale *gotext.Locale
nameSuffix string
agentPoolIndex int
masterFQDN string
logger *log.Entry
}
const (
scaleName = "scale"
scaleShortDescription = "scale a deployed cluster"
scaleLongDescription = "scale a deployed cluster"
)
// NewScaleCmd run a command to upgrade a Kubernetes cluster
func newScaleCmd() *cobra.Command {
sc := scaleCmd{}
scaleCmd := &cobra.Command{
Use: scaleName,
Short: scaleShortDescription,
Long: scaleLongDescription,
RunE: func(cmd *cobra.Command, args []string) error {
return sc.run(cmd, args)
},
}
f := scaleCmd.Flags()
f.StringVar(&sc.location, "location", "", "location the cluster is deployed in")
f.StringVar(&sc.resourceGroupName, "resource-group", "", "the resource group where the cluster is deployed")
f.StringVar(&sc.deploymentDirectory, "deployment-dir", "", "the location of the output from `generate`")
f.IntVar(&sc.newDesiredAgentCount, "new-node-count", 0, "desired number of nodes")
f.BoolVar(&sc.classicMode, "classic-mode", false, "enable classic parameters and outputs")
f.StringVar(&sc.agentPoolToScale, "node-pool", "", "node pool to scale")
f.StringVar(&sc.masterFQDN, "master-FQDN", "", "FQDN for the master load balancer, Needed to scale down Kubernetes agent pools")
addAuthFlags(&sc.authArgs, f)
return scaleCmd
}
func (sc *scaleCmd) validate(cmd *cobra.Command, args []string) {
log.Infoln("validating...")
sc.logger = log.New().WithField("source", "scaling command line")
var err error
sc.locale, err = i18n.LoadTranslations()
if err != nil {
log.Fatalf("error loading translation files: %s", err.Error())
}
if sc.resourceGroupName == "" {
cmd.Usage()
log.Fatal("--resource-group must be specified")
}
if sc.location == "" {
cmd.Usage()
log.Fatal("--location must be specified")
}
if sc.newDesiredAgentCount == 0 {
cmd.Usage()
log.Fatal("--new-node-count must be specified")
}
if sc.client, err = sc.authArgs.getClient(); err != nil {
log.Error("Failed to get client:", err)
}
if sc.deploymentDirectory == "" {
cmd.Usage()
log.Fatal("--deployment-dir must be specified")
}
_, err = sc.client.EnsureResourceGroup(sc.resourceGroupName, sc.location, nil)
if err != nil {
log.Fatalln(err)
}
// load apimodel from the deployment directory
sc.apiModelPath = path.Join(sc.deploymentDirectory, "apimodel.json")
if _, err = os.Stat(sc.apiModelPath); os.IsNotExist(err) {
log.Fatalf("specified api model does not exist (%s)", sc.apiModelPath)
}
apiloader := &api.Apiloader{
Translator: &i18n.Translator{
Locale: sc.locale,
},
}
sc.containerService, sc.apiVersion, err = apiloader.LoadContainerServiceFromFile(sc.apiModelPath, true, true, nil)
if err != nil {
log.Fatalf("error parsing the api model: %s", err.Error())
}
if sc.agentPoolToScale == "" {
agentPoolCount := len(sc.containerService.Properties.AgentPoolProfiles)
if agentPoolCount > 1 {
log.Fatal("--node-pool is required if more than one agent pool is defined in the container service")
} else if agentPoolCount == 1 {
sc.agentPool = sc.containerService.Properties.AgentPoolProfiles[0]
sc.agentPoolIndex = 0
sc.agentPoolToScale = sc.containerService.Properties.AgentPoolProfiles[0].Name
} else {
log.Fatal("No node pools found to scale")
}
} else {
agentPoolIndex := -1
for i, pool := range sc.containerService.Properties.AgentPoolProfiles {
if pool.Name == sc.agentPoolToScale {
agentPoolIndex = i
sc.agentPool = pool
sc.agentPoolIndex = i
}
}
if agentPoolIndex == -1 {
log.Fatalf("node pool %s wasn't in the deployed api model", sc.agentPoolToScale)
}
}
templatePath := path.Join(sc.deploymentDirectory, "azuredeploy.json")
contents, _ := ioutil.ReadFile(templatePath)
var template interface{}
json.Unmarshal(contents, &template)
templateMap := template.(map[string]interface{})
templateParameters := templateMap["parameters"].(map[string]interface{})
nameSuffixParam := templateParameters["nameSuffix"].(map[string]interface{})
sc.nameSuffix = nameSuffixParam["defaultValue"].(string)
log.Infoln(fmt.Sprintf("Name suffix: %s", sc.nameSuffix))
}
func (sc *scaleCmd) run(cmd *cobra.Command, args []string) error {
sc.validate(cmd, args)
orchestratorInfo := sc.containerService.Properties.OrchestratorProfile
var currentNodeCount, highestUsedIndex int
indexes := make([]int, 0)
indexToVM := make(map[int]string)
if sc.agentPool.IsAvailabilitySets() {
//TODO handle when there is a nextLink in the response and get more nodes
vms, err := sc.client.ListVirtualMachines(sc.resourceGroupName)
if err != nil {
log.Fatalln("failed to get vms in the resource group. Error: %s", err.Error())
} else if len(*vms.Value) < 1 {
log.Fatalln("The provided resource group does not contain any vms.")
}
for _, vm := range *vms.Value {
poolName, nameSuffix, index, err := utils.K8sLinuxVMNameParts(*vm.Name)
if err != nil || !strings.EqualFold(poolName, sc.agentPoolToScale) || !strings.EqualFold(nameSuffix, sc.nameSuffix) {
continue
}
indexToVM[index] = *vm.Name
indexes = append(indexes, index)
}
sortedIndexes := sort.IntSlice(indexes)
sortedIndexes.Sort()
indexes = []int(sortedIndexes)
currentNodeCount = len(indexes)
if currentNodeCount == sc.newDesiredAgentCount {
log.Info("Cluster is currently at the desired agent count.")
return nil
}
highestUsedIndex = indexes[len(indexes)-1]
// Scale down Scenario
if currentNodeCount > sc.newDesiredAgentCount {
if sc.masterFQDN == "" {
cmd.Usage()
log.Fatal("master-FQDN is required to scale down a kubernetes cluster's agent pool")
}
vmsToDelete := make([]string, 0)
for i := currentNodeCount - 1; i >= sc.newDesiredAgentCount; i-- {
vmsToDelete = append(vmsToDelete, indexToVM[i])
}
if orchestratorInfo.OrchestratorType == api.Kubernetes {
err = sc.drainNodes(vmsToDelete)
if err != nil {
log.Errorf("Got error %+v, while draining the nodes to be deleted", err)
return err
}
}
errList := operations.ScaleDownVMs(sc.client, sc.logger, sc.resourceGroupName, vmsToDelete...)
if errList != nil {
errorMessage := ""
for element := errList.Front(); element != nil; element = element.Next() {
vmError, ok := element.Value.(*operations.VMScalingErrorDetails)
if ok {
error := fmt.Sprintf("Node '%s' failed to delete with error: '%s'", vmError.Name, vmError.Error.Error())
errorMessage = errorMessage + error
}
}
return fmt.Errorf(errorMessage)
}
return nil
}
} else {
vmssList, err := sc.client.ListVirtualMachineScaleSets(sc.resourceGroupName)
if err != nil {
log.Fatalln("failed to get vmss list in the resource group. Error: %s", err.Error())
}
for _, vmss := range *vmssList.Value {
poolName, nameSuffix, err := utils.VmssNameParts(*vmss.Name)
if err != nil || !strings.EqualFold(poolName, sc.agentPoolToScale) || !strings.EqualFold(nameSuffix, sc.nameSuffix) {
continue
}
currentNodeCount = int(*vmss.Sku.Capacity)
highestUsedIndex = 0
}
}
ctx := acsengine.Context{
Translator: &i18n.Translator{
Locale: sc.locale,
},
}
templateGenerator, err := acsengine.InitializeTemplateGenerator(ctx, sc.classicMode)
if err != nil {
log.Fatalln("failed to initialize template generator: %s", err.Error())
}
sc.containerService.Properties.AgentPoolProfiles = []*api.AgentPoolProfile{sc.agentPool}
template, parameters, _, err := templateGenerator.GenerateTemplate(sc.containerService, acsengine.DefaultGeneratorCode)
if err != nil {
log.Fatalf("error generating template %s: %s", sc.apiModelPath, err.Error())
os.Exit(1)
}
if template, err = transform.PrettyPrintArmTemplate(template); err != nil {
log.Fatalf("error pretty printing template: %s \n", err.Error())
}
templateJSON := make(map[string]interface{})
parametersJSON := make(map[string]interface{})
err = json.Unmarshal([]byte(template), &templateJSON)
if err != nil {
log.Fatalln(err)
}
err = json.Unmarshal([]byte(parameters), ¶metersJSON)
if err != nil {
log.Fatalln(err)
}
transformer := transform.Transformer{Translator: ctx.Translator}
// Our templates generate a range of nodes based on a count and offset, it is possible for there to be holes in the template
// So we need to set the count in the template to get enough nodes for the range, if there are holes that number will be larger than the desired count
countForTemplate := sc.newDesiredAgentCount
if highestUsedIndex != 0 {
countForTemplate += highestUsedIndex + 1 - currentNodeCount
}
addValue(parametersJSON, sc.agentPool.Name+"Count", countForTemplate)
switch orchestratorInfo.OrchestratorType {
case api.Kubernetes:
err = transformer.NormalizeForK8sVMASScalingUp(sc.logger, templateJSON)
if err != nil {
log.Fatalf("error tranforming the template for scaling template %s: %s", sc.apiModelPath, err.Error())
os.Exit(1)
}
if sc.agentPool.IsAvailabilitySets() {
addValue(parametersJSON, fmt.Sprintf("%sOffset", sc.agentPool.Name), highestUsedIndex+1)
}
break
case api.Swarm:
case api.SwarmMode:
case api.DCOS:
if sc.agentPool.IsAvailabilitySets() {
log.Fatalf("scaling isn't supported for orchestrator %s, with availability sets", orchestratorInfo.OrchestratorType)
os.Exit(1)
}
transformer.NormalizeForVMSSScaling(sc.logger, templateJSON)
}
random := rand.New(rand.NewSource(time.Now().UnixNano()))
deploymentSuffix := random.Int31()
_, err = sc.client.DeployTemplate(
sc.resourceGroupName,
fmt.Sprintf("%s-%d", sc.resourceGroupName, deploymentSuffix),
templateJSON,
parametersJSON,
nil)
if err != nil {
log.Fatalln(err)
}
apiloader := &api.Apiloader{
Translator: &i18n.Translator{
Locale: sc.locale,
},
}
var apiVersion string
sc.containerService, apiVersion, err = apiloader.LoadContainerServiceFromFile(sc.apiModelPath, false, true, nil)
sc.containerService.Properties.AgentPoolProfiles[sc.agentPoolIndex].Count = sc.newDesiredAgentCount
b, e := apiloader.SerializeContainerService(sc.containerService, apiVersion)
if e != nil {
return e
}
f := acsengine.FileSaver{
Translator: &i18n.Translator{
Locale: sc.locale,
},
}
if e = f.SaveFile(sc.deploymentDirectory, "apimodel.json", b); e != nil {
return e
}
return nil
}
type paramsMap map[string]interface{}
func addValue(m paramsMap, k string, v interface{}) {
m[k] = paramsMap{
"value": v,
}
}
func (sc *scaleCmd) drainNodes(vmsToDelete []string) error {
kubeConfig, err := acsengine.GenerateKubeConfig(sc.containerService.Properties, sc.location)
if err != nil {
log.Fatalf("failed to generate kube config") // TODO: cleanup
}
var errorMessage string
masterURL := sc.masterFQDN
if !strings.HasPrefix(masterURL, "https://") {
masterURL = fmt.Sprintf("https://%s", masterURL)
}
numVmsToDrain := len(vmsToDelete)
errChan := make(chan *operations.VMScalingErrorDetails, numVmsToDrain)
defer close(errChan)
for _, vmName := range vmsToDelete {
go func(vmName string) {
e := operations.SafelyDrainNode(sc.client, sc.logger,
masterURL, kubeConfig, vmName, time.Duration(60)*time.Minute)
if e != nil {
log.Errorf("Failed to drain node %s, got error %s", vmName, e.Error())
errChan <- &operations.VMScalingErrorDetails{Error: e, Name: vmName}
return
}
errChan <- nil
}(vmName)
}
for i := 0; i < numVmsToDrain; i++ {
errDetails := <-errChan
if errDetails != nil {
error := fmt.Sprintf("Node '%s' failed to drain with error: '%s'", errDetails.Name, errDetails.Error.Error())
errorMessage = errorMessage + error
return fmt.Errorf(error)
}
}
return nil
}