-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
nodegroup.go
461 lines (393 loc) · 13.9 KB
/
nodegroup.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package manager
import (
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
cfn "github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/blang/semver"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/builder"
"github.com/weaveworks/eksctl/pkg/cfn/outputs"
"github.com/weaveworks/eksctl/pkg/version"
"github.com/weaveworks/eksctl/pkg/vpc"
)
const (
imageIDPath = resourcesRootPath + ".NodeGroupLaunchTemplate.Properties.LaunchTemplateData.ImageId"
)
// NodeGroupSummary represents a summary of a nodegroup stack
type NodeGroupSummary struct {
StackName string
Cluster string
Name string
Status string
MaxSize int
MinSize int
DesiredCapacity int
InstanceType string
ImageID string
CreationTime *time.Time
NodeInstanceRoleARN string
AutoScalingGroupName string
}
// NodeGroupStack represents a nodegroup and its type
type NodeGroupStack struct {
NodeGroupName string
Type api.NodeGroupType
}
// makeNodeGroupStackName generates the name of the nodegroup stack identified by its name, isolated by the cluster this StackCollection operates on
func (c *StackCollection) makeNodeGroupStackName(name string) string {
return fmt.Sprintf("eksctl-%s-nodegroup-%s", c.spec.Metadata.Name, name)
}
// createNodeGroupTask creates the nodegroup
func (c *StackCollection) createNodeGroupTask(errs chan error, ng *api.NodeGroup, supportsManagedNodes, forceAddCNIPolicy bool, vpcImporter vpc.Importer) error {
name := c.makeNodeGroupStackName(ng.Name)
logger.Info("building nodegroup stack %q", name)
stack := builder.NewNodeGroupResourceSet(c.ec2API, c.iamAPI, c.spec, ng, supportsManagedNodes, forceAddCNIPolicy, vpcImporter)
if err := stack.AddAllResources(); err != nil {
return err
}
if ng.Tags == nil {
ng.Tags = make(map[string]string)
}
ng.Tags[api.NodeGroupNameTag] = ng.Name
ng.Tags[api.OldNodeGroupNameTag] = ng.Name
ng.Tags[api.NodeGroupTypeTag] = string(api.NodeGroupTypeUnmanaged)
return c.CreateStack(name, stack, ng.Tags, nil, errs)
}
func (c *StackCollection) createManagedNodeGroupTask(errorCh chan error, ng *api.ManagedNodeGroup, forceAddCNIPolicy bool, vpcImporter vpc.Importer) error {
name := c.makeNodeGroupStackName(ng.Name)
logger.Info("building managed nodegroup stack %q", name)
stack := builder.NewManagedNodeGroup(c.ec2API, c.spec, ng, builder.NewLaunchTemplateFetcher(c.ec2API), forceAddCNIPolicy, vpcImporter)
if err := stack.AddAllResources(); err != nil {
return err
}
return c.CreateStack(name, stack, ng.Tags, nil, errorCh)
}
// DescribeNodeGroupStacks calls DescribeStacks and filters out nodegroups
func (c *StackCollection) DescribeNodeGroupStacks() ([]*Stack, error) {
stacks, err := c.DescribeStacks()
if err != nil {
return nil, err
}
if len(stacks) == 0 {
return nil, nil
}
nodeGroupStacks := []*Stack{}
for _, s := range stacks {
if *s.StackStatus == cfn.StackStatusDeleteComplete {
continue
}
if c.GetNodeGroupName(s) != "" {
nodeGroupStacks = append(nodeGroupStacks, s)
}
}
logger.Debug("nodegroups = %v", nodeGroupStacks)
return nodeGroupStacks, nil
}
// ListNodeGroupStacks returns a list of NodeGroupStacks
func (c *StackCollection) ListNodeGroupStacks() ([]NodeGroupStack, error) {
stacks, err := c.DescribeNodeGroupStacks()
if err != nil {
return nil, err
}
var nodeGroupStacks []NodeGroupStack
for _, stack := range stacks {
nodeGroupType, err := GetNodeGroupType(stack.Tags)
if err != nil {
return nil, err
}
nodeGroupStacks = append(nodeGroupStacks, NodeGroupStack{
NodeGroupName: c.GetNodeGroupName(stack),
Type: nodeGroupType,
})
}
return nodeGroupStacks, nil
}
// DescribeNodeGroupStacksAndResources calls DescribeNodeGroupStacks and fetches all resources,
// then returns it in a map by nodegroup name
func (c *StackCollection) DescribeNodeGroupStacksAndResources() (map[string]StackInfo, error) {
stacks, err := c.DescribeNodeGroupStacks()
if err != nil {
return nil, err
}
allResources := make(map[string]StackInfo)
for _, s := range stacks {
input := &cfn.DescribeStackResourcesInput{
StackName: s.StackName,
}
template, err := c.GetStackTemplate(*s.StackName)
if err != nil {
return nil, errors.Wrapf(err, "getting template for %q stack", *s.StackName)
}
resources, err := c.cloudformationAPI.DescribeStackResources(input)
if err != nil {
return nil, errors.Wrapf(err, "getting all resources for %q stack", *s.StackName)
}
allResources[c.GetNodeGroupName(s)] = StackInfo{
Resources: resources.StackResources,
Template: &template,
Stack: s,
}
}
return allResources, nil
}
// GetUnmanagedNodeGroupSummaries returns a list of summaries for the unmanaged nodegroups of a cluster
func (c *StackCollection) GetUnmanagedNodeGroupSummaries(name string) ([]*NodeGroupSummary, error) {
stacks, err := c.DescribeNodeGroupStacks()
if err != nil {
return nil, errors.Wrap(err, "getting nodegroup stacks")
}
// Create an empty array here so that an object is returned rather than null
summaries := []*NodeGroupSummary{}
for _, s := range stacks {
nodeGroupType, err := GetNodeGroupType(s.Tags)
if err != nil {
return nil, err
}
if nodeGroupType == api.NodeGroupTypeUnmanaged {
ngPaths, err := getNodeGroupPaths(s.Tags)
if err != nil {
return nil, err
}
summary, err := c.mapStackToNodeGroupSummary(s, ngPaths)
if err != nil {
return nil, errors.Wrap(err, "mapping stack to nodegroup summary")
}
asgName, err := c.getUnmanagedNodeGroupAutoScalingGroupName(s)
if err != nil {
return nil, errors.Wrap(err, "getting autoscalinggroupname")
}
summary.AutoScalingGroupName = asgName
scalingGroup, err := c.GetAutoScalingGroupDesiredCapacity(asgName)
if err != nil {
return nil, errors.Wrap(err, "getting autoscalinggroup desired capacity")
}
summary.DesiredCapacity = int(*scalingGroup.DesiredCapacity)
summary.MinSize = int(*scalingGroup.MinSize)
summary.MaxSize = int(*scalingGroup.MaxSize)
if name == "" || summary.Name == name {
summaries = append(summaries, summary)
}
}
}
return summaries, nil
}
func (c *StackCollection) GetAutoScalingGroupName(s *Stack) (string, error) {
nodeGroupType, err := GetNodeGroupType(s.Tags)
if err != nil {
return "", err
}
switch nodeGroupType {
case api.NodeGroupTypeManaged:
res, err := c.getManagedNodeGroupAutoScalingGroupName(s)
if err != nil {
return "", err
}
return res, nil
case api.NodeGroupTypeUnmanaged, "":
res, err := c.getUnmanagedNodeGroupAutoScalingGroupName(s)
if err != nil {
return "", err
}
return res, nil
default:
return "", fmt.Errorf("cant get autoscaling group name, because unexpected nodegroup type : %q", nodeGroupType)
}
}
// GetNodeGroupAutoScalingGroupName return the unmanaged nodegroup's AutoScalingGroupName
func (c *StackCollection) getUnmanagedNodeGroupAutoScalingGroupName(s *Stack) (string, error) {
input := &cfn.DescribeStackResourceInput{
StackName: s.StackName,
LogicalResourceId: aws.String("NodeGroup"),
}
res, err := c.cloudformationAPI.DescribeStackResource(input)
if err != nil {
return "", err
}
return *res.StackResourceDetail.PhysicalResourceId, nil
}
// GetManagedNodeGroupAutoScalingGroupName returns the managed nodegroup's AutoScalingGroupName
func (c *StackCollection) getManagedNodeGroupAutoScalingGroupName(s *Stack) (string, error) {
input := &eks.DescribeNodegroupInput{
ClusterName: aws.String(getClusterNameTag(s)),
NodegroupName: aws.String(c.GetNodeGroupName(s)),
}
res, err := c.eksAPI.DescribeNodegroup(input)
if err != nil {
logger.Warning("couldn't get managed nodegroup details for stack %q", *s.StackName)
return "", nil
}
asgs := []string{}
if res.Nodegroup.Resources != nil {
for _, v := range res.Nodegroup.Resources.AutoScalingGroups {
asgs = append(asgs, aws.StringValue(v.Name))
}
}
return strings.Join(asgs, ","), nil
}
// GetAutoScalingGroupDesiredCapacity returns the AutoScalingGroup's desired capacity
func (c *StackCollection) GetAutoScalingGroupDesiredCapacity(name string) (autoscaling.Group, error) {
asg, err := c.asgAPI.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{
&name,
},
})
if err != nil {
return autoscaling.Group{}, fmt.Errorf("couldn't describe ASG: %s", name)
}
if len(asg.AutoScalingGroups) != 1 {
logger.Warning("couldn't find ASG %s", name)
return autoscaling.Group{}, fmt.Errorf("couldn't find ASG: %s", name)
}
return *asg.AutoScalingGroups[0], nil
}
// DescribeNodeGroupStack gets the specified nodegroup stack
func (c *StackCollection) DescribeNodeGroupStack(nodeGroupName string) (*Stack, error) {
stackName := c.makeNodeGroupStackName(nodeGroupName)
return c.DescribeStack(&Stack{StackName: &stackName})
}
// GetNodeGroupStackType returns the nodegroup stack type
func (c *StackCollection) GetNodeGroupStackType(name string) (api.NodeGroupType, error) {
stack, err := c.DescribeNodeGroupStack(name)
if err != nil {
return "", err
}
return GetNodeGroupType(stack.Tags)
}
// GetNodeGroupType returns the nodegroup type
func GetNodeGroupType(tags []*cfn.Tag) (api.NodeGroupType, error) {
var nodeGroupType api.NodeGroupType
if ngNameTagValue := GetNodegroupTagName(tags); ngNameTagValue == "" {
return "", errors.New("failed to find the nodegroup name tag")
}
for _, tag := range tags {
switch *tag.Key {
case api.NodeGroupTypeTag:
nodeGroupType = api.NodeGroupType(*tag.Value)
}
}
if nodeGroupType == "" {
nodeGroupType = api.NodeGroupTypeUnmanaged
}
return nodeGroupType, nil
}
// GetEksctlVersionFromTags returns the eksctl version used to create or update the stack
func GetEksctlVersionFromTags(tags []*cfn.Tag) (semver.Version, bool, error) {
for _, tag := range tags {
if *tag.Key == api.EksctlVersionTag {
v, err := version.ParseEksctlVersion(*tag.Value)
if err != nil {
return v, false, errors.Wrapf(err, "unexpected error parsing eksctl version %q", *tag.Value)
}
return v, true, nil
}
}
return semver.Version{}, false, nil
}
type nodeGroupPaths struct {
InstanceType string
DesiredCapacity string
MinSize string
MaxSize string
}
func getNodeGroupPaths(tags []*cfn.Tag) (*nodeGroupPaths, error) {
nodeGroupType, err := GetNodeGroupType(tags)
if err != nil {
return nil, err
}
switch nodeGroupType {
case api.NodeGroupTypeManaged:
makePath := func(fieldPath string) string {
return fmt.Sprintf("%s.ManagedNodeGroup.Properties.%s", resourcesRootPath, fieldPath)
}
makeScalingPath := func(field string) string {
return makePath(fmt.Sprintf("ScalingConfig.%s", field))
}
return &nodeGroupPaths{
InstanceType: makePath("InstanceTypes.0"),
DesiredCapacity: makeScalingPath("DesiredSize"),
MinSize: makeScalingPath("MinSize"),
MaxSize: makeScalingPath("MaxSize"),
}, nil
// Tag may not exist for existing nodegroups
case api.NodeGroupTypeUnmanaged, "":
makePath := func(field string) string {
return fmt.Sprintf("%s.NodeGroup.Properties.%s", resourcesRootPath, field)
}
return &nodeGroupPaths{
InstanceType: resourcesRootPath + ".NodeGroupLaunchTemplate.Properties.LaunchTemplateData.InstanceType",
DesiredCapacity: makePath("DesiredCapacity"),
MinSize: makePath("MinSize"),
MaxSize: makePath("MaxSize"),
}, nil
default:
return nil, fmt.Errorf("unexpected nodegroup type tag: %q", nodeGroupType)
}
}
func (c *StackCollection) mapStackToNodeGroupSummary(stack *Stack, ngPaths *nodeGroupPaths) (*NodeGroupSummary, error) {
template, err := c.GetStackTemplate(*stack.StackName)
if err != nil {
return nil, errors.Wrapf(err, "error getting CloudFormation template for stack %s", *stack.StackName)
}
summary := &NodeGroupSummary{
StackName: *stack.StackName,
Cluster: getClusterNameTag(stack),
Name: c.GetNodeGroupName(stack),
Status: *stack.StackStatus,
MaxSize: int(gjson.Get(template, ngPaths.MaxSize).Int()),
MinSize: int(gjson.Get(template, ngPaths.MinSize).Int()),
DesiredCapacity: int(gjson.Get(template, ngPaths.DesiredCapacity).Int()),
InstanceType: gjson.Get(template, ngPaths.InstanceType).String(),
ImageID: gjson.Get(template, imageIDPath).String(),
CreationTime: stack.CreationTime,
}
nodeGroupType, err := GetNodeGroupType(stack.Tags)
if err != nil {
return nil, err
}
var nodeInstanceRoleARN string
if nodeGroupType == api.NodeGroupTypeUnmanaged {
nodeInstanceRoleARNCollector := func(s string) error {
nodeInstanceRoleARN = s
return nil
}
collectors := map[string]outputs.Collector{
outputs.NodeGroupInstanceRoleARN: nodeInstanceRoleARNCollector,
}
collectorSet := outputs.NewCollectorSet(collectors)
if err := collectorSet.MustCollect(*stack); err != nil {
logger.Warning(errors.Wrapf(err, "error collecting Cloudformation outputs for stack %s", *stack.StackName).Error())
}
}
summary.NodeInstanceRoleARN = nodeInstanceRoleARN
return summary, nil
}
// GetNodeGroupName will return nodegroup name based on tags
func (*StackCollection) GetNodeGroupName(s *Stack) string {
if tagName := GetNodegroupTagName(s.Tags); tagName != "" {
return tagName
}
if strings.HasSuffix(*s.StackName, "-nodegroup-0") {
return "legacy-nodegroup-0"
}
if strings.HasSuffix(*s.StackName, "-DefaultNodeGroup") {
return "legacy-default"
}
return ""
}
// GetNodegroupTagName returns the nodegroup name of a stack based on its tags. Taking into account legacy tags.
func GetNodegroupTagName(tags []*cfn.Tag) string {
for _, tag := range tags {
switch *tag.Key {
case api.NodeGroupNameTag, api.OldNodeGroupNameTag, api.OldNodeGroupIDTag:
return *tag.Value
}
}
return ""
}