-
Notifications
You must be signed in to change notification settings - Fork 93
/
controller.go
252 lines (197 loc) · 4.58 KB
/
controller.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
246
247
248
249
250
251
252
package kail
import (
"context"
"time"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
lifecycle "github.com/boz/go-lifecycle"
logutil "github.com/boz/go-logutil"
"github.com/boz/kcache"
"github.com/boz/kcache/nsname"
"github.com/boz/kcache/types/pod"
)
const (
eventBufsiz = 500
)
type Controller interface {
Events() <-chan Event
Close()
Done() <-chan struct{}
}
func NewController(
ctx context.Context,
cs kubernetes.Interface,
rc *rest.Config,
pcontroller pod.Controller,
filter ContainerFilter,
since time.Duration) (Controller, error) {
pods, err := pcontroller.Subscribe()
if err != nil {
return nil, err
}
initial, err := pods.Cache().List()
if err != nil {
pods.Close()
return nil, err
}
lc := lifecycle.New()
go lc.WatchContext(ctx)
log := logutil.FromContextOrDefault(ctx)
log = log.WithComponent("kail.controller")
c := &controller{
cs: cs,
rc: rc,
pods: pods,
filter: filter,
mconfig: monitorConfig{since: since},
eventch: make(chan Event, eventBufsiz),
monitorch: make(chan eventSource),
monitors: make(map[nsname.NSName]podMonitors),
log: log,
ctx: ctx,
lc: lc,
}
go c.run(initial)
return c, nil
}
type controller struct {
cs kubernetes.Interface
rc *rest.Config
pods pod.Subscription
filter ContainerFilter
eventch chan Event
monitorch chan eventSource
monitors monitors
mconfig monitorConfig
log logutil.Log
ctx context.Context
lc lifecycle.Lifecycle
}
type podMonitors map[eventSource]monitor
type monitors map[nsname.NSName]podMonitors
func (c *controller) Events() <-chan Event {
return c.eventch
}
func (c *controller) Done() <-chan struct{} {
return c.lc.Done()
}
func (c *controller) Close() {
c.lc.Shutdown(nil)
}
func (c *controller) run(initial []*v1.Pod) {
defer c.log.Un(c.log.Trace("run"))
defer c.lc.ShutdownCompleted()
peventch := c.pods.Events()
shutdownch := c.lc.ShutdownRequest()
draining := false
c.createInitialMonitors(initial)
for {
c.log.Debugf("loop draining:%v monitors:%v", draining, len(c.monitors))
if draining && len(c.monitors) == 0 {
break
}
select {
case err := <-shutdownch:
c.log.Debugf("shutdown requested: %v", err)
c.lc.ShutdownInitiated(err)
shutdownch = nil
draining = true
case ev, ok := <-peventch:
if !ok {
c.log.Debugf("pods closed")
peventch = nil
if !draining {
c.lc.ShutdownInitiated(nil)
shutdownch = nil
draining = true
}
break
}
if !draining {
c.handlePodEvent(ev)
}
case source := <-c.monitorch:
if pms, ok := c.monitors[source.id]; ok {
if _, ok := pms[source]; ok {
c.log.Debugf("removing source %v", source)
delete(pms, source)
if len(pms) == 0 {
c.log.Debugf("removing pod %v", source.id)
delete(c.monitors, source.id)
}
break
}
}
c.log.Warnf("attempted to remove unknown source: %v", source)
}
}
c.pods.Close()
<-c.pods.Done()
}
func (c *controller) handlePodEvent(ev pod.Event) {
pod := ev.Resource()
id := nsname.ForObject(pod)
c.log.Debugf("event %v %v/%v",
ev.Type(), ev.Resource().GetName(), ev.Resource().GetNamespace())
if ev.Type() == kcache.EventTypeDelete {
if pms, ok := c.monitors[id]; ok {
for _, pm := range pms {
pm.Shutdown()
}
}
return
}
c.ensureMonitorsForPod(pod)
}
func (c *controller) ensureMonitorsForPod(pod *v1.Pod) {
id, sources := sourcesForPod(c.filter, pod)
c.log.Debugf("pod %v/%v: %v containers ready",
pod.GetNamespace(), pod.GetName(), len(sources))
// delete monitors of not-ready containers
if pms, ok := c.monitors[id]; ok {
for source, pm := range pms {
if !sources[source] {
pm.Shutdown()
}
}
}
if len(sources) == 0 {
return
}
pms, ok := c.monitors[id]
if !ok {
pms = make(map[eventSource]monitor)
}
for source, _ := range sources {
if _, ok := pms[source]; ok {
continue
}
pms[source] = c.createMonitor(source)
}
c.monitors[id] = pms
}
func (c *controller) createMonitor(source eventSource) monitor {
defer c.log.Un(c.log.Trace("createMonitor(%v)", source))
m := newMonitor(c, &source, c.mconfig)
go func() {
select {
case <-m.Done():
case <-c.lc.ShuttingDown():
m.Shutdown()
<-m.Done()
}
select {
case c.monitorch <- source:
case <-c.lc.Done():
c.log.Warnf("done before monitor %v unregistered", source)
}
}()
return m
}
func (c *controller) createInitialMonitors(pods []*v1.Pod) {
defer c.log.Un(c.log.Trace("createInitialMonitors(pods=%v)", len(pods)))
for _, pod := range pods {
c.ensureMonitorsForPod(pod)
}
}