-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2859 from ShravaniVangur/volgrp-snapclass
add VolumeGroupSnapshotClass for CephFS and RBD
- Loading branch information
Showing
20 changed files
with
1,200 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
controllers/storagecluster/volumegroupsnapshotterclasses.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package storagecluster | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
groupsnapapi "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumegroupsnapshot/v1beta1" | ||
snapapi "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1" | ||
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
type groupSnapshotterType string | ||
|
||
type ocsGroupSnapshotClass struct{} | ||
|
||
const ( | ||
rbdGroupSnapshotter groupSnapshotterType = "rbd" | ||
cephfsGroupSnapshotter groupSnapshotterType = "cephfs" | ||
) | ||
|
||
const ( | ||
groupSnapshotterSecretNameKey = "csi.storage.k8s.io/group-snapshotter-secret-name" | ||
groupSnapshotterSecretNamespaceKey = "csi.storage.k8s.io/group-snapshotter-secret-namespace" | ||
) | ||
|
||
type GroupSnapshotClassConfiguration struct { | ||
groupSnapshotClass *groupsnapapi.VolumeGroupSnapshotClass | ||
reconcileStrategy ReconcileStrategy | ||
// The disable field is added to control the disabling of VolumeGroupSnapshotClass when an API or other mechanism for disabling is introduced. | ||
disable bool | ||
} | ||
|
||
func newVolumeGroupSnapshotClass(instance *ocsv1.StorageCluster, groupSnaphotType groupSnapshotterType) *groupsnapapi.VolumeGroupSnapshotClass { | ||
paramKey, paramValue := setParameterBasedOnSnapshotterType(instance, groupSnaphotType) | ||
groupSnapClass := &groupsnapapi.VolumeGroupSnapshotClass{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: generateNameForGroupSnapshotClass(instance, groupSnaphotType), | ||
}, | ||
Driver: generateNameForSnapshotClassDriver(SnapshotterType(groupSnaphotType)), | ||
Parameters: map[string]string{ | ||
"clusterID": instance.Namespace, | ||
paramKey: paramValue, | ||
groupSnapshotterSecretNameKey: generateNameForSnapshotClassSecret(instance, SnapshotterType(groupSnaphotType)), | ||
groupSnapshotterSecretNamespaceKey: instance.Namespace, | ||
}, | ||
DeletionPolicy: snapapi.VolumeSnapshotContentDelete, | ||
} | ||
return groupSnapClass | ||
} | ||
|
||
func newCephFilesystemGroupSnapshotClassConfiguration(instance *ocsv1.StorageCluster) GroupSnapshotClassConfiguration { | ||
return GroupSnapshotClassConfiguration{ | ||
groupSnapshotClass: newVolumeGroupSnapshotClass(instance, cephfsGroupSnapshotter), | ||
reconcileStrategy: ReconcileStrategy(instance.Spec.ManagedResources.CephFilesystems.ReconcileStrategy), | ||
} | ||
} | ||
|
||
func newCephBlockPoolGroupSnapshotClassConfiguration(instance *ocsv1.StorageCluster) GroupSnapshotClassConfiguration { | ||
return GroupSnapshotClassConfiguration{ | ||
groupSnapshotClass: newVolumeGroupSnapshotClass(instance, rbdGroupSnapshotter), | ||
reconcileStrategy: ReconcileStrategy(instance.Spec.ManagedResources.CephBlockPools.ReconcileStrategy), | ||
} | ||
} | ||
|
||
func newGroupSnapshotClassConfigurations(instance *ocsv1.StorageCluster) []GroupSnapshotClassConfiguration { | ||
vsccs := []GroupSnapshotClassConfiguration{ | ||
newCephFilesystemGroupSnapshotClassConfiguration(instance), | ||
newCephBlockPoolGroupSnapshotClassConfiguration(instance), | ||
} | ||
return vsccs | ||
} | ||
|
||
func (r *StorageClusterReconciler) createGroupSnapshotClasses(vsccs []GroupSnapshotClassConfiguration) error { | ||
|
||
for _, vscc := range vsccs { | ||
if vscc.reconcileStrategy == ReconcileStrategyIgnore || vscc.disable { | ||
continue | ||
} | ||
|
||
vsc := vscc.groupSnapshotClass | ||
existing := &groupsnapapi.VolumeGroupSnapshotClass{} | ||
err := r.Client.Get(r.ctx, types.NamespacedName{Name: vsc.Name, Namespace: vsc.Namespace}, existing) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
// Since the SnapshotClass is not found, we will create a new one | ||
r.Log.Info("Creating GroupSnapshotClass.", "GroupSnapshotClass", klog.KRef("", vsc.Name)) | ||
err = r.Client.Create(r.ctx, vsc) | ||
if err != nil { | ||
r.Log.Error(err, "Failed to create GroupSnapshotClass.", "GroupSnapshotClass", klog.KRef("", vsc.Name)) | ||
return err | ||
} | ||
// no error, continue with the next iteration | ||
continue | ||
} | ||
|
||
r.Log.Error(err, "Failed to 'Get' GroupSnapshotClass.", "GroupSnapshotClass", klog.KRef("", vsc.Name)) | ||
return err | ||
} | ||
if vscc.reconcileStrategy == ReconcileStrategyInit { | ||
return nil | ||
} | ||
if existing.DeletionTimestamp != nil { | ||
return fmt.Errorf("failed to restore GroupSnapshotClass %q because it is marked for deletion", existing.Name) | ||
} | ||
// if there is a mismatch in the parameters of existing vs created resources, | ||
if !reflect.DeepEqual(vsc.Parameters, existing.Parameters) { | ||
// we have to update the existing SnapshotClass | ||
r.Log.Info("GroupSnapshotClass needs to be updated", "GroupSnapshotClass", klog.KRef("", existing.Name)) | ||
existing.ObjectMeta.OwnerReferences = vsc.ObjectMeta.OwnerReferences | ||
vsc.ObjectMeta = existing.ObjectMeta | ||
if err := r.Client.Update(r.ctx, vsc); err != nil { | ||
r.Log.Error(err, "GroupSnapshotClass updation failed.", "GroupSnapshotClass", klog.KRef("", existing.Name)) | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (obj *ocsGroupSnapshotClass) ensureCreated(r *StorageClusterReconciler, instance *ocsv1.StorageCluster) (reconcile.Result, error) { | ||
if !r.AvailableCrds[VolumeGroupSnapshotClassCrdName] { | ||
r.Log.Info("VolumeGroupSnapshotClass CRD is not available") | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
vgsc := newGroupSnapshotClassConfigurations(instance) | ||
|
||
err := r.createGroupSnapshotClasses(vgsc) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} | ||
|
||
func (obj *ocsGroupSnapshotClass) ensureDeleted(r *StorageClusterReconciler, instance *ocsv1.StorageCluster) (reconcile.Result, error) { | ||
if !r.AvailableCrds[VolumeGroupSnapshotClassCrdName] { | ||
r.Log.Info("VolumeGroupSnapshotClass CRD doesn't exist") | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
vgscs := newGroupSnapshotClassConfigurations(instance) | ||
for _, vgsc := range vgscs { | ||
sc := vgsc.groupSnapshotClass | ||
err := r.Client.Delete(r.ctx, sc) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
r.Log.Info("Uninstall: GroupSnapshotClass not found, nothing to do.", "GroupSnapshotClass", klog.KRef("", sc.Name)) | ||
} else { | ||
r.Log.Error(err, "Uninstall: Error while deleting GroupSnapshotClass.", "GroupSnapshotClass", klog.KRef("", sc.Name)) | ||
return reconcile.Result{}, err | ||
} | ||
} | ||
} | ||
return reconcile.Result{}, nil | ||
} |
33 changes: 33 additions & 0 deletions
33
controllers/storagecluster/volumegroupsnapshotterclasses_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package storagecluster | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
groupsnapapi "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumegroupsnapshot/v1beta1" | ||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
func TestVolumeGroupSnapshotterClasses(t *testing.T) { | ||
t, reconciler, _, request := initStorageClusterResourceCreateUpdateTest(t, nil, nil) | ||
assertVolumeGroupSnapshotterClasses(t, reconciler, request) | ||
} | ||
|
||
func assertVolumeGroupSnapshotterClasses(t *testing.T, reconciler StorageClusterReconciler, | ||
request reconcile.Request) { | ||
rbdVSCName := "ocsinit-rbdplugin-groupsnapclass" | ||
cephfsVSCName := "ocsinit-cephfsplugin-groupsnapclass" | ||
vscNames := []string{cephfsVSCName, rbdVSCName} | ||
for _, eachVSCName := range vscNames { | ||
actualVSC := &groupsnapapi.VolumeGroupSnapshotClass{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: eachVSCName, | ||
}, | ||
} | ||
request.Name = eachVSCName | ||
err := reconciler.Client.Get(context.TODO(), request.NamespacedName, actualVSC) | ||
assert.NoError(t, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.