Merge pull request #122704 from carlory/fix-introactive-storageclass

If a pvc has an empty storageclass name, don't try to assign a default StorageClass
This commit is contained in:
Kubernetes Prow Robot 2024-01-18 20:10:41 +01:00 committed by GitHub
commit eabd6e4584
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 61 additions and 1 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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)
}
})
}
}