-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
remote_cluster.go
172 lines (136 loc) · 4.76 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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package clustermesh
import (
"context"
"path"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/allocator"
"github.com/cilium/cilium/pkg/clustermesh/internal"
"github.com/cilium/cilium/pkg/clustermesh/types"
cmtypes "github.com/cilium/cilium/pkg/clustermesh/types"
identityCache "github.com/cilium/cilium/pkg/identity/cache"
"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"
nodeStore "github.com/cilium/cilium/pkg/node/store"
serviceStore "github.com/cilium/cilium/pkg/service/store"
)
// remoteCluster implements the clustermesh business logic on top of
// internal.RemoteCluster.
type remoteCluster struct {
// name is the name of the cluster
name string
// clusterConfig is a configuration of the remote cluster taken
// from remote kvstore.
config *cmtypes.CiliumClusterConfig
// mesh is the cluster mesh this remote cluster belongs to
mesh *ClusterMesh
usedIDs *ClusterMeshUsedIDs
// mutex protects the following variables:
// - remoteIdentityCache
mutex lock.RWMutex
// store is the shared store representing all nodes in the remote cluster
remoteNodes store.WatchStore
// remoteServices is the shared store representing services in remote
// clusters
remoteServices store.WatchStore
// 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
// status is the function which fills the internal part of the status.
status internal.StatusFunc
swg *lock.StoppableWaitGroup
}
func (rc *remoteCluster) Run(ctx context.Context, backend kvstore.BackendOperations, config *cmtypes.CiliumClusterConfig, ready chan<- error) {
if err := config.Validate(); err != nil {
ready <- err
close(ready)
return
}
if err := rc.onUpdateConfig(config); err != nil {
ready <- err
close(ready)
return
}
var capabilities types.CiliumClusterConfigCapabilities
if config != nil {
capabilities = config.Capabilities
}
remoteIdentityCache, err := rc.mesh.conf.RemoteIdentityWatcher.WatchRemoteIdentities(rc.name, backend, capabilities.Cached)
if err != nil {
ready <- err
close(ready)
return
}
rc.mutex.Lock()
rc.remoteIdentityCache = remoteIdentityCache
rc.mutex.Unlock()
var mgr store.WatchStoreManager
if capabilities.SyncedCanaries {
mgr = store.NewWatchStoreManagerSync(backend, rc.name)
} else {
mgr = store.NewWatchStoreManagerImmediate(rc.name)
}
adapter := func(prefix string) string { return prefix }
if capabilities.Cached {
adapter = kvstore.StateToCachePrefix
}
mgr.Register(adapter(nodeStore.NodeStorePrefix), func(ctx context.Context) {
rc.remoteNodes.Watch(ctx, backend, path.Join(adapter(nodeStore.NodeStorePrefix), rc.name))
})
mgr.Register(adapter(serviceStore.ServiceStorePrefix), func(ctx context.Context) {
rc.remoteServices.Watch(ctx, backend, path.Join(adapter(serviceStore.ServiceStorePrefix), rc.name))
})
mgr.Register(adapter(ipcache.IPIdentitiesPath), func(ctx context.Context) {
rc.ipCacheWatcher.Watch(ctx, backend, ipcache.WithCachedPrefix(capabilities.Cached))
})
mgr.Register(adapter(identityCache.IdentitiesPath), func(ctx context.Context) {
rc.remoteIdentityCache.Watch(ctx)
})
close(ready)
mgr.Run(ctx)
}
func (rc *remoteCluster) Stop() {}
func (rc *remoteCluster) Remove() {
// Draining shall occur only when the configuration for the remote cluster
// is removed, and not in case the agent is shutting down, otherwise we
// would break existing connections on restart.
rc.remoteNodes.Drain()
rc.remoteServices.Drain()
rc.ipCacheWatcher.Drain()
rc.mesh.conf.RemoteIdentityWatcher.RemoveRemoteIdentities(rc.name)
rc.mesh.globalServices.onClusterDelete(rc.name)
if rc.config != nil {
rc.usedIDs.releaseClusterID(rc.config.ID)
}
}
func (rc *remoteCluster) Status() *models.RemoteCluster {
status := rc.status()
rc.mutex.RLock()
defer rc.mutex.RUnlock()
status.NumNodes = int64(rc.remoteNodes.NumEntries())
status.NumSharedServices = int64(rc.remoteServices.NumEntries())
status.NumIdentities = int64(rc.remoteIdentityCache.NumEntries())
return status
}
func (rc *remoteCluster) onUpdateConfig(newConfig *cmtypes.CiliumClusterConfig) error {
oldConfig := rc.config
if newConfig != nil && oldConfig != nil && newConfig.ID == oldConfig.ID {
return nil
}
if newConfig != nil {
if err := rc.usedIDs.reserveClusterID(newConfig.ID); err != nil {
return err
}
}
if oldConfig != nil {
rc.usedIDs.releaseClusterID(oldConfig.ID)
}
rc.config = newConfig
return nil
}