-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
remote_cluster.go
368 lines (307 loc) · 10.7 KB
/
remote_cluster.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package clustermesh
import (
"context"
"fmt"
"path"
"time"
"github.com/go-openapi/strfmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/allocator"
"github.com/cilium/cilium/pkg/controller"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/kvstore"
"github.com/cilium/cilium/pkg/kvstore/store"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/metrics"
nodeStore "github.com/cilium/cilium/pkg/node/store"
serviceStore "github.com/cilium/cilium/pkg/service/store"
)
// remoteCluster represents another cluster other than the cluster the agent is
// running in
type remoteCluster struct {
// name is the name of the cluster
name string
// configPath is the path to the etcd configuration to be used to
// connect to the etcd cluster of the remote cluster
configPath string
// changed receives an event when the remote cluster configuration has
// changed and is closed when the configuration file was removed
changed chan bool
// mesh is the cluster mesh this remote cluster belongs to
mesh *ClusterMesh
controllers *controller.Manager
// remoteConnectionControllerName is the name of the backing controller
// that maintains the remote connection
remoteConnectionControllerName string
// mutex protects the following variables
// - backend
// - store
// - remoteNodes
// - ipCacheWatcher
// - remoteIdentityCache
mutex lock.RWMutex
// store is the shared store representing all nodes in the remote cluster
remoteNodes *store.SharedStore
// remoteServices is the shared store representing services in remote
// clusters
remoteServices *store.SharedStore
// ipCacheWatcher is the watcher that notifies about IP<->identity
// changes in the remote cluster
ipCacheWatcher *ipcache.IPIdentityWatcher
// remoteIdentityCache is a locally cached copy of the identity
// allocations in the remote cluster
remoteIdentityCache *allocator.RemoteCache
// backend is the kvstore backend being used
backend kvstore.BackendOperations
swg *lock.StoppableWaitGroup
// failures is the number of observed failures
failures int
// lastFailure is the timestamp of the last failure
lastFailure time.Time
metricLastFailureTimestamp *prometheus.GaugeVec
metricReadinessStatus *prometheus.GaugeVec
metricTotalFailures *prometheus.GaugeVec
metricTotalNodes *prometheus.GaugeVec
}
var (
// skipKvstoreConnection skips the etcd connection, used for testing
skipKvstoreConnection bool
)
func (rc *remoteCluster) getLogger() *logrus.Entry {
var (
status string
err error
)
if rc.backend != nil {
status, err = rc.backend.Status()
}
return log.WithFields(logrus.Fields{
fieldClusterName: rc.name,
fieldConfig: rc.configPath,
fieldKVStoreStatus: status,
fieldKVStoreErr: err,
})
}
// releaseOldConnection releases the etcd connection to a remote cluster
func (rc *remoteCluster) releaseOldConnection() {
rc.mutex.Lock()
ipCacheWatcher := rc.ipCacheWatcher
rc.ipCacheWatcher = nil
remoteNodes := rc.remoteNodes
rc.remoteNodes = nil
remoteIdentityCache := rc.remoteIdentityCache
rc.remoteIdentityCache = nil
remoteServices := rc.remoteServices
rc.remoteServices = nil
backend := rc.backend
rc.backend = nil
rc.metricTotalNodes.WithLabelValues(rc.mesh.conf.Name, rc.mesh.conf.NodeName, rc.name).Set(float64(rc.remoteNodes.NumEntries()))
rc.metricReadinessStatus.WithLabelValues(rc.mesh.conf.Name, rc.mesh.conf.NodeName, rc.name).Set(metrics.BoolToFloat64(rc.isReadyLocked()))
rc.mutex.Unlock()
// Release resources asynchronously in the background. Many of these
// operations may time out if the connection was closed due to an error
// condition.
go func() {
if ipCacheWatcher != nil {
ipCacheWatcher.Close()
}
if remoteNodes != nil {
remoteNodes.Close(context.TODO())
}
if remoteIdentityCache != nil {
remoteIdentityCache.Close()
}
if remoteServices != nil {
remoteServices.Close(context.TODO())
}
if backend != nil {
backend.Close()
}
}()
}
func (rc *remoteCluster) restartRemoteConnection(allocator RemoteIdentityWatcher) {
rc.controllers.UpdateController(rc.remoteConnectionControllerName,
controller.ControllerParams{
DoFunc: func(ctx context.Context) error {
rc.releaseOldConnection()
extraOpts := kvstore.ExtraOptions{NoLockQuorumCheck: true}
if rc.mesh.conf.NodeManager != nil {
extraOpts.ClusterSizeDependantInterval = rc.mesh.conf.NodeManager.ClusterSizeDependantInterval
}
backend, errChan := kvstore.NewClient(context.TODO(), kvstore.EtcdBackendName,
map[string]string{
kvstore.EtcdOptionConfig: rc.configPath,
}, &extraOpts)
// Block until either an error is returned or
// the channel is closed due to success of the
// connection
rc.getLogger().Debugf("Waiting for connection to be established")
err, isErr := <-errChan
if isErr {
if backend != nil {
backend.Close()
}
rc.getLogger().WithError(err).Warning("Unable to establish etcd connection to remote cluster")
return err
}
rc.getLogger().Info("Connection to remote cluster established")
remoteNodes, err := store.JoinSharedStore(store.Configuration{
Prefix: path.Join(nodeStore.NodeStorePrefix, rc.name),
KeyCreator: rc.mesh.conf.NodeKeyCreator,
SynchronizationInterval: time.Minute,
Backend: backend,
Observer: rc.mesh.conf.NodeObserver(),
})
if err != nil {
backend.Close()
return err
}
remoteServices, err := store.JoinSharedStore(store.Configuration{
Prefix: path.Join(serviceStore.ServiceStorePrefix, rc.name),
KeyCreator: func() store.Key {
svc := serviceStore.ClusterService{}
return &svc
},
SynchronizationInterval: time.Minute,
Backend: backend,
Observer: &remoteServiceObserver{
remoteCluster: rc,
swg: rc.swg,
},
})
if err != nil {
remoteNodes.Close(context.TODO())
backend.Close()
return err
}
rc.swg.Stop()
remoteIdentityCache, err := allocator.WatchRemoteIdentities(backend)
if err != nil {
remoteServices.Close(context.TODO())
remoteNodes.Close(context.TODO())
backend.Close()
return err
}
ipCacheWatcher := ipcache.NewIPIdentityWatcher(rc.mesh.ipcache, backend)
go ipCacheWatcher.Watch(ctx)
rc.mutex.Lock()
rc.remoteNodes = remoteNodes
rc.remoteServices = remoteServices
rc.backend = backend
rc.ipCacheWatcher = ipCacheWatcher
rc.remoteIdentityCache = remoteIdentityCache
rc.metricTotalNodes.WithLabelValues(rc.mesh.conf.Name, rc.mesh.conf.NodeName, rc.name).Set(float64(rc.remoteNodes.NumEntries()))
rc.metricReadinessStatus.WithLabelValues(rc.mesh.conf.Name, rc.mesh.conf.NodeName, rc.name).Set(metrics.BoolToFloat64(rc.isReadyLocked()))
rc.mutex.Unlock()
rc.getLogger().Info("Established connection to remote etcd")
return nil
},
StopFunc: func(ctx context.Context) error {
rc.releaseOldConnection()
rc.metricTotalNodes.WithLabelValues(rc.mesh.conf.Name, rc.mesh.conf.NodeName, rc.name).Set(float64(rc.remoteNodes.NumEntries()))
rc.metricReadinessStatus.WithLabelValues(rc.mesh.conf.Name, rc.mesh.conf.NodeName, rc.name).Set(metrics.BoolToFloat64(rc.isReadyLocked()))
rc.getLogger().Info("All resources of remote cluster cleaned up")
return nil
},
},
)
}
func (rc *remoteCluster) onInsert(allocator RemoteIdentityWatcher) {
rc.getLogger().Info("New remote cluster configuration")
if skipKvstoreConnection {
return
}
rc.remoteConnectionControllerName = fmt.Sprintf("remote-etcd-%s", rc.name)
rc.restartRemoteConnection(allocator)
go func() {
for {
val := <-rc.changed
if val {
rc.getLogger().Info("etcd configuration has changed, re-creating connection")
rc.restartRemoteConnection(allocator)
} else {
rc.getLogger().Info("Closing connection to remote etcd")
return
}
}
}()
go func() {
for {
select {
// terminate routine when remote cluster is removed
case _, ok := <-rc.changed:
if !ok {
return
}
default:
}
// wait for backend to appear
rc.mutex.RLock()
if rc.backend == nil {
rc.mutex.RUnlock()
time.Sleep(10 * time.Millisecond)
continue
}
statusCheckErrors := rc.backend.StatusCheckErrors()
rc.mutex.RUnlock()
err, ok := <-statusCheckErrors
if ok && err != nil {
rc.getLogger().WithError(err).Warning("Error observed on etcd connection, reconnecting etcd")
rc.mutex.Lock()
rc.failures++
rc.lastFailure = time.Now()
rc.metricLastFailureTimestamp.WithLabelValues(rc.mesh.conf.Name, rc.mesh.conf.NodeName, rc.name).SetToCurrentTime()
rc.metricTotalFailures.WithLabelValues(rc.mesh.conf.Name, rc.mesh.conf.NodeName, rc.name).Set(float64(rc.failures))
rc.metricTotalNodes.WithLabelValues(rc.mesh.conf.Name, rc.mesh.conf.NodeName, rc.name).Set(float64(rc.remoteNodes.NumEntries()))
rc.metricReadinessStatus.WithLabelValues(rc.mesh.conf.Name, rc.mesh.conf.NodeName, rc.name).Set(metrics.BoolToFloat64(rc.isReadyLocked()))
rc.mutex.Unlock()
rc.restartRemoteConnection(allocator)
}
}
}()
}
func (rc *remoteCluster) onRemove() {
rc.controllers.RemoveAllAndWait()
close(rc.changed)
metrics.Unregister(rc.metricLastFailureTimestamp)
metrics.Unregister(rc.metricReadinessStatus)
metrics.Unregister(rc.metricTotalFailures)
metrics.Unregister(rc.metricTotalNodes)
rc.getLogger().Info("Remote cluster disconnected")
}
func (rc *remoteCluster) isReady() bool {
rc.mutex.RLock()
defer rc.mutex.RUnlock()
return rc.isReadyLocked()
}
func (rc *remoteCluster) isReadyLocked() bool {
return rc.backend != nil && rc.remoteNodes != nil && rc.ipCacheWatcher != nil
}
func (rc *remoteCluster) status() *models.RemoteCluster {
rc.mutex.RLock()
defer rc.mutex.RUnlock()
// This can happen when the controller in restartRemoteConnection is waiting
// for the first connection to succeed.
var backendStatus = "Waiting for initial connection to be established"
if rc.backend != nil {
var backendError error
backendStatus, backendError = rc.backend.Status()
if backendError != nil {
backendStatus = backendError.Error()
}
}
return &models.RemoteCluster{
Name: rc.name,
Ready: rc.isReadyLocked(),
NumNodes: int64(rc.remoteNodes.NumEntries()),
NumSharedServices: int64(rc.remoteServices.NumEntries()),
NumIdentities: int64(rc.remoteIdentityCache.NumEntries()),
Status: backendStatus,
NumFailures: int64(rc.failures),
LastFailure: strfmt.DateTime(rc.lastFailure),
}
}