-
Notifications
You must be signed in to change notification settings - Fork 570
/
machine.go
379 lines (316 loc) · 12.3 KB
/
machine.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
/*
Copyright 2018 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"
"encoding/base64"
"fmt"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2/klogr"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/noderefutil"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
)
// MachineScopeParams defines the input parameters used to create a new MachineScope.
type MachineScopeParams struct {
Client client.Client
Logger *logr.Logger
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
InfraCluster EC2Scope
AWSMachine *infrav1.AWSMachine
}
// NewMachineScope creates a new MachineScope from the supplied parameters.
// This is meant to be called for each reconcile iteration.
func NewMachineScope(params MachineScopeParams) (*MachineScope, error) {
if params.Client == nil {
return nil, errors.New("client is required when creating a MachineScope")
}
if params.Machine == nil {
return nil, errors.New("machine is required when creating a MachineScope")
}
if params.Cluster == nil {
return nil, errors.New("cluster is required when creating a MachineScope")
}
if params.AWSMachine == nil {
return nil, errors.New("aws machine is required when creating a MachineScope")
}
if params.InfraCluster == nil {
return nil, errors.New("aws cluster is required when creating a MachineScope")
}
if params.Logger == nil {
log := klogr.New()
params.Logger = &log
}
helper, err := patch.NewHelper(params.AWSMachine, params.Client)
if err != nil {
return nil, errors.Wrap(err, "failed to init patch helper")
}
return &MachineScope{
Logger: *params.Logger,
client: params.Client,
patchHelper: helper,
Cluster: params.Cluster,
Machine: params.Machine,
InfraCluster: params.InfraCluster,
AWSMachine: params.AWSMachine,
}, nil
}
// MachineScope defines a scope defined around a machine and its cluster.
type MachineScope struct {
logr.Logger
client client.Client
patchHelper *patch.Helper
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
InfraCluster EC2Scope
AWSMachine *infrav1.AWSMachine
}
// Name returns the AWSMachine name.
func (m *MachineScope) Name() string {
return m.AWSMachine.Name
}
// Namespace returns the namespace name.
func (m *MachineScope) Namespace() string {
return m.AWSMachine.Namespace
}
// IsControlPlane returns true if the machine is a control plane.
func (m *MachineScope) IsControlPlane() bool {
return util.IsControlPlaneMachine(m.Machine)
}
// Role returns the machine role from the labels.
func (m *MachineScope) Role() string {
if util.IsControlPlaneMachine(m.Machine) {
return "control-plane"
}
return "node"
}
// GetInstanceID returns the AWSMachine instance id by parsing Spec.ProviderID.
func (m *MachineScope) GetInstanceID() *string {
parsed, err := noderefutil.NewProviderID(m.GetProviderID())
if err != nil {
return nil
}
return pointer.StringPtr(parsed.ID())
}
// GetProviderID returns the AWSMachine providerID from the spec.
func (m *MachineScope) GetProviderID() string {
if m.AWSMachine.Spec.ProviderID != nil {
return *m.AWSMachine.Spec.ProviderID
}
return ""
}
// SetProviderID sets the AWSMachine providerID in spec.
func (m *MachineScope) SetProviderID(instanceID, availabilityZone string) {
providerID := fmt.Sprintf("aws:///%s/%s", availabilityZone, instanceID)
m.AWSMachine.Spec.ProviderID = pointer.StringPtr(providerID)
}
// SetInstanceID sets the AWSMachine instanceID in spec.
func (m *MachineScope) SetInstanceID(instanceID string) {
m.AWSMachine.Spec.InstanceID = pointer.StringPtr(instanceID)
}
// GetInstanceState returns the AWSMachine instance state from the status.
func (m *MachineScope) GetInstanceState() *infrav1.InstanceState {
return m.AWSMachine.Status.InstanceState
}
// SetInstanceState sets the AWSMachine status instance state.
func (m *MachineScope) SetInstanceState(v infrav1.InstanceState) {
m.AWSMachine.Status.InstanceState = &v
}
// SetReady sets the AWSMachine Ready Status.
func (m *MachineScope) SetReady() {
m.AWSMachine.Status.Ready = true
}
// SetNotReady sets the AWSMachine Ready Status to false.
func (m *MachineScope) SetNotReady() {
m.AWSMachine.Status.Ready = false
}
// SetFailureMessage sets the AWSMachine status failure message.
func (m *MachineScope) SetFailureMessage(v error) {
m.AWSMachine.Status.FailureMessage = pointer.StringPtr(v.Error())
}
// SetFailureReason sets the AWSMachine status failure reason.
func (m *MachineScope) SetFailureReason(v capierrors.MachineStatusError) {
m.AWSMachine.Status.FailureReason = &v
}
// SetAnnotation sets a key value annotation on the AWSMachine.
func (m *MachineScope) SetAnnotation(key, value string) {
if m.AWSMachine.Annotations == nil {
m.AWSMachine.Annotations = map[string]string{}
}
m.AWSMachine.Annotations[key] = value
}
// UseSecretsManager returns the computed value of whether or not
// userdata should be stored using AWS Secrets Manager.
func (m *MachineScope) UseSecretsManager(userDataFormat string) bool {
return !m.AWSMachine.Spec.CloudInit.InsecureSkipSecretsManager && !m.UseIgnition(userDataFormat)
}
func (m *MachineScope) UseIgnition(userDataFormat string) bool {
return userDataFormat == "ignition" || (m.AWSMachine.Spec.Ignition != nil)
}
// SecureSecretsBackend returns the chosen secret backend.
func (m *MachineScope) SecureSecretsBackend() infrav1.SecretBackend {
return m.AWSMachine.Spec.CloudInit.SecureSecretsBackend
}
// CompressUserData returns the computed value of whether or not
// userdata should be compressed using gzip.
func (m *MachineScope) CompressUserData(userDataFormat string) bool {
if m.UseIgnition(userDataFormat) {
return false
}
return m.AWSMachine.Spec.UncompressedUserData != nil && !*m.AWSMachine.Spec.UncompressedUserData
}
// GetSecretPrefix returns the prefix for the secrets belonging
// to the AWSMachine in AWS Secrets Manager.
func (m *MachineScope) GetSecretPrefix() string {
return m.AWSMachine.Spec.CloudInit.SecretPrefix
}
// SetSecretPrefix sets the prefix for the secrets belonging
// to the AWSMachine in AWS Secrets Manager.
func (m *MachineScope) SetSecretPrefix(value string) {
m.AWSMachine.Spec.CloudInit.SecretPrefix = value
}
// DeleteSecretPrefix deletes the prefix for the secret belonging
// to the AWSMachine in AWS Secrets Manager.
func (m *MachineScope) DeleteSecretPrefix() {
m.AWSMachine.Spec.CloudInit.SecretPrefix = ""
}
// GetSecretCount returns the number of AWS Secret Manager entries making up
// the complete userdata.
func (m *MachineScope) GetSecretCount() int32 {
return m.AWSMachine.Spec.CloudInit.SecretCount
}
// SetSecretCount sets the number of AWS Secret Manager entries making up
// the complete userdata.
func (m *MachineScope) SetSecretCount(i int32) {
m.AWSMachine.Spec.CloudInit.SecretCount = i
}
// SetAddresses sets the AWSMachine address status.
func (m *MachineScope) SetAddresses(addrs []clusterv1.MachineAddress) {
m.AWSMachine.Status.Addresses = addrs
}
// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName as base64.
func (m *MachineScope) GetBootstrapData() (string, error) {
value, err := m.GetRawBootstrapData()
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(value), nil
}
// GetRawBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
func (m *MachineScope) GetRawBootstrapData() ([]byte, error) {
data, _, err := m.GetRawBootstrapDataWithFormat()
return data, err
}
func (m *MachineScope) GetRawBootstrapDataWithFormat() ([]byte, string, error) {
if m.Machine.Spec.Bootstrap.DataSecretName == nil {
return nil, "", errors.New("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil")
}
secret := &corev1.Secret{}
key := types.NamespacedName{Namespace: m.Namespace(), Name: *m.Machine.Spec.Bootstrap.DataSecretName}
if err := m.client.Get(context.TODO(), key, secret); err != nil {
return nil, "", errors.Wrapf(err, "failed to retrieve bootstrap data secret for AWSMachine %s/%s", m.Namespace(), m.Name())
}
value, ok := secret.Data["value"]
if !ok {
return nil, "", errors.New("error retrieving bootstrap data: secret value key is missing")
}
return value, string(secret.Data["format"]), nil
}
// PatchObject persists the machine spec and status.
func (m *MachineScope) PatchObject() error {
// Always update the readyCondition by summarizing the state of other conditions.
// A step counter is added to represent progress during the provisioning process (instead we are hiding during the deletion process).
applicableConditions := []clusterv1.ConditionType{
infrav1.InstanceReadyCondition,
infrav1.SecurityGroupsReadyCondition,
}
if m.IsControlPlane() {
applicableConditions = append(applicableConditions, infrav1.ELBAttachedCondition)
}
conditions.SetSummary(m.AWSMachine,
conditions.WithConditions(applicableConditions...),
conditions.WithStepCounterIf(m.AWSMachine.ObjectMeta.DeletionTimestamp.IsZero()),
conditions.WithStepCounter(),
)
return m.patchHelper.Patch(
context.TODO(),
m.AWSMachine,
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
clusterv1.ReadyCondition,
infrav1.InstanceReadyCondition,
infrav1.SecurityGroupsReadyCondition,
infrav1.ELBAttachedCondition,
}})
}
// Close the MachineScope by updating the machine spec, machine status.
func (m *MachineScope) Close() error {
return m.PatchObject()
}
// AdditionalTags merges AdditionalTags from the scope's AWSCluster and AWSMachine. If the same key is present in both,
// the value from AWSMachine takes precedence. The returned Tags will never be nil.
func (m *MachineScope) AdditionalTags() infrav1.Tags {
tags := make(infrav1.Tags)
// Start with the cluster-wide tags...
tags.Merge(m.InfraCluster.AdditionalTags())
// ... and merge in the Machine's
tags.Merge(m.AWSMachine.Spec.AdditionalTags)
return tags
}
// HasFailed returns the failure state of the machine scope.
func (m *MachineScope) HasFailed() bool {
return m.AWSMachine.Status.FailureReason != nil || m.AWSMachine.Status.FailureMessage != nil
}
// InstanceIsRunning returns the instance state of the machine scope.
func (m *MachineScope) InstanceIsRunning() bool {
state := m.GetInstanceState()
return state != nil && infrav1.InstanceRunningStates.Has(string(*state))
}
// InstanceIsOperational returns the operational state of the machine scope.
func (m *MachineScope) InstanceIsOperational() bool {
state := m.GetInstanceState()
return state != nil && infrav1.InstanceOperationalStates.Has(string(*state))
}
// InstanceIsInKnownState checks if the machine scope's instance state is known.
func (m *MachineScope) InstanceIsInKnownState() bool {
state := m.GetInstanceState()
return state != nil && infrav1.InstanceKnownStates.Has(string(*state))
}
// AWSMachineIsDeleted checks if the machine was deleted.
func (m *MachineScope) AWSMachineIsDeleted() bool {
return !m.AWSMachine.ObjectMeta.DeletionTimestamp.IsZero()
}
// IsEKSManaged checks if the machine is EKS managed.
func (m *MachineScope) IsEKSManaged() bool {
return m.InfraCluster.InfraCluster().GetObjectKind().GroupVersionKind().Kind == "AWSManagedControlPlane"
}
// IsExternallyManaged checks if the machine is externally managed.
func (m *MachineScope) IsExternallyManaged() bool {
return annotations.IsExternallyManaged(m.InfraCluster.InfraCluster())
}
// SetInterruptible sets the AWSMachine status Interruptible.
func (m *MachineScope) SetInterruptible() {
if m.AWSMachine.Spec.SpotMarketOptions != nil {
m.AWSMachine.Status.Interruptible = true
}
}