From a99873ad9269cdec97ef89701cb1d0b7968150a4 Mon Sep 17 00:00:00 2001 From: Praveen M Date: Fri, 17 May 2024 13:07:13 +0530 Subject: [PATCH 1/2] reclaimspace: support for storageclass annotation This commit adds support for auto ReclaimSpace by StorageClass annotations. A ReclaimSpaceCronjob will be created for PVCs associated with a StorageClass that has the ReclaimSpace annotation. Signed-off-by: Praveen M --- config/rbac/role.yaml | 8 ++++++++ .../csiaddons/persistentvolumeclaim_controller.go | 15 +++++++++++++++ deploy/controller/rbac.yaml | 8 ++++++++ 3 files changed, 31 insertions(+) diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index c9b90c80a..fa30b20da 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -219,6 +219,14 @@ rules: - volumereplications/status verbs: - update +- apiGroups: + - storage.k8s.io + resources: + - storageclasses + verbs: + - get + - list + - watch - apiGroups: - storage.k8s.io resources: diff --git a/controllers/csiaddons/persistentvolumeclaim_controller.go b/controllers/csiaddons/persistentvolumeclaim_controller.go index 0d52dfcb4..7b7de7ad7 100644 --- a/controllers/csiaddons/persistentvolumeclaim_controller.go +++ b/controllers/csiaddons/persistentvolumeclaim_controller.go @@ -31,6 +31,7 @@ import ( "github.com/go-logr/logr" "github.com/robfig/cron/v3" corev1 "k8s.io/api/core/v1" + storagev1 "k8s.io/api/storage/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -68,6 +69,7 @@ const ( //+kubebuilder:rbac:groups=core,resources=persistentvolumeclaims/finalizers,verbs=update //+kubebuilder:rbac:groups=csiaddons.openshift.io,resources=reclaimspacecronjobs,verbs=get;list;watch;create;delete;update //+kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch +//+kubebuilder:rbac:groups=storage.k8s.io,resources=storageclasses,verbs=get;list;watch // Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. This is @@ -260,6 +262,7 @@ func (r *PersistentVolumeClaimReconciler) determineScheduleAndRequeue( if scheduleFound { return schedule, nil } + // check for namespace schedule annotation. // We cannot have a generic solution for all CSI drivers to get the driver // name from PV and check if driver supports space reclamation or not and @@ -294,6 +297,18 @@ func (r *PersistentVolumeClaimReconciler) determineScheduleAndRequeue( return "", ErrConnNotFoundRequeueNeeded } + // check for storageclass schedule annotation. + sc := &storagev1.StorageClass{} + err = r.Client.Get(ctx, types.NamespacedName{Name: *pvc.Spec.StorageClassName}, sc) + if err != nil { + logger.Error(err, "Failed to get StorageClass", "StorageClass", *pvc.Spec.StorageClassName) + return "", err + } + schedule, scheduleFound = getScheduleFromAnnotation(logger, sc.Annotations) + if scheduleFound { + return schedule, nil + } + return "", ErrScheduleNotFound } diff --git a/deploy/controller/rbac.yaml b/deploy/controller/rbac.yaml index a45cba171..2897add4b 100644 --- a/deploy/controller/rbac.yaml +++ b/deploy/controller/rbac.yaml @@ -269,6 +269,14 @@ rules: - volumereplications/status verbs: - update +- apiGroups: + - storage.k8s.io + resources: + - storageclasses + verbs: + - get + - list + - watch - apiGroups: - storage.k8s.io resources: From 56d2a5cf427a65c76a587033b0f8b40faa9d1a09 Mon Sep 17 00:00:00 2001 From: Praveen M Date: Thu, 6 Jun 2024 15:01:36 +0530 Subject: [PATCH 2/2] doc: support for storageclass annotation Signed-off-by: Praveen M --- docs/reclaimspace.md | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/docs/reclaimspace.md b/docs/reclaimspace.md index bc938d267..487eca17f 100644 --- a/docs/reclaimspace.md +++ b/docs/reclaimspace.md @@ -121,8 +121,31 @@ $ kubectl annotate namespace default "reclaimspace.csiaddons.openshift.io/schedu namespace/default annotated ``` -**Note** Please note that the PersistentVolumeClaim annotation takes priority -over Namespace annotation. The kubernetes-csi-addons only generate a -`ReclaimSpaceCronJob` if the annotation exists on the Namespace. If an admin -needs to modify or delete the annotation on the Namespace, they must perform -the same action on all the PersistentVolumeClaims within that Namespace. +**Note** Please note that the PersistentVolumeClaim annotation takes priority over Namespace +annotation. The kubernetes-csi-addons only generate a `ReclaimSpaceCronJob` if the annotation +exists on the Namespace. If an admin needs to modify or delete the annotation on the Namespace, +they must perform the same action on all the PersistentVolumeClaims within that Namespace. + +## Annotating StorageClass + +You can create `ReclaimSpaceCronJob` CR automatically by adding the +`reclaimspace.csiaddons.openshift.io/schedule: "@midnight"` annotations to the StorageClass object. +This will only affect new PersistentVolumeClaims created from this StorageClass for ReclaimSpace +operations. To include existing PersistentVolumeClaims for ReclaimSpace operations, you must restart +the controller. This will ensure that reclaimspace annotations are added to the existing +PersistentVolumeClaims and `ReclaimSpaceCronJob` resources are created for them. + +```console +$ kubectl get storageclass rbd-sc +NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE +rbd-sc rbd.csi.ceph.com Delete Immediate true 5d2h + +$ kubectl annotate storageclass rbd-sc "reclaimspace.csiaddons.openshift.io/schedule=@midnight" +storageclass.storage.k8s.io/rbd-sc annotated +``` + +**Note** Please note that the PersistentVolumeClaim and Namespace annotation takes priority +over StorageClass annotation. The kubernetes-csi-addons only generate a `ReclaimSpaceCronJob` +if the annotation exists on the StorageClass. If an admin needs to modify or delete the +annotation on the StorageClass, they must perform the same action on all the PersistentVolumeClaims +created from this StorageClass. \ No newline at end of file