-
Notifications
You must be signed in to change notification settings - Fork 24
/
eksclusterprovider.go
620 lines (513 loc) · 18.3 KB
/
eksclusterprovider.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package provider
import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/kubecost/cluster-turndown/v2/pkg/async"
"github.com/kubecost/cluster-turndown/v2/pkg/cluster/helper"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/eks"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const (
LabelEKSNodePool = "eks.amazonaws.com/nodegroup"
)
//--------------------------------------------------------------------------
// EKS NodePool Implementation
//--------------------------------------------------------------------------
// NodePool contains a node pool identifier and the initial number of nodes
// in the pool
type EKSNodePool struct {
ng *eks.Nodegroup
asg *autoscaling.Group
tags map[string]string
}
func (np *EKSNodePool) Name() string { return aws.StringValue(np.ng.NodegroupName) }
func (np *EKSNodePool) Project() string { return "" }
func (np *EKSNodePool) Zone() string { return aws.StringValue(np.asg.AvailabilityZones[0]) }
func (np *EKSNodePool) ClusterID() string { return aws.StringValue(np.ng.ClusterName) }
func (np *EKSNodePool) MinNodes() int32 { return int32(aws.Int64Value(np.asg.MinSize)) }
func (np *EKSNodePool) MaxNodes() int32 { return int32(aws.Int64Value(np.asg.MaxSize)) }
func (np *EKSNodePool) NodeCount() int32 { return int32(aws.Int64Value(np.asg.DesiredCapacity)) }
func (np *EKSNodePool) AutoScaling() bool { return false }
func (np *EKSNodePool) MachineType() string { return aws.StringValue(np.ng.InstanceTypes[0]) }
func (np *EKSNodePool) Tags() map[string]string { return np.tags }
func (np *EKSNodePool) IsMaster() bool { return false }
//--------------------------------------------------------------------------
// EKS ClusterProvider Implementation
//--------------------------------------------------------------------------
// EKSClusterData is used to store cluster specific data
type EKSClusterData struct {
ClusterName string
NodeRole string
SubnetIDs []string
Cluster *eks.Cluster
}
// ClusterProvider implementation for EKS
type EKSClusterProvider struct {
kubernetes kubernetes.Interface
clusterManager *eks.EKS
asgManager *autoscaling.AutoScaling
clusterData *EKSClusterData
log zerolog.Logger
// Getting a list of node pools can be somewhat expensive for EKS
// due to the nature of abstraction used by AWS. It's better if we
// cache the last known list of nodepools for access on other helper
// functions, but always overwrite the cache on GetNodePools().
npLock *sync.RWMutex
nodePools []*EKSNodePool
}
// NewEKSClusterProvider creates a new EKSClusterProvider instance as the ClusterProvider
func NewEKSClusterProvider(kubernetes kubernetes.Interface) (ClusterProvider, error) {
region := findAWSRegion(kubernetes)
clusterManager, asgManager, err := newEKSClusterManager(region)
if err != nil {
return nil, err
}
cp := &EKSClusterProvider{
kubernetes: kubernetes,
clusterManager: clusterManager,
asgManager: asgManager,
npLock: new(sync.RWMutex),
nodePools: []*EKSNodePool{},
log: log.With().Str("component", "EKSClusterProvider").Logger(),
}
err = cp.initClusterData()
if err != nil {
return nil, err
}
return cp, nil
}
// IsNodePool determines if there is a node pool with the name or not.
func (p *EKSClusterProvider) IsNodePool(name string) bool {
resp, err := p.clusterManager.DescribeNodegroup(&eks.DescribeNodegroupInput{
ClusterName: aws.String(p.clusterData.ClusterName),
NodegroupName: aws.String(name),
})
if err != nil {
return false
}
return resp.Nodegroup != nil
}
// GetNodePoolName returns the name of a NodePool for a specific kubernetes node.
func (p *EKSClusterProvider) GetNodePoolName(node *v1.Node) string {
nodePoolName, _, _ := eksInstanceInfoFor(node)
return nodePoolName
}
// GetNodesFor returns a slice of kubernetes Node instances for the NodePool instance
// provided.
func (p *EKSClusterProvider) GetNodesFor(np NodePool) ([]*v1.Node, error) {
name := np.Name()
allNodes, err := p.kubernetes.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
// NOTE: We just use the nodepool name comparison here. If we want to be
// NOTE: exact, we could dive into the instance ids for the autoscaling groups,
// NOTE: but is there any reason to exact match?
nodes := []*v1.Node{}
for _, n := range allNodes.Items {
node := helper.NodePtr(n)
nodePool, _, _ := eksInstanceInfoFor(node)
if strings.EqualFold(nodePool, name) {
nodes = append(nodes, node)
}
}
return nodes, nil
}
// GetNodePools loads all of the provider NodePools in a cluster and returns them.
func (p *EKSClusterProvider) GetNodePools() ([]NodePool, error) {
nodePools := []NodePool{}
ngNames, err := p.clusterManager.ListNodegroups(&eks.ListNodegroupsInput{
ClusterName: aws.String(p.clusterData.ClusterName),
})
if err != nil {
return nodePools, err
}
for _, ngName := range ngNames.Nodegroups {
ngResp, err := p.clusterManager.DescribeNodegroup(&eks.DescribeNodegroupInput{
ClusterName: aws.String(p.clusterData.ClusterName),
NodegroupName: ngName,
})
if err != nil {
return nodePools, err
}
nodeGroup := ngResp.Nodegroup
asgResp, err := p.asgManager.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{nodeGroup.Resources.AutoScalingGroups[0].Name},
})
if err != nil {
return nodePools, err
}
asg := asgResp.AutoScalingGroups[0]
nodePools = append(nodePools, &EKSNodePool{
ng: nodeGroup,
asg: asg,
tags: p.mergeLabelsAndTags(nodeGroup),
})
}
// overwrite the node pool cache each time we make this call
// the cache provides support to functionality leveraging the
// node pool list without having to reload the list each time.
p.cacheNodePools(nodePools)
return nodePools, nil
}
// updates the cached node pools for the provider
func (p *EKSClusterProvider) cacheNodePools(nodePools []NodePool) {
eksNodePools := make([]*EKSNodePool, len(nodePools))
for i, np := range nodePools {
eksNodePools[i] = np.(*EKSNodePool)
}
p.npLock.Lock()
defer p.npLock.Unlock()
p.nodePools = eksNodePools
}
// CreateNodePool creates a new node pool with the provided specs.
func (p *EKSClusterProvider) CreateNodePool(c context.Context, name, machineType string, nodeCount int32, diskType string, diskSizeGB int32, labels map[string]string) error {
if diskSizeGB == 0 {
diskSizeGB = 20
}
if labels == nil {
labels = map[string]string{}
}
_, err := p.clusterManager.CreateNodegroupWithContext(c, &eks.CreateNodegroupInput{
ClusterName: aws.String(p.clusterData.ClusterName),
NodeRole: aws.String(p.clusterData.NodeRole),
NodegroupName: aws.String(name),
InstanceTypes: aws.StringSlice([]string{machineType}),
Subnets: aws.StringSlice(p.clusterData.SubnetIDs),
DiskSize: aws.Int64(int64(diskSizeGB)),
Labels: aws.StringMap(labels),
ScalingConfig: &eks.NodegroupScalingConfig{
DesiredSize: aws.Int64(int64(nodeCount)),
MinSize: aws.Int64(int64(nodeCount)),
MaxSize: aws.Int64(int64(nodeCount)),
},
})
if err != nil {
return err
}
// Instead of our kubernetes helper here, we can utilize the aws client to wait for the nodegroup to be
// active
return p.clusterManager.WaitUntilNodegroupActiveWithContext(c, &eks.DescribeNodegroupInput{
ClusterName: aws.String(p.clusterData.ClusterName),
NodegroupName: aws.String(name),
})
}
// CreateAutoScalingNodePool creates a new autoscaling node pool. The semantics behind autoscaling depend on the provider.
func (p *EKSClusterProvider) CreateAutoScalingNodePool(c context.Context, name, machineType string, minNodes, nodeCount, maxNodes int32, diskType string, diskSizeGB int32, labels map[string]string) error {
if diskSizeGB == 0 {
diskSizeGB = 20
}
if labels == nil {
labels = map[string]string{}
}
_, err := p.clusterManager.CreateNodegroupWithContext(c, &eks.CreateNodegroupInput{
ClusterName: aws.String(p.clusterData.ClusterName),
NodeRole: aws.String(p.clusterData.NodeRole),
NodegroupName: aws.String(name),
InstanceTypes: aws.StringSlice([]string{machineType}),
Subnets: aws.StringSlice(p.clusterData.SubnetIDs),
DiskSize: aws.Int64(int64(diskSizeGB)),
Labels: aws.StringMap(labels),
ScalingConfig: &eks.NodegroupScalingConfig{
DesiredSize: aws.Int64(int64(nodeCount)),
MinSize: aws.Int64(int64(minNodes)),
MaxSize: aws.Int64(int64(maxNodes)),
},
})
if err != nil {
return err
}
// Wait for the nodegroup to be active
return p.clusterManager.WaitUntilNodegroupActiveWithContext(c, &eks.DescribeNodegroupInput{
ClusterName: aws.String(p.clusterData.ClusterName),
NodegroupName: aws.String(name),
})
}
// UpdateNodePoolSize updates the number of nodes in a NodePool
func (p *EKSClusterProvider) UpdateNodePoolSize(c context.Context, nodePool NodePool, size int32) error {
// NOTE: NodeGroups on EKS have a hard size limit of 1, even though AutoScalingGroups allow size: 0.
// NOTE: To work-around this, we can just update the autoscaling group and the nodegroup parameters will
// NOTE: automatically adjust.
if size == 0 {
eksNodePool, ok := nodePool.(*EKSNodePool)
if !ok {
return fmt.Errorf("Failed to update node pool size. NodePool was not an EKSNodePool.")
}
asgName := aws.StringValue(eksNodePool.asg.AutoScalingGroupName)
_, err := p.asgManager.UpdateAutoScalingGroupWithContext(c, &autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: aws.String(asgName),
DesiredCapacity: aws.Int64(int64(size)),
MinSize: aws.Int64(int64(size)),
MaxSize: aws.Int64(int64(size)),
})
if err != nil {
return err
}
return nil
}
// Any other sizes other than 0 can be made directly with the EKS API
resp, err := p.clusterManager.UpdateNodegroupConfigWithContext(c, &eks.UpdateNodegroupConfigInput{
ClusterName: aws.String(nodePool.ClusterID()),
NodegroupName: aws.String(nodePool.Name()),
ScalingConfig: &eks.NodegroupScalingConfig{
DesiredSize: aws.Int64(int64(size)),
MinSize: aws.Int64(int64(size)),
MaxSize: aws.Int64(int64(size)),
},
})
if err != nil {
// TODO: look into reporting errors from the update object
var _ = resp.Update
}
return err
}
// UpdateNodePoolSizes updates the number of nodes in multiple NodePool instances.
func (p *EKSClusterProvider) UpdateNodePoolSizes(c context.Context, nodePools []NodePool, size int32) error {
if len(nodePools) == 0 {
return nil
}
ctx, cancel := context.WithCancel(c)
waitChannel := async.NewWaitChannel()
waitChannel.Add(len(nodePools))
for _, np := range nodePools {
go func(nodePool NodePool) {
defer waitChannel.Done()
p.UpdateNodePoolSize(ctx, nodePool, size)
}(np)
}
defer cancel()
select {
case <-waitChannel.Wait():
return nil
case <-time.After(30 * time.Minute):
return fmt.Errorf("Resize Requests timed out after 30 minutes.")
}
}
// DeleteNodePool deletes the NodePool.
func (p *EKSClusterProvider) DeleteNodePool(c context.Context, nodePool NodePool) error {
_, err := p.clusterManager.DeleteNodegroupWithContext(c, &eks.DeleteNodegroupInput{
ClusterName: aws.String(nodePool.ClusterID()),
NodegroupName: aws.String(nodePool.Name()),
})
if err != nil {
return err
}
return p.clusterManager.WaitUntilNodegroupDeletedWithContext(c, &eks.DescribeNodegroupInput{
ClusterName: aws.String(nodePool.ClusterID()),
NodegroupName: aws.String(nodePool.Name()),
})
}
// CreateOrUpdateTags creates or updates the tags for NodePool instances.
func (p *EKSClusterProvider) CreateOrUpdateTags(c context.Context, nodePool NodePool, updateNodes bool, tags map[string]string) error {
eksNodePool, ok := nodePool.(*EKSNodePool)
if !ok {
return fmt.Errorf("NodePool provided was not from EKS.")
}
// Default underlying labels and tags if nil
if eksNodePool.ng.Labels == nil {
eksNodePool.ng.Labels = make(map[string]*string)
}
if eksNodePool.ng.Tags == nil {
eksNodePool.ng.Tags = make(map[string]*string)
}
// For Updating Node Labels, we just update the nodegroup config labels
if updateNodes {
_, err := p.clusterManager.UpdateNodegroupConfigWithContext(c, &eks.UpdateNodegroupConfigInput{
ClusterName: aws.String(nodePool.ClusterID()),
Labels: &eks.UpdateLabelsPayload{
AddOrUpdateLabels: aws.StringMap(tags),
},
})
for k, v := range tags {
eksNodePool.ng.Labels[k] = aws.String(v)
nodePool.Tags()[k] = v
}
return err
}
// For updating the tags, we apply the tags directly to the resource
_, err := p.clusterManager.TagResourceWithContext(c, &eks.TagResourceInput{
ResourceArn: eksNodePool.ng.NodegroupArn,
Tags: aws.StringMap(tags),
})
if err != nil {
return err
}
for k, v := range tags {
eksNodePool.ng.Tags[k] = aws.String(v)
nodePool.Tags()[k] = v
}
return nil
}
// DeleteTags deletes the tags by key on a NodePool instance.
func (p *EKSClusterProvider) DeleteTags(c context.Context, nodePool NodePool, keys []string) error {
eksNodePool, ok := nodePool.(*EKSNodePool)
if !ok {
return fmt.Errorf("NodePool provided was not from EKS.")
}
// Default underlying labels and tags if nil
if eksNodePool.ng.Labels == nil {
eksNodePool.ng.Labels = make(map[string]*string)
}
if eksNodePool.ng.Tags == nil {
eksNodePool.ng.Tags = make(map[string]*string)
}
labels := eksNodePool.ng.Labels
tags := eksNodePool.ng.Tags
labelsToDelete := []string{}
tagsToDelete := []string{}
for _, key := range keys {
if _, ok := labels[key]; ok {
labelsToDelete = append(labelsToDelete, key)
}
if _, ok := tags[key]; ok {
tagsToDelete = append(tagsToDelete, key)
}
}
if len(tagsToDelete) > 0 {
_, err := p.clusterManager.UntagResourceWithContext(c, &eks.UntagResourceInput{
ResourceArn: eksNodePool.ng.NodegroupArn,
TagKeys: aws.StringSlice(tagsToDelete),
})
if err != nil {
return err
}
}
if len(labelsToDelete) > 0 {
_, err := p.clusterManager.UpdateNodegroupConfigWithContext(c, &eks.UpdateNodegroupConfigInput{
ClusterName: aws.String(nodePool.ClusterID()),
Labels: &eks.UpdateLabelsPayload{
RemoveLabels: aws.StringSlice(labelsToDelete),
},
})
if err != nil {
return err
}
}
for _, k := range keys {
delete(nodePool.Tags(), k)
delete(eksNodePool.ng.Labels, k)
delete(eksNodePool.ng.Tags, k)
}
return nil
}
// Creates a new EKS based cluster manager API to execute cluster commands
func newEKSClusterManager(region string) (*eks.EKS, *autoscaling.AutoScaling, error) {
accessKey, err := loadAWSAccessKey(AWSAccessKey)
if err == nil {
os.Setenv(AWSAccessKeyID, accessKey.AccessKeyID)
os.Setenv(AWSSecretAccessKey, accessKey.SecretAccessKey)
} else {
log.Warn().Msgf("Failed to load valid access key from secret. Err=%s", err)
}
c := aws.NewConfig().
WithRegion(region).
WithCredentialsChainVerboseErrors(true)
clusterManager := eks.New(session.New(c))
asgManager := autoscaling.New(session.New(c))
return clusterManager, asgManager, nil
}
// initClusterData looks up specific cluster data for use with all EKS APIs.
func (p *EKSClusterProvider) initClusterData() error {
currentNodeName := os.Getenv("NODE_NAME")
if currentNodeName == "" {
return fmt.Errorf("NODE_NAME env variable was not set.")
}
currentNode, err := p.kubernetes.CoreV1().Nodes().Get(context.TODO(), currentNodeName, metav1.GetOptions{})
if err != nil {
return err
}
nodePoolName, _, instanceID := eksInstanceInfoFor(currentNode)
clustersResp, err := p.clusterManager.ListClusters(&eks.ListClustersInput{})
if err != nil {
return err
}
var (
clusterName *string
nodeRole string
)
clusterNames := aws.StringValueSlice(clustersResp.Clusters)
for _, cName := range clusterNames {
nodeGroupResp, err := p.clusterManager.DescribeNodegroup(&eks.DescribeNodegroupInput{
ClusterName: aws.String(cName),
NodegroupName: aws.String(nodePoolName),
})
if err != nil || nodeGroupResp.Nodegroup == nil {
log.Info().Msgf("Could not find NodeGroup: %s in Cluster: %s", nodePoolName, cName)
continue
}
ng := nodeGroupResp.Nodegroup
if p.hasInstance(ng, instanceID) {
clusterName = aws.String(cName)
nodeRole = aws.StringValue(ng.NodeRole)
break
}
}
if clusterName == nil {
return fmt.Errorf("Failed to locate Clusters which have node groups containing the current instance: %s", instanceID)
}
dcr, err := p.clusterManager.DescribeCluster(&eks.DescribeClusterInput{
Name: clusterName,
})
if err != nil {
return err
}
p.clusterData = &EKSClusterData{
ClusterName: aws.StringValue(clusterName),
NodeRole: nodeRole,
SubnetIDs: aws.StringValueSlice(dcr.Cluster.ResourcesVpcConfig.SubnetIds),
Cluster: dcr.Cluster,
}
return nil
}
// hasInstance determines if the autoscaling group fronted by the nodegroup contains
// node instances equal to the provided instanceID.
func (p *EKSClusterProvider) hasInstance(ng *eks.Nodegroup, instanceID string) bool {
asgNames := []*string{}
for _, asgShim := range ng.Resources.AutoScalingGroups {
asgNames = append(asgNames, asgShim.Name)
}
asgResp, err := p.asgManager.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: asgNames,
})
if err != nil {
log.Info().Msgf("Could not load autoscaling groups for names: %+v", aws.StringValueSlice(asgNames))
return false
}
for _, group := range asgResp.AutoScalingGroups {
for _, instance := range group.Instances {
if strings.EqualFold(aws.StringValue(instance.InstanceId), instanceID) {
return true
}
}
}
return false
}
// helper function that merges nodepool labels (appear on nodes) and tags (specific to aws)
func (p *EKSClusterProvider) mergeLabelsAndTags(ng *eks.Nodegroup) map[string]string {
m := make(map[string]string)
for k, v := range aws.StringValueMap(ng.Labels) {
m[k] = v
}
for k, v := range aws.StringValueMap(ng.Tags) {
m[k] = v
}
return m
}
// Pulls the instance id and zone from the Node.Spec.ProviderID
func eksInstanceInfoFor(node *v1.Node) (nodePoolName string, zone string, instanceID string) {
name := node.Labels[LabelEKSNodePool]
id := node.Spec.ProviderID
splitted := strings.Split(id[7:], "/")
return name, splitted[0], splitted[1]
}