-
Notifications
You must be signed in to change notification settings - Fork 290
/
watch.go
279 lines (245 loc) · 7.43 KB
/
watch.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package k8s
import (
"context"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
"github.com/windmilleng/tilt/pkg/logger"
)
var PodGVR = v1.SchemeGroupVersion.WithResource("pods")
var ServiceGVR = v1.SchemeGroupVersion.WithResource("services")
var EventGVR = v1.SchemeGroupVersion.WithResource("events")
// A wrapper object around SharedInformer objects, to make them
// a bit easier to use correctly.
type ObjectUpdate struct {
obj interface{}
isDelete bool
}
// Returns a Pod if this is a pod Add or a pod Update.
func (r ObjectUpdate) AsPod() (*v1.Pod, bool) {
if r.isDelete {
return nil, false
}
pod, ok := r.obj.(*v1.Pod)
return pod, ok
}
// Returns (namespace, name, isDelete).
//
// The informer's OnDelete handler sometimes gives us a structured object, and
// sometimes returns a DeletedFinalStateUnknown object. To make this easier to
// handle correctly, we never allow access to the OnDelete object. Instead, we
// force the caller to use AsDeletedKey() to get the identifier of the object.
//
// For more info, see:
// https://godoc.org/k8s.io/client-go/tools/cache#ResourceEventHandler
func (r ObjectUpdate) AsDeletedKey() (Namespace, string, bool) {
if !r.isDelete {
return "", "", false
}
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(r.obj)
if err != nil {
return "", "", false
}
ns, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return "", "", false
}
return Namespace(ns), name, true
}
type watcherFactory func(namespace string) watcher
type watcher interface {
Watch(options metav1.ListOptions) (watch.Interface, error)
}
func (kCli K8sClient) makeWatcher(f watcherFactory, ls labels.Selector) (watch.Interface, Namespace, error) {
// passing "" gets us all namespaces
w := f("")
if w == nil {
return nil, "", nil
}
watcher, err := w.Watch(metav1.ListOptions{LabelSelector: ls.String()})
if err == nil {
return watcher, "", nil
}
// If the request failed, we might be able to recover.
statusErr, isStatusErr := err.(*apiErrors.StatusError)
if !isStatusErr {
return nil, "", err
}
status := statusErr.ErrStatus
if status.Code == http.StatusForbidden {
// If this is a forbidden error, maybe the user just isn't allowed to watch this namespace.
// Let's narrow our request to just the config namespace, and see if that helps.
w := f(kCli.configNamespace.String())
if w == nil {
return nil, "", nil
}
watcher, err := w.Watch(metav1.ListOptions{LabelSelector: ls.String()})
if err == nil {
return watcher, kCli.configNamespace, nil
}
// ugh, it still failed. return the original error.
}
return nil, "", fmt.Errorf("%s, Reason: %s, Code: %d", status.Message, status.Reason, status.Code)
}
func (kCli K8sClient) makeInformer(
ctx context.Context,
gvr schema.GroupVersionResource,
ls labels.Selector) (cache.SharedInformer, error) {
// HACK(dmiller): There's no way to get errors out of an informer. See https://github.com/kubernetes/client-go/issues/155
// In the meantime, at least to get authorization and some other errors let's try to set up a watcher and then just
// throw it away.
watcher, ns, err := kCli.makeWatcher(func(ns string) watcher {
return kCli.dynamic.Resource(gvr).Namespace(ns)
}, ls)
if err != nil {
return nil, errors.Wrap(err, "makeInformer")
}
watcher.Stop()
options := []informers.SharedInformerOption{}
if !ls.Empty() {
options = append(options, informers.WithTweakListOptions(func(o *metav1.ListOptions) {
o.LabelSelector = ls.String()
}))
}
if ns != "" {
options = append(options, informers.WithNamespace(ns.String()))
}
factory := informers.NewSharedInformerFactoryWithOptions(kCli.clientset, 5*time.Second, options...)
resFactory, err := factory.ForResource(gvr)
if err != nil {
return nil, errors.Wrap(err, "makeInformer")
}
return resFactory.Informer(), nil
}
func (kCli K8sClient) WatchEvents(ctx context.Context) (<-chan *v1.Event, error) {
gvr := EventGVR
informer, err := kCli.makeInformer(ctx, gvr, labels.Everything())
if err != nil {
return nil, errors.Wrap(err, "WatchEvents")
}
ch := make(chan *v1.Event)
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
mObj, ok := obj.(*v1.Event)
if ok {
ch <- mObj
}
},
UpdateFunc: func(oldObj interface{}, newObj interface{}) {
mObj, ok := newObj.(*v1.Event)
if ok {
oldObj, ok := oldObj.(*v1.Event)
// the informer regularly gives us updates for events where cmp.Equal(oldObj, newObj) returns true.
// we have not investigated why it does this, but these updates seem to always be spurious and
// uninteresting.
// we could check cmp.Equal here, but really, `Count` is probably the only reason we even care about
// updates at all.
if !ok || oldObj.Count < mObj.Count {
ch <- mObj
}
}
},
})
go runInformer(ctx, "events", informer)
return ch, nil
}
func (kCli K8sClient) WatchPods(ctx context.Context, ls labels.Selector) (<-chan ObjectUpdate, error) {
gvr := PodGVR
informer, err := kCli.makeInformer(ctx, gvr, ls)
if err != nil {
return nil, errors.Wrap(err, "WatchPods")
}
ch := make(chan ObjectUpdate)
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
mObj, ok := obj.(*v1.Pod)
if ok {
FixContainerStatusImages(mObj)
}
ch <- ObjectUpdate{obj: obj}
},
DeleteFunc: func(obj interface{}) {
mObj, ok := obj.(*v1.Pod)
if ok {
FixContainerStatusImages(mObj)
}
ch <- ObjectUpdate{obj: obj, isDelete: true}
},
UpdateFunc: func(oldObj interface{}, newObj interface{}) {
oldPod, ok := oldObj.(*v1.Pod)
if !ok {
return
}
newPod, ok := newObj.(*v1.Pod)
if !ok || oldPod == newPod {
return
}
FixContainerStatusImages(newPod)
ch <- ObjectUpdate{obj: newPod}
},
})
go runInformer(ctx, "pods", informer)
return ch, nil
}
func (kCli K8sClient) WatchServices(ctx context.Context, ls labels.Selector) (<-chan *v1.Service, error) {
gvr := ServiceGVR
informer, err := kCli.makeInformer(ctx, gvr, ls)
if err != nil {
return nil, errors.Wrap(err, "WatchServices")
}
ch := make(chan *v1.Service)
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
mObj, ok := obj.(*v1.Service)
if ok {
ch <- mObj
}
},
UpdateFunc: func(oldObj interface{}, newObj interface{}) {
newService, ok := newObj.(*v1.Service)
if ok {
ch <- newService
}
},
})
go runInformer(ctx, "services", informer)
return ch, nil
}
func runInformer(ctx context.Context, name string, informer cache.SharedInformer) {
originalDuration := 3 * time.Second
originalBackoff := wait.Backoff{
Steps: 1000,
Duration: originalDuration,
Factor: 3.0,
Jitter: 0.5,
Cap: time.Hour,
}
backoff := originalBackoff
_ = informer.SetDropWatchHandler(func(err error, doneCh <-chan struct{}) {
sleepTime := originalDuration
if err != nil {
sleepTime = backoff.Step()
logger.Get(ctx).Warnf("Pausing k8s %s watcher for %s: %v",
name,
sleepTime.Truncate(time.Second),
err)
} else {
backoff = originalBackoff
}
select {
case <-doneCh:
case <-time.After(sleepTime):
}
})
informer.Run(ctx.Done())
}