forked from kubernetes-sigs/cluster-api-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 44
/
utils.go
355 lines (303 loc) · 11.8 KB
/
utils.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
/*
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 machine
import (
"fmt"
"github.com/golang/glog"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
machinev1 "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
"golang.org/x/net/context"
"k8s.io/apimachinery/pkg/types"
providerconfigv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1beta1"
awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client"
)
// existingInstanceStates returns the list of states an EC2 instance can be in
// while being considered "existing", i.e. mostly anything but "Terminated".
func existingInstanceStates() []*string {
return []*string{
aws.String(ec2.InstanceStateNameRunning),
aws.String(ec2.InstanceStateNamePending),
aws.String(ec2.InstanceStateNameStopped),
aws.String(ec2.InstanceStateNameStopping),
aws.String(ec2.InstanceStateNameShuttingDown),
}
}
// getRunningInstance returns the AWS instance for a given machine. If multiple instances match our machine,
// the most recently launched will be returned. If no instance exists, an error will be returned.
func getRunningInstance(machine *machinev1.Machine, client awsclient.Client) (*ec2.Instance, error) {
instances, err := getRunningInstances(machine, client)
if err != nil {
return nil, err
}
if len(instances) == 0 {
return nil, fmt.Errorf("no instance found for machine: %s", machine.Name)
}
sortInstances(instances)
return instances[0], nil
}
// getRunningInstances returns all running instances that have a tag matching our machine name,
// and cluster ID.
func getRunningInstances(machine *machinev1.Machine, client awsclient.Client) ([]*ec2.Instance, error) {
runningInstanceStateFilter := []*string{aws.String(ec2.InstanceStateNameRunning)}
return getInstances(machine, client, runningInstanceStateFilter)
}
// getRunningFromInstances returns all running instances from a list of instances.
func getRunningFromInstances(instances []*ec2.Instance) []*ec2.Instance {
var runningInstances []*ec2.Instance
for _, instance := range instances {
if *instance.State.Name == ec2.InstanceStateNameRunning {
runningInstances = append(runningInstances, instance)
}
}
return runningInstances
}
// getStoppedInstances returns all stopped instances that have a tag matching our machine name,
// and cluster ID.
func getStoppedInstances(machine *machinev1.Machine, client awsclient.Client) ([]*ec2.Instance, error) {
stoppedInstanceStateFilter := []*string{aws.String(ec2.InstanceStateNameStopped), aws.String(ec2.InstanceStateNameStopping)}
return getInstances(machine, client, stoppedInstanceStateFilter)
}
func getExistingInstances(machine *machinev1.Machine, client awsclient.Client) ([]*ec2.Instance, error) {
return getInstances(machine, client, existingInstanceStates())
}
func getExistingInstanceByID(id string, client awsclient.Client) (*ec2.Instance, error) {
return getInstanceByID(id, client, existingInstanceStates())
}
// getInstanceByID returns the instance with the given ID if it exists.
func getInstanceByID(id string, client awsclient.Client, instanceStateFilter []*string) (*ec2.Instance, error) {
if id == "" {
return nil, fmt.Errorf("instance-id not specified")
}
requestFilters := []*ec2.Filter{
{
Name: aws.String("instance-id"),
Values: aws.StringSlice([]string{id}),
},
}
if instanceStateFilter != nil {
requestFilters = append(requestFilters, &ec2.Filter{
Name: aws.String("instance-state-name"),
Values: instanceStateFilter,
})
}
request := &ec2.DescribeInstancesInput{Filters: requestFilters}
result, err := client.DescribeInstances(request)
if err != nil {
return nil, err
}
if len(result.Reservations) != 1 {
return nil, fmt.Errorf("found %d reservations for instance-id %s", len(result.Reservations), id)
}
reservation := result.Reservations[0]
if len(reservation.Instances) != 1 {
return nil, fmt.Errorf("found %d instances for instance-id %s", len(reservation.Instances), id)
}
return reservation.Instances[0], nil
}
// getInstances returns all instances that have a tag matching our machine name,
// and cluster ID.
func getInstances(machine *machinev1.Machine, client awsclient.Client, instanceStateFilter []*string) ([]*ec2.Instance, error) {
clusterID, ok := getClusterID(machine)
if !ok {
return []*ec2.Instance{}, fmt.Errorf("unable to get cluster ID for machine: %q", machine.Name)
}
requestFilters := []*ec2.Filter{
{
Name: awsTagFilter("Name"),
Values: aws.StringSlice([]string{machine.Name}),
},
clusterFilter(clusterID),
}
if instanceStateFilter != nil {
requestFilters = append(requestFilters, &ec2.Filter{
Name: aws.String("instance-state-name"),
Values: instanceStateFilter,
})
}
// Query instances with our machine's name, and in running/pending state.
request := &ec2.DescribeInstancesInput{
Filters: requestFilters,
}
result, err := client.DescribeInstances(request)
if err != nil {
return []*ec2.Instance{}, err
}
instances := make([]*ec2.Instance, 0, len(result.Reservations))
for _, reservation := range result.Reservations {
for _, instance := range reservation.Instances {
instances = append(instances, instance)
}
}
return instances, nil
}
func getVolume(client awsclient.Client, volumeID string) (*ec2.Volume, error) {
request := &ec2.DescribeVolumesInput{
VolumeIds: []*string{&volumeID},
}
result, err := client.DescribeVolumes(request)
if err != nil {
return &ec2.Volume{}, err
}
if len(result.Volumes) != 1 {
return &ec2.Volume{}, fmt.Errorf("unable to get volume ID: %q", volumeID)
}
return result.Volumes[0], nil
}
// terminateInstances terminates all provided instances with a single EC2 request.
func terminateInstances(client awsclient.Client, instances []*ec2.Instance) ([]*ec2.InstanceStateChange, error) {
instanceIDs := []*string{}
// Cleanup all older instances:
for _, instance := range instances {
glog.Infof("Cleaning up extraneous instance for machine: %v, state: %v, launchTime: %v", *instance.InstanceId, *instance.State.Name, *instance.LaunchTime)
instanceIDs = append(instanceIDs, instance.InstanceId)
}
for _, instanceID := range instanceIDs {
glog.Infof("Terminating %v instance", *instanceID)
}
terminateInstancesRequest := &ec2.TerminateInstancesInput{
InstanceIds: instanceIDs,
}
output, err := client.TerminateInstances(terminateInstancesRequest)
if err != nil {
glog.Errorf("Error terminating instances: %v", err)
return nil, fmt.Errorf("error terminating instances: %v", err)
}
if output == nil {
return nil, nil
}
return output.TerminatingInstances, nil
}
// providerConfigFromMachine gets the machine provider config MachineSetSpec from the
// specified cluster-api MachineSpec.
func providerConfigFromMachine(machine *machinev1.Machine, codec *providerconfigv1.AWSProviderConfigCodec) (*providerconfigv1.AWSMachineProviderConfig, error) {
if machine.Spec.ProviderSpec.Value == nil {
return nil, fmt.Errorf("unable to find machine provider config: Spec.ProviderSpec.Value is not set")
}
var config providerconfigv1.AWSMachineProviderConfig
if err := codec.DecodeProviderSpec(&machine.Spec.ProviderSpec, &config); err != nil {
return nil, err
}
return &config, nil
}
// isMaster returns true if the machine is part of a cluster's control plane
func (a *Actuator) isMaster(machine *machinev1.Machine) (bool, error) {
if machine.Status.NodeRef == nil {
glog.Errorf("NodeRef not found in machine %s", machine.Name)
return false, nil
}
node := &corev1.Node{}
nodeKey := types.NamespacedName{
Namespace: machine.Status.NodeRef.Namespace,
Name: machine.Status.NodeRef.Name,
}
err := a.client.Get(context.Background(), nodeKey, node)
if err != nil {
return false, fmt.Errorf("failed to get node from machine %s", machine.Name)
}
if _, exists := node.Labels["node-role.kubernetes.io/master"]; exists {
return true, nil
}
return false, nil
}
// updateConditionCheck tests whether a condition should be updated from the
// old condition to the new condition. Returns true if the condition should
// be updated.
type updateConditionCheck func(oldReason, oldMessage, newReason, newMessage string) bool
// updateConditionAlways returns true. The condition will always be updated.
func updateConditionAlways(_, _, _, _ string) bool {
return true
}
// updateConditionNever return false. The condition will never be updated,
// unless there is a change in the status of the condition.
func updateConditionNever(_, _, _, _ string) bool {
return false
}
// updateConditionIfReasonOrMessageChange returns true if there is a change
// in the reason or the message of the condition.
func updateConditionIfReasonOrMessageChange(oldReason, oldMessage, newReason, newMessage string) bool {
return oldReason != newReason ||
oldMessage != newMessage
}
func shouldUpdateCondition(
oldStatus corev1.ConditionStatus, oldReason, oldMessage string,
newStatus corev1.ConditionStatus, newReason, newMessage string,
updateConditionCheck updateConditionCheck,
) bool {
if oldStatus != newStatus {
return true
}
return updateConditionCheck(oldReason, oldMessage, newReason, newMessage)
}
// setAWSMachineProviderCondition sets the condition for the machine and
// returns the new slice of conditions.
// If the machine does not already have a condition with the specified type,
// a condition will be added to the slice if and only if the specified
// status is True.
// If the machine does already have a condition with the specified type,
// the condition will be updated if either of the following are true.
// 1) Requested status is different than existing status.
// 2) The updateConditionCheck function returns true.
func setAWSMachineProviderCondition(
conditions []providerconfigv1.AWSMachineProviderCondition,
conditionType providerconfigv1.AWSMachineProviderConditionType,
status corev1.ConditionStatus,
reason string,
message string,
updateConditionCheck updateConditionCheck,
) []providerconfigv1.AWSMachineProviderCondition {
now := metav1.Now()
existingCondition := findAWSMachineProviderCondition(conditions, conditionType)
if existingCondition == nil {
if status == corev1.ConditionTrue {
conditions = append(
conditions,
providerconfigv1.AWSMachineProviderCondition{
Type: conditionType,
Status: status,
Reason: reason,
Message: message,
LastTransitionTime: now,
LastProbeTime: now,
},
)
}
} else {
if shouldUpdateCondition(
existingCondition.Status, existingCondition.Reason, existingCondition.Message,
status, reason, message,
updateConditionCheck,
) {
if existingCondition.Status != status {
existingCondition.LastTransitionTime = now
}
existingCondition.Status = status
existingCondition.Reason = reason
existingCondition.Message = message
existingCondition.LastProbeTime = now
}
}
return conditions
}
// findAWSMachineProviderCondition finds in the machine the condition that has the
// specified condition type. If none exists, then returns nil.
func findAWSMachineProviderCondition(conditions []providerconfigv1.AWSMachineProviderCondition, conditionType providerconfigv1.AWSMachineProviderConditionType) *providerconfigv1.AWSMachineProviderCondition {
for i, condition := range conditions {
if condition.Type == conditionType {
return &conditions[i]
}
}
return nil
}