diff --git a/pkg/controller/volume/persistentvolume/index_test.go b/pkg/controller/volume/persistentvolume/index_test.go index 2d5bb791c20..a4b72acee36 100644 --- a/pkg/controller/volume/persistentvolume/index_test.go +++ b/pkg/controller/volume/persistentvolume/index_test.go @@ -447,6 +447,25 @@ func TestFindingVolumeWithDifferentAccessModes(t *testing.T) { } } +// createVolumeNodeAffinity returns a VolumeNodeAffinity for given key and value. +func createNodeAffinity(key string, value string) *v1.VolumeNodeAffinity { + return &v1.VolumeNodeAffinity{ + Required: &v1.NodeSelector{ + NodeSelectorTerms: []v1.NodeSelectorTerm{ + { + MatchExpressions: []v1.NodeSelectorRequirement{ + { + Key: key, + Operator: v1.NodeSelectorOpIn, + Values: []string{value}, + }, + }, + }, + }, + }, + } +} + func createTestVolumes() []*v1.PersistentVolume { fs := v1.PersistentVolumeFilesystem // these volumes are deliberately out-of-order to test indexing and sorting @@ -770,7 +789,7 @@ func createTestVolumes() []*v1.PersistentVolume { v1.ReadOnlyMany, }, StorageClassName: classWait, - NodeAffinity: volume.GetVolumeNodeAffinity("key1", "value1"), + NodeAffinity: createNodeAffinity("key1", "value1"), VolumeMode: &fs, }, Status: v1.PersistentVolumeStatus{ @@ -794,7 +813,7 @@ func createTestVolumes() []*v1.PersistentVolume { v1.ReadOnlyMany, }, StorageClassName: classWait, - NodeAffinity: volume.GetVolumeNodeAffinity("key1", "value1"), + NodeAffinity: createNodeAffinity("key1", "value1"), VolumeMode: &fs, }, Status: v1.PersistentVolumeStatus{ @@ -819,7 +838,7 @@ func createTestVolumes() []*v1.PersistentVolume { }, StorageClassName: classWait, ClaimRef: &v1.ObjectReference{Name: "claim02", Namespace: "myns"}, - NodeAffinity: volume.GetVolumeNodeAffinity("key1", "value1"), + NodeAffinity: createNodeAffinity("key1", "value1"), VolumeMode: &fs, }, Status: v1.PersistentVolumeStatus{ @@ -843,7 +862,7 @@ func createTestVolumes() []*v1.PersistentVolume { v1.ReadOnlyMany, }, StorageClassName: classWait, - NodeAffinity: volume.GetVolumeNodeAffinity("key1", "value3"), + NodeAffinity: createNodeAffinity("key1", "value3"), VolumeMode: &fs, }, Status: v1.PersistentVolumeStatus{ @@ -867,7 +886,7 @@ func createTestVolumes() []*v1.PersistentVolume { v1.ReadOnlyMany, }, StorageClassName: classWait, - NodeAffinity: volume.GetVolumeNodeAffinity("key1", "value4"), + NodeAffinity: createNodeAffinity("key1", "value4"), VolumeMode: &fs, }, Status: v1.PersistentVolumeStatus{ @@ -891,7 +910,7 @@ func createTestVolumes() []*v1.PersistentVolume { v1.ReadOnlyMany, }, StorageClassName: classWait, - NodeAffinity: volume.GetVolumeNodeAffinity("key1", "value4"), + NodeAffinity: createNodeAffinity("key1", "value4"), VolumeMode: &fs, }, Status: v1.PersistentVolumeStatus{ @@ -915,7 +934,7 @@ func createTestVolumes() []*v1.PersistentVolume { v1.ReadOnlyMany, }, StorageClassName: classWait, - NodeAffinity: volume.GetVolumeNodeAffinity("key1", "value4"), + NodeAffinity: createNodeAffinity("key1", "value4"), VolumeMode: &fs, }, Status: v1.PersistentVolumeStatus{ @@ -939,7 +958,7 @@ func createTestVolumes() []*v1.PersistentVolume { v1.ReadOnlyMany, }, StorageClassName: classWait, - NodeAffinity: volume.GetVolumeNodeAffinity("key1", "value4"), + NodeAffinity: createNodeAffinity("key1", "value4"), VolumeMode: &fs, }, }, diff --git a/pkg/scheduler/framework/plugins/volumebinding/binder_test.go b/pkg/scheduler/framework/plugins/volumebinding/binder_test.go index 707f26ba06b..cedc27d6f31 100644 --- a/pkg/scheduler/framework/plugins/volumebinding/binder_test.go +++ b/pkg/scheduler/framework/plugins/volumebinding/binder_test.go @@ -666,7 +666,21 @@ func makeTestPV(name, node, capacity, version string, boundToPVC *v1.PersistentV }, } if node != "" { - pv.Spec.NodeAffinity = volume.GetVolumeNodeAffinity(nodeLabelKey, node) + pv.Spec.NodeAffinity = &v1.VolumeNodeAffinity{ + Required: &v1.NodeSelector{ + NodeSelectorTerms: []v1.NodeSelectorTerm{ + { + MatchExpressions: []v1.NodeSelectorRequirement{ + { + Key: nodeLabelKey, + Operator: v1.NodeSelectorOpIn, + Values: []string{node}, + }, + }, + }, + }, + }, + } } if boundToPVC != nil { diff --git a/staging/src/k8s.io/component-helpers/storage/volume/pv_helpers.go b/staging/src/k8s.io/component-helpers/storage/volume/pv_helpers.go index cf18af43a37..f927b72314a 100644 --- a/staging/src/k8s.io/component-helpers/storage/volume/pv_helpers.go +++ b/staging/src/k8s.io/component-helpers/storage/volume/pv_helpers.go @@ -340,22 +340,3 @@ func CheckAccessModes(claim *v1.PersistentVolumeClaim, volume *v1.PersistentVolu func claimToClaimKey(claim *v1.PersistentVolumeClaim) string { return fmt.Sprintf("%s/%s", claim.Namespace, claim.Name) } - -// GetVolumeNodeAffinity returns a VolumeNodeAffinity for given key and value. -func GetVolumeNodeAffinity(key string, value string) *v1.VolumeNodeAffinity { - return &v1.VolumeNodeAffinity{ - Required: &v1.NodeSelector{ - NodeSelectorTerms: []v1.NodeSelectorTerm{ - { - MatchExpressions: []v1.NodeSelectorRequirement{ - { - Key: key, - Operator: v1.NodeSelectorOpIn, - Values: []string{value}, - }, - }, - }, - }, - }, - } -} diff --git a/staging/src/k8s.io/component-helpers/storage/volume/pv_helpers_test.go b/staging/src/k8s.io/component-helpers/storage/volume/pv_helpers_test.go index 74badfd01ab..6482ecfa6fc 100644 --- a/staging/src/k8s.io/component-helpers/storage/volume/pv_helpers_test.go +++ b/staging/src/k8s.io/component-helpers/storage/volume/pv_helpers_test.go @@ -119,6 +119,25 @@ func TestDelayBindingMode(t *testing.T) { } } +// makeVolumeNodeAffinity returns a VolumeNodeAffinity for given key and value. +func makeNodeAffinity(key string, value string) *v1.VolumeNodeAffinity { + return &v1.VolumeNodeAffinity{ + Required: &v1.NodeSelector{ + NodeSelectorTerms: []v1.NodeSelectorTerm{ + { + MatchExpressions: []v1.NodeSelectorRequirement{ + { + Key: key, + Operator: v1.NodeSelectorOpIn, + Values: []string{value}, + }, + }, + }, + }, + }, + } +} + func TestFindMatchVolumeWithNode(t *testing.T) { volumes := []*v1.PersistentVolume{ makeTestVolume("local-small", "local001", "5G", true, nil), @@ -127,24 +146,24 @@ func TestFindMatchVolumeWithNode(t *testing.T) { }), makeTestVolume("affinity-pv", "affinity001", "100G", true, func(pv *v1.PersistentVolume) { pv.Spec.StorageClassName = "wait" - pv.Spec.NodeAffinity = GetVolumeNodeAffinity("key1", "value1") + pv.Spec.NodeAffinity = makeNodeAffinity("key1", "value1") }), makeTestVolume("affinity-pv2", "affinity002", "150G", true, func(pv *v1.PersistentVolume) { pv.Spec.StorageClassName = "wait" - pv.Spec.NodeAffinity = GetVolumeNodeAffinity("key1", "value1") + pv.Spec.NodeAffinity = makeNodeAffinity("key1", "value1") }), makeTestVolume("affinity-prebound", "affinity003", "100G", true, func(pv *v1.PersistentVolume) { pv.Spec.StorageClassName = "wait" pv.Spec.ClaimRef = &v1.ObjectReference{Name: "claim02", Namespace: "myns"} - pv.Spec.NodeAffinity = GetVolumeNodeAffinity("key1", "value1") + pv.Spec.NodeAffinity = makeNodeAffinity("key1", "value1") }), makeTestVolume("affinity-pv3", "affinity003", "200G", true, func(pv *v1.PersistentVolume) { pv.Spec.StorageClassName = "wait" - pv.Spec.NodeAffinity = GetVolumeNodeAffinity("key1", "value3") + pv.Spec.NodeAffinity = makeNodeAffinity("key1", "value3") }), makeTestVolume("affinity-pv4", "affinity004", "200G", false, func(pv *v1.PersistentVolume) { pv.Spec.StorageClassName = "wait" - pv.Spec.NodeAffinity = GetVolumeNodeAffinity("key1", "value4") + pv.Spec.NodeAffinity = makeNodeAffinity("key1", "value4") }), }