@@ -21,8 +21,10 @@ import (
21
21
"time"
22
22
23
23
"k8s.io/api/core/v1"
24
+ discovery "k8s.io/api/discovery/v1alpha1"
24
25
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
25
26
coreinformers "k8s.io/client-go/informers/core/v1"
27
+ discoveryinformers "k8s.io/client-go/informers/discovery/v1alpha1"
26
28
"k8s.io/client-go/tools/cache"
27
29
"k8s.io/klog"
28
30
)
@@ -61,6 +63,40 @@ type EndpointsHandler interface {
61
63
OnEndpointsSynced ()
62
64
}
63
65
66
+ // EndpointSliceHandler is an abstract interface of objects which receive
67
+ // notifications about endpoint slice object changes.
68
+ type EndpointSliceHandler interface {
69
+ // OnEndpointSliceAdd is called whenever creation of new endpoint slice
70
+ // object is observed.
71
+ OnEndpointSliceAdd (endpointSlice * discovery.EndpointSlice )
72
+ // OnEndpointSliceUpdate is called whenever modification of an existing
73
+ // endpoint slice object is observed.
74
+ OnEndpointSliceUpdate (oldEndpointSlice , newEndpointSlice * discovery.EndpointSlice )
75
+ // OnEndpointSliceDelete is called whenever deletion of an existing
76
+ // endpoint slice object is observed.
77
+ OnEndpointSliceDelete (endpointSlice * discovery.EndpointSlice )
78
+ // OnEndpointSlicesSynced is called once all the initial event handlers were
79
+ // called and the state is fully propagated to local cache.
80
+ OnEndpointSlicesSynced ()
81
+ }
82
+
83
+ // NoopEndpointSliceHandler is a noop handler for proxiers that have not yet
84
+ // implemented a full EndpointSliceHandler.
85
+ type NoopEndpointSliceHandler struct {}
86
+
87
+ // OnEndpointSliceAdd is a noop handler for EndpointSlice creates.
88
+ func (* NoopEndpointSliceHandler ) OnEndpointSliceAdd (endpointSlice * discovery.EndpointSlice ) {}
89
+
90
+ // OnEndpointSliceUpdate is a noop handler for EndpointSlice updates.
91
+ func (* NoopEndpointSliceHandler ) OnEndpointSliceUpdate (oldEndpointSlice , newEndpointSlice * discovery.EndpointSlice ) {
92
+ }
93
+
94
+ // OnEndpointSliceDelete is a noop handler for EndpointSlice deletes.
95
+ func (* NoopEndpointSliceHandler ) OnEndpointSliceDelete (endpointSlice * discovery.EndpointSlice ) {}
96
+
97
+ // OnEndpointSlicesSynced is a noop handler for EndpointSlice syncs.
98
+ func (* NoopEndpointSliceHandler ) OnEndpointSlicesSynced () {}
99
+
64
100
// EndpointsConfig tracks a set of endpoints configurations.
65
101
type EndpointsConfig struct {
66
102
listerSynced cache.InformerSynced
@@ -152,6 +188,97 @@ func (c *EndpointsConfig) handleDeleteEndpoints(obj interface{}) {
152
188
}
153
189
}
154
190
191
+ // EndpointSliceConfig tracks a set of endpoints configurations.
192
+ type EndpointSliceConfig struct {
193
+ listerSynced cache.InformerSynced
194
+ eventHandlers []EndpointSliceHandler
195
+ }
196
+
197
+ // NewEndpointSliceConfig creates a new EndpointSliceConfig.
198
+ func NewEndpointSliceConfig (endpointSliceInformer discoveryinformers.EndpointSliceInformer , resyncPeriod time.Duration ) * EndpointSliceConfig {
199
+ result := & EndpointSliceConfig {
200
+ listerSynced : endpointSliceInformer .Informer ().HasSynced ,
201
+ }
202
+
203
+ endpointSliceInformer .Informer ().AddEventHandlerWithResyncPeriod (
204
+ cache.ResourceEventHandlerFuncs {
205
+ AddFunc : result .handleAddEndpointSlice ,
206
+ UpdateFunc : result .handleUpdateEndpointSlice ,
207
+ DeleteFunc : result .handleDeleteEndpointSlice ,
208
+ },
209
+ resyncPeriod ,
210
+ )
211
+
212
+ return result
213
+ }
214
+
215
+ // RegisterEventHandler registers a handler which is called on every endpoint slice change.
216
+ func (c * EndpointSliceConfig ) RegisterEventHandler (handler EndpointSliceHandler ) {
217
+ c .eventHandlers = append (c .eventHandlers , handler )
218
+ }
219
+
220
+ // Run waits for cache synced and invokes handlers after syncing.
221
+ func (c * EndpointSliceConfig ) Run (stopCh <- chan struct {}) {
222
+ klog .Info ("Starting endpoint slice config controller" )
223
+
224
+ if ! cache .WaitForNamedCacheSync ("endpoint slice config" , stopCh , c .listerSynced ) {
225
+ return
226
+ }
227
+
228
+ for _ , h := range c .eventHandlers {
229
+ klog .V (3 ).Infof ("Calling handler.OnEndpointSlicesSynced()" )
230
+ h .OnEndpointSlicesSynced ()
231
+ }
232
+ }
233
+
234
+ func (c * EndpointSliceConfig ) handleAddEndpointSlice (obj interface {}) {
235
+ endpointSlice , ok := obj .(* discovery.EndpointSlice )
236
+ if ! ok {
237
+ utilruntime .HandleError (fmt .Errorf ("unexpected object type: %T" , obj ))
238
+ return
239
+ }
240
+ for _ , h := range c .eventHandlers {
241
+ klog .V (4 ).Infof ("Calling handler.OnEndpointSliceUpdate %+v" , endpointSlice )
242
+ h .OnEndpointSliceAdd (endpointSlice )
243
+ }
244
+ }
245
+
246
+ func (c * EndpointSliceConfig ) handleUpdateEndpointSlice (oldObj , newObj interface {}) {
247
+ oldEndpointSlice , ok := oldObj .(* discovery.EndpointSlice )
248
+ if ! ok {
249
+ utilruntime .HandleError (fmt .Errorf ("unexpected object type: %T" , newObj ))
250
+ return
251
+ }
252
+ newEndpointSlice , ok := newObj .(* discovery.EndpointSlice )
253
+ if ! ok {
254
+ utilruntime .HandleError (fmt .Errorf ("unexpected object type: %T" , newObj ))
255
+ return
256
+ }
257
+ for _ , h := range c .eventHandlers {
258
+ klog .V (4 ).Infof ("Calling handler.OnEndpointSliceUpdate" )
259
+ h .OnEndpointSliceUpdate (oldEndpointSlice , newEndpointSlice )
260
+ }
261
+ }
262
+
263
+ func (c * EndpointSliceConfig ) handleDeleteEndpointSlice (obj interface {}) {
264
+ endpointSlice , ok := obj .(* discovery.EndpointSlice )
265
+ if ! ok {
266
+ tombstone , ok := obj .(cache.DeletedFinalStateUnknown )
267
+ if ! ok {
268
+ utilruntime .HandleError (fmt .Errorf ("unexpected object type: %T" , obj ))
269
+ return
270
+ }
271
+ if endpointSlice , ok = tombstone .Obj .(* discovery.EndpointSlice ); ! ok {
272
+ utilruntime .HandleError (fmt .Errorf ("unexpected object type: %T" , obj ))
273
+ return
274
+ }
275
+ }
276
+ for _ , h := range c .eventHandlers {
277
+ klog .V (4 ).Infof ("Calling handler.OnEndpointsDelete" )
278
+ h .OnEndpointSliceDelete (endpointSlice )
279
+ }
280
+ }
281
+
155
282
// ServiceConfig tracks a set of service configurations.
156
283
type ServiceConfig struct {
157
284
listerSynced cache.InformerSynced
0 commit comments