From 58ee52b150753164e27809c99bd9cff24a937dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20M=C3=B6ller?= Date: Mon, 24 Jun 2024 10:10:30 +0200 Subject: [PATCH] fix: discover topolvm based SCs that are not part of the generated device classes from LVMCluster MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakob Möller --- .../persistent-volume-claim/controller.go | 24 +++++++-- .../controller_test.go | 53 +++++++++++++++++-- 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/internal/controllers/persistent-volume-claim/controller.go b/internal/controllers/persistent-volume-claim/controller.go index a3dd0461f..3a9fd0bed 100644 --- a/internal/controllers/persistent-volume-claim/controller.go +++ b/internal/controllers/persistent-volume-claim/controller.go @@ -9,7 +9,9 @@ import ( "time" "github.com/openshift/lvm-operator/internal/controllers/constants" + "github.com/topolvm/topolvm" corev1 "k8s.io/api/core/v1" + v1 "k8s.io/api/storage/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/client-go/tools/record" @@ -68,13 +70,27 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu } // Skip if StorageClassName does not contain the lvms prefix - lvmsPrefix, deviceClass, exists := strings.Cut(*pvc.Spec.StorageClassName, "-") - if !exists || fmt.Sprintf("%s-", lvmsPrefix) != constants.StorageClassPrefix { - logger.Info("skipping pvc as the storageClassName does not contain desired prefix", - "desired-prefix", constants.StorageClassPrefix) + storageClassName := pvc.Spec.StorageClassName + + if storageClassName == nil { return ctrl.Result{}, nil } + var sc v1.StorageClass + if err = r.Client.Get(ctx, client.ObjectKey{Name: *storageClassName}, &sc); err != nil { + return ctrl.Result{}, err + } + + if sc.Provisioner != constants.TopolvmCSIDriverName { + logger.Info("skipping pvc as the storage class is not provisioned by topolvm") + return ctrl.Result{}, nil + } + + deviceClass, deviceClassKeyPresent := sc.Parameters[constants.DeviceClassKey] + if !deviceClassKeyPresent { + deviceClass = topolvm.DefaultDeviceClassAnnotationName + } + // Skip if the PVC is not in Pending state. if pvc.Status.Phase != "Pending" { return ctrl.Result{}, nil diff --git a/internal/controllers/persistent-volume-claim/controller_test.go b/internal/controllers/persistent-volume-claim/controller_test.go index 2f698cd3f..034bc09a1 100644 --- a/internal/controllers/persistent-volume-claim/controller_test.go +++ b/internal/controllers/persistent-volume-claim/controller_test.go @@ -10,6 +10,7 @@ import ( "github.com/openshift/lvm-operator/internal/controllers/constants" persistentvolumeclaim "github.com/openshift/lvm-operator/internal/controllers/persistent-volume-claim" "github.com/stretchr/testify/assert" + storagev1 "k8s.io/api/storage/v1" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client/interceptor" "sigs.k8s.io/controller-runtime/pkg/event" @@ -106,6 +107,10 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { StorageClassName: ptr.To("blabla"), }, }, + &storagev1.StorageClass{ + ObjectMeta: metav1.ObjectMeta{Name: "blabla"}, + Provisioner: "random-provisioner", + }, }, }, { @@ -118,12 +123,19 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { &v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{Namespace: defaultNamespace, Name: "test-non-pending-PVC"}, Spec: v1.PersistentVolumeClaimSpec{ - StorageClassName: ptr.To(constants.StorageClassPrefix + "bla"), + StorageClassName: ptr.To("my-provisioner"), }, Status: v1.PersistentVolumeClaimStatus{ Phase: v1.ClaimBound, }, }, + &storagev1.StorageClass{ + ObjectMeta: metav1.ObjectMeta{Name: "my-provisioner"}, + Provisioner: constants.TopolvmCSIDriverName, + Parameters: map[string]string{ + constants.DeviceClassKey: "my-device-class", + }, + }, }, }, { @@ -136,12 +148,19 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { &v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{Namespace: defaultNamespace, Name: "test-pending-PVC"}, Spec: v1.PersistentVolumeClaimSpec{ - StorageClassName: ptr.To(constants.StorageClassPrefix + "bla"), + StorageClassName: ptr.To("my-provisioner"), }, Status: v1.PersistentVolumeClaimStatus{ Phase: v1.ClaimPending, }, }, + &storagev1.StorageClass{ + ObjectMeta: metav1.ObjectMeta{Name: "my-provisioner"}, + Provisioner: constants.TopolvmCSIDriverName, + Parameters: map[string]string{ + constants.DeviceClassKey: "my-device-class", + }, + }, }, clientListErr: assert.AnError, wantErr: true, @@ -156,12 +175,19 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { &v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{Namespace: defaultNamespace, Name: "test-pending-PVC"}, Spec: v1.PersistentVolumeClaimSpec{ - StorageClassName: ptr.To(constants.StorageClassPrefix + "bla"), + StorageClassName: ptr.To("my-provisioner"), }, Status: v1.PersistentVolumeClaimStatus{ Phase: v1.ClaimPending, }, }, + &storagev1.StorageClass{ + ObjectMeta: metav1.ObjectMeta{Name: "my-provisioner"}, + Provisioner: constants.TopolvmCSIDriverName, + Parameters: map[string]string{ + constants.DeviceClassKey: "my-device-class", + }, + }, }, expectNoStorageAvailable: true, expectRequeue: true, @@ -190,6 +216,13 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { &v1.Node{ ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{persistentvolumeclaim.CapacityAnnotation + "bla": "10"}}, }, + &storagev1.StorageClass{ + ObjectMeta: metav1.ObjectMeta{Name: constants.StorageClassPrefix + "bla"}, + Provisioner: constants.TopolvmCSIDriverName, + Parameters: map[string]string{ + constants.DeviceClassKey: "bla", + }, + }, }, expectNoStorageAvailable: true, expectRequeue: true, @@ -218,6 +251,13 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { &v1.Node{ ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{persistentvolumeclaim.CapacityAnnotation + "bla": "100"}}, }, + &storagev1.StorageClass{ + ObjectMeta: metav1.ObjectMeta{Name: constants.StorageClassPrefix + "bla"}, + Provisioner: constants.TopolvmCSIDriverName, + Parameters: map[string]string{ + constants.DeviceClassKey: "bla", + }, + }, }, expectNoStorageAvailable: false, expectRequeue: true, @@ -249,6 +289,13 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { &v1.Node{ ObjectMeta: metav1.ObjectMeta{Name: "idle"}, }, + &storagev1.StorageClass{ + ObjectMeta: metav1.ObjectMeta{Name: constants.StorageClassPrefix + "bla"}, + Provisioner: constants.TopolvmCSIDriverName, + Parameters: map[string]string{ + constants.DeviceClassKey: "bla", + }, + }, }, expectNoStorageAvailable: false, expectRequeue: true,