-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
scale.go
139 lines (111 loc) · 3.3 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
package nodegroup
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/kris-nova/logger"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
"github.com/weaveworks/eksctl/pkg/utils/waiters"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/eks"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
)
func (m *Manager) Scale(ng *api.NodeGroup) error {
logger.Info("scaling nodegroup %q in cluster %s", ng.Name, m.cfg.Metadata.Name)
nodegroupStackInfos, err := m.stackManager.DescribeNodeGroupStacksAndResources()
if err != nil {
return err
}
var stackInfo manager.StackInfo
var ok, isUnmanagedNodegroup bool
stackInfo, ok = nodegroupStackInfos[ng.Name]
if ok {
nodegroupType, err := manager.GetNodeGroupType(stackInfo.Stack.Tags)
if err != nil {
return err
}
isUnmanagedNodegroup = nodegroupType == api.NodeGroupTypeUnmanaged
}
if isUnmanagedNodegroup {
err = m.scaleUnmanagedNodeGroup(ng, stackInfo)
} else {
err = m.scaleManagedNodeGroup(ng)
}
if err != nil {
return fmt.Errorf("failed to scale nodegroup for cluster %q, error: %v", m.cfg.Metadata.Name, err)
}
return nil
}
func (m *Manager) scaleUnmanagedNodeGroup(ng *api.NodeGroup, stackInfo manager.StackInfo) error {
asgName := ""
for _, resource := range stackInfo.Resources {
if *resource.LogicalResourceId == "NodeGroup" {
asgName = *resource.PhysicalResourceId
break
}
}
if asgName == "" {
return fmt.Errorf("failed to find NodeGroup auto scaling group")
}
input := &autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: &asgName,
}
if ng.MaxSize != nil {
input.MaxSize = aws.Int64(int64(*ng.MaxSize))
}
if ng.MinSize != nil {
input.MinSize = aws.Int64(int64(*ng.MinSize))
}
if ng.DesiredCapacity != nil {
input.DesiredCapacity = aws.Int64(int64(*ng.DesiredCapacity))
}
out, err := m.ctl.Provider.ASG().UpdateAutoScalingGroup(input)
if err != nil {
logger.Debug("ASG update output: %s", out.String())
return err
}
logger.Info("nodegroup successfully scaled")
return nil
}
func (m *Manager) scaleManagedNodeGroup(ng *api.NodeGroup) error {
scalingConfig := &eks.NodegroupScalingConfig{}
if ng.MaxSize != nil {
scalingConfig.MaxSize = aws.Int64(int64(*ng.MaxSize))
}
if ng.MinSize != nil {
scalingConfig.MinSize = aws.Int64(int64(*ng.MinSize))
}
if ng.DesiredCapacity != nil {
scalingConfig.DesiredSize = aws.Int64(int64(*ng.DesiredCapacity))
}
_, err := m.ctl.Provider.EKS().UpdateNodegroupConfig(&eks.UpdateNodegroupConfigInput{
ScalingConfig: scalingConfig,
ClusterName: &m.cfg.Metadata.Name,
NodegroupName: &ng.Name,
})
if err != nil {
return err
}
newRequest := func() *request.Request {
input := &eks.DescribeNodegroupInput{
ClusterName: &m.cfg.Metadata.Name,
NodegroupName: &ng.Name,
}
req, _ := m.ctl.Provider.EKS().DescribeNodegroupRequest(input)
return req
}
msg := fmt.Sprintf("waiting for scaling of nodegroup %q to complete", ng.Name)
acceptors := waiters.MakeAcceptors(
"Nodegroup.Status",
eks.NodegroupStatusActive,
[]string{
eks.NodegroupStatusDegraded,
},
)
err = m.wait(ng.Name, msg, acceptors, newRequest, m.ctl.Provider.WaitTimeout(), nil)
if err != nil {
return err
}
logger.Info("nodegroup successfully scaled")
return nil
}