-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
services.go
164 lines (136 loc) · 5.07 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
// Copyright 2018 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 clustermesh
import (
"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/service"
"github.com/sirupsen/logrus"
)
// 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 *service.ClusterService)
MergeExternalServiceDelete(service *service.ClusterService)
}
type globalService struct {
clusterServices map[string]*service.ClusterService
}
func newGlobalService() *globalService {
return &globalService{
clusterServices: map[string]*service.ClusterService{},
}
}
type globalServiceCache struct {
mutex lock.RWMutex
byName map[string]*globalService
}
func newGlobalServiceCache() *globalServiceCache {
return &globalServiceCache{
byName: map[string]*globalService{},
}
}
func (c *globalServiceCache) onUpdate(svc *service.ClusterService) {
c.mutex.Lock()
scopedLog := log.WithFields(logrus.Fields{
logfields.ServiceName: svc.String(),
logfields.ClusterName: svc.Cluster,
})
// 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())
}
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) {
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
}
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)
}
}
func (c *globalServiceCache) onDelete(svc *service.ClusterService) {
scopedLog := log.WithFields(logrus.Fields{logfields.ServiceName: svc.String()})
scopedLog.Debug("Delete event for service")
c.mutex.Lock()
if globalService, ok := c.byName[svc.NamespaceServiceName()]; ok {
c.delete(globalService, svc.NamespaceServiceName(), svc.Cluster)
} else {
scopedLog.Debugf("Ignoring delete request for unknown global service")
}
c.mutex.Unlock()
}
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, serviceName, clusterName)
}
c.mutex.Unlock()
}
type remoteServiceObserver struct {
remoteCluster *remoteCluster
}
// OnUpdate is called when a service in a remote cluster is updated
func (r *remoteServiceObserver) OnUpdate(key store.Key) {
if svc, ok := key.(*service.ClusterService); ok {
scopedLog := log.WithFields(logrus.Fields{logfields.ServiceName: svc.String()})
scopedLog.Debugf("Update event of remote service %#v", svc)
mesh := r.remoteCluster.mesh
mesh.globalServices.onUpdate(svc)
if merger := mesh.conf.ServiceMerger; merger != nil {
merger.MergeExternalServiceUpdate(svc)
} 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.(*service.ClusterService); ok {
scopedLog := log.WithFields(logrus.Fields{logfields.ServiceName: svc.String()})
scopedLog.Debugf("Update event of remote service %#v", svc)
mesh := r.remoteCluster.mesh
mesh.globalServices.onDelete(svc)
if merger := mesh.conf.ServiceMerger; merger != nil {
merger.MergeExternalServiceDelete(svc)
} else {
scopedLog.Debugf("Ignoring remote service update. Missing merger function")
}
} else {
log.Warningf("Received unexpected remote service delete object %+v", key)
}
}