Skip to content

Commit

Permalink
Merge pull request #122858 from carlory/automated-cherry-pick-of-#122…
Browse files Browse the repository at this point in the history
…704-upstream-release-1.26

Automated cherry pick of #122704: If a pvc has an empty storageclass name, don't try to assign
  • Loading branch information
k8s-ci-robot committed Feb 8, 2024
2 parents 2fc5586 + 7802e91 commit 85ca651
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/controller/volume/persistentvolume/pv_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,8 @@ func (ctrl *PersistentVolumeController) updateVolumePhaseWithEvent(volume *v1.Pe
// Ignores claims that already have a storage class.
// TODO: if resync is ever changed to a larger period, we might need to change how we set the default class on existing unbound claims
func (ctrl *PersistentVolumeController) assignDefaultStorageClass(claim *v1.PersistentVolumeClaim) (bool, error) {
if storagehelpers.GetPersistentVolumeClaimClass(claim) != "" {
if storagehelpers.PersistentVolumeClaimHasClass(claim) {
// The user asked for a class.
return false, nil
}

Expand Down
14 changes: 14 additions & 0 deletions staging/src/k8s.io/component-helpers/storage/volume/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)

var nodeLabels = map[string]string{
Expand Down Expand Up @@ -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: pointer.StringPtr(""),
},
},
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)
}
})
}
}

0 comments on commit 85ca651

Please sign in to comment.