If a pvc has an empty storageclass name, don't try to assign a default StorageClass to it.
This commit is contained in:
		@@ -948,7 +948,8 @@ func (ctrl *PersistentVolumeController) updateVolumePhaseWithEvent(ctx context.C
 | 
				
			|||||||
func (ctrl *PersistentVolumeController) assignDefaultStorageClass(ctx context.Context, claim *v1.PersistentVolumeClaim) (bool, error) {
 | 
					func (ctrl *PersistentVolumeController) assignDefaultStorageClass(ctx context.Context, claim *v1.PersistentVolumeClaim) (bool, error) {
 | 
				
			||||||
	logger := klog.FromContext(ctx)
 | 
						logger := klog.FromContext(ctx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if storagehelpers.GetPersistentVolumeClaimClass(claim) != "" {
 | 
						if storagehelpers.PersistentVolumeClaimHasClass(claim) {
 | 
				
			||||||
 | 
							// The user asked for a class.
 | 
				
			||||||
		return false, nil
 | 
							return false, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,6 +24,20 @@ import (
 | 
				
			|||||||
	"k8s.io/component-helpers/scheduling/corev1"
 | 
						"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
 | 
					// GetPersistentVolumeClaimClass returns StorageClassName. If no storage class was
 | 
				
			||||||
// requested, it returns "".
 | 
					// requested, it returns "".
 | 
				
			||||||
func GetPersistentVolumeClaimClass(claim *v1.PersistentVolumeClaim) string {
 | 
					func GetPersistentVolumeClaimClass(claim *v1.PersistentVolumeClaim) string {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,6 +21,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	v1 "k8s.io/api/core/v1"
 | 
						v1 "k8s.io/api/core/v1"
 | 
				
			||||||
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
						metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
				
			||||||
 | 
						"k8s.io/utils/ptr"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var nodeLabels = map[string]string{
 | 
					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)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user