-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pv and pvc controllers to publish events
Signed-off-by: Suleyman Akbas <[email protected]>
- Loading branch information
1 parent
cd91081
commit 35455db
Showing
9 changed files
with
268 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package persistent_volume_claim | ||
|
||
import ( | ||
"context" | ||
"github.com/openshift/lvm-operator/controllers" | ||
corev1 "k8s.io/api/core/v1" | ||
v1 "k8s.io/api/storage/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/client-go/tools/record" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
crlog "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"strings" | ||
) | ||
|
||
// PersistentVolumeClaimReconciler reconciles a PersistentVolumeClaim object | ||
type PersistentVolumeClaimReconciler struct { | ||
client client.Client | ||
apiReader client.Reader | ||
recorder record.EventRecorder | ||
} | ||
|
||
// NewPersistentVolumeClaimReconciler returns PersistentVolumeClaimReconciler. | ||
func NewPersistentVolumeClaimReconciler(client client.Client, apiReader client.Reader, eventRecorder record.EventRecorder) *PersistentVolumeClaimReconciler { | ||
return &PersistentVolumeClaimReconciler{ | ||
client: client, | ||
apiReader: apiReader, | ||
recorder: eventRecorder, | ||
} | ||
} | ||
|
||
//+kubebuilder:rbac:groups=core,resources=persistentvolumeclaims,verbs=get;list;watch;update | ||
//+kubebuilder:rbac:groups=storage.k8s.io,resources=csistoragecapacities,verbs=get;list;watch | ||
//+kubebuilder:rbac:groups=core,resources=events,verbs=create;update;patch | ||
|
||
// Reconcile PVC | ||
func (r *PersistentVolumeClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
log := crlog.FromContext(ctx) | ||
|
||
log.Info("Reconciling PVC", "PVC", req.NamespacedName) | ||
pvc := &corev1.PersistentVolumeClaim{} | ||
err := r.client.Get(ctx, req.NamespacedName, pvc) | ||
switch { | ||
case err == nil: | ||
case apierrors.IsNotFound(err): | ||
return ctrl.Result{}, nil | ||
default: | ||
return ctrl.Result{}, err | ||
} | ||
|
||
// Skip if the PVC is deleted or does not use the lvms storage class. | ||
if pvc.DeletionTimestamp != nil || !strings.HasPrefix(*pvc.Spec.StorageClassName, controllers.StorageClassPrefix) { | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// Skip if the PVC is not in Pending state. | ||
if pvc.Status.Phase != "Pending" { | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// List csi storage capacities | ||
storageCapacityList := &v1.CSIStorageCapacityList{} | ||
err = r.client.List(ctx, storageCapacityList, &client.ListOptions{Namespace: controllers.OperatorNamespace}) | ||
switch { | ||
case err == nil: | ||
case apierrors.IsNotFound(err): | ||
return ctrl.Result{}, nil | ||
default: | ||
return ctrl.Result{}, err | ||
} | ||
|
||
// Check if there is any node that has enough storage capacity for the PVC | ||
found := false | ||
for _, sc := range storageCapacityList.Items { | ||
if strings.HasPrefix(sc.StorageClassName, controllers.StorageClassPrefix) && pvc.Spec.Resources.Requests.Storage().Cmp(*sc.Capacity) < 0 { | ||
found = true | ||
} | ||
} | ||
|
||
// Publish an event if the requested storage is greater than the available capacity | ||
if !found { | ||
r.recorder.Event(pvc, "Warning", "NotEnoughCapacity", "Requested storage is greater than available capacity on all the nodes. Check available capacity by `oc get csistoragecapacity`.") | ||
log.Info("Event published for the PVC", "PVC", req.NamespacedName) | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *PersistentVolumeClaimReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
pred := predicate.Funcs{ | ||
CreateFunc: func(event.CreateEvent) bool { return true }, | ||
DeleteFunc: func(event.DeleteEvent) bool { return false }, | ||
UpdateFunc: func(event.UpdateEvent) bool { return true }, | ||
GenericFunc: func(event.GenericEvent) bool { return false }, | ||
} | ||
return ctrl.NewControllerManagedBy(mgr). | ||
WithEventFilter(pred). | ||
For(&corev1.PersistentVolumeClaim{}). | ||
Complete(r) | ||
} |
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,77 @@ | ||
package persistent_volume | ||
|
||
import ( | ||
"context" | ||
"github.com/openshift/lvm-operator/controllers" | ||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/client-go/tools/record" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
crlog "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"strings" | ||
) | ||
|
||
// PersistentVolumeReconciler reconciles a PersistentVolume object | ||
type PersistentVolumeReconciler struct { | ||
client client.Client | ||
apiReader client.Reader | ||
recorder record.EventRecorder | ||
} | ||
|
||
// NewPersistentVolumeReconciler returns PersistentVolumeReconciler. | ||
func NewPersistentVolumeReconciler(client client.Client, apiReader client.Reader, eventRecorder record.EventRecorder) *PersistentVolumeReconciler { | ||
return &PersistentVolumeReconciler{ | ||
client: client, | ||
apiReader: apiReader, | ||
recorder: eventRecorder, | ||
} | ||
} | ||
|
||
//+kubebuilder:rbac:groups=core,resources=persistentvolumes,verbs=get;list;watch;update | ||
//+kubebuilder:rbac:groups=core,resources=events,verbs=create;update;patch | ||
|
||
// Reconcile PV | ||
func (r *PersistentVolumeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
log := crlog.FromContext(ctx) | ||
|
||
log.Info("Reconciling PV", "PV", req.NamespacedName) | ||
pv := &corev1.PersistentVolume{} | ||
err := r.client.Get(ctx, req.NamespacedName, pv) | ||
switch { | ||
case err == nil: | ||
case apierrors.IsNotFound(err): | ||
return ctrl.Result{}, nil | ||
default: | ||
return ctrl.Result{}, err | ||
} | ||
|
||
// Skip if the PV is deleted or PV does not use the lvms storage class. | ||
if pv.DeletionTimestamp != nil || !strings.HasPrefix(pv.Spec.StorageClassName, controllers.StorageClassPrefix) { | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// Publish an event if PV has no claimRef | ||
if pv.Spec.ClaimRef == nil { | ||
r.recorder.Event(pv, "Warning", "RemovedClaimReference", "Claim reference is removed. This PV is dynamically managed. Please do not modify it.") | ||
log.Info("Event published for the PV", "PV", req.NamespacedName) | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *PersistentVolumeReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
pred := predicate.Funcs{ | ||
CreateFunc: func(event.CreateEvent) bool { return true }, | ||
DeleteFunc: func(event.DeleteEvent) bool { return false }, | ||
UpdateFunc: func(event.UpdateEvent) bool { return true }, | ||
GenericFunc: func(event.GenericEvent) bool { return false }, | ||
} | ||
return ctrl.NewControllerManagedBy(mgr). | ||
WithEventFilter(pred). | ||
For(&corev1.PersistentVolume{}). | ||
Complete(r) | ||
} |
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