-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
get.go
354 lines (298 loc) · 10 KB
/
get.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
package nodegroup
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/eks"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/kris-nova/logger"
"github.com/tidwall/gjson"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
"github.com/weaveworks/eksctl/pkg/cfn/outputs"
kubewrapper "github.com/weaveworks/eksctl/pkg/kubernetes"
)
const (
imageIDPath = "Resources.NodeGroupLaunchTemplate.Properties.LaunchTemplateData.ImageId"
resourcesRootPath = "Resources"
)
// Summary represents a summary of a nodegroup stack
type Summary 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
Version string
NodeGroupType api.NodeGroupType `json:"Type"`
}
func (m *Manager) GetAll(ctx context.Context) ([]*Summary, error) {
unmanagedSummaries, err := m.getUnmanagedSummaries(ctx)
if err != nil {
return nil, err
}
managedSummaries, err := m.getManagedSummaries(ctx)
if err != nil {
return nil, err
}
return append(unmanagedSummaries, managedSummaries...), nil
}
func (m *Manager) Get(ctx context.Context, name string) (*Summary, error) {
summary, err := m.getUnmanagedSummary(ctx, name)
if err != nil && !manager.IsStackDoesNotExistError(err) {
return nil, fmt.Errorf("getting nodegroup stack summaries: %w", err)
}
if summary != nil {
return summary, nil
}
return m.getManagedSummary(ctx, name)
}
func (m *Manager) getManagedSummaries(ctx context.Context) ([]*Summary, error) {
var summaries []*Summary
managedNodeGroups, err := m.ctl.AWSProvider.EKS().ListNodegroups(ctx, &eks.ListNodegroupsInput{
ClusterName: aws.String(m.cfg.Metadata.Name),
})
if err != nil {
return nil, err
}
for _, ngName := range managedNodeGroups.Nodegroups {
summary, err := m.getManagedSummary(ctx, ngName)
if err != nil {
return nil, err
}
summaries = append(summaries, summary)
}
return summaries, nil
}
func (m *Manager) getUnmanagedSummaries(ctx context.Context) ([]*Summary, error) {
stacks, err := m.stackManager.ListNodeGroupStacks(ctx)
if err != nil {
return nil, fmt.Errorf("getting nodegroup stacks: %w", err)
}
// Create an empty array here so that an object is returned rather than null
summaries := make([]*Summary, 0)
for _, s := range stacks {
summary, err := m.unmanagedStackToSummary(ctx, s)
if err != nil {
return nil, err
}
if summary != nil {
summaries = append(summaries, summary)
}
}
return summaries, nil
}
func (m *Manager) getUnmanagedSummary(ctx context.Context, name string) (*Summary, error) {
stack, err := m.stackManager.DescribeNodeGroupStack(ctx, name)
if err != nil {
return nil, err
}
return m.unmanagedStackToSummary(ctx, stack)
}
func (m *Manager) unmanagedStackToSummary(ctx context.Context, s *manager.Stack) (*Summary, error) {
nodeGroupType, err := manager.GetNodeGroupType(s.Tags)
if err != nil {
return nil, err
}
if nodeGroupType != api.NodeGroupTypeUnmanaged {
return nil, nil
}
ngPaths, err := getNodeGroupPaths(s.Tags)
if err != nil {
return nil, err
}
summary, err := m.mapStackToNodeGroupSummary(ctx, s, ngPaths)
if err != nil {
return nil, fmt.Errorf("mapping stack to nodegroup summary: %w", err)
}
summary.NodeGroupType = api.NodeGroupTypeUnmanaged
asgName, err := m.stackManager.GetUnmanagedNodeGroupAutoScalingGroupName(ctx, s)
if err != nil {
return nil, fmt.Errorf("getting autoscalinggroupname: %w", err)
}
summary.AutoScalingGroupName = asgName
scalingGroup, err := m.stackManager.GetAutoScalingGroupDesiredCapacity(ctx, asgName)
if err != nil {
return nil, fmt.Errorf("getting autoscalinggroup desired capacity: %w", err)
}
summary.DesiredCapacity = int(*scalingGroup.DesiredCapacity)
summary.MinSize = int(*scalingGroup.MinSize)
summary.MaxSize = int(*scalingGroup.MaxSize)
if summary.DesiredCapacity > 0 {
summary.Version, err = kubewrapper.GetNodegroupKubernetesVersion(m.clientSet.CoreV1().Nodes(), summary.Name)
if err != nil {
return nil, fmt.Errorf("getting nodegroup's kubernetes version: %w", err)
}
}
return summary, nil
}
func getNodeGroupPaths(tags []types.Tag) (*nodeGroupPaths, error) {
nodeGroupType, err := manager.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)
}
}
type nodeGroupPaths struct {
InstanceType string
DesiredCapacity string
MinSize string
MaxSize string
}
func (m *Manager) mapStackToNodeGroupSummary(ctx context.Context, stack *manager.Stack, ngPaths *nodeGroupPaths) (*Summary, error) {
template, err := m.stackManager.GetStackTemplate(ctx, *stack.StackName)
if err != nil {
return nil, fmt.Errorf("error getting CloudFormation template for stack %s: %w", *stack.StackName, err)
}
summary := &Summary{
StackName: *stack.StackName,
Cluster: getClusterNameTag(stack),
Name: m.stackManager.GetNodeGroupName(stack),
Status: string(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 := manager.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("error collecting Cloudformation outputs for stack %s: %v", *stack.StackName, err)
}
}
summary.NodeInstanceRoleARN = nodeInstanceRoleARN
return summary, nil
}
func getClusterNameTag(s *manager.Stack) string {
for _, tag := range s.Tags {
if *tag.Key == api.ClusterNameTag || *tag.Key == api.OldClusterNameTag {
return *tag.Value
}
}
return ""
}
func (m *Manager) getManagedSummary(ctx context.Context, nodeGroupName string) (*Summary, error) {
var stack *types.Stack
stack, err := m.stackManager.DescribeNodeGroupStack(ctx, nodeGroupName)
if err != nil {
stack = &types.Stack{}
}
describeOutput, err := m.ctl.AWSProvider.EKS().DescribeNodegroup(ctx, &eks.DescribeNodegroupInput{
ClusterName: aws.String(m.cfg.Metadata.Name),
NodegroupName: aws.String(nodeGroupName),
})
if err != nil {
return nil, err
}
ng := describeOutput.Nodegroup
var asgs []string
if ng.Resources != nil {
for _, asg := range ng.Resources.AutoScalingGroups {
asgs = append(asgs, aws.ToString(asg.Name))
}
}
var imageID string
if ng.AmiType == ekstypes.AMITypesCustom {
// ReleaseVersion contains the AMI ID for custom AMIs.
imageID = *ng.ReleaseVersion
} else {
imageID = string(ng.AmiType)
}
return &Summary{
StackName: aws.ToString(stack.StackName),
Name: *ng.NodegroupName,
Cluster: *ng.ClusterName,
Status: string(ng.Status),
MaxSize: int(*ng.ScalingConfig.MaxSize),
MinSize: int(*ng.ScalingConfig.MinSize),
DesiredCapacity: int(*ng.ScalingConfig.DesiredSize),
InstanceType: m.getInstanceTypes(ctx, ng),
ImageID: imageID,
CreationTime: *ng.CreatedAt,
NodeInstanceRoleARN: *ng.NodeRole,
AutoScalingGroupName: strings.Join(asgs, ","),
Version: getOptionalValue(ng.Version),
NodeGroupType: api.NodeGroupTypeManaged,
}, nil
}
func (m *Manager) getInstanceTypes(ctx context.Context, ng *ekstypes.Nodegroup) string {
if len(ng.InstanceTypes) > 0 {
return strings.Join(ng.InstanceTypes, ",")
}
if ng.LaunchTemplate == nil {
logger.Info("no instance type found for nodegroup %q", *ng.NodegroupName)
return "-"
}
resp, err := m.ctl.AWSProvider.EC2().DescribeLaunchTemplateVersions(ctx, &ec2.DescribeLaunchTemplateVersionsInput{
LaunchTemplateId: ng.LaunchTemplate.Id,
})
if err != nil {
return "-"
}
for _, template := range resp.LaunchTemplateVersions {
if strconv.Itoa(int(*template.VersionNumber)) == *ng.LaunchTemplate.Version {
return string(template.LaunchTemplateData.InstanceType)
}
}
logger.Info("no instance type found for nodegroup %q", *ng.NodegroupName)
return "-"
}
func getOptionalValue(v *string) string {
if v == nil {
return "-"
}
return *v
}