generated from kyma-project/template-repository
/
watcher.go
162 lines (137 loc) · 3.73 KB
/
watcher.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
package config
import (
"fmt"
"reflect"
"time"
"k8s.io/apimachinery/pkg/util/runtime"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"github.com/kyma-project/eventing-tools/internal/logger"
)
const (
defaultSize = 10
defaultResync = time.Minute
fieldSelectorFormat = "metadata.name=%s"
)
// compile-time check for interfaces implementation.
var (
_ DeleteNotifiable = &Watcher{}
)
type Watcher struct {
client kubernetes.Interface
namespace string
name string
addNotifiableList []AddNotifiable
updateNotifiableList []UpdateNotifiable
deleteNotifiableList []DeleteNotifiable
stopCh chan struct{}
}
func NewWatcher(client kubernetes.Interface, namespace, name string) *Watcher {
return &Watcher{
client: client,
namespace: namespace,
name: name,
addNotifiableList: make([]AddNotifiable, 0, defaultSize),
updateNotifiableList: make([]UpdateNotifiable, 0, defaultSize),
deleteNotifiableList: make([]DeleteNotifiable, 0, defaultSize),
}
}
func (w *Watcher) init() {
w.stopCh = make(chan struct{})
}
func (w *Watcher) start() {
defer runtime.HandleCrash()
factory := informers.NewSharedInformerFactoryWithOptions(
w.client,
defaultResync,
informers.WithNamespace(w.namespace),
informers.WithTweakListOptions(func(o *metav1.ListOptions) {
o.FieldSelector = fmt.Sprintf(fieldSelectorFormat, w.name)
}),
)
configMapsInformer := factory.Core().V1().ConfigMaps().Informer()
_, err := configMapsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: w.addFunc,
UpdateFunc: w.updateFunc,
DeleteFunc: w.deleteFunc,
})
if err != nil {
runtime.HandleError(err)
}
factory.Start(w.stopCh)
factory.WaitForCacheSync(w.stopCh)
if !cache.WaitForCacheSync(w.stopCh, configMapsInformer.HasSynced) {
runtime.HandleError(fmt.Errorf("timeout waiting for caches to sync"))
return
}
}
func (w *Watcher) stop() {
// recover from closing already closed channels
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic: ", r)
}
}()
w.stopCh <- struct{}{}
close(w.stopCh)
}
func (w *Watcher) Watch() {
w.init()
w.start()
}
func (w *Watcher) NotifyDelete(*corev1.ConfigMap) {
w.stop()
w.Watch()
}
func (w *Watcher) OnAddNotify(notifiable AddNotifiable) *Watcher {
w.addNotifiableList = append(w.addNotifiableList, notifiable)
return w
}
func (w *Watcher) OnUpdateNotify(notifiable UpdateNotifiable) *Watcher {
w.updateNotifiableList = append(w.updateNotifiableList, notifiable)
return w
}
func (w *Watcher) OnDeleteNotify(notifiable DeleteNotifiable) *Watcher {
w.deleteNotifiableList = append(w.deleteNotifiableList, notifiable)
return w
}
func (w *Watcher) OnDeleteNotifyMe() *Watcher {
return w.OnDeleteNotify(w)
}
func (w *Watcher) addFunc(o interface{}) {
if cm, ok := o.(*corev1.ConfigMap); ok {
for _, n := range w.addNotifiableList {
n.NotifyAdd(cm)
}
}
}
func (w *Watcher) updateFunc(o interface{}, n interface{}) {
var (
ok bool
oldCM *corev1.ConfigMap
newCM *corev1.ConfigMap
)
if oldCM, ok = o.(*corev1.ConfigMap); !ok {
logger.LogIfError(fmt.Errorf("cannot convert old object to configmap"))
return
}
if newCM, ok = n.(*corev1.ConfigMap); !ok {
logger.LogIfError(fmt.Errorf("cannot convert new object to configmap"))
return
}
if !reflect.DeepEqual(oldCM.Data, newCM.Data) {
for _, n := range w.updateNotifiableList {
n.NotifyUpdate(newCM)
}
}
}
func (w *Watcher) deleteFunc(o interface{}) {
if cm, ok := o.(*corev1.ConfigMap); ok {
for _, n := range w.deleteNotifiableList {
n.NotifyDelete(cm)
}
}
}