-
Notifications
You must be signed in to change notification settings - Fork 7
/
controller.go
415 lines (361 loc) · 11.8 KB
/
controller.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package multicluster
import (
"context"
"errors"
"fmt"
k8s "github.com/coredns/coredns/plugin/kubernetes/object"
"github.com/coredns/multicluster/object"
api "k8s.io/api/core/v1"
discovery "k8s.io/api/discovery/v1"
discoveryV1beta1 "k8s.io/api/discovery/v1beta1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
mcs "sigs.k8s.io/mcs-api/pkg/client/clientset/versioned/typed/apis/v1alpha1"
"sync"
"sync/atomic"
"time"
)
const (
svcNameNamespaceIndex = "ServiceNameNamespace"
epNameNamespaceIndex = "EndpointNameNamespace"
)
type controller interface {
ServiceList() []*object.ServiceImport
EndpointsList() []*object.Endpoints
SvcIndex(string) []*object.ServiceImport
EpIndex(string) []*object.Endpoints
GetNamespaceByName(string) (*object.Namespace, error)
Run()
HasSynced() bool
Stop() error
// Modified returns the timestamp of the most recent changes
Modified() int64
}
type control struct {
// Modified tracks timestamp of the most recent changes
// It needs to be first because it is guaranteed to be 8-byte
// aligned ( we use sync.LoadAtomic with this )
modified int64
k8sClient kubernetes.Interface
mcsClient mcs.MulticlusterV1alpha1Interface
svcImportController cache.Controller
svcImportLister cache.Indexer
nsController cache.Controller
nsLister cache.Store
epController cache.Controller
epLister cache.Indexer
// stopLock is used to enforce only a single call to Stop is active.
// Needed because we allow stopping through an http endpoint and
// allowing concurrent stoppers leads to stack traces.
stopLock sync.Mutex
shutdown bool
stopCh chan struct{}
}
type controllerOpts struct {
initEndpointsCache bool
}
func newController(ctx context.Context, k8sClient kubernetes.Interface, mcsClient mcs.MulticlusterV1alpha1Interface, opts controllerOpts) *control {
ctl := control{
k8sClient: k8sClient,
mcsClient: mcsClient,
stopCh: make(chan struct{}),
}
// enable ServiceImport watch
ctl.watchServiceImport(ctx)
// enable Namespace watch
ctl.watchNamespace(ctx)
if opts.initEndpointsCache {
ctl.watchEndpointSlice(ctx)
}
return &ctl
}
func (c *control) watchServiceImport(ctx context.Context) {
c.svcImportLister, c.svcImportController = k8s.NewIndexerInformer(
&cache.ListWatch{
ListFunc: serviceImportListFunc(ctx, c.mcsClient, api.NamespaceAll),
WatchFunc: serviceImportWatchFunc(ctx, c.mcsClient, api.NamespaceAll),
},
&v1alpha1.ServiceImport{},
cache.ResourceEventHandlerFuncs{AddFunc: c.Add, UpdateFunc: c.Update, DeleteFunc: c.Delete},
cache.Indexers{svcNameNamespaceIndex: svcNameNamespaceIndexFunc},
k8s.DefaultProcessor(object.ToServiceImport, nil),
)
}
func (c *control) watchNamespace(ctx context.Context) {
c.nsLister, c.nsController = k8s.NewIndexerInformer(
&cache.ListWatch{
ListFunc: namespaceListFunc(ctx, c.k8sClient),
WatchFunc: namespaceWatchFunc(ctx, c.k8sClient),
},
&api.Namespace{},
cache.ResourceEventHandlerFuncs{},
cache.Indexers{},
k8s.DefaultProcessor(object.ToNamespace, nil),
)
}
func (c *control) watchEndpointSlice(ctx context.Context) {
c.epLister, c.epController = k8s.NewIndexerInformer(
&cache.ListWatch{
ListFunc: endpointSliceListFunc(ctx, c.k8sClient, api.NamespaceAll),
WatchFunc: endpointSliceWatchFunc(ctx, c.k8sClient, api.NamespaceAll),
},
&discovery.EndpointSlice{},
cache.ResourceEventHandlerFuncs{AddFunc: c.Add, UpdateFunc: c.Update, DeleteFunc: c.Delete},
cache.Indexers{epNameNamespaceIndex: epNameNamespaceIndexFunc},
k8s.DefaultProcessor(object.EndpointSliceToEndpoints, nil),
)
}
// watchEndpointSliceV1beta1 will set the endpoint Lister and Controller to watch v1beta1
// instead of the default v1.
func (c *control) WatchEndpointSliceV1beta1(ctx context.Context) {
c.epLister, c.epController = k8s.NewIndexerInformer(
&cache.ListWatch{
ListFunc: endpointSliceListFuncV1beta1(ctx, c.k8sClient, api.NamespaceAll),
WatchFunc: endpointSliceWatchFuncV1beta1(ctx, c.k8sClient, api.NamespaceAll),
},
&discoveryV1beta1.EndpointSlice{},
cache.ResourceEventHandlerFuncs{AddFunc: c.Add, UpdateFunc: c.Update, DeleteFunc: c.Delete},
cache.Indexers{epNameNamespaceIndex: epNameNamespaceIndexFunc},
k8s.DefaultProcessor(object.EndpointSliceV1beta1ToEndpoints, nil),
)
}
// Stop stops the controller.
func (c *control) Stop() error {
c.stopLock.Lock()
defer c.stopLock.Unlock()
// Only try draining the workqueue if we haven't already.
if !c.shutdown {
close(c.stopCh)
c.shutdown = true
return nil
}
return fmt.Errorf("shutdown already in progress")
}
// Run starts the controller.
func (c *control) Run() {
go c.svcImportController.Run(c.stopCh)
go c.nsController.Run(c.stopCh)
if c.epController != nil {
c.epController.Run(c.stopCh)
}
<-c.stopCh
}
// HasSynced calls on all controllers.
func (c *control) HasSynced() bool {
return c.svcImportController.HasSynced() && c.nsController.HasSynced()
}
func (c *control) SvcIndex(idx string) (svcs []*object.ServiceImport) {
os, err := c.svcImportLister.ByIndex(svcNameNamespaceIndex, idx)
if err != nil {
return nil
}
for _, o := range os {
s, ok := o.(*object.ServiceImport)
if !ok {
continue
}
svcs = append(svcs, s)
}
return svcs
}
func (c *control) ServiceList() (svcs []*object.ServiceImport) {
os := c.svcImportLister.List()
for _, o := range os {
s, ok := o.(*object.ServiceImport)
if !ok {
continue
}
svcs = append(svcs, s)
}
return svcs
}
func (c *control) EndpointsList() (eps []*object.Endpoints) {
os := c.epLister.List()
for _, o := range os {
ep, ok := o.(*object.Endpoints)
if !ok {
continue
}
eps = append(eps, ep)
}
return eps
}
func (c *control) EpIndex(idx string) (ep []*object.Endpoints) {
os, err := c.epLister.ByIndex(epNameNamespaceIndex, idx)
if err != nil {
return nil
}
for _, o := range os {
e, ok := o.(*object.Endpoints)
if !ok {
continue
}
ep = append(ep, e)
}
return ep
}
func serviceImportListFunc(ctx context.Context, c mcs.MulticlusterV1alpha1Interface, ns string) func(meta.ListOptions) (runtime.Object, error) {
return func(opts meta.ListOptions) (runtime.Object, error) {
return c.ServiceImports(ns).List(ctx, opts)
}
}
func serviceImportWatchFunc(ctx context.Context, c mcs.MulticlusterV1alpha1Interface, ns string) func(options meta.ListOptions) (watch.Interface, error) {
return func(opts meta.ListOptions) (watch.Interface, error) {
return c.ServiceImports(ns).Watch(ctx, opts)
}
}
func namespaceListFunc(ctx context.Context, c kubernetes.Interface) func(meta.ListOptions) (runtime.Object, error) {
return func(opts meta.ListOptions) (runtime.Object, error) {
return c.CoreV1().Namespaces().List(ctx, opts)
}
}
func namespaceWatchFunc(ctx context.Context, c kubernetes.Interface) func(options meta.ListOptions) (watch.Interface, error) {
return func(opts meta.ListOptions) (watch.Interface, error) {
return c.CoreV1().Namespaces().Watch(ctx, opts)
}
}
func endpointSliceListFunc(ctx context.Context, c kubernetes.Interface, ns string) func(meta.ListOptions) (runtime.Object, error) {
return func(opts meta.ListOptions) (runtime.Object, error) {
opts.LabelSelector = v1alpha1.LabelServiceName // only slices created by MCS controller
return c.DiscoveryV1().EndpointSlices(ns).List(ctx, opts)
}
}
func endpointSliceWatchFunc(ctx context.Context, c kubernetes.Interface, ns string) func(options meta.ListOptions) (watch.Interface, error) {
return func(opts meta.ListOptions) (watch.Interface, error) {
opts.LabelSelector = v1alpha1.LabelServiceName // only slices created by MCS controller
return c.DiscoveryV1().EndpointSlices(ns).Watch(ctx, opts)
}
}
func endpointSliceListFuncV1beta1(ctx context.Context, c kubernetes.Interface, ns string) func(meta.ListOptions) (runtime.Object, error) {
return func(opts meta.ListOptions) (runtime.Object, error) {
opts.LabelSelector = v1alpha1.LabelServiceName // only slices created by MCS controller
return c.DiscoveryV1beta1().EndpointSlices(ns).List(ctx, opts)
}
}
func endpointSliceWatchFuncV1beta1(ctx context.Context, c kubernetes.Interface, ns string) func(options meta.ListOptions) (watch.Interface, error) {
return func(opts meta.ListOptions) (watch.Interface, error) {
opts.LabelSelector = v1alpha1.LabelServiceName // only slices created by MCS controller
return c.DiscoveryV1beta1().EndpointSlices(ns).Watch(ctx, opts)
}
}
// GetNamespaceByName returns the namespace by name. If nothing is found an error is returned.
func (c *control) GetNamespaceByName(name string) (*object.Namespace, error) {
o, exists, err := c.nsLister.GetByKey(name)
if err != nil {
return nil, err
}
if !exists {
return nil, fmt.Errorf("namespace not found")
}
ns, ok := o.(*object.Namespace)
if !ok {
return nil, fmt.Errorf("found key but not namespace")
}
return ns, nil
}
func (c *control) Add(obj interface{}) { c.updateModified() }
func (c *control) Delete(obj interface{}) { c.updateModified() }
func (c *control) Update(oldObj, newObj interface{}) { c.detectChanges(oldObj, newObj) }
// detectChanges detects changes in objects, and updates the modified timestamp
func (c *control) detectChanges(oldObj, newObj interface{}) {
// If both objects have the same resource version, they are identical.
if newObj != nil && oldObj != nil && (oldObj.(meta.Object).GetResourceVersion() == newObj.(meta.Object).GetResourceVersion()) {
return
}
obj := newObj
if obj == nil {
obj = oldObj
}
switch ob := obj.(type) {
case *object.ServiceImport:
c.updateModified()
case *object.Endpoints:
if !endpointsEquivalent(oldObj.(*object.Endpoints), newObj.(*object.Endpoints)) {
c.updateModified()
}
default:
log.Warningf("Updates for %T not supported.", ob)
}
}
// endpointsEquivalent checks if the update to an endpoint is something
// that matters to us or if they are effectively equivalent.
func endpointsEquivalent(a, b *object.Endpoints) bool {
if a == nil || b == nil {
return false
}
if len(a.Subsets) != len(b.Subsets) {
return false
}
// we should be able to rely on
// these being sorted and able to be compared
// they are supposed to be in a canonical format
for i, sa := range a.Subsets {
sb := b.Subsets[i]
if !subsetsEquivalent(sa, sb) {
return false
}
}
return true
}
// subsetsEquivalent checks if two endpoint subsets are significantly equivalent
// I.e. that they have the same ready addresses, host names, ports (including protocol
// and service names for SRV)
func subsetsEquivalent(sa, sb object.EndpointSubset) bool {
if len(sa.Addresses) != len(sb.Addresses) {
return false
}
if len(sa.Ports) != len(sb.Ports) {
return false
}
// in Addresses and Ports, we should be able to rely on
// these being sorted and able to be compared
// they are supposed to be in a canonical format
for addr, aaddr := range sa.Addresses {
baddr := sb.Addresses[addr]
if aaddr.IP != baddr.IP {
return false
}
if aaddr.Hostname != baddr.Hostname {
return false
}
}
for port, aport := range sa.Ports {
bport := sb.Ports[port]
if aport.Name != bport.Name {
return false
}
if aport.Port != bport.Port {
return false
}
if aport.Protocol != bport.Protocol {
return false
}
}
return true
}
func (c *control) Modified() int64 {
unix := atomic.LoadInt64(&c.modified)
return unix
}
func (c *control) updateModified() {
unix := time.Now().Unix()
atomic.StoreInt64(&c.modified, unix)
}
func svcNameNamespaceIndexFunc(obj interface{}) ([]string, error) {
s, ok := obj.(*object.ServiceImport)
if !ok {
return nil, errors.New("obj was not of the correct type")
}
return []string{s.Index}, nil
}
func epNameNamespaceIndexFunc(obj interface{}) ([]string, error) {
s, ok := obj.(*object.Endpoints)
if !ok {
return nil, errors.New("obj was not of the correct type")
}
return []string{s.Index}, nil
}