-
Notifications
You must be signed in to change notification settings - Fork 26
/
helpers.go
424 lines (376 loc) · 11.7 KB
/
helpers.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
/*
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 nodereaper
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func parseTaint(t string) (v1.Taint, bool, error) {
var key, value string
var effect v1.TaintEffect
var taint v1.Taint
parts := strings.Split(t, ":")
switch len(parts) {
case 1:
key = parts[0]
case 2:
effect = v1.TaintEffect(parts[1])
KV := strings.Split(parts[0], "=")
if len(KV) > 2 {
return taint, false, errors.Errorf("invalid taint %v provided", t)
}
key = KV[0]
if len(KV) == 2 {
value = KV[1]
}
default:
return taint, false, errors.Errorf("invalid taint %v provided", t)
}
taint.Key = key
taint.Value = value
taint.Effect = effect
taint.TimeAdded = &metav1.Time{Time: time.Time{}}
return taint, true, nil
}
func runCommand(call string, arg []string) (string, error) {
log.Infof("invoking >> %s %s", call, arg)
out, err := exec.Command(call, arg...).CombinedOutput()
if err != nil {
log.Errorf("call failed with output: %s, error: %s", string(out), err)
return string(out), err
}
log.Infof("call succeeded with output: %s", string(out))
return string(out), err
}
func runCommandWithContext(call string, args []string, timeoutSeconds int) (string, error) {
// Create a new context and add a timeout to it
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSeconds)*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, call, args...)
out, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
timeoutErr := fmt.Errorf("command execution timed out")
log.Error(timeoutErr)
return string(out), timeoutErr
}
if err != nil {
log.Errorf("call failed with output: %s, error: %s", string(out), err)
return string(out), err
}
return string(out), nil
}
func (ctx *ReaperContext) uncordonNode(name string, dryRun bool) error {
uncordonArgs := []string{"uncordon", name}
uncordonCommand := ctx.KubectlLocalPath
if dryRun {
log.Warnf("dry run is on, instance not uncordoned")
} else {
_, err := runCommand(uncordonCommand, uncordonArgs)
if err != nil {
log.Errorf("failed to uncrodon node %v", name)
return err
}
}
return nil
}
func (ctx *ReaperContext) terminateInstance(w autoscalingiface.AutoScalingAPI, id string, nodeName string) error {
terminateInput := &autoscaling.TerminateInstanceInAutoScalingGroupInput{
InstanceId: &id,
ShouldDecrementDesiredCapacity: aws.Bool(false),
}
_, err := w.TerminateInstanceInAutoScalingGroup(terminateInput)
if err != nil {
return err
}
if err := ctx.annotateNode(nodeName, stateAnnotationKey, terminatedStateName); err != nil {
log.Warnf("failed to update state annotation on node '%v'", nodeName)
}
log.Info("instance terminate event occurred")
return nil
}
func (ctx *ReaperContext) drainNode(name string, dryRun bool) error {
log.Infof("draining node %v", name)
drainArgs := []string{"drain", name, "--ignore-daemonsets=true", "--delete-local-data=true", "--force", "--grace-period=-1"}
drainCommand := ctx.KubectlLocalPath
// try draining for 10 minutes
drainTimeoutSeconds := 600
if dryRun {
log.Warnf("dry run is on, instance not drained")
} else {
if err := ctx.annotateNode(name, stateAnnotationKey, drainingStateName); err != nil {
log.Warnf("failed to update state annotation on node '%v'", name)
}
_, err := runCommandWithContext(drainCommand, drainArgs, drainTimeoutSeconds)
if err != nil {
event := ctx.getUnreapableDrainFailureEvent(name, err.Error())
ctx.publishEvent(ctx.SelfNamespace, event)
if err.Error() == "command execution timed out" {
log.Warnf("failed to drain node %v, drain command timed-out", name)
ctx.annotateNode(name, ageUnreapableAnnotationKey, "true")
ctx.uncordonNode(name, dryRun)
return err
}
log.Warnf("failed to drain node: %v", err)
ctx.uncordonNode(name, dryRun)
return err
}
ctx.DrainedInstances++
}
return nil
}
func (ctx *ReaperContext) getUnreapableDrainFailureEvent(nodeName, message string) *v1.Event {
event := &v1.Event{
Reason: "NodeDrainFailed",
Message: fmt.Sprintf("Node %v is unreapable: %v", nodeName, message),
Type: "Warning",
LastTimestamp: metav1.Time{
Time: time.Now(),
},
}
event.SetName(fmt.Sprintf("node-reaper.%v", strconv.FormatInt(time.Now().UTC().UnixNano(), 10)))
event.SetNamespace(ctx.SelfNamespace)
event.InvolvedObject.Kind = "Node"
event.InvolvedObject.Name = ctx.SelfName
event.InvolvedObject.Namespace = ctx.SelfNamespace
return event
}
func (ctx *ReaperContext) annotateNode(nodeName, annotationKey, annotationValue string) error {
annotation := fmt.Sprintf("%v=%v", annotationKey, annotationValue)
annotateArgs := []string{"annotate", "--overwrite", "node", nodeName, annotation}
annotateCommand := ctx.KubectlLocalPath
if ctx.DryRun {
log.Warnf("dry run is on, node not annotated")
} else {
_, err := runCommand(annotateCommand, annotateArgs)
if err != nil {
log.Errorf("failed to annotate node %v", nodeName)
return err
}
}
return nil
}
func (ctx *ReaperContext) publishEvent(namespace string, event *v1.Event) error {
log.Infof("publishing event: %v", event.Reason)
_, err := ctx.KubernetesClient.CoreV1().Events(namespace).Create(event)
if err != nil {
log.Errorf("failed to publish event: %v", err)
return err
}
return nil
}
func nodeHasActivePods(n *v1.Node, allPods []v1.Pod) bool {
nodeName := n.ObjectMeta.Name
log.Infof("inspecting pods assigned to %v", nodeName)
for _, pod := range allPods {
if pod.Spec.NodeName == nodeName {
if pod.Status.Reason == "NodeLost" {
continue
}
podConditions := pod.Status.Conditions
for _, condition := range podConditions {
if condition.Type == "Ready" {
if condition.Status == "True" {
return true
}
}
}
}
}
return false
}
func getNodeInstanceID(n *v1.Node) string {
providerID := n.Spec.ProviderID
splitProviderID := strings.Split(providerID, "/")
instanceID := splitProviderID[len(splitProviderID)-1]
return instanceID
}
func getNodeAgeMinutes(n *v1.Node) int {
now := time.Now().UTC()
createdDate := n.ObjectMeta.GetCreationTimestamp().UTC()
nodeAge := int(now.Sub(createdDate).Minutes())
return nodeAge
}
func getNodeRegion(n *v1.Node) string {
providerID := n.Spec.ProviderID
splitProviderID := strings.Split(providerID, "/")
regionFullName := splitProviderID[len(splitProviderID)-2]
regionName := regionFullName[:len(regionFullName)-1]
return regionName
}
func getLastTransitionDurationMinutes(n *v1.Node) float64 {
var minuteDiff float64
now := time.Now().UTC()
conditions := n.Status.Conditions
for _, condition := range conditions {
if condition.Type == "Ready" {
transitionTimestamp := condition.LastTransitionTime.UTC()
minuteDiff = now.Sub(transitionTimestamp).Minutes()
}
}
return minuteDiff
}
func nodeStateIsNotReady(n *v1.Node) bool {
conditions := n.Status.Conditions
for _, condition := range conditions {
if condition.Type == "Ready" {
if condition.Status == "False" {
return true
}
}
}
return false
}
func nodeStateIsReady(n *v1.Node) bool {
conditions := n.Status.Conditions
for _, condition := range conditions {
if condition.Type == "Ready" {
if condition.Status == "True" {
return true
}
}
}
return false
}
func nodeStateIsUnknown(n *v1.Node) bool {
conditions := n.Status.Conditions
for _, condition := range conditions {
if condition.Type == "Ready" {
if condition.Status == "Unknown" {
return true
}
}
}
return false
}
func getInstanceTagValue(w ec2iface.EC2API, instance string, key string) (string, error) {
filters := []*ec2.Filter{
{Name: aws.String("resource-id"), Values: []*string{&instance}},
{Name: aws.String("key"), Values: []*string{&key}}}
describeTagsInput := &ec2.DescribeTagsInput{Filters: filters}
response, err := w.DescribeTags(describeTagsInput)
if err != nil {
return "", err
}
if len(response.Tags) != 1 {
err := fmt.Errorf("failed to find tag %v for instance %v", key, instance)
return "", err
}
return *response.Tags[0].Value, nil
}
func getAutoScalingGroup(w autoscalingiface.AutoScalingAPI, name string) (autoscaling.Group, error) {
describeInput := &autoscaling.DescribeAutoScalingGroupsInput{AutoScalingGroupNames: []*string{&name}}
response, err := w.DescribeAutoScalingGroups(describeInput)
if err != nil {
return autoscaling.Group{}, err
}
if len(response.AutoScalingGroups) != 1 {
err := fmt.Errorf("failed to find ASG %v", name)
return autoscaling.Group{}, err
}
return *response.AutoScalingGroups[0], nil
}
func dumpSpec(nodeName string, kubeClient kubernetes.Interface) error {
nodeObject, err := kubeClient.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
if err != nil {
return err
}
nodeDump, err := json.Marshal(nodeObject)
if err != nil {
return err
}
log.Infof("node dump: %v", string(nodeDump))
return nil
}
func nodeMeetsReapAfterThreshold(minuteThreshold float64, minutesSinceTransition float64) bool {
if minutesSinceTransition > minuteThreshold {
return true
}
return false
}
func isMaster(node string, kubeClient kubernetes.Interface) (bool, error) {
corev1 := kubeClient.CoreV1()
nodeObject, err := corev1.Nodes().Get(node, metav1.GetOptions{})
if err != nil {
log.Errorf("failed to get node, %v", err)
return false, err
}
labels := nodeObject.ObjectMeta.GetLabels()
if labels["kubernetes.io/role"] == "master" {
return true, nil
}
return false, nil
}
func getHealthyMasterCount(kubeClient kubernetes.Interface) (int, error) {
corev1 := kubeClient.CoreV1()
masterCount := 0
nodeList, err := corev1.Nodes().List(metav1.ListOptions{LabelSelector: "kubernetes.io/role=master"})
if err != nil {
log.Errorf("failed to list master nodes, %v", err)
return 0, err
}
for _, node := range nodeList.Items {
if nodeStateIsReady(&node) {
masterCount++
}
}
return masterCount, nil
}
func allNodesAreReady(kubeClient kubernetes.Interface) (bool, error) {
corev1 := kubeClient.CoreV1()
nodeList, err := corev1.Nodes().List(metav1.ListOptions{})
if err != nil {
log.Errorf("failed to list all nodes, %v", err)
return false, err
}
for _, node := range nodeList.Items {
if nodeStateIsNotReady(&node) || nodeStateIsUnknown(&node) {
return false, nil
}
}
return true, nil
}
func isTerminated(instances []*ec2.Instance, instanceID string) bool {
var terminatedStateName = "terminated"
for _, instance := range instances {
if aws.StringValue(instance.InstanceId) == instanceID {
if aws.StringValue(instance.State.Name) == terminatedStateName {
return true
}
}
}
return false
}
func getInstanceIDByPrivateDNS(instances []*ec2.Instance, dnsName string) string {
var runningStateName = "running"
for _, instance := range instances {
if aws.StringValue(instance.PrivateDnsName) == dnsName {
if aws.StringValue(instance.State.Name) == runningStateName {
return aws.StringValue(instance.InstanceId)
}
}
}
return ""
}