-
Notifications
You must be signed in to change notification settings - Fork 8
/
generated.go
505 lines (424 loc) · 10.8 KB
/
generated.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
497
498
499
500
501
502
503
504
505
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny
package daemonset
import (
"context"
"fmt"
logutil "github.com/boz/go-logutil"
"github.com/boz/kcache"
"github.com/boz/kcache/client"
"github.com/boz/kcache/filter"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
var (
ErrInvalidType = fmt.Errorf("invalid type")
adapter = _adapter{}
)
var _ = appsv1.Deployment{}
type Event interface {
Type() kcache.EventType
Resource() *appsv1.DaemonSet
}
type CacheReader interface {
Get(ns string, name string) (*appsv1.DaemonSet, error)
List() ([]*appsv1.DaemonSet, error)
}
type CacheController interface {
Cache() CacheReader
Ready() <-chan struct{}
}
type Subscription interface {
CacheController
Events() <-chan Event
Close()
Done() <-chan struct{}
}
type Publisher interface {
Subscribe() (Subscription, error)
SubscribeWithFilter(filter.Filter) (FilterSubscription, error)
SubscribeForFilter() (FilterSubscription, error)
Clone() (Controller, error)
CloneWithFilter(filter.Filter) (FilterController, error)
CloneForFilter() (FilterController, error)
}
type Controller interface {
CacheController
Publisher
Done() <-chan struct{}
Close()
Error() error
}
type FilterSubscription interface {
Subscription
Refilter(filter.Filter) error
}
type FilterController interface {
Controller
Refilter(filter.Filter) error
}
type BaseHandler interface {
OnCreate(*appsv1.DaemonSet)
OnUpdate(*appsv1.DaemonSet)
OnDelete(*appsv1.DaemonSet)
}
type Handler interface {
BaseHandler
OnInitialize([]*appsv1.DaemonSet)
}
type HandlerBuilder interface {
OnInitialize(func([]*appsv1.DaemonSet)) HandlerBuilder
OnCreate(func(*appsv1.DaemonSet)) HandlerBuilder
OnUpdate(func(*appsv1.DaemonSet)) HandlerBuilder
OnDelete(func(*appsv1.DaemonSet)) HandlerBuilder
Create() Handler
}
type UnitaryHandler interface {
BaseHandler
OnInitialize(*appsv1.DaemonSet)
}
type UnitaryHandlerBuilder interface {
OnInitialize(func(*appsv1.DaemonSet)) UnitaryHandlerBuilder
OnCreate(func(*appsv1.DaemonSet)) UnitaryHandlerBuilder
OnUpdate(func(*appsv1.DaemonSet)) UnitaryHandlerBuilder
OnDelete(func(*appsv1.DaemonSet)) UnitaryHandlerBuilder
Create() UnitaryHandler
}
type _adapter struct{}
func (_adapter) adaptObject(obj metav1.Object) (*appsv1.DaemonSet, error) {
if obj, ok := obj.(*appsv1.DaemonSet); ok {
return obj, nil
}
return nil, ErrInvalidType
}
func (a _adapter) adaptList(objs []metav1.Object) ([]*appsv1.DaemonSet, error) {
var ret []*appsv1.DaemonSet
for _, orig := range objs {
adapted, err := a.adaptObject(orig)
if err != nil {
continue
}
ret = append(ret, adapted)
}
return ret, nil
}
func newCache(parent kcache.CacheReader) CacheReader {
return &cache{parent}
}
type cache struct {
parent kcache.CacheReader
}
func (c *cache) Get(ns string, name string) (*appsv1.DaemonSet, error) {
obj, err := c.parent.Get(ns, name)
switch {
case err != nil:
return nil, err
case obj == nil:
return nil, nil
default:
return adapter.adaptObject(obj)
}
}
func (c *cache) List() ([]*appsv1.DaemonSet, error) {
objs, err := c.parent.List()
if err != nil {
return nil, err
}
return adapter.adaptList(objs)
}
type event struct {
etype kcache.EventType
resource *appsv1.DaemonSet
}
func wrapEvent(evt kcache.Event) (Event, error) {
obj, err := adapter.adaptObject(evt.Resource())
if err != nil {
return nil, err
}
return event{evt.Type(), obj}, nil
}
func (e event) Type() kcache.EventType {
return e.etype
}
func (e event) Resource() *appsv1.DaemonSet {
return e.resource
}
type subscription struct {
parent kcache.Subscription
cache CacheReader
outch chan Event
}
func newSubscription(parent kcache.Subscription) *subscription {
s := &subscription{
parent: parent,
cache: newCache(parent.Cache()),
outch: make(chan Event, kcache.EventBufsiz),
}
go s.run()
return s
}
func (s *subscription) run() {
defer close(s.outch)
for pevt := range s.parent.Events() {
evt, err := wrapEvent(pevt)
if err != nil {
continue
}
select {
case s.outch <- evt:
default:
}
}
}
func (s *subscription) Cache() CacheReader {
return s.cache
}
func (s *subscription) Ready() <-chan struct{} {
return s.parent.Ready()
}
func (s *subscription) Events() <-chan Event {
return s.outch
}
func (s *subscription) Close() {
s.parent.Close()
}
func (s *subscription) Done() <-chan struct{} {
return s.parent.Done()
}
func NewController(ctx context.Context, log logutil.Log, cs kubernetes.Interface, ns string) (Controller, error) {
client := NewClient(cs, ns)
return BuildController(ctx, log, client)
}
func BuildController(ctx context.Context, log logutil.Log, client client.Client) (Controller, error) {
parent, err := kcache.NewController(ctx, log, client)
if err != nil {
return nil, err
}
return newController(parent), nil
}
func newController(parent kcache.Controller) *controller {
return &controller{parent, newCache(parent.Cache())}
}
type controller struct {
parent kcache.Controller
cache CacheReader
}
func (c *controller) Close() {
c.parent.Close()
}
func (c *controller) Ready() <-chan struct{} {
return c.parent.Ready()
}
func (c *controller) Done() <-chan struct{} {
return c.parent.Done()
}
func (c *controller) Error() error {
return c.parent.Error()
}
func (c *controller) Cache() CacheReader {
return c.cache
}
func (c *controller) Subscribe() (Subscription, error) {
parent, err := c.parent.Subscribe()
if err != nil {
return nil, err
}
return newSubscription(parent), nil
}
func (c *controller) SubscribeWithFilter(f filter.Filter) (FilterSubscription, error) {
parent, err := c.parent.SubscribeWithFilter(f)
if err != nil {
return nil, err
}
return newFilterSubscription(parent), nil
}
func (c *controller) SubscribeForFilter() (FilterSubscription, error) {
parent, err := c.parent.SubscribeForFilter()
if err != nil {
return nil, err
}
return newFilterSubscription(parent), nil
}
func (c *controller) Clone() (Controller, error) {
parent, err := c.parent.Clone()
if err != nil {
return nil, err
}
return newController(parent), nil
}
func (c *controller) CloneWithFilter(f filter.Filter) (FilterController, error) {
parent, err := c.parent.CloneWithFilter(f)
if err != nil {
return nil, err
}
return newFilterController(parent), nil
}
func (c *controller) CloneForFilter() (FilterController, error) {
parent, err := c.parent.CloneForFilter()
if err != nil {
return nil, err
}
return newFilterController(parent), nil
}
type filterController struct {
controller
filterParent kcache.FilterController
}
func newFilterController(parent kcache.FilterController) FilterController {
return &filterController{
controller: controller{parent, newCache(parent.Cache())},
filterParent: parent,
}
}
func (c *filterController) Refilter(f filter.Filter) error {
return c.filterParent.Refilter(f)
}
type filterSubscription struct {
subscription
filterParent kcache.FilterSubscription
}
func newFilterSubscription(parent kcache.FilterSubscription) FilterSubscription {
return &filterSubscription{
subscription: *newSubscription(parent),
filterParent: parent,
}
}
func (s *filterSubscription) Refilter(f filter.Filter) error {
return s.filterParent.Refilter(f)
}
func NewMonitor(publisher Publisher, handler Handler) (kcache.Monitor, error) {
phandler := kcache.BuildHandler().
OnInitialize(func(objs []metav1.Object) {
aobjs, _ := adapter.adaptList(objs)
handler.OnInitialize(aobjs)
}).
OnCreate(func(obj metav1.Object) {
aobj, _ := adapter.adaptObject(obj)
handler.OnCreate(aobj)
}).
OnUpdate(func(obj metav1.Object) {
aobj, _ := adapter.adaptObject(obj)
handler.OnUpdate(aobj)
}).
OnDelete(func(obj metav1.Object) {
aobj, _ := adapter.adaptObject(obj)
handler.OnDelete(aobj)
}).Create()
switch obj := publisher.(type) {
case *controller:
return kcache.NewMonitor(obj.parent, phandler)
case *filterController:
return kcache.NewMonitor(obj.parent, phandler)
default:
panic(fmt.Sprintf("Invalid publisher type: %T is not a *controller", publisher))
}
}
func ToUnitary(log logutil.Log, delegate UnitaryHandler) Handler {
return BuildHandler().
OnInitialize(func(objs []*appsv1.DaemonSet) {
if count := len(objs); count > 1 {
log.Warnf("initialized with invalid count: %v", count)
return
}
if count := len(objs); count == 0 {
log.Debugf("initialized with empty result, ignoring")
return
}
delegate.OnInitialize(objs[0])
}).
OnCreate(func(obj *appsv1.DaemonSet) {
delegate.OnCreate(obj)
}).
OnUpdate(func(obj *appsv1.DaemonSet) {
delegate.OnUpdate(obj)
}).
OnDelete(func(obj *appsv1.DaemonSet) {
delegate.OnDelete(obj)
}).Create()
}
func BuildHandler() HandlerBuilder {
return &handlerBuilder{}
}
func BuildUnitaryHandler() UnitaryHandlerBuilder {
return &unitaryHandlerBuilder{}
}
type baseHandler struct {
onCreate func(*appsv1.DaemonSet)
onUpdate func(*appsv1.DaemonSet)
onDelete func(*appsv1.DaemonSet)
}
type handler struct {
baseHandler
onInitialize func([]*appsv1.DaemonSet)
}
type handlerBuilder handler
type unitaryHandler struct {
baseHandler
onInitialize func(*appsv1.DaemonSet)
}
type unitaryHandlerBuilder unitaryHandler
func (hb *handlerBuilder) OnInitialize(fn func([]*appsv1.DaemonSet)) HandlerBuilder {
hb.onInitialize = fn
return hb
}
func (hb *handlerBuilder) OnCreate(fn func(*appsv1.DaemonSet)) HandlerBuilder {
hb.onCreate = fn
return hb
}
func (hb *handlerBuilder) OnUpdate(fn func(*appsv1.DaemonSet)) HandlerBuilder {
hb.onUpdate = fn
return hb
}
func (hb *handlerBuilder) OnDelete(fn func(*appsv1.DaemonSet)) HandlerBuilder {
hb.onDelete = fn
return hb
}
func (hb *handlerBuilder) Create() Handler {
return handler(*hb)
}
func (h handler) OnInitialize(objs []*appsv1.DaemonSet) {
if h.onInitialize != nil {
h.onInitialize(objs)
}
}
func (hb *unitaryHandlerBuilder) OnInitialize(fn func(*appsv1.DaemonSet)) UnitaryHandlerBuilder {
hb.onInitialize = fn
return hb
}
func (hb *unitaryHandlerBuilder) OnCreate(fn func(*appsv1.DaemonSet)) UnitaryHandlerBuilder {
hb.onCreate = fn
return hb
}
func (hb *unitaryHandlerBuilder) OnUpdate(fn func(*appsv1.DaemonSet)) UnitaryHandlerBuilder {
hb.onUpdate = fn
return hb
}
func (hb *unitaryHandlerBuilder) OnDelete(fn func(*appsv1.DaemonSet)) UnitaryHandlerBuilder {
hb.onDelete = fn
return hb
}
func (hb *unitaryHandlerBuilder) Create() UnitaryHandler {
return unitaryHandler(*hb)
}
func (h unitaryHandler) OnInitialize(obj *appsv1.DaemonSet) {
if h.onInitialize != nil {
h.onInitialize(obj)
}
}
func (h baseHandler) OnCreate(obj *appsv1.DaemonSet) {
if h.onCreate != nil {
h.onCreate(obj)
}
}
func (h baseHandler) OnUpdate(obj *appsv1.DaemonSet) {
if h.onUpdate != nil {
h.onUpdate(obj)
}
}
func (h baseHandler) OnDelete(obj *appsv1.DaemonSet) {
if h.onDelete != nil {
h.onDelete(obj)
}
}