/
managednodegroup.go
413 lines (349 loc) · 13.8 KB
/
managednodegroup.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
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scope
import (
"context"
"fmt"
awsclient "github.com/aws/aws-sdk-go/aws/client"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/eks/api/v1beta2"
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/throttle"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
"sigs.k8s.io/cluster-api-provider-aws/v2/util/system"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expclusterv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
)
// ManagedMachinePoolScopeParams defines the input parameters used to create a new Scope.
type ManagedMachinePoolScopeParams struct {
Client client.Client
Logger *logger.Logger
Cluster *clusterv1.Cluster
ControlPlane *ekscontrolplanev1.AWSManagedControlPlane
ManagedMachinePool *expinfrav1.AWSManagedMachinePool
MachinePool *expclusterv1.MachinePool
ControllerName string
Endpoints []ServiceEndpoint
Session awsclient.ConfigProvider
EnableIAM bool
AllowAdditionalRoles bool
InfraCluster EC2Scope
}
// NewManagedMachinePoolScope creates a new Scope from the supplied parameters.
// This is meant to be called for each reconcile iteration.
func NewManagedMachinePoolScope(params ManagedMachinePoolScopeParams) (*ManagedMachinePoolScope, error) {
if params.ControlPlane == nil {
return nil, errors.New("failed to generate new scope from nil AWSManagedMachinePool")
}
if params.MachinePool == nil {
return nil, errors.New("failed to generate new scope from nil MachinePool")
}
if params.ManagedMachinePool == nil {
return nil, errors.New("failed to generate new scope from nil ManagedMachinePool")
}
if params.Logger == nil {
log := klog.Background()
params.Logger = logger.NewLogger(log)
}
managedScope := &ManagedControlPlaneScope{
Logger: *params.Logger,
Client: params.Client,
Cluster: params.Cluster,
ControlPlane: params.ControlPlane,
controllerName: params.ControllerName,
}
session, serviceLimiters, err := sessionForClusterWithRegion(params.Client, managedScope, params.ControlPlane.Spec.Region, params.Endpoints, params.Logger)
if err != nil {
return nil, errors.Errorf("failed to create aws session: %v", err)
}
ammpHelper, err := patch.NewHelper(params.ManagedMachinePool, params.Client)
if err != nil {
return nil, errors.Wrap(err, "failed to init AWSManagedMachinePool patch helper")
}
mpHelper, err := patch.NewHelper(params.MachinePool, params.Client)
if err != nil {
return nil, errors.Wrap(err, "failed to init MachinePool patch helper")
}
return &ManagedMachinePoolScope{
Logger: *params.Logger,
Client: params.Client,
patchHelper: ammpHelper,
capiMachinePoolPatchHelper: mpHelper,
Cluster: params.Cluster,
ControlPlane: params.ControlPlane,
ManagedMachinePool: params.ManagedMachinePool,
MachinePool: params.MachinePool,
EC2Scope: params.InfraCluster,
session: session,
serviceLimiters: serviceLimiters,
controllerName: params.ControllerName,
enableIAM: params.EnableIAM,
allowAdditionalRoles: params.AllowAdditionalRoles,
}, nil
}
// ManagedMachinePoolScope defines the basic context for an actuator to operate upon.
type ManagedMachinePoolScope struct {
logger.Logger
client.Client
patchHelper *patch.Helper
capiMachinePoolPatchHelper *patch.Helper
Cluster *clusterv1.Cluster
ControlPlane *ekscontrolplanev1.AWSManagedControlPlane
ManagedMachinePool *expinfrav1.AWSManagedMachinePool
MachinePool *expclusterv1.MachinePool
EC2Scope EC2Scope
session awsclient.ConfigProvider
serviceLimiters throttle.ServiceLimiters
controllerName string
enableIAM bool
allowAdditionalRoles bool
}
// ManagedPoolName returns the managed machine pool name.
func (s *ManagedMachinePoolScope) ManagedPoolName() string {
return s.ManagedMachinePool.Name
}
// ServiceLimiter returns the AWS SDK session. Used for creating clients.
func (s *ManagedMachinePoolScope) ServiceLimiter(service string) *throttle.ServiceLimiter {
if sl, ok := s.serviceLimiters[service]; ok {
return sl
}
return nil
}
// ClusterName returns the cluster name.
func (s *ManagedMachinePoolScope) ClusterName() string {
return s.ControlPlane.Spec.EKSClusterName
}
// EnableIAM indicates that reconciliation should create IAM roles.
func (s *ManagedMachinePoolScope) EnableIAM() bool {
return s.enableIAM
}
// AllowAdditionalRoles indicates if additional roles can be added to the created IAM roles.
func (s *ManagedMachinePoolScope) AllowAdditionalRoles() bool {
return s.allowAdditionalRoles
}
// Partition returns the machine pool subnet IDs.
func (s *ManagedMachinePoolScope) Partition() string {
return system.GetPartitionFromRegion(s.ControlPlane.Spec.Region)
}
// IdentityRef returns the cluster identityRef.
func (s *ManagedMachinePoolScope) IdentityRef() *infrav1.AWSIdentityReference {
return s.ControlPlane.Spec.IdentityRef
}
// AdditionalTags returns AdditionalTags from the scope's ManagedMachinePool
// The returned value will never be nil.
func (s *ManagedMachinePoolScope) AdditionalTags() infrav1.Tags {
tags := make(infrav1.Tags)
// Start with the cluster-wide tags...
tags.Merge(s.EC2Scope.AdditionalTags())
// ... and merge in the Machine's
tags.Merge(s.ManagedMachinePool.Spec.AdditionalTags)
return tags
}
// RoleName returns the node group role name.
func (s *ManagedMachinePoolScope) RoleName() string {
return s.ManagedMachinePool.Spec.RoleName
}
// Version returns the nodegroup Kubernetes version.
func (s *ManagedMachinePoolScope) Version() *string {
return s.MachinePool.Spec.Template.Spec.Version
}
// ControlPlaneSubnets returns the control plane subnets.
func (s *ManagedMachinePoolScope) ControlPlaneSubnets() infrav1.Subnets {
return s.ControlPlane.Spec.NetworkSpec.Subnets
}
// SubnetIDs returns the machine pool subnet IDs.
func (s *ManagedMachinePoolScope) SubnetIDs() ([]string, error) {
strategy, err := newDefaultSubnetPlacementStrategy(&s.Logger)
if err != nil {
return []string{}, fmt.Errorf("getting subnet placement strategy: %w", err)
}
return strategy.Place(&placementInput{
SpecSubnetIDs: s.ManagedMachinePool.Spec.SubnetIDs,
SpecAvailabilityZones: s.ManagedMachinePool.Spec.AvailabilityZones,
ParentAvailabilityZones: s.MachinePool.Spec.FailureDomains,
ControlplaneSubnets: s.ControlPlaneSubnets(),
SubnetPlacementType: s.ManagedMachinePool.Spec.AvailabilityZoneSubnetType,
})
}
// NodegroupReadyFalse marks the ready condition false using warning if error isn't
// empty.
func (s *ManagedMachinePoolScope) NodegroupReadyFalse(reason string, err string) error {
severity := clusterv1.ConditionSeverityWarning
if err == "" {
severity = clusterv1.ConditionSeverityInfo
}
conditions.MarkFalse(
s.ManagedMachinePool,
expinfrav1.EKSNodegroupReadyCondition,
reason,
severity,
err,
)
if err := s.PatchObject(); err != nil {
return errors.Wrap(err, "failed to mark nodegroup not ready")
}
return nil
}
// IAMReadyFalse marks the ready condition false using warning if error isn't
// empty.
func (s *ManagedMachinePoolScope) IAMReadyFalse(reason string, err string) error {
severity := clusterv1.ConditionSeverityWarning
if err == "" {
severity = clusterv1.ConditionSeverityInfo
}
conditions.MarkFalse(
s.ManagedMachinePool,
expinfrav1.IAMNodegroupRolesReadyCondition,
reason,
severity,
err,
)
if err := s.PatchObject(); err != nil {
return errors.Wrap(err, "failed to mark nodegroup role not ready")
}
return nil
}
// PatchObject persists the control plane configuration and status.
func (s *ManagedMachinePoolScope) PatchObject() error {
return s.patchHelper.Patch(
context.TODO(),
s.ManagedMachinePool,
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
expinfrav1.EKSNodegroupReadyCondition,
expinfrav1.IAMNodegroupRolesReadyCondition,
}})
}
// PatchCAPIMachinePoolObject persists the capi machinepool configuration and status.
func (s *ManagedMachinePoolScope) PatchCAPIMachinePoolObject(ctx context.Context) error {
return s.capiMachinePoolPatchHelper.Patch(
ctx,
s.MachinePool,
)
}
// Close closes the current scope persisting the control plane configuration and status.
func (s *ManagedMachinePoolScope) Close() error {
return s.PatchObject()
}
// InfraCluster returns the AWS infrastructure cluster or control plane object.
func (s *ManagedMachinePoolScope) InfraCluster() cloud.ClusterObject {
return s.ControlPlane
}
// ClusterObj returns the cluster object.
func (s *ManagedMachinePoolScope) ClusterObj() cloud.ClusterObject {
return s.Cluster
}
// Session returns the AWS SDK session. Used for creating clients.
func (s *ManagedMachinePoolScope) Session() awsclient.ConfigProvider {
return s.session
}
// ControllerName returns the name of the controller that
// created the ManagedMachinePool.
func (s *ManagedMachinePoolScope) ControllerName() string {
return s.controllerName
}
// KubernetesClusterName is the name of the EKS cluster name.
func (s *ManagedMachinePoolScope) KubernetesClusterName() string {
return s.ControlPlane.Spec.EKSClusterName
}
// NodegroupName is the name of the EKS nodegroup.
func (s *ManagedMachinePoolScope) NodegroupName() string {
return s.ManagedMachinePool.Spec.EKSNodegroupName
}
// Name returns the name of the AWSManagedMachinePool.
func (s *ManagedMachinePoolScope) Name() string {
return s.ManagedMachinePool.Name
}
// Namespace returns the namespace of the AWSManagedMachinePool.
func (s *ManagedMachinePoolScope) Namespace() string {
return s.ManagedMachinePool.Namespace
}
// GetRawBootstrapData returns the raw bootstrap data from the linked Machine's bootstrap.dataSecretName.
func (s *ManagedMachinePoolScope) GetRawBootstrapData() ([]byte, *types.NamespacedName, error) {
if s.MachinePool.Spec.Template.Spec.Bootstrap.DataSecretName == nil {
return nil, nil, errors.New("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil")
}
secret := &corev1.Secret{}
key := types.NamespacedName{Namespace: s.Namespace(), Name: *s.MachinePool.Spec.Template.Spec.Bootstrap.DataSecretName}
if err := s.Client.Get(context.TODO(), key, secret); err != nil {
return nil, nil, errors.Wrapf(err, "failed to retrieve bootstrap data secret for AWSManagedMachinePool %s/%s", s.Namespace(), s.Name())
}
value, ok := secret.Data["value"]
if !ok {
return nil, nil, errors.New("error retrieving bootstrap data: secret value key is missing")
}
return value, &key, nil
}
// GetObjectMeta returns the ObjectMeta for the AWSManagedMachinePool.
func (s *ManagedMachinePoolScope) GetObjectMeta() *metav1.ObjectMeta {
return &s.ManagedMachinePool.ObjectMeta
}
// GetSetter returns the condition setter.
func (s *ManagedMachinePoolScope) GetSetter() conditions.Setter {
return s.ManagedMachinePool
}
// GetEC2Scope returns the EC2Scope.
func (s *ManagedMachinePoolScope) GetEC2Scope() EC2Scope {
return s.EC2Scope
}
// IsEKSManaged returns true if the control plane is managed by EKS.
func (s *ManagedMachinePoolScope) IsEKSManaged() bool {
return true
}
// GetLaunchTemplateIDStatus returns the launch template ID status.
func (s *ManagedMachinePoolScope) GetLaunchTemplateIDStatus() string {
if s.ManagedMachinePool.Status.LaunchTemplateID != nil {
return *s.ManagedMachinePool.Status.LaunchTemplateID
}
return ""
}
// SetLaunchTemplateIDStatus sets the launch template ID status.
func (s *ManagedMachinePoolScope) SetLaunchTemplateIDStatus(id string) {
s.ManagedMachinePool.Status.LaunchTemplateID = &id
}
// GetLaunchTemplateLatestVersionStatus returns the launch template latest version status.
func (s *ManagedMachinePoolScope) GetLaunchTemplateLatestVersionStatus() string {
if s.ManagedMachinePool.Status.LaunchTemplateVersion != nil {
return *s.ManagedMachinePool.Status.LaunchTemplateVersion
}
return ""
}
// SetLaunchTemplateLatestVersionStatus sets the launch template latest version status.
func (s *ManagedMachinePoolScope) SetLaunchTemplateLatestVersionStatus(version string) {
s.ManagedMachinePool.Status.LaunchTemplateVersion = &version
}
// GetLaunchTemplate returns the launch template.
func (s *ManagedMachinePoolScope) GetLaunchTemplate() *expinfrav1.AWSLaunchTemplate {
return s.ManagedMachinePool.Spec.AWSLaunchTemplate
}
// GetMachinePool returns the machine pool.
func (s *ManagedMachinePoolScope) GetMachinePool() *expclusterv1.MachinePool {
return s.MachinePool
}
// LaunchTemplateName returns the launch template name.
func (s *ManagedMachinePoolScope) LaunchTemplateName() string {
return fmt.Sprintf("%s-%s", s.ControlPlane.Name, s.ManagedMachinePool.Name)
}
// GetRuntimeObject returns the AWSManagedMachinePool, in runtime.Object form.
func (s *ManagedMachinePoolScope) GetRuntimeObject() runtime.Object {
return s.ManagedMachinePool
}