/
add-on-node-groups.go
439 lines (398 loc) · 17.2 KB
/
add-on-node-groups.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
package eksconfig
import (
"errors"
"fmt"
"path"
"path/filepath"
"strings"
"github.com/aws/aws-k8s-tester/ec2config"
"github.com/aws/aws-k8s-tester/pkg/timeutil"
"github.com/aws/aws-sdk-go/service/eks"
)
// AddOnNodeGroups defines parameters for EKS "Managed Node Group" creation.
// ref. https://docs.aws.amazon.com/eks/latest/userguide/create-managed-node-group.html
// ref. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eks-nodegroup.html
type AddOnNodeGroups struct {
// Enable is true to auto-create ad node group.
Enable bool `json:"enable"`
// Created is true when the resource has been created.
// Used for delete operations.
Created bool `json:"created" read-only:"true"`
TimeFrameCreate timeutil.TimeFrame `json:"time-frame-create" read-only:"true"`
TimeFrameDelete timeutil.TimeFrame `json:"time-frame-delete" read-only:"true"`
// S3Dir is the S3 directory to store all test results.
// It is under the bucket "eksconfig.Config.S3BucketName".
S3Dir string `json:"s3-dir"`
// FetchLogs is true to fetch logs from remote nodes using SSH.
FetchLogs bool `json:"fetch-logs"`
// RoleName is the name of thed node group.
RoleName string `json:"role-name"`
// RoleCreate is true to auto-create and delete role.
RoleCreate bool `json:"role-create"`
// RoleARN is the role ARN that EKS node group uses to create AWS
// resources for Kubernetes.
// By default, it's empty which triggers tester to create one.
RoleARN string `json:"role-arn"`
// RoleServicePrincipals is the node group Service Principals
RoleServicePrincipals []string `json:"role-service-principals"`
// RoleManagedPolicyARNs is node groupd policy ARNs.
RoleManagedPolicyARNs []string `json:"role-managed-policy-arns"`
RoleCFNStackID string `json:"role-cfn-stack-id" read-only:"true"`
RoleCFNStackYAMLPath string `json:"role-cfn-stack-yaml-path" read-only:"true"`
RoleCFNStackYAMLS3Key string `json:"role-cfn-stack-yaml-s3-key" read-only:"true"`
// NodeGroupSecurityGroupID is the security group ID for the node group.
NodeGroupSecurityGroupID string `json:"node-group-security-group-id" read-only:"true"`
NodeGroupSecurityGroupCFNStackID string `json:"node-group-security-group-cfn-stack-id" read-only:"true"`
NodeGroupSecurityGroupCFNStackYAMLPath string `json:"node-group-security-group-cfn-stack-yaml-path" read-only:"true"`
NodeGroupSecurityGroupCFNStackYAMLS3Key string `json:"node-group-security-group-cfn-stack-yaml-s3-key" read-only:"true"`
// LogsDir is set to specify the target directory to store all remote log files.
// If empty, it stores in the same directory as "ConfigPath".
LogsDir string `json:"logs-dir,omitempty"`
// LogsTarGzPath is the .tar.gz archived file for "LogsDir".
LogsTarGzPath string `json:"logs-tar-gz-path"`
// ASGs maps from EKS Node Group name to "ASG".
// "GetRef.Name" is the reserved key and NG name from eksconfig.Config.Name.
ASGs map[string]ASG `json:"asgs,omitempty"`
}
// NGClusterAutoscaler represents cluster auto-scaler.
// ref. https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler
type NGClusterAutoscaler struct {
Enable bool `json:"enable"`
// Created is true when the resource has been created.
// Used for delete operations.
Created bool `json:"created" read-only:"true"`
TimeFrameCreate timeutil.TimeFrame `json:"time-frame-create" read-only:"true"`
}
// ASG represents an EKS Node Group ASG.
type ASG struct {
ec2config.ASG
// KubeletExtraArgs represents "--kubelet-extra-args".
// e.g. '--kubelet-extra-args --node-labels=nodesgroup=main,subnets=private'
// e.g. '--kubelet-extra-args --hostname-override=string'
// ref. https://github.com/awslabs/amazon-eks-ami/blob/master/files/bootstrap.sh
//
// TODO: handle conflicting flag '--cloud-provider aws'
// ref. https://github.com/kubernetes/kubernetes/issues/64659
KubeletExtraArgs string `json:"kubelet-extra-args"`
// BootstrapArgs additional bootstrap arguments.
// e.g. '--pause-container-account 012345678901 --pause-container-version 3.3'
BootstrapArgs string `json:"bootstrap-args"`
// ClusterAutoscaler is enabled to run cluster auto-scaler per node group.
// ref. https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler
ClusterAutoscaler *NGClusterAutoscaler `json:"cluster-autoscaler,omitempty"`
}
// EnvironmentVariablePrefixAddOnNodeGroups is the environment variable prefix used for "eksconfig".
const EnvironmentVariablePrefixAddOnNodeGroups = AWS_K8S_TESTER_EKS_PREFIX + "ADD_ON_NODE_GROUPS_"
// IsEnabledAddOnNodeGroups returns true if "AddOnNodeGroups" is enabled.
// Otherwise, nil the field for "omitempty".
func (cfg *Config) IsEnabledAddOnNodeGroups() bool {
if cfg.AddOnNodeGroups == nil {
return false
}
if cfg.AddOnNodeGroups.Enable {
return len(cfg.AddOnNodeGroups.ASGs) > 0
}
cfg.AddOnNodeGroups = nil
return false
}
func getDefaultAddOnNodeGroups(name string) *AddOnNodeGroups {
return &AddOnNodeGroups{
Enable: false,
FetchLogs: false,
RoleCreate: true,
LogsDir: "", // to be auto-generated
ASGs: map[string]ASG{
name + "-ng-asg-cpu": {
ASG: ec2config.ASG{
Name: name + "-ng-asg-cpu",
SSMDocumentCreate: false,
SSMDocumentName: "",
SSMDocumentCommands: "",
SSMDocumentExecutionTimeoutSeconds: 3600,
RemoteAccessUserName: "ec2-user", // assume Amazon Linux 2
AMIType: eks.AMITypesAl2X8664,
ImageID: "",
ImageIDSSMParameter: "/aws/service/eks/optimized-ami/1.16/amazon-linux-2/recommended/image_id",
InstanceTypes: []string{DefaultNodeInstanceTypeCPU},
VolumeSize: DefaultNodeVolumeSize,
ASGMinSize: 1,
ASGMaxSize: 1,
ASGDesiredCapacity: 1,
},
KubeletExtraArgs: "",
BootstrapArgs: "",
ClusterAutoscaler: &NGClusterAutoscaler{Enable: false},
},
},
}
}
func (cfg *Config) validateAddOnNodeGroups() error {
if !cfg.IsEnabledAddOnNodeGroups() {
return nil
}
n := len(cfg.AddOnNodeGroups.ASGs)
if n == 0 {
return errors.New("empty ASGs")
}
if n > NGsMaxLimit {
return fmt.Errorf("NGs %d exceeds maximum number of NGs which is %d", n, NGsMaxLimit)
}
if cfg.Parameters.VersionValue < 1.14 {
return fmt.Errorf("Version %q not supported for AddOnNodeGroups", cfg.Parameters.Version)
}
if cfg.AddOnNodeGroups.S3Dir == "" {
cfg.AddOnNodeGroups.S3Dir = path.Join(cfg.Name, "add-on-node-groups")
}
if cfg.AddOnNodeGroups.LogsDir == "" {
cfg.AddOnNodeGroups.LogsDir = filepath.Join(filepath.Dir(cfg.ConfigPath), cfg.Name+"-logs-ngs")
}
if cfg.AddOnNodeGroups.LogsTarGzPath == "" {
cfg.AddOnNodeGroups.LogsTarGzPath = filepath.Join(filepath.Dir(cfg.ConfigPath), cfg.Name+"-logs-ngs.tar.gz")
}
if !strings.HasSuffix(cfg.AddOnNodeGroups.LogsTarGzPath, ".tar.gz") {
return fmt.Errorf("AddOnNodeGroups.LogsTarGzPath %q must end with .tar.gz", cfg.AddOnNodeGroups.LogsTarGzPath)
}
if cfg.AddOnNodeGroups.NodeGroupSecurityGroupCFNStackYAMLPath == "" {
cfg.AddOnNodeGroups.NodeGroupSecurityGroupCFNStackYAMLPath = strings.ReplaceAll(cfg.ConfigPath, ".yaml", "") + ".add-on-node-groups.sg.cfn.yaml"
}
if cfg.AddOnNodeGroups.NodeGroupSecurityGroupCFNStackYAMLS3Key == "" {
cfg.AddOnNodeGroups.NodeGroupSecurityGroupCFNStackYAMLS3Key = path.Join(
cfg.AddOnNodeGroups.S3Dir,
filepath.Base(cfg.AddOnNodeGroups.NodeGroupSecurityGroupCFNStackYAMLPath),
)
}
if cfg.AddOnNodeGroups.RoleCFNStackYAMLPath == "" {
cfg.AddOnNodeGroups.RoleCFNStackYAMLPath = strings.ReplaceAll(cfg.ConfigPath, ".yaml", "") + ".add-on-node-groups.role.cfn.yaml"
}
if cfg.AddOnNodeGroups.RoleCFNStackYAMLS3Key == "" {
cfg.AddOnNodeGroups.RoleCFNStackYAMLS3Key = path.Join(
cfg.AddOnNodeGroups.S3Dir,
filepath.Base(cfg.AddOnNodeGroups.RoleCFNStackYAMLPath),
)
}
switch cfg.AddOnNodeGroups.RoleCreate {
case true: // need create one, or already created
if cfg.AddOnNodeGroups.RoleName == "" {
cfg.AddOnNodeGroups.RoleName = cfg.Name + "-ng-role"
}
if cfg.AddOnNodeGroups.RoleARN != "" {
// just ignore...
// could be populated from previous run
// do not error, so long as RoleCreate false, role won't be deleted
}
if len(cfg.AddOnNodeGroups.RoleServicePrincipals) > 0 {
/*
(InvalidParameterException: Following required service principals [ec2.amazonaws.com] were not found in the trust relationships of nodeRole arn:aws:iam::...:role/test-ng-role
{
ClusterName: "test",
Message_: "Following required service principals [ec2.amazonaws.com] were not found in the trust relationships of nodeRole arn:aws:iam::...:role/test-ng-role",
NodegroupName: "test-ng-cpu"
})
*/
found := false
validSps := []string{"ec2.amazonaws.com", "ec2.amazonaws.com.cn", "ec2.c2s.ic.gov", "ec2.sc2s.sgov.gov"}
for _, pv := range cfg.AddOnNodeGroups.RoleServicePrincipals {
for _, vsp := range validSps {
if pv == vsp {
found = true
break
}
}
}
if !found {
return fmt.Errorf("AddOnNodeGroups.RoleServicePrincipals %q must include one of: %q", cfg.AddOnNodeGroups.RoleServicePrincipals, validSps)
}
}
case false: // use existing one
if cfg.AddOnNodeGroups.RoleARN == "" {
return fmt.Errorf("AddOnNodeGroups.RoleCreate false; expect non-empty RoleARN but got %q", cfg.AddOnNodeGroups.RoleARN)
}
if cfg.AddOnNodeGroups.RoleName == "" {
cfg.AddOnNodeGroups.RoleName = getNameFromARN(cfg.AddOnNodeGroups.RoleARN)
}
if len(cfg.AddOnNodeGroups.RoleManagedPolicyARNs) > 0 {
return fmt.Errorf("AddOnNodeGroups.RoleCreate false; expect empty RoleManagedPolicyARNs but got %q", cfg.AddOnNodeGroups.RoleManagedPolicyARNs)
}
if len(cfg.AddOnNodeGroups.RoleServicePrincipals) > 0 {
return fmt.Errorf("AddOnNodeGroups.RoleCreate false; expect empty RoleServicePrincipals but got %q", cfg.AddOnNodeGroups.RoleServicePrincipals)
}
if cfg.IsEnabledAddOnStresserRemote() {
return errors.New("'AddOnStresserRemote.Enable == true' requires 'AddOnNodeGroups.RoleCreate == true' but got 'false'")
}
}
names, processed := make(map[string]struct{}), make(map[string]ASG)
for k, cur := range cfg.AddOnNodeGroups.ASGs {
k = strings.ReplaceAll(k, "GetRef.Name", cfg.Name)
cur.Name = strings.ReplaceAll(cur.Name, "GetRef.Name", cfg.Name)
if cur.Name == "" {
return fmt.Errorf("AddOnNodeGroups.ASGs[%q].Name is empty", k)
}
if k != cur.Name {
return fmt.Errorf("AddOnNodeGroups.ASGs[%q].Name has different Name field %q", k, cur.Name)
}
_, ok := names[cur.Name]
if !ok {
names[cur.Name] = struct{}{}
} else {
return fmt.Errorf("AddOnNodeGroups.ASGs[%q].Name %q is redundant", k, cur.Name)
}
if cur.ASGCFNStackYAMLPath == "" {
cur.ASGCFNStackYAMLPath = strings.ReplaceAll(cfg.ConfigPath, ".yaml", "") + ".asg.cfn." + k + ".yaml"
}
if cur.ASGCFNStackYAMLS3Key == "" {
cur.ASGCFNStackYAMLS3Key = path.Join(
cfg.AddOnNodeGroups.S3Dir,
filepath.Base(cur.ASGCFNStackYAMLPath),
)
}
if len(cur.InstanceTypes) > 4 {
return fmt.Errorf("too many InstaceTypes[%q]", cur.InstanceTypes)
}
if cur.VolumeSize == 0 {
cur.VolumeSize = DefaultNodeVolumeSize
}
if cur.RemoteAccessUserName == "" {
cur.RemoteAccessUserName = "ec2-user"
}
if cur.ImageID == "" && cur.ImageIDSSMParameter == "" {
return fmt.Errorf("%q both ImageID and ImageIDSSMParameter are empty", cur.Name)
}
// prefer "ImageIDSSMParameter"
if cur.ImageID != "" && cur.ImageIDSSMParameter != "" {
cur.ImageID = ""
}
if !cfg.AddOnNodeGroups.RoleCreate {
if cur.ClusterAutoscaler != nil && cur.ClusterAutoscaler.Enable {
return fmt.Errorf("'ASGs[%q].ClusterAutoscaler.Enable == true' requires 'AddOnNodeGroups.RoleCreate == true' but got 'false'", cur.ASG.Name)
}
}
switch cur.AMIType {
case ec2config.AMITypeBottleRocketCPU:
if cur.RemoteAccessUserName != "ec2-user" {
return fmt.Errorf("AMIType %q but unexpected RemoteAccessUserName %q", cur.AMIType, cur.RemoteAccessUserName)
}
if cur.SSMDocumentName != "" && cfg.S3BucketName == "" {
return fmt.Errorf("AMIType %q requires SSMDocumentName %q but no S3BucketName", cur.AMIType, cur.SSMDocumentName)
}
if cur.KubeletExtraArgs != "" {
return fmt.Errorf("AMIType %q but unexpected KubeletExtraArgs %q", cur.AMIType, cur.KubeletExtraArgs)
}
case eks.AMITypesAl2X8664:
if cur.RemoteAccessUserName != "ec2-user" {
return fmt.Errorf("AMIType %q but unexpected RemoteAccessUserName %q", cur.AMIType, cur.RemoteAccessUserName)
}
case eks.AMITypesAl2X8664Gpu:
if cur.RemoteAccessUserName != "ec2-user" {
return fmt.Errorf("AMIType %q but unexpected RemoteAccessUserName %q", cur.AMIType, cur.RemoteAccessUserName)
}
default:
return fmt.Errorf("unknown ASGs[%q].AMIType %q", k, cur.AMIType)
}
switch cur.AMIType {
case ec2config.AMITypeBottleRocketCPU:
if len(cur.InstanceTypes) == 0 {
cur.InstanceTypes = []string{DefaultNodeInstanceTypeCPU}
}
case eks.AMITypesAl2X8664:
if len(cur.InstanceTypes) == 0 {
cur.InstanceTypes = []string{DefaultNodeInstanceTypeCPU}
}
case eks.AMITypesAl2X8664Gpu:
if len(cur.InstanceTypes) == 0 {
cur.InstanceTypes = []string{DefaultNodeInstanceTypeGPU}
}
default:
return fmt.Errorf("unknown AddOnNodeGroups.ASGs[%q].AMIType %q", k, cur.AMIType)
}
if cfg.IsEnabledAddOnNLBHelloWorld() || cfg.IsEnabledAddOnALB2048() {
// "m3.xlarge" or "c4.xlarge" will fail with "InvalidTarget: Targets {...} are not supported"
// ref. https://github.com/aws/amazon-vpc-cni-k8s/pull/821
// ref. https://github.com/kubernetes/kubernetes/issues/66044#issuecomment-408188524
for _, ivt := range cur.InstanceTypes {
switch {
case strings.HasPrefix(ivt, "m3."),
strings.HasPrefix(ivt, "c4."):
return fmt.Errorf("AddOnNLBHelloWorld.Enable[%v] || AddOnALB2048.Enable[%v], but older instance type InstanceType %q for %q",
cfg.IsEnabledAddOnNLBHelloWorld(),
cfg.IsEnabledAddOnALB2048(),
ivt, k)
}
}
}
if cur.ASGMinSize == 0 && cur.ASGDesiredCapacity == 0 {
return fmt.Errorf("AddOnNodeGroups.ASGs[%q].ASGMinSize/ASGDesiredCapacity must be >0", k)
}
if cur.ASGDesiredCapacity > 0 && cur.ASGMinSize == 0 {
cur.ASGMinSize = cur.ASGDesiredCapacity
}
if cur.ASGDesiredCapacity > 0 && cur.ASGMaxSize == 0 {
cur.ASGMaxSize = cur.ASGDesiredCapacity
}
if cur.ASGMinSize > cur.ASGMaxSize {
return fmt.Errorf("AddOnNodeGroups.ASGs[%q].ASGMinSize %d > ASGMaxSize %d", k, cur.ASGMinSize, cur.ASGMaxSize)
}
if cur.ASGDesiredCapacity > cur.ASGMaxSize {
return fmt.Errorf("AddOnNodeGroups.ASGs[%q].ASGDesiredCapacity %d > ASGMaxSize %d", k, cur.ASGDesiredCapacity, cur.ASGMaxSize)
}
if cur.ASGMaxSize > NGMaxLimit {
return fmt.Errorf("AddOnNodeGroups.ASGs[%q].ASGMaxSize %d > NGMaxLimit %d", k, cur.ASGMaxSize, NGMaxLimit)
}
if cur.ASGDesiredCapacity > NGMaxLimit {
return fmt.Errorf("AddOnNodeGroups.ASGs[%q].ASGDesiredCapacity %d > NGMaxLimit %d", k, cur.ASGDesiredCapacity, NGMaxLimit)
}
if cur.SSMDocumentCFNStackYAMLPath == "" {
cur.SSMDocumentCFNStackYAMLPath = strings.ReplaceAll(cfg.ConfigPath, ".yaml", "") + ".ssm.cfn." + k + ".yaml"
}
if cur.SSMDocumentCFNStackYAMLS3Key == "" {
cur.SSMDocumentCFNStackYAMLS3Key = path.Join(
cfg.AddOnNodeGroups.S3Dir,
filepath.Base(cur.SSMDocumentCFNStackYAMLPath),
)
}
switch cur.SSMDocumentCreate {
case true: // need create one, or already created
if cur.SSMDocumentCFNStackName == "" {
cur.SSMDocumentCFNStackName = cur.Name + "-ssm-document"
}
if cur.SSMDocumentName == "" {
cur.SSMDocumentName = cur.Name + "SSMDocument"
}
cur.SSMDocumentCFNStackName = strings.ReplaceAll(cur.SSMDocumentCFNStackName, "GetRef.Name", cfg.Name)
cur.SSMDocumentName = strings.ReplaceAll(cur.SSMDocumentName, "GetRef.Name", cfg.Name)
cur.SSMDocumentName = regex.ReplaceAllString(cur.SSMDocumentName, "")
if cur.SSMDocumentExecutionTimeoutSeconds == 0 {
cur.SSMDocumentExecutionTimeoutSeconds = 3600
}
case false: // use existing one, or don't run any SSM
}
if cfg.IsEnabledAddOnNLBHelloWorld() && cfg.AddOnNLBHelloWorld.DeploymentReplicas < int32(cur.ASGDesiredCapacity) {
cfg.AddOnNLBHelloWorld.DeploymentReplicas = int32(cur.ASGDesiredCapacity)
}
if cfg.IsEnabledAddOnNLBGuestbook() && cfg.AddOnNLBGuestbook.DeploymentReplicas < int32(cur.ASGDesiredCapacity) {
cfg.AddOnNLBGuestbook.DeploymentReplicas = int32(cur.ASGDesiredCapacity)
}
if cfg.IsEnabledAddOnALB2048() && cfg.AddOnALB2048.DeploymentReplicasALB < int32(cur.ASGDesiredCapacity) {
cfg.AddOnALB2048.DeploymentReplicasALB = int32(cur.ASGDesiredCapacity)
}
if cfg.IsEnabledAddOnALB2048() && cfg.AddOnALB2048.DeploymentReplicas2048 < int32(cur.ASGDesiredCapacity) {
cfg.AddOnALB2048.DeploymentReplicas2048 = int32(cur.ASGDesiredCapacity)
}
processed[k] = cur
}
cfg.AddOnNodeGroups.ASGs = processed
return nil
}
func (addOn *AddOnNodeGroups) IsEnabledClusterAutoscaler() bool {
if addOn == nil {
return false
}
if len(addOn.ASGs) == 0 {
return false
}
for _, cur := range addOn.ASGs {
if cur.ClusterAutoscaler != nil && cur.ClusterAutoscaler.Enable {
return true
}
}
return false
}