-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
managed_nodegroup.go
337 lines (295 loc) · 10.7 KB
/
managed_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
package builder
import (
"context"
"fmt"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/pkg/errors"
gfnec2 "github.com/weaveworks/goformation/v4/cloudformation/ec2"
gfneks "github.com/weaveworks/goformation/v4/cloudformation/eks"
gfnt "github.com/weaveworks/goformation/v4/cloudformation/types"
corev1 "k8s.io/api/core/v1"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/awsapi"
"github.com/weaveworks/eksctl/pkg/nodebootstrap"
instanceutils "github.com/weaveworks/eksctl/pkg/utils/instance"
"github.com/weaveworks/eksctl/pkg/vpc"
)
// ManagedNodeGroupResourceSet defines the CloudFormation resources required for a managed nodegroup
type ManagedNodeGroupResourceSet struct {
clusterConfig *api.ClusterConfig
forceAddCNIPolicy bool
nodeGroup *api.ManagedNodeGroup
launchTemplateFetcher *LaunchTemplateFetcher
ec2API awsapi.EC2
vpcImporter vpc.Importer
bootstrapper nodebootstrap.Bootstrapper
*resourceSet
}
const ManagedNodeGroupResourceName = "ManagedNodeGroup"
// NewManagedNodeGroup creates a new ManagedNodeGroupResourceSet
func NewManagedNodeGroup(ec2API awsapi.EC2, cluster *api.ClusterConfig, nodeGroup *api.ManagedNodeGroup, launchTemplateFetcher *LaunchTemplateFetcher, bootstrapper nodebootstrap.Bootstrapper, forceAddCNIPolicy bool, vpcImporter vpc.Importer) *ManagedNodeGroupResourceSet {
return &ManagedNodeGroupResourceSet{
clusterConfig: cluster,
forceAddCNIPolicy: forceAddCNIPolicy,
nodeGroup: nodeGroup,
launchTemplateFetcher: launchTemplateFetcher,
ec2API: ec2API,
resourceSet: newResourceSet(),
vpcImporter: vpcImporter,
bootstrapper: bootstrapper,
}
}
// AddAllResources adds all required CloudFormation resources
func (m *ManagedNodeGroupResourceSet) AddAllResources(ctx context.Context) error {
m.resourceSet.template.Description = fmt.Sprintf(
"%s (SSH access: %v) %s",
"EKS Managed Nodes",
api.IsEnabled(m.nodeGroup.SSH.Allow),
"[created by eksctl]")
m.template.Mappings[servicePrincipalPartitionMapName] = api.Partitions.ServicePrincipalPartitionMappings()
var nodeRole *gfnt.Value
if m.nodeGroup.IAM.InstanceRoleARN == "" {
if err := createRole(m.resourceSet, m.clusterConfig.IAM, m.nodeGroup.IAM, true, m.forceAddCNIPolicy); err != nil {
return err
}
nodeRole = gfnt.MakeFnGetAttString(cfnIAMInstanceRoleName, "Arn")
} else {
nodeRole = gfnt.NewString(NormalizeARN(m.nodeGroup.IAM.InstanceRoleARN))
}
subnets, err := AssignSubnets(ctx, m.nodeGroup, m.clusterConfig, m.ec2API)
if err != nil {
return err
}
scalingConfig := gfneks.Nodegroup_ScalingConfig{}
if m.nodeGroup.MinSize != nil {
scalingConfig.MinSize = gfnt.NewInteger(*m.nodeGroup.MinSize)
}
if m.nodeGroup.MaxSize != nil {
scalingConfig.MaxSize = gfnt.NewInteger(*m.nodeGroup.MaxSize)
}
if m.nodeGroup.DesiredCapacity != nil {
scalingConfig.DesiredSize = gfnt.NewInteger(*m.nodeGroup.DesiredCapacity)
}
for k, v := range m.clusterConfig.Metadata.Tags {
if _, exists := m.nodeGroup.Tags[k]; !exists {
m.nodeGroup.Tags[k] = v
}
}
taints, err := mapTaints(m.nodeGroup.Taints)
if err != nil {
return err
}
managedResource := &gfneks.Nodegroup{
ClusterName: gfnt.NewString(m.clusterConfig.Metadata.Name),
NodegroupName: gfnt.NewString(m.nodeGroup.Name),
ScalingConfig: &scalingConfig,
Subnets: subnets,
NodeRole: nodeRole,
Labels: m.nodeGroup.Labels,
Tags: m.nodeGroup.Tags,
Taints: taints,
}
if m.nodeGroup.UpdateConfig != nil {
updateConfig := &gfneks.Nodegroup_UpdateConfig{}
if m.nodeGroup.UpdateConfig.MaxUnavailable != nil {
updateConfig.MaxUnavailable = gfnt.NewInteger(*m.nodeGroup.UpdateConfig.MaxUnavailable)
}
if m.nodeGroup.UpdateConfig.MaxUnavailablePercentage != nil {
updateConfig.MaxUnavailablePercentage = gfnt.NewInteger(*m.nodeGroup.UpdateConfig.MaxUnavailablePercentage)
}
managedResource.UpdateConfig = updateConfig
}
if m.nodeGroup.Spot {
// TODO use constant from SDK
managedResource.CapacityType = gfnt.NewString("SPOT")
}
if m.nodeGroup.ReleaseVersion != "" {
managedResource.ReleaseVersion = gfnt.NewString(m.nodeGroup.ReleaseVersion)
}
instanceTypes := m.nodeGroup.InstanceTypeList()
makeAMIType := func() *gfnt.Value {
return gfnt.NewString(string(getAMIType(m.nodeGroup, selectManagedInstanceType(m.nodeGroup))))
}
var launchTemplate *gfneks.Nodegroup_LaunchTemplateSpecification
if m.nodeGroup.LaunchTemplate != nil {
launchTemplateData, err := m.launchTemplateFetcher.Fetch(ctx, m.nodeGroup.LaunchTemplate)
if err != nil {
return err
}
if err := validateLaunchTemplate(launchTemplateData, m.nodeGroup); err != nil {
return err
}
launchTemplate = &gfneks.Nodegroup_LaunchTemplateSpecification{
Id: gfnt.NewString(m.nodeGroup.LaunchTemplate.ID),
}
if version := m.nodeGroup.LaunchTemplate.Version; version != nil {
launchTemplate.Version = gfnt.NewString(*version)
}
if launchTemplateData.ImageId == nil {
if launchTemplateData.InstanceType == "" {
managedResource.AmiType = makeAMIType()
} else {
managedResource.AmiType = gfnt.NewString(string(getAMIType(m.nodeGroup, string(launchTemplateData.InstanceType))))
}
}
if launchTemplateData.InstanceType == "" {
managedResource.InstanceTypes = gfnt.NewStringSlice(instanceTypes...)
}
} else {
launchTemplateData, err := m.makeLaunchTemplateData(ctx)
if err != nil {
return err
}
if launchTemplateData.ImageId == nil {
managedResource.AmiType = makeAMIType()
}
managedResource.InstanceTypes = gfnt.NewStringSlice(instanceTypes...)
ltRef := m.newResource("LaunchTemplate", &gfnec2.LaunchTemplate{
LaunchTemplateName: gfnt.MakeFnSubString(fmt.Sprintf("${%s}", gfnt.StackName)),
LaunchTemplateData: launchTemplateData,
})
launchTemplate = &gfneks.Nodegroup_LaunchTemplateSpecification{
Id: ltRef,
}
}
managedResource.LaunchTemplate = launchTemplate
m.newResource(ManagedNodeGroupResourceName, managedResource)
return nil
}
func mapTaints(taints []api.NodeGroupTaint) ([]gfneks.Nodegroup_Taint, error) {
var ret []gfneks.Nodegroup_Taint
mapEffect := func(effect corev1.TaintEffect) ekstypes.TaintEffect {
switch effect {
case corev1.TaintEffectNoSchedule:
return ekstypes.TaintEffectNoSchedule
case corev1.TaintEffectPreferNoSchedule:
return ekstypes.TaintEffectPreferNoSchedule
case corev1.TaintEffectNoExecute:
return ekstypes.TaintEffectNoExecute
default:
return ""
}
}
for _, t := range taints {
effect := mapEffect(t.Effect)
if effect == "" {
return nil, errors.Errorf("unexpected taint effect: %v", t.Effect)
}
ret = append(ret, gfneks.Nodegroup_Taint{
Key: gfnt.NewString(t.Key),
Value: gfnt.NewString(t.Value),
Effect: gfnt.NewString(string(effect)),
})
}
return ret, nil
}
func selectManagedInstanceType(ng *api.ManagedNodeGroup) string {
if len(ng.InstanceTypes) > 0 {
for _, instanceType := range ng.InstanceTypes {
if instanceutils.IsGPUInstanceType(instanceType) {
return instanceType
}
}
return ng.InstanceTypes[0]
}
return ng.InstanceType
}
func validateLaunchTemplate(launchTemplateData *ec2types.ResponseLaunchTemplateData, ng *api.ManagedNodeGroup) error {
const mngFieldName = "managedNodeGroup"
if launchTemplateData.InstanceType == "" {
if len(ng.InstanceTypes) == 0 {
return errors.Errorf("instance type must be set in the launch template if %s.instanceTypes is not specified", mngFieldName)
}
} else if len(ng.InstanceTypes) > 0 {
return errors.Errorf("instance type must not be set in the launch template if %s.instanceTypes is specified", mngFieldName)
}
// Custom AMI
if launchTemplateData.ImageId != nil {
if launchTemplateData.UserData == nil {
return errors.New("node bootstrapping script (UserData) must be set when using a custom AMI")
}
notSupportedErr := func(fieldName string) error {
return errors.Errorf("cannot set %s.%s when launchTemplate.ImageId is set", mngFieldName, fieldName)
}
if ng.AMI != "" {
return notSupportedErr("ami")
}
if ng.ReleaseVersion != "" {
return notSupportedErr("releaseVersion")
}
}
if launchTemplateData.IamInstanceProfile != nil && launchTemplateData.IamInstanceProfile.Arn != nil {
return errors.New("IAM instance profile must not be set in the launch template")
}
return nil
}
func getAMIType(ng *api.ManagedNodeGroup, instanceType string) ekstypes.AMITypes {
amiTypeMapping := map[string]struct {
X86x64 ekstypes.AMITypes
X86GPU ekstypes.AMITypes
ARM ekstypes.AMITypes
ARMGPU ekstypes.AMITypes
}{
api.NodeImageFamilyAmazonLinux2023: {
X86x64: ekstypes.AMITypesAl2023X8664Standard,
ARM: ekstypes.AMITypesAl2023Arm64Standard,
},
api.NodeImageFamilyAmazonLinux2: {
X86x64: ekstypes.AMITypesAl2X8664,
X86GPU: ekstypes.AMITypesAl2X8664Gpu,
ARM: ekstypes.AMITypesAl2Arm64,
},
api.NodeImageFamilyBottlerocket: {
X86x64: ekstypes.AMITypesBottlerocketX8664,
X86GPU: ekstypes.AMITypesBottlerocketX8664Nvidia,
ARM: ekstypes.AMITypesBottlerocketArm64,
ARMGPU: ekstypes.AMITypesBottlerocketArm64Nvidia,
},
api.NodeImageFamilyWindowsServer2019FullContainer: {
X86x64: ekstypes.AMITypesWindowsFull2019X8664,
X86GPU: ekstypes.AMITypesWindowsFull2019X8664,
},
api.NodeImageFamilyWindowsServer2019CoreContainer: {
X86x64: ekstypes.AMITypesWindowsCore2019X8664,
X86GPU: ekstypes.AMITypesWindowsCore2019X8664,
},
api.NodeImageFamilyWindowsServer2022FullContainer: {
X86x64: ekstypes.AMITypesWindowsFull2022X8664,
X86GPU: ekstypes.AMITypesWindowsFull2022X8664,
},
api.NodeImageFamilyWindowsServer2022CoreContainer: {
X86x64: ekstypes.AMITypesWindowsCore2022X8664,
X86GPU: ekstypes.AMITypesWindowsCore2022X8664,
},
}
amiType, ok := amiTypeMapping[ng.AMIFamily]
if !ok {
return ekstypes.AMITypesCustom
}
switch {
case instanceutils.IsGPUInstanceType(instanceType):
if instanceutils.IsARMInstanceType(instanceType) {
return amiType.ARMGPU
}
return amiType.X86GPU
case instanceutils.IsARMInstanceType(instanceType):
return amiType.ARM
default:
return amiType.X86x64
}
}
// RenderJSON implements the ResourceSet interface
func (m *ManagedNodeGroupResourceSet) RenderJSON() ([]byte, error) {
return m.resourceSet.renderJSON()
}
// WithIAM implements the ResourceSet interface
func (m *ManagedNodeGroupResourceSet) WithIAM() bool {
// eksctl does not support passing pre-created IAM instance roles to Managed Nodes,
// so the IAM capability is always required
return true
}
// WithNamedIAM implements the ResourceSet interface
func (m *ManagedNodeGroupResourceSet) WithNamedIAM() bool {
return m.nodeGroup.IAM.InstanceRoleName != ""
}