-
Notifications
You must be signed in to change notification settings - Fork 268
/
node.go
365 lines (337 loc) · 11.4 KB
/
node.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
// Copyright 2016-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 node
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
"github.com/aws/aws-node-termination-handler/pkg/config"
"github.com/rs/zerolog/log"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/kubectl/pkg/drain"
)
const (
// UncordonAfterRebootLabelVal is a k8s label value that can added to an action label to uncordon a node
UncordonAfterRebootLabelVal = "UncordonAfterReboot"
// ActionLabelKey is a k8s label key that can be added to the k8s node NTH is running on
ActionLabelKey = "aws-node-termination-handler/action"
// ActionLabelTimeKey is a k8s label key whose value is the secs since the epoch when an action label is added
ActionLabelTimeKey = "aws-node-termination-handler/action-time"
// EventIDLabelKey is a k8s label key whose value is the drainable event id
EventIDLabelKey = "aws-node-termination-handler/event-id"
)
var uptimeFile = "/proc/uptime"
// Node represents a kubernetes node with functions to manipulate its state via the kubernetes api server
type Node struct {
nthConfig config.Config
drainHelper *drain.Helper
}
// New will construct a node struct to perform various node function through the kubernetes api server
func New(nthConfig config.Config) (*Node, error) {
drainHelper, err := getDrainHelper(nthConfig)
if err != nil {
return nil, err
}
return &Node{
nthConfig: nthConfig,
drainHelper: drainHelper,
}, nil
}
// NewWithValues will construct a node struct with a drain helper
func NewWithValues(nthConfig config.Config, drainHelper *drain.Helper) (*Node, error) {
return &Node{
nthConfig: nthConfig,
drainHelper: drainHelper,
}, nil
}
// CordonAndDrain will cordon the node and evict pods based on the config
func (n Node) CordonAndDrain() error {
if n.nthConfig.DryRun {
log.Log().Msgf("Node %s would have been cordoned and drained, but dry-run flag was set", n.nthConfig.NodeName)
return nil
}
err := n.Cordon()
if err != nil {
return err
}
// Delete all pods on the node
err = drain.RunNodeDrain(n.drainHelper, n.nthConfig.NodeName)
if err != nil {
return err
}
return nil
}
// Cordon will add a NoSchedule on the node
func (n Node) Cordon() error {
if n.nthConfig.DryRun {
log.Log().Msgf("Node %s would have been cordoned, but dry-run flag was set", n.nthConfig.NodeName)
return nil
}
node, err := n.fetchKubernetesNode()
if err != nil {
return err
}
err = drain.RunCordonOrUncordon(n.drainHelper, node, true)
if err != nil {
return err
}
return nil
}
// Uncordon will remove the NoSchedule on the node
func (n Node) Uncordon() error {
if n.nthConfig.DryRun {
log.Log().Msgf("Node %s would have been uncordoned, but dry-run flag was set", n.nthConfig.NodeName)
return nil
}
node, err := n.fetchKubernetesNode()
if err != nil {
return fmt.Errorf("There was an error fetching the node in preparation for uncordoning: %w", err)
}
err = drain.RunCordonOrUncordon(n.drainHelper, node, false)
if err != nil {
return err
}
return nil
}
// IsUnschedulable checks if the node is marked as unschedulable
func (n Node) IsUnschedulable() (bool, error) {
if n.nthConfig.DryRun {
log.Log().Msg("IsUnschedulable returning false since dry-run is set")
return false, nil
}
node, err := n.fetchKubernetesNode()
if err != nil {
return true, err
}
return node.Spec.Unschedulable, nil
}
// MarkWithEventID will add the drain event ID to the node to be properly ignored after a system restart event
func (n Node) MarkWithEventID(eventID string) error {
err := n.addLabel(EventIDLabelKey, eventID)
if err != nil {
return fmt.Errorf("Unable to label node with event ID %s=%s: %w", EventIDLabelKey, eventID, err)
}
return nil
}
// RemoveNTHLabels will remove all the custom NTH labels added to the node
func (n Node) RemoveNTHLabels() error {
for _, label := range []string{EventIDLabelKey, ActionLabelKey, ActionLabelTimeKey} {
err := n.removeLabel(label)
if err != nil {
return fmt.Errorf("Unable to remove %s from node: %w", label, err)
}
}
return nil
}
// GetEventID will retrieve the event ID value from the node label
func (n Node) GetEventID() (string, error) {
node, err := n.fetchKubernetesNode()
if err != nil {
return "", fmt.Errorf("Could not get event ID label from node: %w", err)
}
val, ok := node.Labels[EventIDLabelKey]
if n.nthConfig.DryRun && !ok {
log.Log().Msgf("Would have returned Error: Event ID Lable %s was not found on the node, but dry-run flag was set", EventIDLabelKey)
return "", nil
}
if !ok {
return "", fmt.Errorf("Event ID Label %s was not found on the node", EventIDLabelKey)
}
return val, nil
}
// MarkForUncordonAfterReboot adds labels to the kubernetes node which NTH will read upon reboot
func (n Node) MarkForUncordonAfterReboot() error {
// adds label to node so that the system will uncordon the node after the scheduled reboot has taken place
err := n.addLabel(ActionLabelKey, UncordonAfterRebootLabelVal)
if err != nil {
return fmt.Errorf("Unable to label node with action to uncordon after system-reboot: %w", err)
}
// adds label with the current time which is checked against the uptime of the node when processing labels on startup
err = n.addLabel(ActionLabelTimeKey, strconv.FormatInt(time.Now().Unix(), 10))
if err != nil {
// if time can't be recorded, rollback the action label
n.removeLabel(ActionLabelKey)
return fmt.Errorf("Unable to label node with action time for uncordon after system-reboot: %w", err)
}
return nil
}
// addLabel will add a label to the node given a label key and value
func (n Node) addLabel(key string, value string) error {
type metadata struct {
Labels map[string]string `json:"labels"`
}
type patch struct {
Metadata metadata `json:"metadata"`
}
labels := make(map[string]string)
labels[key] = value
payload := patch{
Metadata: metadata{
Labels: labels,
},
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("An error occurred while marshalling the json to add a label to the node: %w", err)
}
node, err := n.fetchKubernetesNode()
if err != nil {
return err
}
if n.nthConfig.DryRun {
log.Log().Msgf("Would have added label (%s=%s) to node %s, but dry-run flag was set", key, value, n.nthConfig.NodeName)
return nil
}
_, err = n.drainHelper.Client.CoreV1().Nodes().Patch(node.Name, types.StrategicMergePatchType, payloadBytes)
if err != nil {
return fmt.Errorf("%v node Patch failed when adding a label to the node: %w", node.Name, err)
}
return nil
}
// removeLabel will remove a node label given a label key
func (n Node) removeLabel(key string) error {
type patchRequest struct {
Op string `json:"op"`
Path string `json:"path"`
}
var patchReqs []interface{}
patchRemove := patchRequest{
Op: "remove",
Path: fmt.Sprintf("/metadata/labels/%s", jsonPatchEscape(key)),
}
payload, err := json.Marshal(append(patchReqs, patchRemove))
if err != nil {
return fmt.Errorf("An error occurred while marshalling the json to remove a label from the node: %w", err)
}
node, err := n.fetchKubernetesNode()
if err != nil {
return err
}
if n.nthConfig.DryRun {
log.Log().Msgf("Would have removed label with key %s from node %s, but dry-run flag was set", key, n.nthConfig.NodeName)
return nil
}
_, err = n.drainHelper.Client.CoreV1().Nodes().Patch(node.Name, types.JSONPatchType, payload)
if err != nil {
return fmt.Errorf("%v node Patch failed when removing a label from the node: %w", node.Name, err)
}
return nil
}
// IsLabeledWithAction will return true if the current node is labeled with NTH action labels
func (n Node) IsLabeledWithAction() (bool, error) {
k8sNode, err := n.fetchKubernetesNode()
if err != nil {
return false, fmt.Errorf("Unable to fetch kubernetes node from API: %w", err)
}
_, actionLabelOK := k8sNode.Labels[ActionLabelKey]
_, eventIDLabelOK := k8sNode.Labels[EventIDLabelKey]
return actionLabelOK && eventIDLabelOK, nil
}
// UncordonIfRebooted will check for node labels to trigger an uncordon because of a system-reboot scheduled event
func (n Node) UncordonIfRebooted() error {
k8sNode, err := n.fetchKubernetesNode()
if err != nil {
return fmt.Errorf("Unable to fetch kubernetes node from API: %w", err)
}
timeVal, ok := k8sNode.Labels[ActionLabelTimeKey]
if !ok {
log.Log().Msgf("There was no %s label found requiring action label handling", ActionLabelTimeKey)
return nil
}
timeValNum, err := strconv.ParseInt(timeVal, 10, 64)
if err != nil {
return fmt.Errorf("Cannot convert unix time: %w", err)
}
secondsSinceLabel := time.Now().Unix() - timeValNum
switch actionVal := k8sNode.Labels[ActionLabelKey]; actionVal {
case UncordonAfterRebootLabelVal:
uptime, err := getSystemUptime(uptimeFile)
if err != nil {
return err
}
if secondsSinceLabel < int64(uptime) {
log.Log().Msg("The system has not restarted yet.")
return nil
}
err = n.Uncordon()
if err != nil {
return fmt.Errorf("Unable to uncordon node: %w", err)
}
err = n.RemoveNTHLabels()
if err != nil {
return err
}
log.Log().Msgf("Successfully completed action %s.", UncordonAfterRebootLabelVal)
default:
log.Log().Msg("There are no label actions to handle.")
}
return nil
}
// fetchKubernetesNode will send an http request to the k8s api server and return the corev1 model node
func (n Node) fetchKubernetesNode() (*corev1.Node, error) {
node := &corev1.Node{}
if n.nthConfig.DryRun {
return node, nil
}
fmt.Println(n.nthConfig.NodeName)
return n.drainHelper.Client.CoreV1().Nodes().Get(n.nthConfig.NodeName, metav1.GetOptions{})
}
func getDrainHelper(nthConfig config.Config) (*drain.Helper, error) {
drainHelper := &drain.Helper{
Client: &kubernetes.Clientset{},
Force: true,
GracePeriodSeconds: nthConfig.PodTerminationGracePeriod,
IgnoreAllDaemonSets: nthConfig.IgnoreDaemonSets,
DeleteLocalData: nthConfig.DeleteLocalData,
Timeout: time.Duration(nthConfig.NodeTerminationGracePeriod) * time.Second,
Out: os.Stdout,
ErrOut: os.Stderr,
}
if nthConfig.DryRun {
return drainHelper, nil
}
clusterConfig, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(clusterConfig)
if err != nil {
return nil, err
}
drainHelper.Client = clientset
return drainHelper, nil
}
func getSystemUptime(filename string) (float64, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return 0, fmt.Errorf("Not able to read %s: %w", filename, err)
}
uptime, err := strconv.ParseFloat(strings.Split(string(data), " ")[0], 64)
if err != nil {
return 0, fmt.Errorf("Not able to parse %s to Float64: %w", filename, err)
}
return uptime, nil
}
func jsonPatchEscape(value string) string {
value = strings.Replace(value, "~", "~0", -1)
return strings.Replace(value, "/", "~1", -1)
}