/
definition.go
187 lines (159 loc) · 4.9 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
178
179
180
181
182
183
184
185
186
187
package definition
import (
"fmt"
"github.com/echocat/lingress/support"
"github.com/echocat/slf4g"
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
Logger log.Logger
typeDescription string
informer cache.SharedInformer
lastError atomic.Value
OnElementAdded OnElementChangedFunc
OnElementUpdated OnElementUpdatedFunc
OnElementRemoved OnElementRemovedFunc
OnError OnErrorFunc
}
type OnElementChangedFunc func(ref support.ObjectReference, new metav1.Object) error
type OnElementUpdatedFunc func(ref support.ObjectReference, old, new metav1.Object) error
type OnElementRemovedFunc func(ref support.ObjectReference) error
type OnErrorFunc func(ref support.ObjectReference, event string, err error)
func newDefinition(typeDescription string, informer cache.SharedInformer, logger log.Logger) (*Definition, error) {
return &Definition{
typeDescription: typeDescription,
informer: informer,
Logger: logger.With("type", typeDescription),
}, nil
}
func (this *Definition) SetInformer(informer cache.SharedInformer) {
this.informer = informer
}
func (this *Definition) Init(stop support.Channel) error {
if this.informer == nil {
panic(fmt.Sprintf("definition %s has no informer", this.typeDescription))
}
_, err := this.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: this.onClusterElementAdded,
UpdateFunc: this.onClusterElementUpdated,
DeleteFunc: this.onClusterElementRemoved,
})
if err != nil {
return fmt.Errorf("creation of event handler for %s failed: %w", this.typeDescription, err)
}
go this.Run(stop)
if !cache.WaitForCacheSync(support.ToChan(stop), this.HasSynced) {
stop.Broadcast()
return fmt.Errorf("initial %s synchronization failed", this.typeDescription)
}
if err := this.lastError.Load(); err != nil {
stop.Broadcast()
return fmt.Errorf("initial %s synchronization failed: %w", this.typeDescription, err.(error))
}
return nil
}
func (this *Definition) HasSynced() bool {
return this.informer.HasSynced()
}
func (this *Definition) Run(stop support.Channel) {
defer runtime.HandleCrash()
this.Logger.Info("Definition store started.")
go this.informer.Run(support.ToChan(stop))
stop.Wait()
this.Logger.Info("Definition store stopped.")
}
func (this *Definition) onClusterElementAdded(new interface{}) {
l := this.logEvent("elementAdded")
objSource, ok := new.(support.ObjectReferenceSource)
if !ok {
l.With("objectType", reflect.TypeOf(new)).
Error("Cannot determine reference of an object of type.")
}
ref, err := support.NewObjectReferenceOf(objSource)
if err != nil {
l.WithError(err).
With("objectType", reflect.TypeOf(new)).
Error("Cannot determine reference of an object of type.")
}
if this.OnElementAdded == nil {
return
}
l = l.With("ref", ref)
if err := this.OnElementAdded(ref, objSource); err != nil {
this.lastError.Store(err)
if this.OnError != nil {
this.OnError(ref, "elementRemoved", err)
} else {
l.WithError(err).
Error("Cannot handle element.")
}
} else {
l.Debug("Element added.")
}
}
func (this *Definition) onClusterElementUpdated(old interface{}, new interface{}) {
l := this.logEvent("elementUpdated")
objSource, ok := new.(support.ObjectReferenceSource)
if !ok {
l.With("objectType", reflect.TypeOf(new)).
Error("Cannot determine reference of an object of type.")
}
ref, err := support.NewObjectReferenceOf(objSource)
if err != nil {
l.WithError(err).
With("objectType", reflect.TypeOf(new)).
Error("Cannot determine reference of an object of type.")
}
if this.OnElementUpdated == nil {
return
}
l = l.With("ref", ref)
if err := this.OnElementUpdated(ref, old.(metav1.Object), objSource); err != nil {
this.lastError.Store(err)
if this.OnError != nil {
this.OnError(ref, "elementRemoved", err)
} else {
l.WithError(err).
Error("Cannot handle element.")
}
} else {
l.Debug("Element updated.")
}
}
func (this *Definition) onClusterElementRemoved(old interface{}) {
l := this.logEvent("elementRemoved")
objSource, ok := old.(support.ObjectReferenceSource)
if !ok {
l.With("objectType", reflect.TypeOf(old)).
Error("Cannot determine key of an object of type.")
}
ref, err := support.NewObjectReferenceOf(objSource)
if err != nil {
l.WithError(err).
With("objectType", reflect.TypeOf(old)).
Error("Cannot determine reference of an object of type.")
}
if this.OnElementRemoved == nil {
return
}
l = l.With("ref", ref)
if err := this.OnElementRemoved(ref); err != nil {
this.lastError.Store(err)
if this.OnError != nil {
this.OnError(ref, "elementRemoved", err)
} else {
l.WithError(err).
Error("Cannot handle element.")
}
} else {
l.Debug("Element removed.")
}
}
func (this *Definition) logEvent(event string) log.Logger {
return this.Logger.With("event", event)
}