-
Notifications
You must be signed in to change notification settings - Fork 495
/
service.go
194 lines (171 loc) · 4.11 KB
/
service.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
package ec2
import (
"context"
"fmt"
"sync"
"time"
"github.com/influxdata/kapacitor/services/ec2/client"
"github.com/influxdata/kapacitor/services/scraper"
pec2 "github.com/prometheus/prometheus/discovery/ec2"
"github.com/prometheus/prometheus/discovery/targetgroup"
)
type Diagnostic interface {
scraper.Diagnostic
WithClusterContext(region string) Diagnostic
}
// Service is the ec2 discovery and autoscale service
type Service struct {
Configs []Config
mu sync.Mutex
clusters map[string]*Cluster
registry scraper.Registry
diag Diagnostic
open bool
}
// NewService creates a new unopened service
func NewService(c []Config, r scraper.Registry, d Diagnostic) (*Service, error) {
clusters := make(map[string]*Cluster, len(c))
for i := range c {
cluster, err := NewCluster(c[i], d.WithClusterContext(c[i].ID))
if err != nil {
return nil, err
}
clusters[c[i].ID] = cluster
}
return &Service{
clusters: clusters,
Configs: c,
registry: r,
diag: d,
}, nil
}
// Open starts the service
func (s *Service) Open() error {
s.mu.Lock()
defer s.mu.Unlock()
for _, c := range s.clusters {
if err := c.Open(); err != nil {
return err
}
}
s.register()
return s.registry.Commit()
}
// Close stops the service
func (s *Service) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
for _, c := range s.clusters {
c.Close()
}
s.deregister()
return s.registry.Commit()
}
func (s *Service) deregister() {
// Remove all the configurations in the registry
for _, d := range s.Configs {
s.registry.RemoveDiscoverer(&d)
}
}
func (s *Service) register() {
// Add all configurations to registry
for _, d := range s.Configs {
if d.Enabled {
s.registry.AddDiscoverer(d)
}
}
}
// Update updates configuration while running
func (s *Service) Update(newConfigs []interface{}) error {
s.mu.Lock()
defer s.mu.Unlock()
configs := make([]Config, len(newConfigs))
existingClusters := make(map[string]bool, len(newConfigs))
for i := range newConfigs {
c, ok := newConfigs[i].(Config)
if !ok {
return fmt.Errorf("expected config object to be of type %T, got %T", c, newConfigs[i])
}
configs[i] = c
cluster, ok := s.clusters[c.ID]
if !ok {
var err error
cluster, err = NewCluster(c, s.diag.WithClusterContext(c.ID))
if err != nil {
return err
}
if err := cluster.Open(); err != nil {
return err
}
s.clusters[c.ID] = cluster
} else {
if err := cluster.Update(c); err != nil {
return err
}
}
existingClusters[c.ID] = true
}
// Close and delete any removed clusters
for id := range s.clusters {
if !existingClusters[id] {
s.clusters[id].Close()
delete(s.clusters, id)
}
}
s.deregister()
s.Configs = configs
s.register()
return nil
}
type testOptions struct {
ID string `json:"id"`
}
// TestOptions returns an object that is in turn passed to Test.
func (s *Service) TestOptions() interface{} {
return &testOptions{}
}
// Test a service with the provided options.
func (s *Service) Test(options interface{}) error {
o, ok := options.(*testOptions)
if !ok {
return fmt.Errorf("unexpected options type %T", options)
}
found := -1
for i := range s.Configs {
if s.Configs[i].ID == o.ID && s.Configs[i].Enabled {
found = i
}
}
if found < 0 {
return fmt.Errorf("discoverer %q is not enabled or does not exist", o.ID)
}
sd := s.Configs[found].PromConfig()
discoverer := pec2.NewDiscovery(sd, s.diag)
ctx, cancel := context.WithCancel(context.Background())
updates := make(chan []*targetgroup.Group)
go discoverer.Run(ctx, updates)
var err error
select {
case _, ok := <-updates:
// Handle the case that a target provider exits and closes the channel
// before the context is done.
if !ok {
err = fmt.Errorf("discoverer %q exited ", o.ID)
}
break
case <-time.After(30 * time.Second):
err = fmt.Errorf("timeout waiting for discoverer %q to return", o.ID)
break
}
cancel()
return err
}
func (s *Service) Client(id string) (client.Client, error) {
s.mu.Lock()
cluster, ok := s.clusters[id]
s.mu.Unlock()
if !ok {
return nil, fmt.Errorf("unknown ec2 region %q, cannot get client", id)
}
return cluster.Client()
}