/
observer.go
496 lines (426 loc) · 14.1 KB
/
observer.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package kuber
import (
"bytes"
"context"
"fmt"
"time"
"k8s.io/apimachinery/pkg/labels"
"github.com/MagalixCorp/magalix-agent/v2/utils"
"github.com/MagalixTechnologies/core/logger"
"github.com/pkg/errors"
"github.com/reconquest/karma-go"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
corev1 "k8s.io/api/core/v1"
)
type Observer struct {
dynamicinformer.DynamicSharedInformerFactory
ParentsStore *ParentsStore
stopCh chan struct{}
}
func NewObserver(
client dynamic.Interface,
parentsStore *ParentsStore,
stopCh chan struct{},
defaultResync time.Duration,
) *Observer {
return &Observer{
DynamicSharedInformerFactory: dynamicinformer.NewDynamicSharedInformerFactory(client, defaultResync),
ParentsStore: parentsStore,
stopCh: stopCh,
}
}
func (observer *Observer) Watch(gvrk GroupVersionResourceKind) *watcher {
logger.Debugw("subscribed on changes", "resource", gvrk.String())
watcher := observer.WatcherFor(gvrk)
observer.Start()
return watcher
}
func (observer *Observer) WatchAndWaitForSync(gvrk GroupVersionResourceKind) (*watcher, error) {
watcher := observer.Watch(gvrk)
done := make(chan struct{}, 1)
go func() {
cache.WaitForCacheSync(observer.stopCh, watcher.informer.Informer().HasSynced)
done <- struct{}{}
}()
timeout := 5 * time.Second
select {
case <-done:
return watcher, nil
case <-time.After(timeout):
return nil, fmt.Errorf("Time out waiting for informer sync: (GVRK, %+v), (timeout, %v)", gvrk, timeout)
}
}
func (observer *Observer) WatcherFor(
gvrk GroupVersionResourceKind,
) *watcher {
informer := observer.ForResource(gvrk.GroupVersionResource)
return &watcher{
gvrk: gvrk,
informer: informer,
}
}
func (observer *Observer) Start() {
observer.DynamicSharedInformerFactory.Start(observer.stopCh)
}
func (observer *Observer) Stop() {
observer.stopCh <- struct{}{}
}
func (observer *Observer) WaitForCacheSync() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
finished := make(chan struct{})
go func() {
observer.DynamicSharedInformerFactory.WaitForCacheSync(observer.stopCh)
finished <- struct{}{}
}()
for {
select {
case <-finished:
return nil
case <-ctx.Done():
return karma.Format(
nil,
"timeout waiting for cache sync",
)
}
}
}
func (observer *Observer) GetNodes() ([]corev1.Node, error) {
watcher, err := observer.WatchAndWaitForSync(Nodes)
if err != nil {
return nil, err
}
_nodes, err := watcher.Lister().List(labels.Everything())
if err != nil {
return nil, karma.Format(err, "unable to list nodes")
}
nodes := make([]corev1.Node, len(_nodes))
for i, n := range _nodes {
u := n.(*unstructured.Unstructured)
err = utils.Transcode(u, &nodes[i])
if err != nil {
return nil, karma.Format(err, "unable to transcode unstructured to corev1.Node")
}
}
return nodes, nil
}
func (observer *Observer) GetPods() ([]corev1.Pod, error) {
watcher, err := observer.WatchAndWaitForSync(Pods)
if err != nil {
return nil, err
}
_pods, err := watcher.Lister().List(labels.Everything())
if err != nil {
return nil, karma.Format(err, "unable to list pods")
}
pods := make([]corev1.Pod, len(_pods))
for i, n := range _pods {
u := n.(*unstructured.Unstructured)
err = utils.Transcode(u, &pods[i])
if err != nil {
return nil, karma.Format(err, "unable to transcode unstructured to corev1.Pod")
}
}
return pods, nil
}
func (observer *Observer) FindController(
namespaceName string,
controllerKind string,
controllerName string,
) (*unstructured.Unstructured, error) {
ctx := karma.Describe("namespace-name", namespaceName).
Describe("controller-kind", controllerKind).
Describe("controller-name", controllerName)
gvrk, err := KindToGvrk(controllerKind)
if err != nil {
return nil, karma.Format(ctx.Reason(err), "unable to get GVRK")
}
watcher, err := observer.WatchAndWaitForSync(*gvrk)
if err != nil {
return nil, err
}
controller, err := watcher.informer.Lister().ByNamespace(namespaceName).Get(controllerName)
if err != nil {
return nil, karma.Format(ctx.Reason(err), "unable to find controller")
}
return controller.(*unstructured.Unstructured), err
}
func (observer *Observer) FindContainer(
namespaceName string,
controllerKind string,
controllerName string,
containerName string,
) (*corev1.Container, error) {
ctx := karma.Describe("namespace-name", namespaceName).
Describe("controller-kind", controllerKind).
Describe("controller-name", controllerName).
Describe("container-name", containerName)
controller, err := observer.FindController(namespaceName, controllerKind, controllerName)
if err != nil {
return nil, karma.Format(ctx.Reason(err), "unable to find controller")
}
containersList, found, err := unstructured.NestedSlice(controller.Object, "spec", "template", "spec", "containers")
if err != nil {
return nil, karma.Format(ctx.Reason(err), "unable to find container")
}
if !found {
return nil, karma.Format(ctx.Reason(fmt.Errorf("unable to find containers spec")), "unable to find container")
}
for _, c := range containersList {
var container corev1.Container
err := utils.Transcode(c, &container)
if err != nil {
return nil, karma.Format(ctx.Reason(fmt.Errorf("unable to transcode unstructured to container")), "unable to find container")
}
if container.Name == containerName {
return &container, nil
}
}
return nil, karma.Format(nil, "unable to find container")
}
func (observer *Observer) FindPodController(namespaceName string, podName string) (string, string, error) {
lg := logger.With("pod", podName, "namespace", namespaceName)
watcher, err := observer.WatchAndWaitForSync(Pods)
if err != nil {
return "", "", err
}
pod, err := watcher.informer.Lister().ByNamespace(namespaceName).Get(podName)
if err != nil {
return "", "", errors.Wrap(err, "unable to get pod")
}
parent, err := GetParents(pod.(*unstructured.Unstructured), observer.ParentsStore, func(kind string) (Watcher, bool) {
gvrk, err := KindToGvrk(kind)
if err != nil {
lg.Warnw("unable to get GVRK for kind", "kind", kind, "error", err)
return nil, false
}
watcher, err := observer.WatchAndWaitForSync(*gvrk)
if err != nil {
lg.Errorw("unable to get watcher for parent", "error", err)
return nil, false
}
return watcher, true
})
if err != nil {
return "", "", err
}
if parent == nil {
return podName, Pods.Kind, nil
}
root := RootParent(parent)
return root.Name, root.Kind, nil
}
type Watcher interface {
GetGroupVersionResourceKind() GroupVersionResourceKind
Lister() cache.GenericLister
// AddEventHandler adds an event handler to the shared informer using the shared informer's resync
// period. Events to a single handler are delivered sequentially, but there is no coordination
// between different handlers.
AddEventHandler(handler ResourceEventHandler)
// AddEventHandlerWithResyncPeriod adds an event handler to the
// shared informer using the specified resync period. The resync
// operation consists of delivering to the handler a create
// notification for every object in the informer's local cache; it
// does not add any interactions with the authoritative storage.
AddEventHandlerWithResyncPeriod(handler ResourceEventHandler, resyncPeriod time.Duration)
// HasSynced returns true if the shared informer's store has been
// informed by at least one full LIST of the authoritative state
// of the informer's object collection. This is unrelated to "resync".
HasSynced() bool
// LastSyncResourceVersion is the resource version observed when last synced with the underlying
// store. The value returned is not synchronized with access to the underlying store and is not
// thread-safe.
LastSyncResourceVersion() string
}
type watcher struct {
gvrk GroupVersionResourceKind
informer informers.GenericInformer
}
func (w *watcher) GetGroupVersionResourceKind() GroupVersionResourceKind {
return w.gvrk
}
func (w *watcher) Lister() cache.GenericLister {
return w.informer.Lister()
}
func (w *watcher) AddEventHandler(handler ResourceEventHandler) {
w.informer.Informer().AddEventHandler(wrapHandler(handler, w.gvrk))
}
func (w *watcher) AddEventHandlerWithResyncPeriod(handler ResourceEventHandler, resyncPeriod time.Duration) {
w.informer.Informer().AddEventHandlerWithResyncPeriod(wrapHandler(handler, w.gvrk), resyncPeriod)
}
func (w *watcher) HasSynced() bool {
return w.informer.Informer().HasSynced()
}
func (w *watcher) LastSyncResourceVersion() string {
return w.informer.Informer().LastSyncResourceVersion()
}
func wrapHandler(
wrapped ResourceEventHandler,
gvrk GroupVersionResourceKind,
) cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
now := time.Now().UTC()
objUn, ok := obj.(*unstructured.Unstructured)
if !ok {
logger.Error("unable to cast obj to *Unstructured")
}
if objUn != nil {
objUn, err := maskUnstructured(objUn)
if err != nil {
logger.Errorw("unable to mask Unstructured", "error", err)
return
}
wrapped.OnAdd(now, gvrk, *objUn)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
now := time.Now().UTC()
// TODO: can we have a better way to suppress update events when
// a resync is forced because of a network error
oldUn, oldOk := oldObj.(*unstructured.Unstructured)
if !oldOk {
logger.Error("unable to cast oldObj to *Unstructured")
}
newUn, newOk := newObj.(*unstructured.Unstructured)
if !newOk {
logger.Error("unable to cast newObj to *Unstructured")
}
if oldOk && newOk &&
oldUn.GetResourceVersion() == newUn.GetResourceVersion() {
// deep check that nothing has changed
oldJson, err := oldUn.MarshalJSON()
if err != nil {
logger.Errorw("unable to marshal oldUn to json", "error", err)
}
newJson, err := newUn.MarshalJSON()
if err != nil {
logger.Errorf("unable to marshal newUn to json", "error", err)
}
if err == nil {
if bytes.Equal(oldJson, newJson) {
return
}
}
}
if oldUn != nil && newUn != nil {
oldUn, err := maskUnstructured(oldUn)
if err != nil {
logger.Errorw("unable to mask Unstructured", "error", err)
}
newUn, err := maskUnstructured(newUn)
if err != nil {
logger.Errorw("unable to mask Unstructured", "error", err)
return
}
wrapped.OnUpdate(now, gvrk, *oldUn, *newUn)
}
},
DeleteFunc: func(obj interface{}) {
now := time.Now().UTC()
objUn, ok := obj.(*unstructured.Unstructured)
if !ok {
logger.Error("unable to cast obj to *Unstructured")
}
if objUn != nil {
objUn, err := maskUnstructured(objUn)
if err != nil {
logger.Errorw("unable to mask Unstructured", "error", err)
return
}
wrapped.OnDelete(now, gvrk, *objUn)
}
},
}
}
var (
podSpecMap = map[string][]string{
Pods.Kind: {"spec"},
ReplicationControllers.Kind: {"spec", "template", "spec"},
Deployments.Kind: {"spec", "template", "spec"},
StatefulSets.Kind: {"spec", "template", "spec"},
DaemonSets.Kind: {"spec", "template", "spec"},
ReplicaSets.Kind: {"spec", "template", "spec"},
Jobs.Kind: {"spec", "template", "spec"},
CronJobs.Kind: {"spec", "jobTemplate", "spec", "template", "spec"},
}
)
func maskUnstructured(
obj *unstructured.Unstructured,
) (*unstructured.Unstructured, error) {
kind := obj.GetKind()
ctx := karma.Describe("kind", kind)
podSpecPath, ok := podSpecMap[kind]
if !ok {
// not maskable kind
return obj, nil
}
podSpecU, ok, err := unstructured.NestedFieldNoCopy(obj.Object, podSpecPath...)
if err != nil {
return nil, ctx.
Format(err, "unable to get pod spec")
}
if !ok {
return nil, ctx.
Format(nil, "unable to find pod spec in specified path")
}
var podSpec corev1.PodSpec
err = utils.Transcode(podSpecU, &podSpec)
if err != nil {
return nil, ctx.
Format(err, "unable to transcode pod spec")
}
podSpec.Containers = maskContainers(podSpec.Containers)
podSpec.InitContainers = maskContainers(podSpec.InitContainers)
var podSpecJson map[string]interface{}
err = utils.Transcode(podSpec, &podSpecJson)
if err != nil {
return nil, ctx.
Format(err, "unable to transcode pod spec")
}
// deep copy to not mutate the data from cash store
obj = obj.DeepCopy()
err = unstructured.SetNestedField(obj.Object, podSpecJson, podSpecPath...)
if err != nil {
return nil, ctx.
Format(err, "unable to set pod spec")
}
return obj, nil
}
type ResourceEventHandler interface {
OnAdd(now time.Time, gvrk GroupVersionResourceKind, obj unstructured.Unstructured)
OnUpdate(now time.Time, gvrk GroupVersionResourceKind, oldObj, newObj unstructured.Unstructured)
OnDelete(now time.Time, gvrk GroupVersionResourceKind, obj unstructured.Unstructured)
}
// ResourceEventHandlerFuncs is an adaptor to let you easily specify as many or
// as few of the notification functions as you want while still implementing
// ResourceEventHandler.
type ResourceEventHandlerFuncs struct {
Observer *Observer
AddFunc func(now time.Time, gvrk GroupVersionResourceKind, obj unstructured.Unstructured)
UpdateFunc func(now time.Time, gvrk GroupVersionResourceKind, oldObj, newObj unstructured.Unstructured)
DeleteFunc func(now time.Time, gvrk GroupVersionResourceKind, obj unstructured.Unstructured)
}
// OnAdd calls AddFunc if it's not nil.
func (r ResourceEventHandlerFuncs) OnAdd(now time.Time, gvrk GroupVersionResourceKind, obj unstructured.Unstructured) {
if r.AddFunc != nil {
r.AddFunc(now, gvrk, obj)
}
}
// OnUpdate calls UpdateFunc if it's not nil.
func (r ResourceEventHandlerFuncs) OnUpdate(now time.Time, gvrk GroupVersionResourceKind, oldObj, newObj unstructured.Unstructured) {
if r.UpdateFunc != nil {
r.UpdateFunc(now, gvrk, oldObj, newObj)
}
}
// OnDelete calls DeleteFunc if it's not nil.
func (r ResourceEventHandlerFuncs) OnDelete(now time.Time, gvrk GroupVersionResourceKind, obj unstructured.Unstructured) {
if r.DeleteFunc != nil {
r.DeleteFunc(now, gvrk, obj)
}
r.Observer.ParentsStore.Delete(obj.GetNamespace(), obj.GetKind(), obj.GetName())
}