/
dellcsireplicationgroup_controller.go
480 lines (423 loc) · 17.9 KB
/
dellcsireplicationgroup_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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
Copyright © 2021-2023 Dell Inc. or its subsidiaries. All Rights Reserved.
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 replicationcontroller
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
csireplicator "github.com/dell/csm-replication/controllers/csi-replicator"
"github.com/dell/csm-replication/pkg/common"
repv1 "github.com/dell/csm-replication/api/v1"
controller "github.com/dell/csm-replication/controllers"
"github.com/dell/csm-replication/pkg/connection"
s1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
reconcile "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/ratelimiter"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
eventTypeNormal = "Normal"
eventTypeWarning = "Warning"
eventReasonUpdated = "Updated"
)
// ReplicationGroupReconciler reconciles a ReplicationGroup object
type ReplicationGroupReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
EventRecorder record.EventRecorder
PVCRequeueInterval time.Duration
Config connection.MultiClusterClient
Domain string
}
// +kubebuilder:rbac:groups=replication.storage.dell.com,resources=dellcsireplicationgroups,verbs=get;list;watch;update;patch;delete;create
// +kubebuilder:rbac:groups=replication.storage.dell.com,resources=dellcsireplicationgroups/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=core,resources=events,verbs=list;watch;create;update;patch
// Reconcile contains reconciliation logic that updates ReplicationGroup depending on it's current state
func (r *ReplicationGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("dellcsireplicationgroup", req.Name)
ctx = context.WithValue(ctx, common.LoggerContextKey, log)
localRG := new(repv1.DellCSIReplicationGroup)
err := r.Get(ctx, req.NamespacedName, localRG)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
log.V(common.InfoLevel).Info("Reconciling RG event!!!")
localRGName := req.Name
remoteRGName := localRG.Annotations[controller.RemoteReplicationGroup]
if remoteRGName == "" {
remoteRGName = localRGName
}
rgSyncComplete := false
if localRG.Annotations == nil {
log.V(common.InfoLevel).Info("RG is not ready yet, requeue as we will get another event")
return ctrl.Result{}, nil
} else if localRG.Annotations[controller.RGSyncComplete] == "yes" {
log.V(common.DebugLevel).Info("RG Sync already completed")
remoteRGName = localRG.Annotations[controller.RemoteReplicationGroup]
rgSyncComplete = true
// Continue as we can re verify
}
localClusterID := r.Config.GetClusterID()
remoteClusterID := localRG.Spec.RemoteClusterID
if remoteClusterID == controller.Self {
localClusterID = controller.Self
if !strings.HasPrefix(localRGName, replicated) {
remoteRGName = replicated + "-" + localRGName
}
}
annotations := make(map[string]string)
annotations[controller.RemoteReplicationGroup] = localRGName
annotations[controller.RemoteRGRetentionPolicy] = localRG.Annotations[controller.RemoteRGRetentionPolicy]
annotations[controller.RemoteClusterID] = localClusterID
labels := make(map[string]string)
labels[controller.DriverName] = localRG.Labels[controller.DriverName]
labels[controller.RemoteClusterID] = localClusterID
// Apply driver specific labels
remoteRGAttributes := localRG.Spec.RemoteProtectionGroupAttributes
contextPrefix := localRG.Annotations[controller.ContextPrefix]
if contextPrefix != "" {
for k, v := range remoteRGAttributes {
if strings.HasPrefix(k, contextPrefix) {
labelKey := fmt.Sprintf("%s%s", r.Domain, strings.TrimPrefix(k, contextPrefix))
labels[labelKey] = v
}
}
}
remoteRG := &repv1.DellCSIReplicationGroup{
ObjectMeta: metav1.ObjectMeta{
Name: remoteRGName,
Annotations: annotations,
Labels: labels,
},
Spec: repv1.DellCSIReplicationGroupSpec{
DriverName: localRG.Spec.DriverName,
Action: "",
RemoteClusterID: localClusterID,
ProtectionGroupID: localRG.Spec.RemoteProtectionGroupID,
ProtectionGroupAttributes: localRG.Spec.RemoteProtectionGroupAttributes,
RemoteProtectionGroupID: localRG.Spec.ProtectionGroupID,
RemoteProtectionGroupAttributes: localRG.Spec.ProtectionGroupAttributes,
},
}
// Try to get the client
remoteClient, err := r.Config.GetConnection(remoteClusterID)
if err != nil {
return ctrl.Result{}, err
}
// Check for RG retention policy annotation
retentionPolicy, ok := localRG.Annotations[controller.RemoteRGRetentionPolicy]
if !ok {
log.Info(fmt.Sprintf("RetentionPolicy:found:%v,value-->%s", ok, retentionPolicy))
log.Info("Retention policy not set, using retain as the default policy")
retentionPolicy = controller.RemoteRetentionValueRetain // we will default to retain the RG if there is no retention policy is set
}
// Handle RG deletion if timestamp is set
if !localRG.DeletionTimestamp.IsZero() {
// Process deletion of remote RG
log.V(common.InfoLevel).Info("Deletion timestamp is not zero")
log.V(common.InfoLevel).WithValues(localRG.Annotations).Info("Annotations")
_, ok := localRG.Annotations[controller.DeletionRequested]
log.V(common.InfoLevel).WithValues(ok).Info("Deletion requested?", ok)
if _, ok := localRG.Annotations[controller.DeletionRequested]; !ok {
log.V(common.InfoLevel).Info("Deletion requested annotation not found")
remoteRG, err := remoteClient.GetReplicationGroup(ctx, localRG.Annotations[controller.RemoteReplicationGroup])
if err != nil {
log.V(common.ErrorLevel).WithValues(err.Error()).Info("error getting replication group")
// If remote RG doesn't exist, proceed to removing finalizer
if !errors.IsNotFound(err) {
log.Error(err, "Failed to get remote replication group")
return ctrl.Result{}, err
}
} else {
log.V(common.InfoLevel).Info("Got remote RG")
if strings.ToLower(retentionPolicy) == controller.RemoteRetentionValueDelete {
log.Info("Retention policy is set to Delete")
if _, ok := remoteRG.Annotations[controller.DeletionRequested]; !ok {
// Add annotation on the remote RG to request its deletion
remoteRGCopy := remoteRG.DeepCopy()
controller.AddAnnotation(remoteRGCopy, controller.DeletionRequested, "yes")
err := remoteClient.UpdateReplicationGroup(ctx, remoteRGCopy)
if err != nil {
return ctrl.Result{}, err
}
// Resetting the rate-limiter to requeue for the deletion of remote RG
return ctrl.Result{RequeueAfter: 1 * time.Millisecond}, nil
}
// Requeueing because the remote PV still exists
return ctrl.Result{Requeue: true}, nil
}
}
}
log.V(common.InfoLevel).Info("Removing finalizer RGFinalizer")
finalizerRemoved := controller.RemoveFinalizerIfExists(localRG, controller.RGFinalizer)
if finalizerRemoved {
log.V(common.InfoLevel).Info("Updating rg copy to remove finalizer")
return ctrl.Result{}, r.Update(ctx, localRG)
}
}
rgCopy := localRG.DeepCopy()
log.V(common.InfoLevel).Info("Adding finalizer RGFinalizer")
// Check for the finalizer; add, if doesn't exist
if finalizerAdded := controller.AddFinalizerIfNotExist(rgCopy, controller.RGFinalizer); finalizerAdded {
log.V(common.InfoLevel).Info("Finalizer not found adding it")
return ctrl.Result{}, r.Update(ctx, rgCopy)
}
log.V(common.InfoLevel).Info("Trying to delete RG if deletion request annotation found")
// Check for deletion request annotation
if _, ok := rgCopy.Annotations[controller.DeletionRequested]; ok {
log.V(common.InfoLevel).Info("Deletion Requested annotation found and deleting the remote RG")
return ctrl.Result{}, r.Delete(ctx, rgCopy)
}
createRG := false
// If the RG already exists on the Remote Cluster,
// We treat this as idempotent.
log.V(common.InfoLevel).Info(fmt.Sprintf("Checking if remote RG with the name %s exists on ClusterId: %s",
remoteRGName, remoteClusterID))
rgObj, err := remoteClient.GetReplicationGroup(ctx, remoteRGName)
if err != nil && !errors.IsNotFound(err) {
log.Error(err, "failed to get RG details on the remote cluster")
return ctrl.Result{Requeue: true}, err
} else if errors.IsNotFound(err) {
if rgSyncComplete {
log.Error(err, "Something went wrong. Local RG has already been synced to the remote cluster")
// If the RG has been successfully synced to the remote cluster once
// and now it's not found,
// Let's not recreate the RGs in this case.
log.V(common.InfoLevel).Info("RG not found on target cluster. " +
"Since the local RG carries a SyncComplete annotation, " +
"we will not be creating RG on remote once again.")
return ctrl.Result{}, nil
}
// This is a special case. Controller tries to endlessly create
// replicated RGs in single cluster scenario.
// This check prevents controller from doing that.
if strings.Contains(remoteRGName, "replicated-replicated") {
createRG = false
} else {
createRG = true
}
} else {
// We got the object
log.V(common.InfoLevel).Info(" The RG already exists on the remote cluster")
// First verify the source cluster for this RG
if rgObj.Spec.RemoteClusterID == localClusterID {
// Confirmed that this object was created by this controller
// Check other fields to see if this matches everything from our object
// If fields don't match, then it could mean that this is a leftover object or someone edited it
// Verify driver name
if rgObj.Spec.DriverName != remoteRG.Spec.DriverName {
// Lets create a new object
remoteRGName = fmt.Sprintf("SourceClusterId-%s-%s", localClusterID, localRGName)
remoteRG.Name = remoteRGName
createRG = true
rgSyncComplete = false
} else {
if rgObj.Spec.ProtectionGroupID != remoteRG.Spec.ProtectionGroupID ||
rgObj.Spec.RemoteProtectionGroupID != remoteRG.Spec.RemoteProtectionGroupID {
// Don't know how to proceed here
// Lets raise an event and stop reconciling
r.EventRecorder.Eventf(localRG, eventTypeWarning, eventReasonUpdated,
"Found conflicting RG on remote ClusterId: %s", remoteClusterID)
log.Error(fmt.Errorf("conflicting RG with name: %s exists on ClusterId: %s",
localRGName, remoteClusterID), "stopping reconcile")
return ctrl.Result{}, nil
}
}
} else {
// update the name of the RG and create it
remoteRGName = fmt.Sprintf("SourceClusterId-%s-%s", localClusterID, localRGName)
remoteRG.Name = remoteRGName
createRG = true
rgSyncComplete = false
}
}
if createRG {
err = remoteClient.CreateReplicationGroup(ctx, remoteRG)
if err != nil {
log.Error(err, "failed to create remote CR for DellCSIReplicationGroup")
r.EventRecorder.Eventf(localRG, eventTypeWarning, eventReasonUpdated,
"Failed to create remote CR for DellCSIReplicationGroup on ClusterId: %s", remoteClusterID)
return ctrl.Result{}, err
}
log.V(common.InfoLevel).Info("The remote RG has been successfully created!!")
r.EventRecorder.Eventf(localRG, eventTypeNormal, eventReasonUpdated,
"Created remote ReplicationGroup with name: %s on cluster: %s", remoteRGName, remoteClusterID)
}
// Update the RemoteReplicationGroup annotation on the local RG if required
if !rgSyncComplete {
if strings.Contains(localRGName, replicated) {
remoteRGName = strings.TrimPrefix(localRGName, "replicated-")
}
controller.AddAnnotation(localRG, controller.RemoteReplicationGroup, remoteRGName)
controller.AddAnnotation(localRG, controller.RGSyncComplete, "yes")
err = r.Update(ctx, localRG)
return ctrl.Result{}, err
}
err = r.processLastActionResult(ctx, localRG, remoteClient, log)
if err != nil {
r.EventRecorder.Eventf(localRG, eventTypeWarning, eventReasonUpdated,
"failed to process the last action %s", localRG.Status.LastAction.Condition)
}
log.V(common.InfoLevel).Info("RG has already been synced to the remote cluster")
return ctrl.Result{}, nil
}
func (r *ReplicationGroupReconciler) processLastActionResult(ctx context.Context, group *repv1.DellCSIReplicationGroup, remoteClient connection.RemoteClusterClient, log logr.Logger) error {
if len(group.Status.Conditions) == 0 || group.Status.LastAction.Time == nil {
log.V(common.InfoLevel).Info("No action to process")
return nil
}
if group.Status.LastAction.ErrorMessage != "" {
return fmt.Errorf("last action failed: %s", group.Status.LastAction.Condition)
}
val, ok := group.Annotations[controller.ActionProcessedTime]
if !ok {
log.V(common.InfoLevel).Info("Action Processed does not exist.")
return nil
}
if val == group.Status.LastAction.Time.GoString() {
log.V(common.InfoLevel).Info("Last action has already been processed")
return nil
}
if strings.Contains(group.Status.LastAction.Condition, "CREATE_SNAPSHOT") {
if err := r.processSnapshotEvent(ctx, group, remoteClient, log); err != nil {
return err
}
}
// Informing the RG that the last action has been processed.
controller.AddAnnotation(group, controller.ActionProcessedTime, group.Status.LastAction.Time.GoString())
return r.Update(ctx, group)
}
func (r *ReplicationGroupReconciler) processSnapshotEvent(ctx context.Context, group *repv1.DellCSIReplicationGroup, remoteClient connection.RemoteClusterClient, log logr.Logger) error {
lastAction := group.Status.LastAction
val, ok := group.Annotations[csireplicator.Action]
if !ok {
log.V(common.InfoLevel).Info("No action", "val", val)
return nil
}
var actionAnnotation csireplicator.ActionAnnotation
err := json.Unmarshal([]byte(val), &actionAnnotation)
if err != nil {
log.Error(err, "JSON unmarshal error", "actionAnnotation", actionAnnotation)
return err
}
if _, err := remoteClient.GetSnapshotClass(ctx, actionAnnotation.SnapshotClass); err != nil {
log.Error(err, "Snapshot class does not exist on remote cluster. Not creating the remote snapshots.")
return err
}
if _, err := remoteClient.GetNamespace(ctx, actionAnnotation.SnapshotNamespace); err != nil {
log.V(common.InfoLevel).Info("Namespace - " + actionAnnotation.SnapshotNamespace + " not found, creating it.")
nsRef := makeNamespaceReference(actionAnnotation.SnapshotNamespace)
err = remoteClient.CreateNamespace(ctx, nsRef)
if err != nil {
msg := "unable to create the desired namespace" + actionAnnotation.SnapshotNamespace
log.V(common.ErrorLevel).Error(err, msg)
return err
}
}
for volumeHandle, snapshotHandle := range lastAction.ActionAttributes {
msg := "ActionAttributes - volumeHandle: " + volumeHandle + ", snapshotHandle: " + snapshotHandle
log.V(common.InfoLevel).Info(msg)
snapRef := makeSnapReference(snapshotHandle, actionAnnotation.SnapshotNamespace)
sc := makeStorageClassContent(group.Labels[controller.DriverName], actionAnnotation.SnapshotClass)
snapContent := makeVolSnapContent(snapshotHandle, volumeHandle, *snapRef, sc)
err = remoteClient.CreateSnapshotContent(ctx, snapContent)
if err != nil {
log.Error(err, "unable to create snapshot content")
return err
}
snapshot := makeSnapshotObject(snapRef.Name, snapContent.Name, sc.ObjectMeta.Name, actionAnnotation.SnapshotNamespace)
err = remoteClient.CreateSnapshotObject(ctx, snapshot)
if err != nil {
log.Error(err, "unable to create snapshot object")
return err
}
}
return nil
}
func makeNamespaceReference(namespace string) *v1.Namespace {
return &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}
}
func makeSnapReference(snapName, namespace string) *v1.ObjectReference {
return &v1.ObjectReference{
Kind: "VolumeSnapshot",
APIVersion: "snapshot.storage.k8s.io/v1",
Name: "snapshot-" + snapName,
Namespace: namespace,
}
}
func makeSnapshotObject(snapName, contentName, className, namespace string) *s1.VolumeSnapshot {
volsnap := &s1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: snapName,
Namespace: namespace,
},
Spec: s1.VolumeSnapshotSpec{
Source: s1.VolumeSnapshotSource{
VolumeSnapshotContentName: &contentName,
},
VolumeSnapshotClassName: &className,
},
}
return volsnap
}
func makeStorageClassContent(driver, snapClass string) *s1.VolumeSnapshotClass {
return &s1.VolumeSnapshotClass{
Driver: driver,
DeletionPolicy: "Retain",
ObjectMeta: metav1.ObjectMeta{
Name: snapClass,
},
}
}
func makeVolSnapContent(snapName, volumeName string, snapRef v1.ObjectReference, sc *s1.VolumeSnapshotClass) *s1.VolumeSnapshotContent {
volsnapcontent := &s1.VolumeSnapshotContent{
ObjectMeta: metav1.ObjectMeta{
Name: "volume-" + volumeName + "-" + strconv.FormatInt(time.Now().Unix(), 10),
},
Spec: s1.VolumeSnapshotContentSpec{
VolumeSnapshotRef: snapRef,
Source: s1.VolumeSnapshotContentSource{
SnapshotHandle: &snapName,
},
VolumeSnapshotClassName: &sc.Name,
DeletionPolicy: sc.DeletionPolicy,
Driver: sc.Driver,
},
}
return volsnapcontent
}
// SetupWithManager start using reconciler by creating new controller managed by provided manager
func (r *ReplicationGroupReconciler) SetupWithManager(mgr ctrl.Manager, limiter ratelimiter.RateLimiter, maxReconcilers int) error {
return ctrl.NewControllerManagedBy(mgr).
For(&repv1.DellCSIReplicationGroup{}).
WithOptions(reconcile.Options{
RateLimiter: limiter,
MaxConcurrentReconciles: maxReconcilers,
}).
Complete(r)
}