-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
nodegroup.go
480 lines (418 loc) · 15.5 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package manager
import (
"context"
"fmt"
"strings"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
asgtypes "github.com/aws/aws-sdk-go-v2/service/autoscaling/types"
cfn "github.com/aws/aws-sdk-go-v2/service/cloudformation"
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/blang/semver"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/awsapi"
"github.com/weaveworks/eksctl/pkg/cfn/builder"
"github.com/weaveworks/eksctl/pkg/nodebootstrap"
"github.com/weaveworks/eksctl/pkg/utils/tasks"
"github.com/weaveworks/eksctl/pkg/version"
"github.com/weaveworks/eksctl/pkg/vpc"
)
// NodeGroupStack represents a nodegroup and its type
type NodeGroupStack struct {
NodeGroupName string
Type api.NodeGroupType
UsesAccessEntry bool
Stack *Stack
}
// makeNodeGroupStackName generates the name of the nodegroup stack identified by its name.
func makeNodeGroupStackName(clusterName, ngName string) string {
return fmt.Sprintf("eksctl-%s-nodegroup-%s", clusterName, ngName)
}
// CreateNodeGroupOptions holds options for creating nodegroup tasks.
type CreateNodeGroupOptions struct {
ForceAddCNIPolicy bool
SkipEgressRules bool
DisableAccessEntryCreation bool
VPCImporter vpc.Importer
}
// A NodeGroupStackManager describes and creates nodegroup stacks.
type NodeGroupStackManager interface {
// CreateStack creates a CloudFormation stack.
CreateStack(ctx context.Context, stackName string, resourceSet builder.ResourceSetReader, tags, parameters map[string]string, errs chan error) error
}
// A NodeGroupResourceSet creates resources for a nodegroup.
//
//counterfeiter:generate -o fakes/fake_nodegroup_resource_set.go . NodeGroupResourceSet
type NodeGroupResourceSet interface {
// AddAllResources adds all nodegroup resources.
AddAllResources(ctx context.Context) error
builder.ResourceSetReader
}
// CreateNodeGroupResourceSetFunc creates a new NodeGroupResourceSet.
type CreateNodeGroupResourceSetFunc func(options builder.NodeGroupOptions) NodeGroupResourceSet
// NewBootstrapperFunc creates a new Bootstrapper for ng.
type NewBootstrapperFunc func(clusterConfig *api.ClusterConfig, ng *api.NodeGroup) (nodebootstrap.Bootstrapper, error)
// UnmanagedNodeGroupTask creates tasks for creating self-managed nodegroups.
type UnmanagedNodeGroupTask struct {
ClusterConfig *api.ClusterConfig
NodeGroups []*api.NodeGroup
CreateNodeGroupResourceSet CreateNodeGroupResourceSetFunc
NewBootstrapper NewBootstrapperFunc
EKSAPI awsapi.EKS
StackManager NodeGroupStackManager
}
// Create creates a TaskTree for creating nodegroups.
func (t *UnmanagedNodeGroupTask) Create(ctx context.Context, options CreateNodeGroupOptions) *tasks.TaskTree {
taskTree := &tasks.TaskTree{Parallel: true}
for _, ng := range t.NodeGroups {
ng := ng
createAccessEntryInStack := ng.IAM.InstanceRoleARN == ""
createNodeGroupTask := &tasks.GenericTask{
Description: fmt.Sprintf("create nodegroup %q", ng.NameString()),
Doer: func() error {
return t.createNodeGroup(ctx, ng, options, createAccessEntryInStack)
},
}
if options.DisableAccessEntryCreation || createAccessEntryInStack {
taskTree.Append(createNodeGroupTask)
} else {
var ngTask tasks.TaskTree
ngTask.Append(createNodeGroupTask)
ngTask.Append(&tasks.GenericTask{
Description: fmt.Sprintf("create access entry for nodegroup %q", ng.NameString()),
Doer: func() error {
return t.maybeCreateAccessEntry(ctx, ng)
},
})
taskTree.Append(&ngTask)
}
}
return taskTree
}
func (t *UnmanagedNodeGroupTask) createNodeGroup(ctx context.Context, ng *api.NodeGroup, options CreateNodeGroupOptions, createAccessEntryInStack bool) error {
name := makeNodeGroupStackName(t.ClusterConfig.Metadata.Name, ng.Name)
logger.Info("building nodegroup stack %q", name)
bootstrapper, err := t.NewBootstrapper(t.ClusterConfig, ng)
if err != nil {
return errors.Wrap(err, "error creating bootstrapper")
}
resourceSet := t.CreateNodeGroupResourceSet(builder.NodeGroupOptions{
ClusterConfig: t.ClusterConfig,
NodeGroup: ng,
Bootstrapper: bootstrapper,
ForceAddCNIPolicy: options.ForceAddCNIPolicy,
VPCImporter: options.VPCImporter,
SkipEgressRules: options.SkipEgressRules,
DisableAccessEntry: options.DisableAccessEntryCreation,
DisableAccessEntryResource: !createAccessEntryInStack,
})
if err := resourceSet.AddAllResources(ctx); 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)
errCh := make(chan error)
if err := t.StackManager.CreateStack(ctx, name, resourceSet, ng.Tags, nil, errCh); err != nil {
return err
}
return <-errCh
}
func (t *UnmanagedNodeGroupTask) maybeCreateAccessEntry(ctx context.Context, ng *api.NodeGroup) error {
roleARN := ng.IAM.InstanceRoleARN
_, err := t.EKSAPI.CreateAccessEntry(ctx, &eks.CreateAccessEntryInput{
ClusterName: aws.String(t.ClusterConfig.Metadata.Name),
PrincipalArn: aws.String(roleARN),
Type: aws.String(string(api.GetAccessEntryType(ng))),
Tags: map[string]string{
api.ClusterNameLabel: t.ClusterConfig.Metadata.Name,
},
})
if err != nil {
var resourceInUse *ekstypes.ResourceInUseException
if errors.As(err, &resourceInUse) {
logger.Info("nodegroup %s: access entry for principal ARN %q already exists", ng.Name, roleARN)
return nil
}
return fmt.Errorf("creating access entry for nodegroup %s: %w", ng.Name, err)
}
logger.Info("nodegroup %s: created access entry for principal ARN %q", ng.Name, roleARN)
return nil
}
// makeNodeGroupStackName generates the name of the nodegroup stack identified by its name and this StackCollection's cluster.
func (c *StackCollection) makeNodeGroupStackName(ngName string) string {
return makeNodeGroupStackName(c.spec.Metadata.Name, ngName)
}
func (c *StackCollection) createManagedNodeGroupTask(ctx context.Context, errorCh chan error, ng *api.ManagedNodeGroup, forceAddCNIPolicy bool, vpcImporter vpc.Importer) error {
name := c.makeNodeGroupStackName(ng.Name)
cluster, err := c.DescribeClusterStackIfExists(ctx)
if err != nil {
return err
}
if cluster == nil && c.spec.IPv6Enabled() {
return errors.New("managed nodegroups cannot be created on IPv6 unowned clusters")
}
logger.Info("building managed nodegroup stack %q", name)
bootstrapper, err := nodebootstrap.NewManagedBootstrapper(c.spec, ng)
if err != nil {
return err
}
stack := builder.NewManagedNodeGroup(c.ec2API, c.spec, ng, builder.NewLaunchTemplateFetcher(c.ec2API), bootstrapper, forceAddCNIPolicy, vpcImporter)
if err := stack.AddAllResources(ctx); err != nil {
return err
}
return c.CreateStack(ctx, name, stack, ng.Tags, nil, errorCh)
}
func (c *StackCollection) propagateManagedNodeGroupTagsToASGTask(ctx context.Context, errorCh chan error, ng *api.ManagedNodeGroup,
propagateFunc func(string, map[string]string, []string, chan error) error) error {
// describe node group to retrieve ASG names
input := &eks.DescribeNodegroupInput{
ClusterName: aws.String(c.spec.Metadata.Name),
NodegroupName: aws.String(ng.Name),
}
res, err := c.eksAPI.DescribeNodegroup(ctx, input)
if err != nil {
return errors.Wrapf(err, "couldn't get managed nodegroup details for nodegroup %q", ng.Name)
}
if res.Nodegroup.Resources == nil {
return nil
}
asgNames := []string{}
for _, asg := range res.Nodegroup.Resources.AutoScalingGroups {
if asg.Name != nil && *asg.Name != "" {
asgNames = append(asgNames, *asg.Name)
}
}
// add labels and taints
tags := map[string]string{}
builder.GenerateClusterAutoscalerTags(ng, func(key, value string) {
tags[key] = value
})
// add nodegroup tags
for k, v := range ng.Tags {
tags[k] = v
}
return propagateFunc(ng.Name, tags, asgNames, errorCh)
}
// ListNodeGroupStacks calls ListStacks and filters out nodegroups
func (c *StackCollection) ListNodeGroupStacks(ctx context.Context) ([]*Stack, error) {
stacks, err := c.ListStacks(ctx)
if err != nil {
return nil, err
}
if len(stacks) == 0 {
return nil, nil
}
nodeGroupStacks := []*Stack{}
for _, s := range stacks {
switch s.StackStatus {
case types.StackStatusDeleteComplete:
continue
case types.StackStatusDeleteFailed:
logger.Warning("stack's status of nodegroup named %s is %s", *s.StackName, s.StackStatus)
continue
}
if c.GetNodeGroupName(s) != "" {
nodeGroupStacks = append(nodeGroupStacks, s)
}
}
logger.Debug("nodegroups = %v", nodeGroupStacks)
return nodeGroupStacks, nil
}
// ListNodeGroupStacksWithStatuses returns a list of NodeGroupStacks.
func (c *StackCollection) ListNodeGroupStacksWithStatuses(ctx context.Context) ([]NodeGroupStack, error) {
stacks, err := c.ListNodeGroupStacks(ctx)
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,
UsesAccessEntry: nodeGroupType == api.NodeGroupTypeUnmanaged && usesAccessEntry(stack),
Stack: stack,
})
}
return nodeGroupStacks, nil
}
// DescribeNodeGroupStacksAndResources calls DescribeNodeGroupStackList and fetches all resources,
// then returns it in a map by nodegroup name
func (c *StackCollection) DescribeNodeGroupStacksAndResources(ctx context.Context) (map[string]StackInfo, error) {
stacks, err := c.ListNodeGroupStacks(ctx)
if err != nil {
return nil, err
}
allResources := make(map[string]StackInfo)
for _, s := range stacks {
input := &cfn.DescribeStackResourcesInput{
StackName: s.StackName,
}
resources, err := c.cloudformationAPI.DescribeStackResources(ctx, 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,
Stack: s,
}
}
return allResources, nil
}
func (c *StackCollection) GetAutoScalingGroupName(ctx context.Context, s *Stack) (string, error) {
nodeGroupType, err := GetNodeGroupType(s.Tags)
if err != nil {
return "", err
}
switch nodeGroupType {
case api.NodeGroupTypeManaged:
res, err := c.getManagedNodeGroupAutoScalingGroupName(ctx, s)
if err != nil {
return "", err
}
return res, nil
case api.NodeGroupTypeUnmanaged, "":
res, err := c.GetUnmanagedNodeGroupAutoScalingGroupName(ctx, s)
if err != nil {
return "", err
}
return res, nil
default:
return "", fmt.Errorf("cant get autoscaling group name, because unexpected nodegroup type : %q", nodeGroupType)
}
}
// GetUnmanagedNodeGroupAutoScalingGroupName returns the unmanaged nodegroup's AutoScalingGroupName.
func (c *StackCollection) GetUnmanagedNodeGroupAutoScalingGroupName(ctx context.Context, s *Stack) (string, error) {
input := &cfn.DescribeStackResourceInput{
StackName: s.StackName,
LogicalResourceId: aws.String("NodeGroup"),
}
res, err := c.cloudformationAPI.DescribeStackResource(ctx, input)
if err != nil {
return "", err
}
if res.StackResourceDetail.PhysicalResourceId == nil {
return "", fmt.Errorf("%q resource of stack %q has no physical resource id", *input.LogicalResourceId, *res.StackResourceDetail.LogicalResourceId)
}
return *res.StackResourceDetail.PhysicalResourceId, nil
}
// GetManagedNodeGroupAutoScalingGroupName returns the managed nodegroup's AutoScalingGroup names
func (c *StackCollection) getManagedNodeGroupAutoScalingGroupName(ctx context.Context, s *Stack) (string, error) {
input := &eks.DescribeNodegroupInput{
ClusterName: aws.String(getClusterNameTag(s)),
NodegroupName: aws.String(c.GetNodeGroupName(s)),
}
res, err := c.eksAPI.DescribeNodegroup(ctx, input)
if err != nil {
logger.Warning("couldn't get managed nodegroup details for stack %q", *s.StackName)
return "", nil
}
var asgs []string
if res.Nodegroup.Resources != nil {
for _, v := range res.Nodegroup.Resources.AutoScalingGroups {
asgs = append(asgs, aws.ToString(v.Name))
}
}
return strings.Join(asgs, ","), nil
}
func (c *StackCollection) GetAutoScalingGroupDesiredCapacity(ctx context.Context, name string) (asgtypes.AutoScalingGroup, error) {
asg, err := c.asgAPI.DescribeAutoScalingGroups(ctx, &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []string{
name,
},
})
if err != nil {
return asgtypes.AutoScalingGroup{}, fmt.Errorf("couldn't describe ASG: %s", name)
}
if len(asg.AutoScalingGroups) != 1 {
logger.Warning("couldn't find ASG %s", name)
return asgtypes.AutoScalingGroup{}, fmt.Errorf("couldn't find ASG: %s", name)
}
return asg.AutoScalingGroups[0], nil
}
// DescribeNodeGroupStack gets the specified nodegroup stack
func (c *StackCollection) DescribeNodeGroupStack(ctx context.Context, nodeGroupName string) (*Stack, error) {
stackName := c.makeNodeGroupStackName(nodeGroupName)
return c.DescribeStack(ctx, &Stack{StackName: &stackName})
}
// GetNodeGroupStackType returns the nodegroup stack type
func (c *StackCollection) GetNodeGroupStackType(ctx context.Context, options GetNodegroupOption) (api.NodeGroupType, error) {
var (
err error
stack *Stack
)
if options.Stack != nil && options.Stack.Stack != nil {
stack = options.Stack.Stack
}
if stack == nil {
stack, err = c.DescribeNodeGroupStack(ctx, options.NodeGroupName)
if err != nil {
return "", err
}
}
return GetNodeGroupType(stack.Tags)
}
// GetNodeGroupType returns the nodegroup type
func GetNodeGroupType(tags []types.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 []types.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
}
// 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 []types.Tag) string {
for _, tag := range tags {
switch *tag.Key {
case api.NodeGroupNameTag, api.OldNodeGroupNameTag, api.OldNodeGroupIDTag:
return *tag.Value
}
}
return ""
}