forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
156 lines (140 loc) · 3.24 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
package k8s
import (
"fmt"
"sync"
"github.com/influxdata/kapacitor/services/k8s/client"
"github.com/influxdata/kapacitor/services/scraper"
)
// Doesn't actually get used, but its good to have a template here already
type Diagnostic interface {
WithClusterContext(cluster string) Diagnostic
}
// Service is the kubernetes discovery and autoscale service
type Service struct {
mu sync.Mutex
configs []Config
clusters map[string]*Cluster
registry scraper.Registry
diag Diagnostic
}
// NewService creates a new unopened k8s 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,
diag: d,
registry: r,
}, nil
}
// Open starts the kubernetes 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()
}
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)
}
}
}
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"`
}
func (s *Service) TestOptions() interface{} {
return new(testOptions)
}
func (s *Service) Test(options interface{}) error {
o, ok := options.(*testOptions)
if !ok {
return fmt.Errorf("unexpected options type %T", options)
}
s.mu.Lock()
cluster, ok := s.clusters[o.ID]
s.mu.Unlock()
if !ok {
return fmt.Errorf("unknown kubernetes cluster %q", o.ID)
}
return cluster.Test()
}
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 kubernetes cluster %q, cannot get client", id)
}
return cluster.Client()
}