This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
294 lines (261 loc) · 8.51 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
// Copyright 2017 Istio Authors
//
// 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 aggregate
import (
"sync"
multierror "github.com/hashicorp/go-multierror"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pilot/pkg/serviceregistry"
"istio.io/istio/pkg/log"
)
// Registry specifies the collection of service registry related interfaces
type Registry struct {
Name serviceregistry.ServiceRegistry
ClusterID string
model.Controller
model.ServiceDiscovery
model.ServiceAccounts
}
var (
clusterAddressesMutex sync.Mutex
)
// Controller aggregates data across different registries and monitors for changes
type Controller struct {
registries []Registry
storeLock sync.RWMutex
}
// NewController creates a new Aggregate controller
func NewController() *Controller {
return &Controller{
registries: []Registry{},
}
}
// AddRegistry adds registries into the aggregated controller
func (c *Controller) AddRegistry(registry Registry) {
c.storeLock.Lock()
defer c.storeLock.Unlock()
registries := c.registries
registries = append(registries, registry)
c.registries = registries
}
// DeleteRegistry deletes specified registry from the aggregated controller
func (c *Controller) DeleteRegistry(clusterID string) {
c.storeLock.Lock()
defer c.storeLock.Unlock()
if len(c.registries) == 0 {
log.Warnf("Registry list is empty, nothing to delete")
return
}
index, ok := c.GetRegistryIndex(clusterID)
if !ok {
log.Warnf("Registry is not found in the registries list, nothing to delete")
return
}
registries := c.registries
registries = append(registries[:index], registries[index+1:]...)
c.registries = registries
log.Infof("Registry for the cluster %s has been deleted.", clusterID)
}
// GetRegistries returns a copy of all registries
func (c *Controller) GetRegistries() []Registry {
c.storeLock.Lock()
defer c.storeLock.Unlock()
return c.registries
}
// GetRegistryIndex returns the index of a registry
func (c *Controller) GetRegistryIndex(clusterID string) (int, bool) {
for i, r := range c.registries {
if r.ClusterID == clusterID {
return i, true
}
}
return 0, false
}
// Services lists services from all platforms
func (c *Controller) Services() ([]*model.Service, error) {
// smap is a map of hostname (string) to service, used to identify services that
// are installed in multiple clusters.
smap := make(map[model.Hostname]*model.Service)
services := make([]*model.Service, 0)
var errs error
// Locking Registries list while walking it to prevent inconsistent results
for _, r := range c.GetRegistries() {
svcs, err := r.Services()
if err != nil {
errs = multierror.Append(errs, err)
continue
}
// Race condition: multiple threads may call Services, and multiple services
// may modify one of the service's cluster ID
clusterAddressesMutex.Lock()
for _, s := range svcs {
sp, ok := smap[s.Hostname]
if !ok {
// First time we see a service. The result will have a single service per hostname
// The first cluster will be listed first, so the services in the primary cluster
// will be used for default settings. If a service appears in multiple clusters,
// the order is less clear.
sp = s
smap[s.Hostname] = sp
services = append(services, sp)
}
// If the registry has a cluster ID, keep track of the cluster and the
// local address inside the cluster.
// TODO: what is this used for ? Do we want to support multiple VIPs, or
// only use the 'primary' VIP ?
if r.ClusterID != "" {
if sp.ClusterVIPs == nil {
sp.ClusterVIPs = make(map[string]string)
}
sp.ClusterVIPs[r.ClusterID] = s.Address
smap[s.Hostname] = sp
}
}
clusterAddressesMutex.Unlock()
}
return services, errs
}
// GetService retrieves a service by hostname if exists
func (c *Controller) GetService(hostname model.Hostname) (*model.Service, error) {
var errs error
for _, r := range c.GetRegistries() {
service, err := r.GetService(hostname)
if err != nil {
errs = multierror.Append(errs, err)
} else if service != nil {
if errs != nil {
log.Warnf("GetService() found match but encountered an error: %v", errs)
}
return service, nil
}
}
return nil, errs
}
// ManagementPorts retrieves set of health check ports by instance IP
// Return on the first hit.
func (c *Controller) ManagementPorts(addr string) model.PortList {
for _, r := range c.GetRegistries() {
if portList := r.ManagementPorts(addr); portList != nil {
return portList
}
}
return nil
}
// Instances retrieves instances for a service and its ports that match
// any of the supplied labels. All instances match an empty label list.
func (c *Controller) Instances(hostname model.Hostname, ports []string,
labels model.LabelsCollection) ([]*model.ServiceInstance, error) {
var instances, tmpInstances []*model.ServiceInstance
var errs error
for _, r := range c.GetRegistries() {
var err error
tmpInstances, err = r.Instances(hostname, ports, labels)
if err != nil {
errs = multierror.Append(errs, err)
} else if len(tmpInstances) > 0 {
if errs != nil {
log.Warnf("Instances() found match but encountered an error: %v", errs)
}
instances = append(instances, tmpInstances...)
}
}
if len(instances) > 0 {
errs = nil
}
return instances, errs
}
// InstancesByPort retrieves instances for a service on a given port that match
// any of the supplied labels. All instances match an empty label list.
func (c *Controller) InstancesByPort(hostname model.Hostname, port int,
labels model.LabelsCollection) ([]*model.ServiceInstance, error) {
var instances, tmpInstances []*model.ServiceInstance
var errs error
for _, r := range c.GetRegistries() {
var err error
tmpInstances, err = r.InstancesByPort(hostname, port, labels)
if err != nil {
errs = multierror.Append(errs, err)
} else if len(tmpInstances) > 0 {
if errs != nil {
log.Warnf("Instances() found match but encountered an error: %v", errs)
}
instances = append(instances, tmpInstances...)
}
}
if len(instances) > 0 {
errs = nil
}
return instances, errs
}
// GetProxyServiceInstances lists service instances co-located with a given proxy
func (c *Controller) GetProxyServiceInstances(node *model.Proxy) ([]*model.ServiceInstance, error) {
out := make([]*model.ServiceInstance, 0)
var errs error
// It doesn't make sense for a single proxy to be found in more than one registry.
// TODO: if otherwise, warning or else what to do about it.
for _, r := range c.GetRegistries() {
instances, err := r.GetProxyServiceInstances(node)
if err != nil {
errs = multierror.Append(errs, err)
} else if len(instances) > 0 {
out = append(out, instances...)
node.ClusterID = r.ClusterID
break
}
}
if len(out) > 0 {
if errs != nil {
log.Warnf("GetProxyServiceInstances() found match but encountered an error: %v", errs)
}
return out, nil
}
return out, errs
}
// Run starts all the controllers
func (c *Controller) Run(stop <-chan struct{}) {
for _, r := range c.GetRegistries() {
go r.Run(stop)
}
<-stop
log.Info("Registry Aggregator terminated")
}
// AppendServiceHandler implements a service catalog operation
func (c *Controller) AppendServiceHandler(f func(*model.Service, model.Event)) error {
for _, r := range c.GetRegistries() {
if err := r.AppendServiceHandler(f); err != nil {
log.Infof("Fail to append service handler to adapter %s", r.Name)
return err
}
}
return nil
}
// AppendInstanceHandler implements a service instance catalog operation
func (c *Controller) AppendInstanceHandler(f func(*model.ServiceInstance, model.Event)) error {
for _, r := range c.GetRegistries() {
if err := r.AppendInstanceHandler(f); err != nil {
log.Infof("Fail to append instance handler to adapter %s", r.Name)
return err
}
}
return nil
}
// GetIstioServiceAccounts implements model.ServiceAccounts operation
func (c *Controller) GetIstioServiceAccounts(hostname model.Hostname, ports []string) []string {
for _, r := range c.GetRegistries() {
if svcAccounts := r.GetIstioServiceAccounts(hostname, ports); svcAccounts != nil {
return svcAccounts
}
}
return nil
}