Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DFBUGS-1483:[release-4.18] add VolumeGroupSnapshotClass for CephFS and RBD #2982

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ rules:
- namespaces
verbs:
- get
- apiGroups:
- groupsnapshot.storage.k8s.io
resources:
- volumegroupsnapshotclasses
verbs:
- create
- delete
- get
- list
- update
- watch
- apiGroups:
- k8s.cni.cncf.io
resources:
Expand Down
10 changes: 10 additions & 0 deletions controllers/storagecluster/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,20 @@ func generateNameForSnapshotClass(initData *ocsv1.StorageCluster, snapshotType S
return fmt.Sprintf("%s-%splugin-snapclass", initData.Name, snapshotType)
}

func generateNameForGroupSnapshotClass(initData *ocsv1.StorageCluster, groupSnapshotType groupSnapshotterType) string {
return fmt.Sprintf("%s-%splugin-groupsnapclass", initData.Name, groupSnapshotType)
}

func generateNameForSnapshotClassDriver(snapshotType SnapshotterType) string {
return fmt.Sprintf("%s.%s.csi.ceph.com", util.StorageClassDriverNamePrefix, snapshotType)
}

func setParameterBasedOnSnapshotterType(instance *ocsv1.StorageCluster, groupSnapshotType groupSnapshotterType) (string, string) {
if groupSnapshotType == rbdGroupSnapshotter {
return "pool", generateNameForCephBlockPool(instance)
}
return "fsName", generateNameForCephFilesystem(instance)
}
func generateNameForSnapshotClassSecret(instance *ocsv1.StorageCluster, snapshotType SnapshotterType) string {
// nfs uses the same cephfs secrets
if snapshotType == "nfs" {
Expand Down
6 changes: 4 additions & 2 deletions controllers/storagecluster/initialization_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,14 @@ func createFakeInitializationStorageClusterReconciler(t *testing.T, obj ...runti
ocsProviderService,
createVirtualMachineCRD(),
createStorageClientCRD(),
createVolumeGroupSnapshotClassCRD(),
)
client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(runtimeObjects...).WithStatusSubresource(statusSubresourceObjs...).Build()

availCrds := map[string]bool{
VirtualMachineCrdName: true,
StorageClientCrdName: true,
VirtualMachineCrdName: true,
StorageClientCrdName: true,
VolumeGroupSnapshotClassCrdName: true,
}

return StorageClusterReconciler{
Expand Down
10 changes: 8 additions & 2 deletions controllers/storagecluster/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
error1 "errors"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"strings"
"time"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
"github.com/red-hat-storage/ocs-operator/v4/controllers/util"
Expand Down Expand Up @@ -77,6 +78,8 @@ const (
VirtualMachineCrdName = "virtualmachines.kubevirt.io"

StorageClientCrdName = "storageclients.ocs.openshift.io"

VolumeGroupSnapshotClassCrdName = "volumegroupsnapshotclasses.groupsnapshot.storage.k8s.io"
)

var storageClusterFinalizer = "storagecluster.ocs.openshift.io"
Expand Down Expand Up @@ -123,6 +126,7 @@ var validTopologyLabelKeys = []string{
// +kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors;prometheusrules,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups=template.openshift.io,resources=templates,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups=snapshot.storage.k8s.io,resources=volumesnapshotclasses,verbs=get;watch;create;update;delete
// +kubebuilder:rbac:groups=groupsnapshot.storage.k8s.io,resources=volumegroupsnapshotclasses,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups=config.openshift.io,resources=infrastructures;networks,verbs=get;list;watch
// +kubebuilder:rbac:groups=config.openshift.io,resources=clusterversions;networks,verbs=get;list;watch
// +kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get;list;watch;create;update
Expand Down Expand Up @@ -150,7 +154,7 @@ func (r *StorageClusterReconciler) Reconcile(ctx context.Context, request reconc
r.Log = r.Log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
r.ctx = ctrllog.IntoContext(ctx, r.Log)

for _, crdName := range []string{VirtualMachineCrdName, StorageClientCrdName} {
for _, crdName := range []string{VirtualMachineCrdName, StorageClientCrdName, VolumeGroupSnapshotClassCrdName} {
crd := &metav1.PartialObjectMetadata{}
crd.SetGroupVersionKind(extv1.SchemeGroupVersion.WithKind("CustomResourceDefinition"))
crd.Name = crdName
Expand Down Expand Up @@ -440,6 +444,7 @@ func (r *StorageClusterReconciler) reconcilePhases(
&ocsStorageClass{},
&ocsNoobaaSystem{},
&ocsSnapshotClass{},
&ocsGroupSnapshotClass{},
&ocsJobTemplates{},
&ocsCephRbdMirrors{},
&odfInfoConfig{},
Expand All @@ -459,6 +464,7 @@ func (r *StorageClusterReconciler) reconcilePhases(
&ocsStorageQuota{},
&ocsCephCluster{},
&ocsSnapshotClass{},
&ocsGroupSnapshotClass{},
&ocsNoobaaSystem{},
&odfInfoConfig{},
}
Expand Down
27 changes: 27 additions & 0 deletions controllers/storagecluster/storageclasses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,33 @@ var (
},
}
}
createVolumeGroupSnapshotClassCRD = func() *extv1.CustomResourceDefinition {
pluralName := "volumegroupsnapshotclasses"
return &extv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
Kind: "CustomResourceDefinition",
APIVersion: extv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: pluralName + "." + "groupsnapshot.storage.k8s.io",
UID: "uid",
},
Spec: extv1.CustomResourceDefinitionSpec{
Group: "groupsnapshot.storage.k8s.io",
Scope: extv1.ClusterScoped,
Names: extv1.CustomResourceDefinitionNames{
Plural: pluralName,
Kind: "VolumeGroupSnapshotClass",
},
Versions: []extv1.CustomResourceDefinitionVersion{
{
Name: "v1beta1",
Served: true,
},
},
},
}
}
)

func TestDefaultStorageClasses(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion controllers/storagecluster/storagecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,12 @@ func (r *StorageClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
predicate.Or(
util.NamePredicate(VirtualMachineCrdName),
util.NamePredicate(StorageClientCrdName),
util.NamePredicate(VolumeGroupSnapshotClassCrdName),
),
util.EventTypePredicate(
// the values in the below map are filled before controller watches are setup and these conditions
// will just be evaluated to a boolean by the time builder completes setting up watches.
!r.AvailableCrds[VirtualMachineCrdName] || !r.AvailableCrds[StorageClientCrdName],
!r.AvailableCrds[VirtualMachineCrdName] || !r.AvailableCrds[StorageClientCrdName] || !r.AvailableCrds[VolumeGroupSnapshotClassCrdName],
false,
true,
false,
Expand Down
5 changes: 5 additions & 0 deletions controllers/storagecluster/storagecluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
ocsversion "github.com/red-hat-storage/ocs-operator/v4/version"

"github.com/blang/semver/v4"
groupsnapapi "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumegroupsnapshot/v1beta1"
snapapi "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1"
nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
configv1 "github.com/openshift/api/config/v1"
Expand Down Expand Up @@ -1253,6 +1254,10 @@ func createFakeScheme(t *testing.T) *runtime.Scheme {
if err != nil {
assert.Fail(t, "failed to add volume-snapshot scheme")
}
err = groupsnapapi.AddToScheme(scheme)
if err != nil {
assert.Fail(t, "failed to add volume-group-snapshot scheme")
}
err = monitoringv1.AddToScheme(scheme)
if err != nil {
assert.Fail(t, "failed to add monitoringv1 scheme")
Expand Down
1 change: 1 addition & 0 deletions controllers/storagecluster/uninstall_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ func (r *StorageClusterReconciler) deleteResources(sc *ocsv1.StorageCluster) (re
&ocsCephFilesystems{},
&ocsCephBlockPools{},
&ocsSnapshotClass{},
&ocsGroupSnapshotClass{},
&ocsStorageQuota{},
&ocsStorageClass{},
&ocsCephCluster{},
Expand Down
161 changes: 161 additions & 0 deletions controllers/storagecluster/volumegroupsnapshotterclasses.go
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 controllers/storagecluster/volumegroupsnapshotterclasses_test.go
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)
}
}
11 changes: 11 additions & 0 deletions deploy/csv-templates/ocs-operator.csv.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ spec:
- namespaces
verbs:
- get
- apiGroups:
- groupsnapshot.storage.k8s.io
resources:
- volumegroupsnapshotclasses
verbs:
- create
- delete
- get
- list
- update
- watch
- apiGroups:
- k8s.cni.cncf.io
resources:
Expand Down
Loading
Loading