-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
services.go
204 lines (167 loc) · 6.26 KB
/
services.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package clustermesh
import (
"github.com/sirupsen/logrus"
"github.com/cilium/cilium/pkg/kvstore/store"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/metrics/metric"
serviceStore "github.com/cilium/cilium/pkg/service/store"
)
// ServiceMerger is the interface to be implemented by the owner of local
// services. The functions have to merge service updates and deletions with
// local services to provide a shared view.
type ServiceMerger interface {
MergeExternalServiceUpdate(service *serviceStore.ClusterService, swg *lock.StoppableWaitGroup)
MergeExternalServiceDelete(service *serviceStore.ClusterService, swg *lock.StoppableWaitGroup)
}
type globalService struct {
clusterServices map[string]*serviceStore.ClusterService
}
func newGlobalService() *globalService {
return &globalService{
clusterServices: map[string]*serviceStore.ClusterService{},
}
}
type globalServiceCache struct {
mutex lock.RWMutex
byName map[string]*globalService
// metricTotalGlobalServices is the gauge metric for total of global services
metricTotalGlobalServices metric.Gauge
}
func newGlobalServiceCache(metricTotalGlobalServices metric.Gauge) *globalServiceCache {
return &globalServiceCache{
byName: map[string]*globalService{},
metricTotalGlobalServices: metricTotalGlobalServices,
}
}
// has returns whether a given service is present in the cache.
func (c *globalServiceCache) has(svc *serviceStore.ClusterService) bool {
c.mutex.RLock()
defer c.mutex.RUnlock()
if globalSvc, ok := c.byName[svc.NamespaceServiceName()]; ok {
_, ok = globalSvc.clusterServices[svc.Cluster]
return ok
}
return false
}
func (c *globalServiceCache) onUpdate(svc *serviceStore.ClusterService) {
scopedLog := log.WithFields(logrus.Fields{
logfields.ServiceName: svc.String(),
logfields.ClusterName: svc.Cluster,
})
c.mutex.Lock()
// Validate that the global service is known
globalSvc, ok := c.byName[svc.NamespaceServiceName()]
if !ok {
globalSvc = newGlobalService()
c.byName[svc.NamespaceServiceName()] = globalSvc
scopedLog.Debugf("Created global service %s", svc.NamespaceServiceName())
c.metricTotalGlobalServices.Set(float64(len(c.byName)))
}
scopedLog.Debugf("Updated service definition of remote cluster %#v", svc)
globalSvc.clusterServices[svc.Cluster] = svc
c.mutex.Unlock()
}
// must be called with c.mutex held
func (c *globalServiceCache) delete(globalService *globalService, clusterName, serviceName string) bool {
scopedLog := log.WithFields(logrus.Fields{
logfields.ServiceName: serviceName,
logfields.ClusterName: clusterName,
})
if _, ok := globalService.clusterServices[clusterName]; !ok {
scopedLog.Debug("Ignoring delete request for unknown cluster")
return false
}
scopedLog.Debugf("Deleted service definition of remote cluster")
delete(globalService.clusterServices, clusterName)
// After the last cluster service is removed, remove the
// global service
if len(globalService.clusterServices) == 0 {
scopedLog.Debugf("Deleted global service %s", serviceName)
delete(c.byName, serviceName)
c.metricTotalGlobalServices.Set(float64(len(c.byName)))
}
return true
}
func (c *globalServiceCache) onDelete(svc *serviceStore.ClusterService) bool {
scopedLog := log.WithFields(logrus.Fields{logfields.ServiceName: svc.String()})
scopedLog.Debug("Delete event for service")
c.mutex.Lock()
defer c.mutex.Unlock()
if globalService, ok := c.byName[svc.NamespaceServiceName()]; ok {
return c.delete(globalService, svc.Cluster, svc.NamespaceServiceName())
} else {
scopedLog.Debugf("Ignoring delete request for unknown global service")
return false
}
}
func (c *globalServiceCache) onClusterDelete(clusterName string) {
scopedLog := log.WithFields(logrus.Fields{logfields.ClusterName: clusterName})
scopedLog.Debugf("Cluster deletion event")
c.mutex.Lock()
for serviceName, globalService := range c.byName {
c.delete(globalService, clusterName, serviceName)
}
c.mutex.Unlock()
}
// size returns the number of global services in the cache
func (c *globalServiceCache) size() (num int) {
c.mutex.RLock()
num = len(c.byName)
c.mutex.RUnlock()
return
}
type remoteServiceObserver struct {
remoteCluster *remoteCluster
// swg provides a mechanism to known when the services were synchronized
// with the datapath.
swg *lock.StoppableWaitGroup
}
// OnUpdate is called when a service in a remote cluster is updated
func (r *remoteServiceObserver) OnUpdate(key store.Key) {
if svc, ok := key.(*serviceStore.ClusterService); ok {
scopedLog := log.WithFields(logrus.Fields{logfields.ServiceName: svc.String()})
scopedLog.Debugf("Update event of remote service %#v", svc)
mesh := r.remoteCluster.mesh
// Short-circuit the handling of non-shared services
if !svc.Shared {
if mesh.globalServices.has(svc) {
scopedLog.Debug("Previously shared service is no longer shared: triggering deletion event")
r.OnDelete(key)
} else {
scopedLog.Debug("Ignoring remote service update: service is not shared")
}
return
}
mesh.globalServices.onUpdate(svc)
if merger := mesh.conf.ServiceMerger; merger != nil {
merger.MergeExternalServiceUpdate(svc, r.swg)
} else {
scopedLog.Debugf("Ignoring remote service update. Missing merger function")
}
} else {
log.Warningf("Received unexpected remote service update object %+v", key)
}
}
// OnDelete is called when a service in a remote cluster is deleted
func (r *remoteServiceObserver) OnDelete(key store.NamedKey) {
if svc, ok := key.(*serviceStore.ClusterService); ok {
scopedLog := log.WithFields(logrus.Fields{logfields.ServiceName: svc.String()})
scopedLog.Debugf("Delete event of remote service %#v", svc)
mesh := r.remoteCluster.mesh
// Short-circuit the deletion logic if the service was not present (i.e., not shared)
if !mesh.globalServices.onDelete(svc) {
scopedLog.Debugf("Ignoring remote service delete. Service was not shared")
return
}
if merger := mesh.conf.ServiceMerger; merger != nil {
merger.MergeExternalServiceDelete(svc, r.swg)
} else {
scopedLog.Debugf("Ignoring remote service delete. Missing merger function")
}
} else {
log.Warningf("Received unexpected remote service delete object %+v", key)
}
}