-
Notifications
You must be signed in to change notification settings - Fork 290
/
event_watch_manager.go
202 lines (170 loc) · 5.59 KB
/
event_watch_manager.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
package k8swatch
import (
"context"
"sync"
"time"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/windmilleng/tilt/internal/k8s"
"github.com/windmilleng/tilt/internal/store"
"github.com/windmilleng/tilt/pkg/logger"
"github.com/windmilleng/tilt/pkg/model"
)
// TODO(nick): Right now, the EventWatchManager, PodWatcher, and ServiceWatcher
// all look very similar, with a few subtle differences (particularly in how
// we decide whether two objects are related, and how we index those relationships).
//
// We're probably missing some abstractions here.
//
// TODO(nick): We should also add garbage collection and/or handle Delete events
// from the kubernetes informer properly.
type EventWatchManager struct {
kClient k8s.Client
ownerFetcher k8s.OwnerFetcher
watching bool
mu sync.RWMutex
knownDeployedUIDs map[types.UID]model.ManifestName
// An index that maps the UID of Kubernetes resources to the UIDs of
// all events that they own (transitively).
//
// For example, a Deployment UID might contain a set of N event UIDs.
knownDescendentEventUIDs map[types.UID]store.UIDSet
// An index of all the known events, by UID
knownEvents map[types.UID]*v1.Event
}
func NewEventWatchManager(kClient k8s.Client, ownerFetcher k8s.OwnerFetcher) *EventWatchManager {
return &EventWatchManager{
kClient: kClient,
ownerFetcher: ownerFetcher,
knownDeployedUIDs: make(map[types.UID]model.ManifestName),
knownDescendentEventUIDs: make(map[types.UID]store.UIDSet),
knownEvents: make(map[types.UID]*v1.Event),
}
}
type eventWatchTaskList struct {
watcherTaskList
tiltStartTime time.Time
}
func (m *EventWatchManager) diff(st store.RStore) eventWatchTaskList {
state := st.RLockState()
defer st.RUnlockState()
m.mu.RLock()
defer m.mu.RUnlock()
watcherTaskList := createWatcherTaskList(state, m.knownDeployedUIDs)
if m.watching {
watcherTaskList.needsWatch = false
}
return eventWatchTaskList{
watcherTaskList: watcherTaskList,
tiltStartTime: state.TiltStartTime,
}
}
func (m *EventWatchManager) OnChange(ctx context.Context, st store.RStore) {
taskList := m.diff(st)
if taskList.needsWatch {
m.setupWatch(ctx, st, taskList.tiltStartTime)
}
if len(taskList.newUIDs) > 0 {
m.setupNewUIDs(ctx, st, taskList.newUIDs)
}
}
func (m *EventWatchManager) setupWatch(ctx context.Context, st store.RStore, tiltStartTime time.Time) {
m.watching = true
ch, err := m.kClient.WatchEvents(ctx)
if err != nil {
err = errors.Wrap(err, "Error watching k8s events\n")
st.Dispatch(store.NewErrorAction(err))
return
}
go m.dispatchEventsLoop(ctx, ch, st, tiltStartTime)
}
// When new UIDs are deployed, go through all our known events and dispatch
// new actions. This handles the case where we get the event
// before the deploy id shows up in the manifest, which is way more common than
// you would think.
func (m *EventWatchManager) setupNewUIDs(ctx context.Context, st store.RStore, newUIDs map[types.UID]model.ManifestName) {
m.mu.Lock()
defer m.mu.Unlock()
for uid, mn := range newUIDs {
m.knownDeployedUIDs[uid] = mn
descendants := m.knownDescendentEventUIDs[uid]
for uid := range descendants {
event, ok := m.knownEvents[uid]
if ok {
st.Dispatch(store.NewK8sEventAction(event, mn))
}
}
}
}
// Check to see if this event corresponds to any of our manifests.
//
// We do this by comparing the event's InvolvedObject UID and its owner UIDs
// against what we've deployed to the cluster. Returns the ManifestName that it
// matched against.
//
// If the event doesn't match an existing deployed resource, keep it in local
// state, so we can match it later if the owner UID shows up.
func (m *EventWatchManager) triageEventUpdate(event *v1.Event, objTree k8s.ObjectRefTree) model.ManifestName {
m.mu.Lock()
defer m.mu.Unlock()
uid := event.UID
m.knownEvents[uid] = event
// Set up the descendent index of the involved object
for _, ownerUID := range objTree.UIDs() {
set, ok := m.knownDescendentEventUIDs[ownerUID]
if !ok {
set = store.NewUIDSet()
m.knownDescendentEventUIDs[ownerUID] = set
}
set.Add(uid)
}
// Find the manifest name
for _, ownerUID := range objTree.UIDs() {
mn, ok := m.knownDeployedUIDs[ownerUID]
if ok {
return mn
}
}
return ""
}
func (m *EventWatchManager) dispatchEventChange(ctx context.Context, event *v1.Event, st store.RStore) {
objTree, err := m.ownerFetcher.OwnerTreeOfRef(ctx, event.InvolvedObject)
if err != nil {
logger.Get(ctx).Infof("Error handling event update (%q): %v", event.Name, err)
return
}
mn := m.triageEventUpdate(event, objTree)
if mn == "" {
return
}
st.Dispatch(store.NewK8sEventAction(event, mn))
}
func (m *EventWatchManager) dispatchEventsLoop(ctx context.Context, ch <-chan *v1.Event, st store.RStore, tiltStartTime time.Time) {
for {
select {
case event, ok := <-ch:
if !ok {
return
}
// on startup, k8s will give us a bunch of event objects that happened
// before tilt started, which leads to flooding the k8s api with lookups
// on those events' involvedObjects we don't care about those events, so
// ignore them.
//
// TODO(nick): We might need to remove this check and optimize
// it in a different way. We want Tilt to be to attach to existing
// resources, and these resources might have pre-existing events.
if event.ObjectMeta.CreationTimestamp.Time.Before(tiltStartTime) {
continue
}
// Ignore normal events.
if event.Type == v1.EventTypeNormal {
continue
}
go m.dispatchEventChange(ctx, event, st)
case <-ctx.Done():
return
}
}
}