-
Notifications
You must be signed in to change notification settings - Fork 34
/
spot.go
96 lines (84 loc) · 2.34 KB
/
spot.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
package lifecycled
import (
"context"
"errors"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/sirupsen/logrus"
)
// NewSpotListener ...
func NewSpotListener(instanceID string, metadata *ec2metadata.EC2Metadata, interval time.Duration) *SpotListener {
return &SpotListener{
listenerType: "spot",
instanceID: instanceID,
metadata: metadata,
interval: interval,
}
}
// SpotListener ...
type SpotListener struct {
listenerType string
instanceID string
metadata *ec2metadata.EC2Metadata
interval time.Duration
}
// Type returns a string describing the listener type.
func (l *SpotListener) Type() string {
return l.listenerType
}
// Start the spot termination notice listener.
func (l *SpotListener) Start(ctx context.Context, notices chan<- TerminationNotice, log *logrus.Entry) error {
if !l.metadata.Available() {
return errors.New("ec2 metadata is not available")
}
ticker := time.NewTicker(l.interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
log.Debug("Polling ec2 metadata for spot termination notices")
out, err := l.metadata.GetMetadata("spot/termination-time")
if err != nil {
if e, ok := err.(awserr.Error); ok && strings.Contains(e.OrigErr().Error(), "404") {
// Metadata returns 404 when there is no termination notice available
continue
} else {
log.WithError(err).Warn("Failed to get spot termination")
continue
}
}
if out == "" {
log.Error("Empty response from metadata")
continue
}
t, err := time.Parse(time.RFC3339, out)
if err != nil {
log.WithError(err).Error("Failed to parse termination time")
continue
}
notices <- &spotTerminationNotice{
noticeType: l.Type(),
instanceID: l.instanceID,
transition: "ec2:SPOT_INSTANCE_TERMINATION",
terminationTime: t,
}
return nil
}
}
}
type spotTerminationNotice struct {
noticeType string
instanceID string
transition string
terminationTime time.Time
}
func (n *spotTerminationNotice) Type() string {
return n.noticeType
}
func (n *spotTerminationNotice) Handle(ctx context.Context, handler Handler, _ *logrus.Entry) error {
return handler.Execute(ctx, n.transition, n.instanceID, n.terminationTime.Format(time.RFC3339))
}