-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
service_cache.go
555 lines (473 loc) · 16.7 KB
/
service_cache.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Copyright 2018-2020 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package k8s
import (
"net"
"github.com/cilium/cilium/pkg/datapath"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/core/v1"
slim_discovery_v1beta1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/discovery/v1beta1"
"github.com/cilium/cilium/pkg/loadbalancer"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/option"
serviceStore "github.com/cilium/cilium/pkg/service/store"
"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
)
// CacheAction is the type of action that was performed on the cache
type CacheAction int
const (
// UpdateService reflects that the service was updated or added
UpdateService CacheAction = iota
// DeleteService reflects that the service was deleted
DeleteService
)
// String returns the cache action as a string
func (c CacheAction) String() string {
switch c {
case UpdateService:
return "service-updated"
case DeleteService:
return "service-deleted"
default:
return "unknown"
}
}
// ServiceEvent is emitted via the Events channel of ServiceCache and describes
// the change that occurred in the cache
type ServiceEvent struct {
// Action is the action that was performed in the cache
Action CacheAction
// ID is the identified of the service
ID ServiceID
// Service is the service structure
Service *Service
// OldService is the service structure
OldService *Service
// Endpoints is the endpoints structured correlated with the service
Endpoints *Endpoints
// SWG provides a mechanism to detect if a service was synchronized with
// the datapath.
SWG *lock.StoppableWaitGroup
}
// ServiceCache is a list of services correlated with the matching endpoints.
// The Events member will receive events as services.
type ServiceCache struct {
Events chan ServiceEvent
// mutex protects the maps below including the concurrent access of each
// value.
mutex lock.RWMutex
services map[ServiceID]*Service
// endpoints maps a service to a map of endpointSlices. In case the cluster
// is still using the v1.Endpoints, the key used in the internal map of
// endpointSlices is the v1.Endpoint name.
endpoints map[ServiceID]*endpointSlices
// externalEndpoints is a list of additional service backends derived from source other than the local cluster
externalEndpoints map[ServiceID]externalEndpoints
nodeAddressing datapath.NodeAddressing
}
// NewServiceCache returns a new ServiceCache
func NewServiceCache(nodeAddressing datapath.NodeAddressing) ServiceCache {
return ServiceCache{
services: map[ServiceID]*Service{},
endpoints: map[ServiceID]*endpointSlices{},
externalEndpoints: map[ServiceID]externalEndpoints{},
Events: make(chan ServiceEvent, option.Config.K8sServiceCacheSize),
nodeAddressing: nodeAddressing,
}
}
// GetServiceIP returns a random L3n4Addr that is backing the given Service ID.
// The returned IP is with external scope since its string representation might
// be used for net Dialer.
func (s *ServiceCache) GetServiceIP(svcID ServiceID) *loadbalancer.L3n4Addr {
s.mutex.RLock()
defer s.mutex.RUnlock()
svc := s.services[svcID]
if svc == nil {
return nil
}
for _, port := range svc.Ports {
return loadbalancer.NewL3n4Addr(port.Protocol, svc.FrontendIP, port.Port,
loadbalancer.ScopeExternal)
}
return nil
}
// GetServiceFrontendIP returns the frontend IP (aka clusterIP) for the given service with type.
func (s *ServiceCache) GetServiceFrontendIP(svcID ServiceID, svcType loadbalancer.SVCType) net.IP {
s.mutex.RLock()
defer s.mutex.RUnlock()
svc := s.services[svcID]
if svc == nil || svc.Type != svcType {
return nil
}
return svc.FrontendIP
}
// GetServiceAddrWithPortsAndType returns a slice of all the L3n4Addr that are backing the
// given Service ID with given type.
// Note: The returned IPs are with External scope.
func (s *ServiceCache) GetServiceAddrsWithType(svcID ServiceID, svcType loadbalancer.SVCType) map[loadbalancer.FEPortName]*loadbalancer.L3n4Addr {
s.mutex.RLock()
defer s.mutex.RUnlock()
svc := s.services[svcID]
if svc == nil || svc.Type != svcType {
return nil
}
addrsByPort := make(map[loadbalancer.FEPortName]*loadbalancer.L3n4Addr)
for pName, l4Addr := range svc.Ports {
addrsByPort[pName] = loadbalancer.NewL3n4Addr(l4Addr.Protocol, svc.FrontendIP,
l4Addr.Port, loadbalancer.ScopeExternal)
}
return addrsByPort
}
// GetNodeAddressing returns the registered node addresses to this service cache.
func (s *ServiceCache) GetNodeAddressing() datapath.NodeAddressing {
return s.nodeAddressing
}
// UpdateService parses a Kubernetes service and adds or updates it in the
// ServiceCache. Returns the ServiceID unless the Kubernetes service could not
// be parsed and a bool to indicate whether the service was changed in the
// cache or not.
func (s *ServiceCache) UpdateService(k8sSvc *slim_corev1.Service, swg *lock.StoppableWaitGroup) ServiceID {
svcID, newService := ParseService(k8sSvc, s.nodeAddressing)
if newService == nil {
return svcID
}
s.mutex.Lock()
defer s.mutex.Unlock()
oldService, ok := s.services[svcID]
if ok {
if oldService.DeepEquals(newService) {
return svcID
}
}
s.services[svcID] = newService
// Check if the corresponding Endpoints resource is already available
endpoints, serviceReady := s.correlateEndpoints(svcID)
if serviceReady {
swg.Add()
s.Events <- ServiceEvent{
Action: UpdateService,
ID: svcID,
Service: newService,
OldService: oldService,
Endpoints: endpoints,
SWG: swg,
}
}
return svcID
}
func (s *ServiceCache) EnsureService(svcID ServiceID, swg *lock.StoppableWaitGroup) bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
if svc, found := s.services[svcID]; found {
if endpoints, serviceReady := s.correlateEndpoints(svcID); serviceReady {
swg.Add()
s.Events <- ServiceEvent{
Action: UpdateService,
ID: svcID,
Service: svc,
OldService: svc,
Endpoints: endpoints,
SWG: swg,
}
return true
}
}
return false
}
// DeleteService parses a Kubernetes service and removes it from the
// ServiceCache
func (s *ServiceCache) DeleteService(k8sSvc *slim_corev1.Service, swg *lock.StoppableWaitGroup) {
svcID := ParseServiceID(k8sSvc)
s.mutex.Lock()
defer s.mutex.Unlock()
oldService, serviceOK := s.services[svcID]
endpoints, _ := s.correlateEndpoints(svcID)
delete(s.services, svcID)
if serviceOK {
swg.Add()
s.Events <- ServiceEvent{
Action: DeleteService,
ID: svcID,
Service: oldService,
Endpoints: endpoints,
SWG: swg,
}
}
}
func (s *ServiceCache) updateEndpoints(esID EndpointSliceID, newEndpoints *Endpoints, swg *lock.StoppableWaitGroup) (ServiceID, *Endpoints) {
s.mutex.Lock()
defer s.mutex.Unlock()
eps, ok := s.endpoints[esID.ServiceID]
if ok {
if eps.epSlices[esID.EndpointSliceName].DeepEquals(newEndpoints) {
return esID.ServiceID, newEndpoints
}
} else {
eps = newEndpointsSlices()
s.endpoints[esID.ServiceID] = eps
}
eps.Upsert(esID.EndpointSliceName, newEndpoints)
// Check if the corresponding Endpoints resource is already available
svc, ok := s.services[esID.ServiceID]
endpoints, serviceReady := s.correlateEndpoints(esID.ServiceID)
if ok && serviceReady {
swg.Add()
s.Events <- ServiceEvent{
Action: UpdateService,
ID: esID.ServiceID,
Service: svc,
Endpoints: endpoints,
SWG: swg,
}
}
return esID.ServiceID, newEndpoints
}
// UpdateEndpoints parses a Kubernetes endpoints and adds or updates it in the
// ServiceCache. Returns the ServiceID unless the Kubernetes endpoints could not
// be parsed and a bool to indicate whether the endpoints was changed in the
// cache or not.
func (s *ServiceCache) UpdateEndpoints(k8sEndpoints *slim_corev1.Endpoints, swg *lock.StoppableWaitGroup) (ServiceID, *Endpoints) {
svcID, newEndpoints := ParseEndpoints(k8sEndpoints)
epSliceID := EndpointSliceID{
ServiceID: svcID,
EndpointSliceName: k8sEndpoints.GetName(),
}
return s.updateEndpoints(epSliceID, newEndpoints, swg)
}
func (s *ServiceCache) UpdateEndpointSlices(epSlice *slim_discovery_v1beta1.EndpointSlice, swg *lock.StoppableWaitGroup) (ServiceID, *Endpoints) {
svcID, newEndpoints := ParseEndpointSlice(epSlice)
return s.updateEndpoints(svcID, newEndpoints, swg)
}
func (s *ServiceCache) deleteEndpoints(svcID EndpointSliceID, swg *lock.StoppableWaitGroup) ServiceID {
s.mutex.Lock()
defer s.mutex.Unlock()
svc, serviceOK := s.services[svcID.ServiceID]
isEmpty := s.endpoints[svcID.ServiceID].Delete(svcID.EndpointSliceName)
if isEmpty {
delete(s.endpoints, svcID.ServiceID)
}
endpoints, _ := s.correlateEndpoints(svcID.ServiceID)
if serviceOK {
swg.Add()
event := ServiceEvent{
Action: UpdateService,
ID: svcID.ServiceID,
Service: svc,
Endpoints: endpoints,
SWG: swg,
}
s.Events <- event
}
return svcID.ServiceID
}
// DeleteEndpoints parses a Kubernetes endpoints and removes it from the
// ServiceCache
func (s *ServiceCache) DeleteEndpoints(k8sEndpoints *slim_corev1.Endpoints, swg *lock.StoppableWaitGroup) ServiceID {
svcID := ParseEndpointsID(k8sEndpoints)
epSliceID := EndpointSliceID{
ServiceID: svcID,
EndpointSliceName: k8sEndpoints.GetName(),
}
return s.deleteEndpoints(epSliceID, swg)
}
func (s *ServiceCache) DeleteEndpointSlices(epSlice *slim_discovery_v1beta1.EndpointSlice, swg *lock.StoppableWaitGroup) ServiceID {
svcID := ParseEndpointSliceID(epSlice)
return s.deleteEndpoints(svcID, swg)
}
// FrontendList is the list of all k8s service frontends
type FrontendList map[string]struct{}
// LooseMatch returns true if the provided frontend is found in the
// FrontendList. If the frontend has a protocol value set, it only matches a
// k8s service with a matching protocol. If no protocol is set, any k8s service
// matching frontend IP and port is considered a match, regardless of protocol.
func (l FrontendList) LooseMatch(frontend loadbalancer.L3n4Addr) (exists bool) {
switch frontend.Protocol {
case loadbalancer.NONE:
for _, protocol := range loadbalancer.AllProtocols {
frontend.Protocol = protocol
_, exists = l[frontend.StringWithProtocol()]
if exists {
return
}
}
// If the protocol is set, perform an exact match
default:
_, exists = l[frontend.StringWithProtocol()]
}
return
}
// UniqueServiceFrontends returns all externally scoped services known to
// the service cache as a map, indexed by the string representation of a
// loadbalancer.L3n4Addr. This helper is only used in unit tests.
func (s *ServiceCache) UniqueServiceFrontends() FrontendList {
uniqueFrontends := FrontendList{}
s.mutex.RLock()
defer s.mutex.RUnlock()
for _, svc := range s.services {
for _, p := range svc.Ports {
address := loadbalancer.L3n4Addr{
IP: svc.FrontendIP,
L4Addr: *p,
Scope: loadbalancer.ScopeExternal,
}
uniqueFrontends[address.StringWithProtocol()] = struct{}{}
}
for _, nodePortFEs := range svc.NodePorts {
for _, fe := range nodePortFEs {
if fe.Scope == loadbalancer.ScopeExternal {
uniqueFrontends[fe.StringWithProtocol()] = struct{}{}
}
}
}
}
return uniqueFrontends
}
// correlateEndpoints builds a combined Endpoints of the local endpoints and
// all external endpoints if the service is marked as a global service. Also
// returns a boolean that indicates whether the service is ready to be plumbed,
// this is true if:
// IF If ta local endpoints resource is present. Regardless whether the
// endpoints resource contains actual backends or not.
// OR Remote endpoints exist which correlate to the service.
func (s *ServiceCache) correlateEndpoints(id ServiceID) (*Endpoints, bool) {
endpoints := newEndpoints()
localEndpoints := s.endpoints[id].GetEndpoints()
hasLocalEndpoints := localEndpoints != nil
if hasLocalEndpoints {
for ip, e := range localEndpoints.Backends {
endpoints.Backends[ip] = e
}
}
svc, hasExternalService := s.services[id]
if hasExternalService && svc.IncludeExternal {
externalEndpoints, hasExternalEndpoints := s.externalEndpoints[id]
if hasExternalEndpoints {
// remote cluster endpoints already contain all Endpoints from all
// EndpointSlices so no need to search the endpoints of a particular
// EndpointSlice.
for clusterName, remoteClusterEndpoints := range externalEndpoints.endpoints {
if clusterName == option.Config.ClusterName {
continue
}
for ip, e := range remoteClusterEndpoints.Backends {
if _, ok := endpoints.Backends[ip]; ok {
log.WithFields(logrus.Fields{
logfields.K8sSvcName: id.Name,
logfields.K8sNamespace: id.Namespace,
logfields.IPAddr: ip,
"cluster": clusterName,
}).Warning("Conflicting service backend IP")
} else {
endpoints.Backends[ip] = e
}
}
}
}
}
// Report the service as ready if a local endpoints object exists or if
// external endpoints have have been identified
return endpoints, hasLocalEndpoints || len(endpoints.Backends) > 0
}
// MergeExternalServiceUpdate merges a cluster service of a remote cluster into
// the local service cache. The service endpoints are stored as external endpoints
// and are correlated on demand with local services via correlateEndpoints().
func (s *ServiceCache) MergeExternalServiceUpdate(service *serviceStore.ClusterService, swg *lock.StoppableWaitGroup) {
id := ServiceID{Name: service.Name, Namespace: service.Namespace}
scopedLog := log.WithFields(logrus.Fields{logfields.ServiceName: service.String()})
// Ignore updates of own cluster
if service.Cluster == option.Config.ClusterName {
scopedLog.Debug("Not merging external service. Own cluster")
return
}
s.mutex.Lock()
defer s.mutex.Unlock()
externalEndpoints, ok := s.externalEndpoints[id]
if !ok {
externalEndpoints = newExternalEndpoints()
s.externalEndpoints[id] = externalEndpoints
}
scopedLog.Debugf("Updating backends to %+v", service.Backends)
backends := map[string]*Backend{}
for ipString, portConfig := range service.Backends {
backends[ipString] = &Backend{Ports: portConfig}
}
externalEndpoints.endpoints[service.Cluster] = &Endpoints{
Backends: backends,
}
svc, ok := s.services[id]
endpoints, serviceReady := s.correlateEndpoints(id)
// Only send event notification if service is shared and ready.
// External endpoints are still tracked but correlation will not happen
// until the service is marked as shared.
if ok && svc.Shared && serviceReady {
swg.Add()
s.Events <- ServiceEvent{
Action: UpdateService,
ID: id,
Service: svc,
Endpoints: endpoints,
SWG: swg,
}
}
}
// MergeExternalServiceDelete merges the deletion of a cluster service in a
// remote cluster into the local service cache. The service endpoints are
// stored as external endpoints and are correlated on demand with local
// services via correlateEndpoints().
func (s *ServiceCache) MergeExternalServiceDelete(service *serviceStore.ClusterService, swg *lock.StoppableWaitGroup) {
scopedLog := log.WithFields(logrus.Fields{logfields.ServiceName: service.String()})
id := ServiceID{Name: service.Name, Namespace: service.Namespace}
// Ignore updates of own cluster
if service.Cluster == option.Config.ClusterName {
scopedLog.Debug("Not merging external service. Own cluster")
return
}
s.mutex.Lock()
defer s.mutex.Unlock()
externalEndpoints, ok := s.externalEndpoints[id]
if ok {
scopedLog.Debug("Deleting external endpoints")
delete(externalEndpoints.endpoints, service.Cluster)
svc, ok := s.services[id]
endpoints, serviceReady := s.correlateEndpoints(id)
// Only send event notification if service is shared. External
// endpoints are still tracked but correlation will not happen
// until the service is marked as shared.
if ok && svc.Shared {
swg.Add()
event := ServiceEvent{
Action: UpdateService,
ID: id,
Service: svc,
Endpoints: endpoints,
SWG: swg,
}
if !serviceReady {
event.Action = DeleteService
}
s.Events <- event
}
} else {
scopedLog.Debug("Received delete event for non-existing endpoints")
}
}
// DebugStatus implements debug.StatusObject to provide debug status collection
// ability
func (s *ServiceCache) DebugStatus() string {
s.mutex.RLock()
str := spew.Sdump(s)
s.mutex.RUnlock()
return str
}