Skip to content

Commit

Permalink
controller/pvc: add checks for nil/empty pvc.Spec.StorageClassName
Browse files Browse the repository at this point in the history
`pvc.Spec.StorageClassName` is a string pointer and can be nil or empty.
This commit adds check for both nil and empty.

Signed-off-by: Rakshith R <rar@redhat.com>
  • Loading branch information
Rakshith-R authored and mergify[bot] committed Sep 24, 2024
1 parent 1ac6ece commit df8c048
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,22 +277,23 @@ func (r *PersistentVolumeClaimReconciler) determineScheduleAndRequeue(
}
}

// For static provisioned PVs, StorageClassName is empty.
if len(*pvc.Spec.StorageClassName) == 0 {
// For static provisioned PVs, StorageClassName is nil or empty.
if pvc.Spec.StorageClassName == nil || len(*pvc.Spec.StorageClassName) == 0 {
logger.Info("StorageClassName is empty")
return "", ErrScheduleNotFound
}
storageClassName := *pvc.Spec.StorageClassName

// check for storageclass schedule annotation.
sc := &storagev1.StorageClass{}
err = r.Client.Get(ctx, types.NamespacedName{Name: *pvc.Spec.StorageClassName}, sc)
err = r.Client.Get(ctx, types.NamespacedName{Name: storageClassName}, sc)
if err != nil {
if apierrors.IsNotFound(err) {
logger.Error(err, "StorageClass not found", "StorageClass", *pvc.Spec.StorageClassName)
logger.Error(err, "StorageClass not found", "StorageClass", storageClassName)
return "", ErrScheduleNotFound
}

logger.Error(err, "Failed to get StorageClass", "StorageClass", *pvc.Spec.StorageClassName)
logger.Error(err, "Failed to get StorageClass", "StorageClass", storageClassName)
return "", err
}
schedule, scheduleFound = getScheduleFromAnnotation(annotationKey, logger, sc.Annotations)
Expand Down Expand Up @@ -376,7 +377,7 @@ func (r *PersistentVolumeClaimReconciler) setupIndexers(mgr ctrl.Manager) error
field: "spec.storageClassName",
indexFn: func(rawObj client.Object) []string {
pvc, ok := rawObj.(*corev1.PersistentVolumeClaim)
if !ok {
if !ok || (pvc.Spec.StorageClassName == nil) || len(*pvc.Spec.StorageClassName) == 0 {
return nil
}
return []string{*pvc.Spec.StorageClassName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ func TestDetermineScheduleAndRequeue(t *testing.T) {
assert.Equal(t, "", schedule)
})

// test for StorageClassName is nil
t.Run("StorageClassName is nil", func(t *testing.T) {
pvc.Spec.StorageClassName = nil
pvc.Annotations = nil
schedule, error := r.determineScheduleAndRequeue(ctx, &logger, pvc, driverName, rsCronJobScheduleTimeAnnotation)
assert.ErrorIs(t, error, ErrScheduleNotFound)
assert.Equal(t, "", schedule)
})
}

func TestAnnotationValueMissing(t *testing.T) {
Expand Down

0 comments on commit df8c048

Please sign in to comment.