forked from kubeedge/kubeedge
/
common.go
47 lines (38 loc) · 1.31 KB
/
common.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
package manager
import (
"github.com/kubeedge/beehive/pkg/common/log"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
)
// Manager define the interface of a Manager, configmapManager and podManager implement it
type Manager interface {
Events() chan watch.Event
}
// CommonResourceEventHandler can be used by configmapManager and podManager
type CommonResourceEventHandler struct {
events chan watch.Event
}
func (c *CommonResourceEventHandler) obj2Event(t watch.EventType, obj interface{}) {
eventObj, ok := obj.(runtime.Object)
if !ok {
log.LOGGER.Warnf("unknown type: %T, ignore", obj)
return
}
c.events <- watch.Event{Type: t, Object: eventObj}
}
// OnAdd handle Add event
func (c *CommonResourceEventHandler) OnAdd(obj interface{}) {
c.obj2Event(watch.Added, obj)
}
// OnUpdate handle Update event
func (c *CommonResourceEventHandler) OnUpdate(oldObj, newObj interface{}) {
c.obj2Event(watch.Modified, newObj)
}
// OnDelete handle Delete event
func (c *CommonResourceEventHandler) OnDelete(obj interface{}) {
c.obj2Event(watch.Deleted, obj)
}
// NewCommonResourceEventHandler create CommonResourceEventHandler used by configmapManager and podManager
func NewCommonResourceEventHandler(events chan watch.Event) *CommonResourceEventHandler {
return &CommonResourceEventHandler{events: events}
}