Skip to content

Commit

Permalink
Merge pull request #179 from red-hat-storage/sync_us--main
Browse files Browse the repository at this point in the history
Syncing latest changes from upstream main for kubernetes-csi-addons
  • Loading branch information
openshift-merge-bot[bot] committed Jul 31, 2024
2 parents b9979ef + dd4b168 commit 4bd4194
Show file tree
Hide file tree
Showing 223 changed files with 13,253 additions and 18,978 deletions.
4 changes: 0 additions & 4 deletions docs/reclaimspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ they must perform the same action on all the PersistentVolumeClaims within that

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
Expand Down
78 changes: 78 additions & 0 deletions internal/controller/csiaddons/persistentvolumeclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -288,6 +290,46 @@ func (r *PersistentVolumeClaimReconciler) determineScheduleAndRequeue(
return "", ErrScheduleNotFound
}

// storageClassEventHandler returns an EventHandler that responds to changes
// in StorageClass objects and generates reconciliation requests for all
// PVCs associated with the changed StorageClass.
// PVCs with rsCronJobScheduleTimeAnnotation are not enqueued.
func (r *PersistentVolumeClaimReconciler) storageClassEventHandler() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(
func(ctx context.Context, obj client.Object) []reconcile.Request {
ok := false
obj, ok = obj.(*storagev1.StorageClass)
if !ok {
return nil
}

// get all PVCs with the same storageclass.
pvcList := &corev1.PersistentVolumeClaimList{}
err := r.List(ctx, pvcList, client.MatchingFields{"spec.storageClassName": obj.GetName()})
if err != nil {
log.FromContext(ctx).Error(err, "Failed to list PVCs")

return nil
}

var requests []reconcile.Request
for _, pvc := range pvcList.Items {
if _, ok := pvc.GetAnnotations()[rsCronJobScheduleTimeAnnotation]; ok {
continue
}
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: pvc.Name,
Namespace: pvc.Namespace,
},
})
}

return requests
},
)
}

// SetupWithManager sets up the controller with the Manager.
func (r *PersistentVolumeClaimReconciler) SetupWithManager(mgr ctrl.Manager, ctrlOptions controller.Options) error {
err := mgr.GetFieldIndexer().IndexField(
Expand All @@ -299,6 +341,21 @@ func (r *PersistentVolumeClaimReconciler) SetupWithManager(mgr ctrl.Manager, ctr
return err
}

err = mgr.GetFieldIndexer().IndexField(
context.Background(),
&corev1.PersistentVolumeClaim{},
"spec.storageClassName",
func(rawObj client.Object) []string {
pvc, ok := rawObj.(*corev1.PersistentVolumeClaim)
if !ok {
return nil
}
return []string{*pvc.Spec.StorageClassName}
})
if err != nil {
return err
}

err = mgr.GetFieldIndexer().IndexField(
context.Background(),
&csiaddonsv1alpha1.EncryptionKeyRotationCronJob{},
Expand Down Expand Up @@ -338,8 +395,29 @@ func (r *PersistentVolumeClaimReconciler) SetupWithManager(mgr ctrl.Manager, ctr
},
}

scPred := predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
if e.ObjectNew == nil || e.ObjectOld == nil {
return false
}
// reconcile only if reclaimspace annotation between old and new objects have changed.
oldSchdeule, oldOk := e.ObjectOld.GetAnnotations()[rsCronJobScheduleTimeAnnotation]
newSchdeule, newOk := e.ObjectNew.GetAnnotations()[rsCronJobScheduleTimeAnnotation]

krcOldSchdeule, krcOldOk := e.ObjectOld.GetAnnotations()[krcJobScheduleTimeAnnotation]
krcNewSchdeule, krcNewOk := e.ObjectNew.GetAnnotations()[krcJobScheduleTimeAnnotation]

return (oldOk != newOk || oldSchdeule != newSchdeule) || (krcOldOk != krcNewOk || krcOldSchdeule != krcNewSchdeule)
},
}

return ctrl.NewControllerManagedBy(mgr).
For(&corev1.PersistentVolumeClaim{}).
Watches(
&storagev1.StorageClass{},
r.storageClassEventHandler(),
builder.WithPredicates(scPred),
).
Owns(&csiaddonsv1alpha1.ReclaimSpaceCronJob{}).
Owns(&csiaddonsv1alpha1.EncryptionKeyRotationCronJob{}).
WithEventFilter(pred).
Expand Down
25 changes: 18 additions & 7 deletions internal/proto/encryptionkeyrotation_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions internal/proto/networkfence_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions internal/proto/reclaimspace_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions internal/proto/replication_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4bd4194

Please sign in to comment.