From 8af9a177b2acc7e75e85c58b3220abfd5c5df206 Mon Sep 17 00:00:00 2001 From: carlory Date: Thu, 11 Jan 2024 16:49:16 +0800 Subject: [PATCH] If a pvc has an empty storageclass name, don't try to assign a default StorageClass to it. --- .../volume/persistentvolume/pv_controller.go | 3 +- .../storage/volume/helpers.go | 14 ++++++ .../storage/volume/helpers_test.go | 45 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/pkg/controller/volume/persistentvolume/pv_controller.go b/pkg/controller/volume/persistentvolume/pv_controller.go index 8ee7afdb329..04ca6d797a9 100644 --- a/pkg/controller/volume/persistentvolume/pv_controller.go +++ b/pkg/controller/volume/persistentvolume/pv_controller.go @@ -948,7 +948,8 @@ func (ctrl *PersistentVolumeController) updateVolumePhaseWithEvent(ctx context.C func (ctrl *PersistentVolumeController) assignDefaultStorageClass(ctx context.Context, claim *v1.PersistentVolumeClaim) (bool, error) { logger := klog.FromContext(ctx) - if storagehelpers.GetPersistentVolumeClaimClass(claim) != "" { + if storagehelpers.PersistentVolumeClaimHasClass(claim) { + // The user asked for a class. return false, nil } diff --git a/staging/src/k8s.io/component-helpers/storage/volume/helpers.go b/staging/src/k8s.io/component-helpers/storage/volume/helpers.go index 7ec376f34a0..5068c654624 100644 --- a/staging/src/k8s.io/component-helpers/storage/volume/helpers.go +++ b/staging/src/k8s.io/component-helpers/storage/volume/helpers.go @@ -24,6 +24,20 @@ import ( "k8s.io/component-helpers/scheduling/corev1" ) +// PersistentVolumeClaimHasClass returns true if given claim has set StorageClassName field. +func PersistentVolumeClaimHasClass(claim *v1.PersistentVolumeClaim) bool { + // Use beta annotation first + if _, found := claim.Annotations[v1.BetaStorageClassAnnotation]; found { + return true + } + + if claim.Spec.StorageClassName != nil { + return true + } + + return false +} + // GetPersistentVolumeClaimClass returns StorageClassName. If no storage class was // requested, it returns "". func GetPersistentVolumeClaimClass(claim *v1.PersistentVolumeClaim) string { diff --git a/staging/src/k8s.io/component-helpers/storage/volume/helpers_test.go b/staging/src/k8s.io/component-helpers/storage/volume/helpers_test.go index 9eb56750ed7..0d5176d7999 100644 --- a/staging/src/k8s.io/component-helpers/storage/volume/helpers_test.go +++ b/staging/src/k8s.io/component-helpers/storage/volume/helpers_test.go @@ -21,6 +21,7 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" ) var nodeLabels = map[string]string{ @@ -214,3 +215,47 @@ func testVolumeWithNodeAffinity(t *testing.T, affinity *v1.VolumeNodeAffinity) * }, } } + +func TestPersistentVolumeClaimHasClass(t *testing.T) { + testCases := []struct { + name string + pvc *v1.PersistentVolumeClaim + want bool + }{ + { + name: "no storage class", + pvc: &v1.PersistentVolumeClaim{}, + want: false, + }, + { + name: "storage class set on annotation", + pvc: &v1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + v1.BetaStorageClassAnnotation: "", + }, + }, + }, + want: true, + }, + { + name: "storage class set on spec", + pvc: &v1.PersistentVolumeClaim{ + Spec: v1.PersistentVolumeClaimSpec{ + StorageClassName: ptr.To(""), + }, + }, + want: true, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + got := PersistentVolumeClaimHasClass(tc.pvc) + if got != tc.want { + t.Errorf("PersistentVolumeClaimHasClass() = %v, want %v", got, tc.want) + } + }) + } +}