-
Notifications
You must be signed in to change notification settings - Fork 1
/
definition.go
177 lines (147 loc) · 4.58 KB
/
definition.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package definition
import (
"fmt"
"github.com/echocat/lingress/support"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"reflect"
"sync/atomic"
)
type Definition struct {
MaxTries int
typeDescription string
informer cache.SharedInformer
lastError atomic.Value
OnElementAdded OnElementChangedFunc
OnElementUpdated OnElementUpdatedFunc
OnElementRemoved OnElementRemovedFunc
OnError OnErrorFunc
}
type OnElementChangedFunc func(key string, new metav1.Object) error
type OnElementUpdatedFunc func(key string, old, new metav1.Object) error
type OnElementRemovedFunc func(key string) error
type OnErrorFunc func(key string, event string, err error)
func newDefinition(typeDescription string, informer cache.SharedInformer) (*Definition, error) {
return &Definition{
typeDescription: typeDescription,
informer: informer,
}, nil
}
func (instance *Definition) SetInformer(informer cache.SharedInformer) {
instance.informer = informer
}
func (instance *Definition) Init(stop support.Channel) error {
if instance.informer == nil {
panic(fmt.Sprintf("definition %s has no informer", instance.typeDescription))
}
instance.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: instance.onClusterElementAdded,
UpdateFunc: instance.onClusterElementUpdated,
DeleteFunc: instance.onClusterElementRemoved,
})
go instance.Run(stop)
if !cache.WaitForCacheSync(support.ToChan(stop), instance.HasSynced) {
stop.Broadcast()
return fmt.Errorf("initial %s synchronization failed", instance.typeDescription)
}
if err := instance.lastError.Load(); err != nil {
stop.Broadcast()
return errors.Wrapf(err.(error), "initial %s synchronization failed", instance.typeDescription)
}
return nil
}
func (instance *Definition) HasSynced() bool {
return instance.informer.HasSynced()
}
func (instance *Definition) Run(stop support.Channel) {
l := instance.log()
defer runtime.HandleCrash()
l.Info("definition store started")
go instance.informer.Run(support.ToChan(stop))
stop.Wait()
l.Info("definition store stopped")
}
func (instance *Definition) onClusterElementAdded(new interface{}) {
l := instance.logEvent("elementAdded")
key, err := cache.MetaNamespaceKeyFunc(new)
if err != nil {
l.WithError(err).
WithField("objectType", reflect.TypeOf(new).String()).
Error("cannot determine key of an object of type")
}
if instance.OnElementAdded == nil {
return
}
l = l.WithField("key", key)
if err := instance.OnElementAdded(key, new.(metav1.Object)); err != nil {
instance.lastError.Store(err)
if instance.OnError != nil {
instance.OnError(key, "elementRemoved", err)
} else {
l.WithError(err).
Error("cannot handle element")
}
} else {
l.Debug("element added")
}
}
func (instance *Definition) onClusterElementUpdated(old interface{}, new interface{}) {
l := instance.logEvent("elementUpdated")
key, err := cache.MetaNamespaceKeyFunc(new)
if err != nil {
l.WithError(err).
WithField("objectType", reflect.TypeOf(new).String()).
Error("cannot determine key of an object of type")
}
if instance.OnElementUpdated == nil {
return
}
l = l.WithField("key", key)
if err := instance.OnElementUpdated(key, old.(metav1.Object), new.(metav1.Object)); err != nil {
instance.lastError.Store(err)
if instance.OnError != nil {
instance.OnError(key, "elementRemoved", err)
} else {
l.WithError(err).
Error("cannot handle element")
}
} else {
l.Debug("element updated")
}
}
func (instance *Definition) onClusterElementRemoved(old interface{}) {
l := instance.logEvent("elementRemoved")
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(old)
if err != nil {
l.WithError(err).
WithField("objectType", reflect.TypeOf(old).String()).
Error("cannot determine key of an object of type")
}
if instance.OnElementRemoved == nil {
return
}
l = l.WithField("key", key)
if err := instance.OnElementRemoved(key); err != nil {
instance.lastError.Store(err)
if instance.OnError != nil {
instance.OnError(key, "elementRemoved", err)
} else {
l.WithError(err).
Error("cannot handle element")
}
} else {
l.Debug("element removed")
}
}
func (instance *Definition) log() *log.Entry {
return log.WithField("type", instance.typeDescription)
}
func (instance *Definition) logEvent(event string) *log.Entry {
return instance.log().WithField("event", event)
}
func (instance *Definition) logKey(event string, key string) *log.Entry {
return instance.logEvent(event).WithField("key", key)
}