-
Notifications
You must be signed in to change notification settings - Fork 8
/
watch_session.go
152 lines (119 loc) · 3.05 KB
/
watch_session.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
package kcache
import (
"context"
lifecycle "github.com/boz/go-lifecycle"
logutil "github.com/boz/go-logutil"
"github.com/boz/kcache/client"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
)
type watchSession interface {
events() <-chan Event
done() <-chan struct{}
stop()
Error() error
}
type nullWatchSession struct{}
func (nullWatchSession) events() <-chan Event { return nil }
func (nullWatchSession) done() <-chan struct{} { return nil }
func (nullWatchSession) stop() {}
func (nullWatchSession) Error() error { return nil }
type _watchSession struct {
client client.WatchClient
version string
outch chan Event
ctx context.Context
cancel context.CancelFunc
log logutil.Log
lc lifecycle.Lifecycle
}
func newWatchSession(ctx context.Context, log logutil.Log, client client.WatchClient, version string) watchSession {
lc := lifecycle.New()
ctx, cancel := context.WithCancel(ctx)
s := &_watchSession{
client: client,
version: version,
outch: make(chan Event, EventBufsiz),
ctx: ctx,
cancel: cancel,
log: log.WithComponent("watch-session"),
lc: lc,
}
go lc.WatchContext(ctx)
go s.run()
return s
}
func (s *_watchSession) done() <-chan struct{} {
return s.lc.Done()
}
func (s *_watchSession) stop() {
s.lc.ShutdownAsync(nil)
}
func (s *_watchSession) Error() error {
return s.lc.Error()
}
func (s *_watchSession) events() <-chan Event {
return s.outch
}
func (s *_watchSession) run() {
defer s.lc.ShutdownCompleted()
defer s.cancel()
conn, err := s.connect()
if err != nil {
s.log.Debugf("connecting to server: %v", err)
s.lc.ShutdownInitiated(errors.Wrap(err, "connecting to server"))
return
}
defer conn.Stop()
for {
select {
case err := <-s.lc.ShutdownRequest():
s.lc.ShutdownInitiated(err)
return
case kevt, ok := <-conn.ResultChan():
if !ok {
s.lc.ShutdownInitiated(nil)
return
}
if status, ok := kevt.Object.(*metav1.Status); ok {
s.logStatus(status)
continue
}
obj, err := meta.Accessor(kevt.Object)
if err != nil {
s.lc.ShutdownInitiated(errors.Wrap(err, "meta accessor"))
return
}
var evt Event
switch kevt.Type {
case watch.Added:
evt = NewEvent(EventTypeCreate, obj)
case watch.Modified:
evt = NewEvent(EventTypeUpdate, obj)
case watch.Deleted:
evt = NewEvent(EventTypeDelete, obj)
}
if evt == nil {
s.log.Debugf("unknown event type: %v", kevt.Type)
continue
}
select {
case s.outch <- evt:
default:
s.log.Warnf("output buffer full; event missed.")
}
}
}
}
func (s *_watchSession) connect() (watch.Interface, error) {
response, err := s.client.Watch(s.ctx, metav1.ListOptions{
ResourceVersion: s.version,
Watch: true,
})
return response, err
}
func (s *_watchSession) logStatus(status *metav1.Status) {
s.log.Debugf("STATUS: %v %v %v [code: %v vsn: %v]", status.Status, status.Message, status.Reason, status.Code, status.GetResourceVersion())
}