-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotdrain.go
242 lines (208 loc) · 5.92 KB
/
spotdrain.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
package main
import (
"context"
"encoding/json"
"errors"
"io"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/smithy-go"
awshttp "github.com/aws/smithy-go/transport/http"
nomadapi "github.com/hashicorp/nomad/api"
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
ddapi "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
)
// Global channel used to gracefully stop ticker
var done = make(chan bool)
func main() {
imdsClient := createImdsClient()
nomadClient := createNomadClient()
ddClient, ddCtx, ddEnv := createDataDogClient()
if !isSpotInstance(imdsClient) {
log.Print("This is not a spot instance. Spotdrain will not run")
log.Print("Exiting ...")
os.Exit(0)
}
instanceId := getEC2InstanceId(imdsClient)
registered, nodeId := checkNodeRegistered(nomadClient, instanceId)
if !registered {
log.Print("This instance is not registered on Nomad. Spotdrain will not run")
log.Print("Exiting...")
os.Exit(1)
}
ticker := time.NewTicker(10 * time.Second)
go func() {
for {
select {
case <-done:
log.Print("Stopping ticker")
return
case <-ticker.C:
log.Print("Waking up and executing check")
if checkMarkedForInterruption(imdsClient) {
triggerNomadNodeDrain(nomadClient, nodeId)
sendDatadogEvent(ddClient, ddCtx, instanceId, ddEnv)
log.Print("Job's done. Exiting...")
os.Exit(0)
}
}
}
}()
// Block so we don't exit
select {}
}
func init() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
log.Print("Stopping ...")
done <- true
// Wait half a second for the ticker to properly close
time.Sleep(500 * time.Millisecond)
os.Exit(155)
}()
}
func createNomadClient() *nomadapi.Client {
nomadToken := getNomadTokenFromEnv()
config := nomadapi.DefaultConfig()
config.Address = "https://127.0.0.1:4646"
config.Headers = make(http.Header)
config.Headers.Add("X-Nomad-Token", nomadToken)
client, err := nomadapi.NewClient(config)
if err != nil {
log.Fatal("Fatal: ", err)
}
return client
}
func getNomadTokenFromEnv() string {
token, exists := os.LookupEnv("SPOTDRAIN_NOMAD_TOKEN")
if !exists {
log.Fatal("Fatal: Nomad Auth Token not present in environment variable SPOTDRAIN_NOMAD_TOKEN")
}
return token
}
func createImdsClient() *imds.Client {
return imds.New(imds.Options{})
}
func createDataDogClient() (*datadog.APIClient, context.Context, string) {
ctx := context.WithValue(
context.Background(),
datadog.ContextAPIKeys,
map[string]datadog.APIKey{
"apiKeyAuth": {
Key: os.Getenv("DD_CLIENT_API_KEY"),
},
"appKeyAuth": {
Key: os.Getenv("DD_CLIENT_APP_KEY"),
},
},
)
conf := datadog.NewConfiguration()
client := datadog.NewAPIClient(conf)
env, exists := os.LookupEnv("DD_ENV")
if !exists {
log.Print("WARN: Datadog env not found. Event will not have env tag")
}
return client, ctx, env
}
func isSpotInstance(client *imds.Client) bool {
mdi := &imds.GetMetadataInput{Path: "/instance-life-cycle"}
output, err := client.GetMetadata(context.TODO(), mdi)
if err != nil {
log.Fatal("Fatal: ", err)
}
bytes, err := io.ReadAll(output.Content)
if err != nil {
log.Fatal("Fatal: ", err)
}
return strings.EqualFold(string(bytes), "spot")
}
func getEC2InstanceId(client *imds.Client) string {
mdi := &imds.GetMetadataInput{Path: "/instance-id"}
output, err := client.GetMetadata(context.TODO(), mdi)
if err != nil {
log.Fatal("Fatal: ", err)
}
bytes, err := io.ReadAll(output.Content)
if err != nil {
log.Fatal("Fatal: ", err)
}
return string(bytes)
}
func checkMarkedForInterruption(client *imds.Client) bool {
marked := false
mdi := &imds.GetMetadataInput{Path: "/spot/instance-action"}
output, err := client.GetMetadata(context.TODO(), mdi)
if err != nil {
var oe *smithy.OperationError
if errors.As(err, &oe) {
var re *awshttp.ResponseError
if errors.As(oe.Unwrap(), &re) {
if re.HTTPStatusCode() == 404 {
log.Print("Metadata not available")
return marked
}
}
}
log.Fatal("Fatal: ", err)
}
bytes, err := io.ReadAll(output.Content)
if err != nil {
log.Fatal("Fatal: ", err)
}
var ia InstanceAction
err = json.Unmarshal(bytes, &ia)
if err != nil {
log.Fatal("Fatal: ", err)
}
marked = true
log.Printf("Instance is Marked for interruption! Action: %s, Time: %s", ia.Action, ia.Time)
return marked
}
func checkNodeRegistered(client *nomadapi.Client, instanceId string) (bool, string) {
nodeList, _, err := client.Nodes().List(&nomadapi.QueryOptions{})
if err != nil {
log.Fatal("Fatal: ", err)
}
for _, node := range nodeList {
if strings.EqualFold(instanceId, node.Name) {
log.Print("This host is registered on Nomad")
return true, node.ID
}
}
log.Print("Could not find this instance registered in Nomad")
return false, ""
}
func triggerNomadNodeDrain(client *nomadapi.Client, nodeId string) {
ds := &nomadapi.DrainSpec{Deadline: 60 * time.Second, IgnoreSystemJobs: true}
wo := &nomadapi.WriteOptions{}
_, err := client.Nodes().UpdateDrain(nodeId, ds, false, wo)
if err != nil {
log.Fatal("Fatal: Error triggering Nomad Node Drain: ", err)
}
log.Println("Triggered Nomad Node Drain")
}
func sendDatadogEvent(client *datadog.APIClient, ctx context.Context, instanceId string, ddEnv string) {
tags := []string{"service:spotdrain", "spotdrain:termination_notice", "env:" + ddEnv}
er := ddapi.NewEventCreateRequest("This instance has received a termination notice from AWS EC2", "Spot-Instance-Termination-Notice")
er.SetPriority(ddapi.EVENTPRIORITY_NORMAL)
er.SetAlertType(ddapi.EVENTALERTTYPE_WARNING)
er.SetHost(instanceId)
er.SetTags(tags)
ecr, _, err := ddapi.NewEventsApi(client).CreateEvent(ctx, *er)
if err != nil {
log.Fatal("Fatal: Error creating datadog event: ", err)
}
log.Print("Sending Datadog event: ", ecr.GetStatus())
}
type InstanceAction struct {
Action string `json:"action"`
Time time.Time `json:"time"`
}