Skip to content

Commit

Permalink
fix: discover topolvm based SCs that are not part of the generated de…
Browse files Browse the repository at this point in the history
…vice classes from LVMCluster

Signed-off-by: Jakob Möller <[email protected]>
  • Loading branch information
jakobmoellerdev committed Jun 24, 2024
1 parent 284c13e commit bf5accd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
19 changes: 14 additions & 5 deletions internal/controllers/persistent-volume-claim/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -67,14 +69,21 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
return ctrl.Result{}, nil
}

// 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)
var sc v1.StorageClass
if err = r.Client.Get(ctx, client.ObjectKey{Name: *pvc.Spec.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
Expand Down
53 changes: 50 additions & 3 deletions internal/controllers/persistent-volume-claim/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -106,6 +107,10 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) {
StorageClassName: ptr.To("blabla"),
},
},
&storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{Name: "blabla"},
Provisioner: "random-provisioner",
},
},
},
{
Expand All @@ -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",
},
},
},
},
{
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit bf5accd

Please sign in to comment.