-
Notifications
You must be signed in to change notification settings - Fork 684
/
kubewatchman.go
245 lines (209 loc) · 5.68 KB
/
kubewatchman.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
243
244
245
package thingkube
import (
"fmt"
"sync"
"github.com/datawire/ambassador/cmd/watt/watchapi"
"github.com/datawire/ambassador/pkg/k8s"
"github.com/datawire/ambassador/pkg/supervisor"
"github.com/datawire/ambassador/pkg/watt"
)
type K8sEvent struct {
WatchID string
Kind string
Resources []k8s.Resource
Errors []watt.Error
}
// makeErrorEvent returns a K8sEvent that contains one error entry for each
// message passed in, all attributed to the same source.
func makeErrorEvent(source string, messages ...string) K8sEvent {
errors := make([]watt.Error, len(messages))
for idx, message := range messages {
errors[idx] = watt.NewError(source, message)
}
return K8sEvent{Errors: errors}
}
type KubernetesWatchMaker struct {
kubeAPI *k8s.Client
notify chan<- K8sEvent
}
func (m *KubernetesWatchMaker) MakeKubernetesWatch(spec watchapi.KubernetesWatchSpec) (*supervisor.Worker, error) {
var worker *supervisor.Worker
var err error
worker = &supervisor.Worker{
Name: fmt.Sprintf("kubernetes:%s", spec.WatchId()),
Work: func(p *supervisor.Process) error {
watcher := m.kubeAPI.Watcher()
watchFunc := func(watchId, ns, kind string) func(watcher *k8s.Watcher) {
return func(watcher *k8s.Watcher) {
resources := watcher.List(kind)
p.Debugf("found %d %q in namespace %q", len(resources), kind, fmtNamespace(ns))
m.notify <- K8sEvent{WatchID: watchId, Kind: kind, Resources: resources}
p.Debugf("sent %q to receivers", kind)
}
}
watcherErr := watcher.WatchQuery(k8s.Query{
Namespace: spec.Namespace,
Kind: spec.Kind,
FieldSelector: spec.FieldSelector,
LabelSelector: spec.LabelSelector,
}, watchFunc(spec.WatchId(), spec.Namespace, spec.Kind))
if watcherErr != nil {
return watcherErr
}
watcher.Start()
<-p.Shutdown()
watcher.Stop()
return nil
},
Retry: true,
}
return worker, err
}
type kubewatchman struct {
WatchMaker watchapi.IKubernetesWatchMaker
in <-chan []watchapi.KubernetesWatchSpec
mu sync.RWMutex
watched map[string]*supervisor.Worker
}
type KubeWatchMan interface {
Work(*supervisor.Process) error
NumWatched() int
WithWatched(func(map[string]*supervisor.Worker))
}
func NewKubeWatchMan(
client *k8s.Client,
eventsCh chan<- K8sEvent,
watchesCh <-chan []watchapi.KubernetesWatchSpec,
) KubeWatchMan {
return &kubewatchman{
WatchMaker: &KubernetesWatchMaker{
kubeAPI: client,
notify: eventsCh,
},
in: watchesCh,
}
}
func (w *kubewatchman) Work(p *supervisor.Process) error {
p.Ready()
w.mu.Lock()
w.watched = make(map[string]*supervisor.Worker)
w.mu.Unlock()
for {
select {
case watches := <-w.in:
w.mu.Lock()
found := make(map[string]*supervisor.Worker)
p.Debugf("processing %d kubernetes watch specs", len(watches))
for _, spec := range watches {
worker, err := w.WatchMaker.MakeKubernetesWatch(spec)
if err != nil {
p.Logf("failed to create kubernetes watcher: %v", err)
continue
}
if _, exists := w.watched[worker.Name]; exists {
found[worker.Name] = w.watched[worker.Name]
} else {
p.Debugf("add kubernetes watcher %s\n", worker.Name)
p.Supervisor().Supervise(worker)
w.watched[worker.Name] = worker
found[worker.Name] = worker
}
}
for workerName, worker := range w.watched {
if _, exists := found[workerName]; !exists {
p.Debugf("remove kubernetes watcher %s\n", workerName)
worker.Shutdown()
worker.Wait()
}
}
w.watched = found
w.mu.Unlock()
case <-p.Shutdown():
p.Debugf("shutdown initiated")
return nil
}
}
}
type kubebootstrap struct {
namespace string
kinds []string
fieldSelector string
labelSelector string
notify []chan<- K8sEvent
kubeAPIWatcher *k8s.Watcher
}
type KubeBootstrap interface {
SaveError(message string)
Work(*supervisor.Process) error
}
func NewKubeBootstrap(
namespace string,
kinds []string,
fieldSelector string,
labelSelector string,
notify []chan<- K8sEvent,
kubeAPIWatcher *k8s.Watcher,
) KubeBootstrap {
return &kubebootstrap{
namespace: namespace,
kinds: kinds,
fieldSelector: fieldSelector,
labelSelector: labelSelector,
notify: notify,
kubeAPIWatcher: kubeAPIWatcher,
}
}
func fmtNamespace(ns string) string {
if ns == "" {
return "*"
}
return ns
}
// SaveError emits an error from kubebootstrap with the given message
func (b *kubebootstrap) SaveError(message string) {
evt := makeErrorEvent("kubebootstrap", message)
for _, n := range b.notify {
n <- evt
}
}
func (b *kubebootstrap) Work(p *supervisor.Process) error {
for _, kind := range b.kinds {
p.Debugf("adding kubernetes watch for %q in namespace %q", kind, fmtNamespace(b.namespace))
watcherFunc := func(ns, kind string) func(watcher *k8s.Watcher) {
return func(watcher *k8s.Watcher) {
resources := watcher.List(kind)
p.Debugf("found %d %q in namespace %q", len(resources), kind, fmtNamespace(ns))
for _, n := range b.notify {
n <- K8sEvent{Kind: kind, Resources: resources}
}
p.Debugf("sent %q to %d receivers", kind, len(b.notify))
}
}
err := b.kubeAPIWatcher.WatchQuery(k8s.Query{
Namespace: b.namespace,
Kind: kind,
FieldSelector: b.fieldSelector,
LabelSelector: b.labelSelector,
}, watcherFunc(b.namespace, kind))
if err != nil {
return err
}
}
b.kubeAPIWatcher.Start()
p.Ready()
for range p.Shutdown() {
p.Debugf("shutdown initiated")
b.kubeAPIWatcher.Stop()
}
return nil
}
func (w *kubewatchman) NumWatched() int {
w.mu.RLock()
defer w.mu.RUnlock()
return len(w.watched)
}
func (w *kubewatchman) WithWatched(fn func(map[string]*supervisor.Worker)) {
w.mu.RLock()
defer w.mu.RUnlock()
fn(w.watched)
}