Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controller/pvc: add checks for nil/empty pvc.Spec.StorageClassName #685

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading