-
Notifications
You must be signed in to change notification settings - Fork 8
/
event_handlers.go
125 lines (105 loc) · 4.45 KB
/
event_handlers.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
// Code generated by skv2. DO NOT EDIT.
//go:generate mockgen -source ./event_handlers.go -destination mocks/event_handlers.go
// Definitions for the Kubernetes Controllers
package controller
import (
"context"
multicluster_solo_io_v1alpha1 "github.com/solo-io/skv2/pkg/api/multicluster.solo.io/v1alpha1"
"github.com/pkg/errors"
"github.com/solo-io/skv2/pkg/events"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)
// Handle events for the KubernetesCluster Resource
// DEPRECATED: Prefer reconciler pattern.
type KubernetesClusterEventHandler interface {
CreateKubernetesCluster(obj *multicluster_solo_io_v1alpha1.KubernetesCluster) error
UpdateKubernetesCluster(old, new *multicluster_solo_io_v1alpha1.KubernetesCluster) error
DeleteKubernetesCluster(obj *multicluster_solo_io_v1alpha1.KubernetesCluster) error
GenericKubernetesCluster(obj *multicluster_solo_io_v1alpha1.KubernetesCluster) error
}
type KubernetesClusterEventHandlerFuncs struct {
OnCreate func(obj *multicluster_solo_io_v1alpha1.KubernetesCluster) error
OnUpdate func(old, new *multicluster_solo_io_v1alpha1.KubernetesCluster) error
OnDelete func(obj *multicluster_solo_io_v1alpha1.KubernetesCluster) error
OnGeneric func(obj *multicluster_solo_io_v1alpha1.KubernetesCluster) error
}
func (f *KubernetesClusterEventHandlerFuncs) CreateKubernetesCluster(obj *multicluster_solo_io_v1alpha1.KubernetesCluster) error {
if f.OnCreate == nil {
return nil
}
return f.OnCreate(obj)
}
func (f *KubernetesClusterEventHandlerFuncs) DeleteKubernetesCluster(obj *multicluster_solo_io_v1alpha1.KubernetesCluster) error {
if f.OnDelete == nil {
return nil
}
return f.OnDelete(obj)
}
func (f *KubernetesClusterEventHandlerFuncs) UpdateKubernetesCluster(objOld, objNew *multicluster_solo_io_v1alpha1.KubernetesCluster) error {
if f.OnUpdate == nil {
return nil
}
return f.OnUpdate(objOld, objNew)
}
func (f *KubernetesClusterEventHandlerFuncs) GenericKubernetesCluster(obj *multicluster_solo_io_v1alpha1.KubernetesCluster) error {
if f.OnGeneric == nil {
return nil
}
return f.OnGeneric(obj)
}
type KubernetesClusterEventWatcher interface {
AddEventHandler(ctx context.Context, h KubernetesClusterEventHandler, predicates ...predicate.Predicate) error
}
type kubernetesClusterEventWatcher struct {
watcher events.EventWatcher
}
func NewKubernetesClusterEventWatcher(name string, mgr manager.Manager) KubernetesClusterEventWatcher {
return &kubernetesClusterEventWatcher{
watcher: events.NewWatcher(name, mgr, &multicluster_solo_io_v1alpha1.KubernetesCluster{}),
}
}
func (c *kubernetesClusterEventWatcher) AddEventHandler(ctx context.Context, h KubernetesClusterEventHandler, predicates ...predicate.Predicate) error {
handler := genericKubernetesClusterHandler{handler: h}
if err := c.watcher.Watch(ctx, handler, predicates...); err != nil {
return err
}
return nil
}
// genericKubernetesClusterHandler implements a generic events.EventHandler
type genericKubernetesClusterHandler struct {
handler KubernetesClusterEventHandler
}
func (h genericKubernetesClusterHandler) Create(object runtime.Object) error {
obj, ok := object.(*multicluster_solo_io_v1alpha1.KubernetesCluster)
if !ok {
return errors.Errorf("internal error: KubernetesCluster handler received event for %T", object)
}
return h.handler.CreateKubernetesCluster(obj)
}
func (h genericKubernetesClusterHandler) Delete(object runtime.Object) error {
obj, ok := object.(*multicluster_solo_io_v1alpha1.KubernetesCluster)
if !ok {
return errors.Errorf("internal error: KubernetesCluster handler received event for %T", object)
}
return h.handler.DeleteKubernetesCluster(obj)
}
func (h genericKubernetesClusterHandler) Update(old, new runtime.Object) error {
objOld, ok := old.(*multicluster_solo_io_v1alpha1.KubernetesCluster)
if !ok {
return errors.Errorf("internal error: KubernetesCluster handler received event for %T", old)
}
objNew, ok := new.(*multicluster_solo_io_v1alpha1.KubernetesCluster)
if !ok {
return errors.Errorf("internal error: KubernetesCluster handler received event for %T", new)
}
return h.handler.UpdateKubernetesCluster(objOld, objNew)
}
func (h genericKubernetesClusterHandler) Generic(object runtime.Object) error {
obj, ok := object.(*multicluster_solo_io_v1alpha1.KubernetesCluster)
if !ok {
return errors.Errorf("internal error: KubernetesCluster handler received event for %T", object)
}
return h.handler.GenericKubernetesCluster(obj)
}