mirror of
https://github.com/vee1e/kubeedge.git
synced 2026-09-02 02:37:43 +00:00
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package manager
|
|
|
|
import (
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/watch"
|
|
"k8s.io/client-go/tools/cache"
|
|
"k8s.io/klog/v2"
|
|
)
|
|
|
|
// 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 {
|
|
klog.Warningf("unknown type: %T, ignore", obj)
|
|
return
|
|
}
|
|
c.events <- watch.Event{Type: t, Object: eventObj}
|
|
}
|
|
|
|
// OnAdd handle Add event
|
|
func (c *CommonResourceEventHandler) OnAdd(obj interface{}, _ bool) {
|
|
c.obj2Event(watch.Added, obj)
|
|
}
|
|
|
|
// OnUpdate handle Update event
|
|
func (c *CommonResourceEventHandler) OnUpdate(_, newObj interface{}) {
|
|
c.obj2Event(watch.Modified, newObj)
|
|
}
|
|
|
|
// OnDelete handle Delete event
|
|
func (c *CommonResourceEventHandler) OnDelete(obj interface{}) {
|
|
if tombstone, ok := obj.(cache.DeletedFinalStateUnknown); ok {
|
|
obj = tombstone.Obj
|
|
}
|
|
c.obj2Event(watch.Deleted, obj)
|
|
}
|
|
|
|
// NewCommonResourceEventHandler create CommonResourceEventHandler used by configmapManager and podManager
|
|
func NewCommonResourceEventHandler(events chan watch.Event) *CommonResourceEventHandler {
|
|
return &CommonResourceEventHandler{events: events}
|
|
}
|