-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.go
68 lines (56 loc) · 1.44 KB
/
watcher.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
package shutter
import (
"github.com/aws/aws-sdk-go/service/autoscaling"
"go.uber.org/zap"
"time"
)
var (
inServiceLifecycleState = "InService"
terminatingLifecycleState = "Terminating:Wait"
)
// watcher watches terminating EC2 instances under a Lifecycle Hook
type watcher struct {
client AwsClient
config *Config
logger *zap.Logger
}
func NewWatcher(client AwsClient, config *Config, logger *zap.Logger) *watcher {
return &watcher{
client: client,
config: config,
logger: logger,
}
}
func (w *watcher) Watch() ([]autoscaling.Instance, error) {
g, err := w.client.DescribeAutoscalingGroup(w.config.Watcher.AutoscalingGroupName)
if err != nil {
return nil, err
}
w.logger.Info("describe instances under the autoscaling group", zap.Reflect("instances", g.Instances))
instances := []autoscaling.Instance{}
for _, i := range g.Instances {
if *i.LifecycleState == terminatingLifecycleState {
instances = append(instances, *i)
}
}
w.logger.Info("filter terminating instances", zap.Reflect("instances", instances))
return instances, nil
}
func (w *watcher) Notify(channel chan autoscaling.Instance) error {
instances, err := w.Watch()
if err != nil {
return err
}
for _, i := range instances {
channel <- i
}
return nil
}
func (w *watcher) Start(channel chan autoscaling.Instance) error {
for {
if err := w.Notify(channel); err != nil {
return err
}
time.Sleep(time.Second * w.config.Watcher.IntervalSec)
}
}