-
Notifications
You must be signed in to change notification settings - Fork 346
/
remote_common_area.go
427 lines (353 loc) · 13.4 KB
/
remote_common_area.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
Copyright 2021 Antrea 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 commonarea
import (
"context"
"fmt"
"sync"
"time"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
multiclusterv1alpha1 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha1"
"antrea.io/antrea/multicluster/controllers/multicluster/common"
)
const (
TimestampAnnotationKey = "touch-ts"
)
var (
ReasonDisconnected = "Disconnected"
)
// CommonArea is an interface that provides access to the Common Area of a ClusterSet.
// Common Area of a ClusterSet is a Namespace in the leader cluster.
type CommonArea interface {
// Client grants read/write to the Namespace of the cluster that is backing this CommonArea.
client.Client
// GetClusterID returns the clusterID of the leader cluster.
GetClusterID() common.ClusterID
// GetNamespace returns the Namespace backing this CommonArea.
GetNamespace() string
}
// RemoteCommonArea is an abstraction to connect to CommonArea of the leader cluster.
type RemoteCommonArea interface {
CommonArea
Start() context.CancelFunc
Stop()
// IsConnected returns whether the RemoteCommonArea is accessible or not.
IsConnected() bool
// StartWatching sets up a Manager to reconcile resource crud operations from CommonArea of RemoteCommonArea.
StartWatching() error
// StopWatching stops the Manager so the crud operations in RemoteCommonArea no longer invoke the reconcilers.
StopWatching()
GetStatus() []multiclusterv1alpha1.ClusterCondition
GetLocalClusterID() string
}
// remoteCommonArea implements the CommonArea interface and allows local cluster to read/write into
// the CommonArea of RemoteCommonArea.
type remoteCommonArea struct {
// mutex to synchronize access to connectivity state since it is updated by
// a background routine running in remoteCommonArea.
mutex sync.RWMutex
// client that provides read/write access into the remoteCommonArea.
client.Client
// ClusterManager to set up controllers for resources that need to be monitored in the remoteCommonArea.
ClusterManager manager.Manager
// ClusterID of this remoteCommonArea.
ClusterID common.ClusterID
// ClusterSetID of this remoteCommonArea.
ClusterSetID common.ClusterSetID
// config necessary to access the remoteCommonArea.
config *rest.Config
// scheme necessary to access the remoteCommonArea.
scheme *runtime.Scheme
// Namespace this ClusterSet is associated with.
Namespace string
// connected is a state to know whether the remoteCommonArea is connected or not.
connected bool
clusterStatus multiclusterv1alpha1.ClusterCondition
leaderStatus multiclusterv1alpha1.ClusterCondition
// The ID of the local member cluster
localClusterID common.ClusterSetID
// client that provides read/write access into the local cluster
localClusterClient client.Client
// localNamespace is the Namespace where the controller is running.
localNamespace string
// stopFunc to stop all background operations when the RemoteCommonArea is stopped.
stopFunc context.CancelFunc
// managerStopFunc to stop the manager when the RemoteCommonArea is stopped.
managerStopFunc context.CancelFunc
}
// NewRemoteCommonArea returns a RemoteCommonArea instance which will use access credentials from the Secret to
// connect to the leader cluster's CommonArea.
func NewRemoteCommonArea(clusterID common.ClusterID, clusterSetID common.ClusterSetID, localClusterID common.ClusterSetID, url string, secret *v1.Secret,
scheme *runtime.Scheme, localClusterClient client.Client, clusterSetNamespace string, localNamespace string) (RemoteCommonArea, error) {
klog.InfoS("Create a RemoteCommonArea", "cluster", clusterID)
crtData, token, err := GetSecretCACrtAndToken(secret)
if err != nil {
return nil, err
}
// Create manager for the RemoteCommonArea
klog.InfoS("Connecting to RemoteCommonArea", "cluster", clusterID, "url", url)
config, err := clientcmd.BuildConfigFromFlags(url, "")
if err != nil {
return nil, err
}
config.BearerToken = string(token)
config.CAData = crtData
mgr, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: "0",
Namespace: clusterSetNamespace,
})
if err != nil {
klog.ErrorS(err, "Error creating manager for RemoteCommonArea", "cluster", clusterID)
return nil, err
}
remoteClient, e := client.New(config, client.Options{Scheme: scheme})
if e != nil {
return nil, e
}
remote := &remoteCommonArea{
Client: remoteClient,
ClusterManager: mgr,
ClusterSetID: clusterSetID,
ClusterID: clusterID,
config: config,
scheme: scheme,
Namespace: clusterSetNamespace,
connected: false,
localClusterClient: localClusterClient,
localNamespace: localNamespace,
localClusterID: localClusterID,
}
remote.clusterStatus.Type = multiclusterv1alpha1.ClusterReady
remote.clusterStatus.Status = v1.ConditionUnknown
remote.clusterStatus.Message = "Leader cluster added"
remote.clusterStatus.LastTransitionTime = metav1.Now()
remote.leaderStatus.Type = multiclusterv1alpha1.ClusterIsLeader
remote.leaderStatus.Status = v1.ConditionFalse
remote.leaderStatus.Message = "Leader cluster added"
remote.leaderStatus.LastTransitionTime = metav1.Now()
return remote, nil
}
/**
* GetSecretCACrtAndToken returns the access credentials from Secret.
*/
func GetSecretCACrtAndToken(secretObj *v1.Secret) ([]byte, []byte, error) {
caData, found := secretObj.Data[v1.ServiceAccountRootCAKey]
if !found {
return nil, nil, fmt.Errorf("ca.crt data not found in Secret %v", secretObj.GetName())
}
token, found := secretObj.Data[v1.ServiceAccountTokenKey]
if !found {
return nil, nil, fmt.Errorf("token not found in Secret %v", secretObj.GetName())
}
return caData, token, nil
}
func (r *remoteCommonArea) SendMemberAnnounce() error {
var err error
memberAnnounceName := "member-announce-from-" + r.GetLocalClusterID()
existingMemberAnnounce := &multiclusterv1alpha1.MemberClusterAnnounce{}
if err = r.Get(context.TODO(), types.NamespacedName{
Namespace: r.GetNamespace(),
Name: memberAnnounceName,
}, existingMemberAnnounce); err != nil && !apierrors.IsNotFound(err) {
return err
}
localClusterMemberAnnounceExists := err == nil
localClusterMemberAnnounce := *existingMemberAnnounce
if localClusterMemberAnnounceExists {
r.updateLeaderStatus()
if localClusterMemberAnnounce.Annotations == nil {
localClusterMemberAnnounce.Annotations = make(map[string]string)
}
// Add timestamp to force update on MemberClusterAnnounce. Leader cluster requires
// periodic updates to detect connectivity. Without this, no-op updates will be ignored.
localClusterMemberAnnounce.Annotations[TimestampAnnotationKey] = time.Now().Format(time.RFC3339)
if err := r.Update(context.TODO(), &localClusterMemberAnnounce, &client.UpdateOptions{}); err != nil {
klog.ErrorS(err, "Error updating MemberClusterAnnounce", "cluster", r.GetClusterID())
return err
}
return nil
}
// Create happens first before the leader validation passes. When the creation is successful,
// it marks the connectivity status and then the validation on the leader can happen.
localClusterMemberAnnounce.ClusterID = r.GetLocalClusterID()
localClusterMemberAnnounce.Name = memberAnnounceName
localClusterMemberAnnounce.Namespace = r.Namespace
localClusterMemberAnnounce.ClusterSetID = string(r.ClusterSetID)
localClusterMemberAnnounce.LeaderClusterID = string(r.GetClusterID())
if err := r.Create(context.TODO(), &localClusterMemberAnnounce, &client.CreateOptions{}); err != nil {
klog.ErrorS(err, "Error creating MemberClusterAnnounce", "cluster", r.GetClusterID())
return err
}
return nil
}
func (r *remoteCommonArea) updateRemoteCommonAreaStatus(connected bool, err error) {
defer r.mutex.Unlock()
r.mutex.Lock()
if r.connected == connected {
return
}
klog.InfoS("Updating RemoteCommonArea status", "cluster", r.GetClusterID(), "connected", connected)
// TODO: Tolerate transient failures so we dont oscillate between connected and disconnected.
r.connected = connected
r.clusterStatus.Status = v1.ConditionTrue
r.clusterStatus.Message = ""
r.clusterStatus.Reason = ""
r.clusterStatus.LastTransitionTime = metav1.Now()
if !connected {
r.clusterStatus.Status = v1.ConditionFalse
r.clusterStatus.Message = err.Error()
r.clusterStatus.Reason = ReasonDisconnected
}
}
func (r *remoteCommonArea) updateLeaderStatus() {
defer r.mutex.Unlock()
r.mutex.Lock()
if r.leaderStatus.Status != v1.ConditionTrue {
r.leaderStatus.Status = v1.ConditionTrue
r.leaderStatus.Message = "This leader cluster is the leader for local cluster"
r.leaderStatus.Reason = ""
r.leaderStatus.LastTransitionTime = metav1.Now()
}
}
/**
* ---------------------------
* CommonArea Implementation
* ---------------------------
*/
func (r *remoteCommonArea) GetClusterID() common.ClusterID {
return r.ClusterID
}
func (r *remoteCommonArea) GetNamespace() string {
return r.Namespace
}
/**
* ---------------------------
* RemoteCommonArea Implementation
* ---------------------------
*/
// Start starts a background routine.
// Once connected to the RemoteCommonArea, the Start method runs a timer
// on a go routine to periodically write MemberClusterAnnounce into the
// RemoteCommonArea's CommonArea and also maintain its connectivity status.
func (r *remoteCommonArea) Start() context.CancelFunc {
stopCtx, stopFunc := context.WithCancel(context.Background())
ticker := time.NewTicker(10 * time.Second)
go func() {
klog.InfoS("Starting MemberAnnounce to RemoteCommonArea", "cluster", r.GetClusterID())
r.doMemberAnnounce()
startedImporter := false
for {
select {
case <-stopCtx.Done():
klog.InfoS("Stopping MemberAnnounce to RemoteCommonArea", "cluster", r.GetClusterID())
ticker.Stop()
return
case <-ticker.C:
r.doMemberAnnounce()
if !startedImporter && r.connected {
if err := r.StartWatching(); err != nil {
// Will retry in next tick.
klog.ErrorS(err, "Failed to start watching events")
return
}
startedImporter = true
}
}
}
}()
r.stopFunc = stopFunc
return stopFunc
}
func (r *remoteCommonArea) doMemberAnnounce() {
if err := r.SendMemberAnnounce(); err != nil {
klog.ErrorS(err, "Error updating MemberClusterAnnounce", "cluster", r.GetClusterID())
r.updateRemoteCommonAreaStatus(false, err)
} else {
r.updateRemoteCommonAreaStatus(true, nil)
}
}
func (r *remoteCommonArea) Stop() {
if r.stopFunc == nil {
return
}
r.stopFunc()
r.stopFunc = nil
r.StopWatching()
}
func (r *remoteCommonArea) IsConnected() bool {
defer r.mutex.RUnlock()
r.mutex.RLock()
return r.connected
}
func (r *remoteCommonArea) StartWatching() error {
if r.managerStopFunc != nil {
klog.InfoS("Manager already watching resources from RemoteCommonArea", "cluster", r.ClusterID)
return nil
}
klog.V(2).InfoS("Start monitoring ResourceImport from RemoteCommonArea", "cluster", r.ClusterID)
resImportReconciler := NewResourceImportReconciler(
r.ClusterManager.GetClient(),
r.ClusterManager.GetScheme(),
r.localClusterClient,
string(r.GetLocalClusterID()),
r.localNamespace,
r,
)
if err := resImportReconciler.SetupWithManager(r.ClusterManager); err != nil {
klog.V(2).ErrorS(err, "Error creating ResourceImport controller for RemoteCommonArea", "cluster", r.ClusterID)
return fmt.Errorf("error creating ResourceImport controller for RemoteCommonArea: %v", err)
}
go func() {
stopCtx, stopFunc := context.WithCancel(context.Background())
r.managerStopFunc = stopFunc
// This starts the Manager and blocks; Manager performs reconciliation of resources from the RemoteCommonArea.
// When this RemoteCommonArea is not a leader anymore, stopCtx will be closed in StopWatching,
// so this blocking routine can return and finish. And the next time this RemoteCommonArea is connected as
// the leader again, it starts the Manager again.
err := r.ClusterManager.Start(stopCtx)
if err != nil {
klog.ErrorS(err, "Error starting ClusterManager for RemoteCommonArea", "cluster", r.ClusterID)
}
klog.InfoS("Stopping ClusterManager for RemoteCommonArea", "cluster", r.ClusterID)
}()
return nil
}
func (r *remoteCommonArea) StopWatching() {
if r.managerStopFunc == nil {
return
}
r.managerStopFunc()
r.managerStopFunc = nil
}
func (r *remoteCommonArea) GetStatus() []multiclusterv1alpha1.ClusterCondition {
defer r.mutex.Unlock()
r.mutex.Lock()
statues := make([]multiclusterv1alpha1.ClusterCondition, 0, 2)
statues = append(statues, r.clusterStatus) // This will be a copy
statues = append(statues, r.leaderStatus) // This will be a copy
return statues
}
func (r *remoteCommonArea) GetLocalClusterID() string {
return string(r.localClusterID)
}