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

reclaimspace: support for storageclass annotation #585

Merged
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
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ rules:
- volumereplications/status
verbs:
- update
- apiGroups:
- storage.k8s.io
resources:
- storageclasses
verbs:
- get
- list
- watch
- apiGroups:
- storage.k8s.io
resources:
Expand Down
15 changes: 15 additions & 0 deletions controllers/csiaddons/persistentvolumeclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Madhu-1 marked this conversation as resolved.
Show resolved Hide resolved
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
}

Expand Down
8 changes: 8 additions & 0 deletions deploy/controller/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ rules:
- volumereplications/status
verbs:
- update
- apiGroups:
- storage.k8s.io
resources:
- storageclasses
iPraveenParihar marked this conversation as resolved.
Show resolved Hide resolved
verbs:
- get
- list
- watch
- apiGroups:
- storage.k8s.io
resources:
Expand Down
33 changes: 28 additions & 5 deletions docs/reclaimspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Madhu-1 marked this conversation as resolved.
Show resolved Hide resolved
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.
Loading