/
handler.go
97 lines (84 loc) · 2.41 KB
/
handler.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
package controller
import (
"github.com/containous/maesh/internal/k8s"
"github.com/containous/maesh/internal/message"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
// Handler is an implementation of a ResourceEventHandler.
type Handler struct {
ignored k8s.IgnoreWrapper
messageQueue workqueue.RateLimitingInterface
}
// NewHandler creates a handler.
func NewHandler(ignored k8s.IgnoreWrapper, messageQueue workqueue.RateLimitingInterface) *Handler {
h := &Handler{
ignored: ignored,
messageQueue: messageQueue,
}
if err := h.Init(); err != nil {
log.Errorln("Could not initialize MeshControllerHandler")
}
return h
}
// Init handles any handler initialization.
func (h *Handler) Init() error {
log.Debugln("MeshControllerHandler.Init")
return nil
}
// OnAdd executed when an object is added.
func (h *Handler) OnAdd(obj interface{}) {
// convert the resource object into a key (in this case
// we are just doing it in the format of 'namespace/name')
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
return
}
// add the key to the queue for the handler to get
// If object key is not in our list of ignored namespaces
if !k8s.ObjectKeyInNamespace(key, h.ignored.Namespaces) {
event := message.Message{
Key: key,
Object: obj,
Action: message.TypeCreated,
}
h.messageQueue.Add(event)
}
}
// OnUpdate executed when an object is updated.
func (h *Handler) OnUpdate(oldObj, newObj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(newObj)
if err != nil {
return
}
if !k8s.ObjectKeyInNamespace(key, h.ignored.Namespaces) {
event := message.Message{
Key: key,
Object: newObj,
OldObject: oldObj,
Action: message.TypeUpdated,
}
h.messageQueue.Add(event)
}
}
// OnDelete executed when an object is deleted.
func (h *Handler) OnDelete(obj interface{}) {
// DeletionHandlingMetaNamsespaceKeyFunc is a helper function that allows
// us to check the DeletedFinalStateUnknown existence in the event that
// a resource was deleted but it is still contained in the index
//
// this then in turn calls MetaNamespaceKeyFunc
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
return
}
if !k8s.ObjectKeyInNamespace(key, h.ignored.Namespaces) {
event := message.Message{
Key: key,
Object: obj,
Action: message.TypeDeleted,
}
h.messageQueue.Add(event)
}
}