Skip to content

Commit 38c58d7

Browse files
committed
controllers: Generates VGSClass
Signed-off-by: raaizik <[email protected]>
1 parent 484006e commit 38c58d7

File tree

7 files changed

+932
-0
lines changed

7 files changed

+932
-0
lines changed

config/rbac/role.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ rules:
133133
- get
134134
- list
135135
- watch
136+
- apiGroups:
137+
- groupsnapshot.storage.k8s.io
138+
resources:
139+
- volumegroupsnapshotclasses
140+
verbs:
141+
- create
142+
- delete
143+
- get
144+
- list
145+
- watch
136146
- apiGroups:
137147
- monitoring.coreos.com
138148
resources:

controllers/storageclaim_controller.go

+83
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/red-hat-storage/ocs-client-operator/pkg/utils"
3333

3434
"github.com/go-logr/logr"
35+
groupsnapapi "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumegroupsnapshot/v1alpha1"
3536
snapapi "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
3637
providerclient "github.com/red-hat-storage/ocs-operator/v4/services/provider/client"
3738
corev1 "k8s.io/api/core/v1"
@@ -110,6 +111,7 @@ func (r *StorageClaimReconciler) SetupWithManager(mgr ctrl.Manager) error {
110111
For(&v1alpha1.StorageClaim{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
111112
Owns(&storagev1.StorageClass{}).
112113
Owns(&snapapi.VolumeSnapshotClass{}).
114+
Owns(&groupsnapapi.VolumeGroupSnapshotClass{}).
113115
Complete(r)
114116
}
115117

@@ -119,6 +121,7 @@ func (r *StorageClaimReconciler) SetupWithManager(mgr ctrl.Manager) error {
119121
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;delete
120122
//+kubebuilder:rbac:groups=storage.k8s.io,resources=storageclasses,verbs=get;list;watch;create;update;patch;delete
121123
//+kubebuilder:rbac:groups=snapshot.storage.k8s.io,resources=volumesnapshotclasses,verbs=get;list;watch;create;delete
124+
//+kubebuilder:rbac:groups=groupsnapshot.storage.k8s.io,resources=volumegroupsnapshotclasses,verbs=get;list;watch;create;delete
122125
//+kubebuilder:rbac:groups=core,resources=persistentvolumes,verbs=get;list;watch
123126
//+kubebuilder:rbac:groups=snapshot.storage.k8s.io,resources=volumesnapshotcontents,verbs=get;list;watch
124127

@@ -398,6 +401,8 @@ func (r *StorageClaimReconciler) reconcilePhases() (reconcile.Result, error) {
398401
// storageclaim is clusterscoped resources using its
399402
// hash as the clusterID
400403
data["clusterID"] = r.storageClaimHash
404+
data["ramendr.openshift.io/storageID"] = r.storageClaimHash
405+
data["ramendr.openshift.io/replicationID"] = r.storageClaimHash
401406
if resource.Name == "cephfs" {
402407
volumeSnapshotClass = r.getCephFSVolumeSnapshotClass(data)
403408
} else if resource.Name == "ceph-rbd" {
@@ -407,6 +412,24 @@ func (r *StorageClaimReconciler) reconcilePhases() (reconcile.Result, error) {
407412
if err := r.createOrReplaceVolumeSnapshotClass(volumeSnapshotClass); err != nil {
408413
return reconcile.Result{}, fmt.Errorf("failed to create or update VolumeSnapshotClass: %s", err)
409414
}
415+
case "VolumeGroupSnapshotClass":
416+
var volumeGroupSnapshotClass *groupsnapapi.VolumeGroupSnapshotClass
417+
data["csi.storage.k8s.io/snapshotter-secret-namespace"] = r.OperatorNamespace
418+
// generate a new clusterID for cephfs subvolumegroup, as
419+
// storageclaim is clusterscoped resources using its
420+
// hash as the clusterID
421+
data["clusterID"] = r.storageClaimHash
422+
data["ramendr.openshift.io/storageID"] = r.storageClaimHash
423+
data["ramendr.openshift.io/replicationID"] = r.storageClaimHash
424+
if resource.Name == "cephfs" {
425+
volumeGroupSnapshotClass = r.getCephFSVolumeGroupSnapshotClass(data)
426+
} else if resource.Name == "ceph-rbd" {
427+
volumeGroupSnapshotClass = r.getCephRBDVolumeGroupSnapshotClass(data)
428+
}
429+
utils.AddAnnotation(volumeGroupSnapshotClass, storageClaimAnnotation, r.storageClaim.Name)
430+
if err := r.createOrReplaceVolumeGroupSnapshotClass(volumeGroupSnapshotClass); err != nil {
431+
return reconcile.Result{}, fmt.Errorf("failed to create or update volumeGroupSnapshot: %s", err)
432+
}
410433
}
411434
}
412435

@@ -651,6 +674,66 @@ func (r *StorageClaimReconciler) hasVolumeSnapshotContents() (bool, error) {
651674
return false, nil
652675
}
653676

677+
func (r *StorageClaimReconciler) getCephFSVolumeGroupSnapshotClass(data map[string]string) *groupsnapapi.VolumeGroupSnapshotClass {
678+
volumgroupesnapshotclass := &groupsnapapi.VolumeGroupSnapshotClass{
679+
ObjectMeta: metav1.ObjectMeta{
680+
Name: r.storageClaim.Name,
681+
},
682+
Driver: csi.GetCephFSDriverName(),
683+
DeletionPolicy: snapapi.VolumeSnapshotContentDelete,
684+
Parameters: data,
685+
}
686+
return volumgroupesnapshotclass
687+
}
688+
689+
func (r *StorageClaimReconciler) getCephRBDVolumeGroupSnapshotClass(data map[string]string) *groupsnapapi.VolumeGroupSnapshotClass {
690+
volumegroupsnapshotclass := &groupsnapapi.VolumeGroupSnapshotClass{
691+
ObjectMeta: metav1.ObjectMeta{
692+
Name: r.storageClaim.Name,
693+
},
694+
Driver: csi.GetRBDDriverName(),
695+
DeletionPolicy: snapapi.VolumeSnapshotContentDelete,
696+
Parameters: data,
697+
}
698+
return volumegroupsnapshotclass
699+
}
700+
701+
func (r *StorageClaimReconciler) createOrReplaceVolumeGroupSnapshotClass(volumeGroupSnapshotClass *groupsnapapi.VolumeGroupSnapshotClass) error {
702+
existing := &groupsnapapi.VolumeGroupSnapshotClass{}
703+
existing.Name = r.storageClaim.Name
704+
705+
if err := r.own(volumeGroupSnapshotClass); err != nil {
706+
return fmt.Errorf("failed to own volumegroupsnapshotclass: %v", err)
707+
}
708+
709+
if err := r.get(existing); err != nil && !errors.IsNotFound(err) {
710+
return fmt.Errorf("failed to get VolumeGroupSnapshotClass: %v", err)
711+
}
712+
713+
// If present then compare the existing VolumeGroupSnapshotClass parameters with
714+
// the received VolumeGroupSnapshotClass parameters, and only proceed if they differ.
715+
if reflect.DeepEqual(existing.Parameters, volumeGroupSnapshotClass.Parameters) {
716+
return nil
717+
}
718+
719+
// VolumeGroupSnapshotClass already exists, but parameters have changed. Delete the existing VolumeGroupSnapshotClass and create a new one.
720+
if existing.UID != "" {
721+
// Since we have to update the existing VolumeGroupSnapshotClass, so we will delete the existing VolumeGroupSnapshotClass and create a new one.
722+
r.log.Info("VolumeGroupSnapshotClass needs to be updated, deleting it.", "Name", existing.Name)
723+
724+
// Delete the VolumeGroupSnapshotClass.
725+
if err := r.delete(existing); err != nil {
726+
r.log.Error(err, "Failed to delete VolumeGroupSnapshotClass.", "Name", existing.Name)
727+
return fmt.Errorf("failed to delete VolumeGroupSnapshotClass: %v", err)
728+
}
729+
}
730+
r.log.Info("Creating VolumeGroupSnapshotClass.", "Name", existing.Name)
731+
if err := r.Client.Create(r.ctx, volumeGroupSnapshotClass); err != nil {
732+
return fmt.Errorf("failed to create VolumeGroupSnapshotClass: %v", err)
733+
}
734+
return nil
735+
}
736+
654737
func getMD5Hash(text string) string {
655738
hash := md5.Sum([]byte(text))
656739
return hex.EncodeToString(hash[:])

vendor/github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumegroupsnapshot/v1alpha1/doc.go

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumegroupsnapshot/v1alpha1/register.go

+57
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)