From fe817674abac9a7a2a8ac53d9f2debae9c9d36be Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Tue, 16 Aug 2016 12:24:18 -0400 Subject: [PATCH 1/2] Don't bind pre-bound pvc & pv if size request not satisfied --- pkg/controller/volume/persistentvolume/index.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/controller/volume/persistentvolume/index.go b/pkg/controller/volume/persistentvolume/index.go index 60699e6e653..55610721d9c 100644 --- a/pkg/controller/volume/persistentvolume/index.go +++ b/pkg/controller/volume/persistentvolume/index.go @@ -117,9 +117,14 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *api.PersistentVo // the claim. for _, volume := range volumes { if isVolumeBoundToClaim(volume, claim) { - // this claim and volume are bound; return it, - // whether the claim is prebound or for volumes - // intended for dynamic provisioning v1 + // this claim and volume are pre-bound; return + // the volume if the size request is satisfied, + // otherwise leave the claim pending + volumeQty := volume.Spec.Capacity[api.ResourceStorage] + volumeSize := volumeQty.Value() + if volumeSize < requestedSize { + return nil, nil + } return volume, nil } From 6486576f56136d09dc74080470c8a63f052ef767 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Wed, 17 Aug 2016 10:42:52 -0400 Subject: [PATCH 2/2] continue searching on bad size and add tests for bad size&mode --- .../volume/persistentvolume/index.go | 4 ++-- .../volume/persistentvolume/index_test.go | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pkg/controller/volume/persistentvolume/index.go b/pkg/controller/volume/persistentvolume/index.go index 55610721d9c..e71fc4c6ccf 100644 --- a/pkg/controller/volume/persistentvolume/index.go +++ b/pkg/controller/volume/persistentvolume/index.go @@ -119,11 +119,11 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *api.PersistentVo if isVolumeBoundToClaim(volume, claim) { // this claim and volume are pre-bound; return // the volume if the size request is satisfied, - // otherwise leave the claim pending + // otherwise continue searching for a match volumeQty := volume.Spec.Capacity[api.ResourceStorage] volumeSize := volumeQty.Value() if volumeSize < requestedSize { - return nil, nil + continue } return volume, nil } diff --git a/pkg/controller/volume/persistentvolume/index_test.go b/pkg/controller/volume/persistentvolume/index_test.go index c055c3fa3cb..1d985335e2b 100644 --- a/pkg/controller/volume/persistentvolume/index_test.go +++ b/pkg/controller/volume/persistentvolume/index_test.go @@ -610,11 +610,16 @@ func TestFindingPreboundVolumes(t *testing.T) { pv1 := testVolume("pv1", "1Gi") pv5 := testVolume("pv5", "5Gi") pv8 := testVolume("pv8", "8Gi") + pvBadSize := testVolume("pvBadSize", "1Mi") + pvBadMode := testVolume("pvBadMode", "1Gi") + pvBadMode.Spec.AccessModes = []api.PersistentVolumeAccessMode{api.ReadOnlyMany} index := newPersistentVolumeOrderedIndex() index.store.Add(pv1) index.store.Add(pv5) index.store.Add(pv8) + index.store.Add(pvBadSize) + index.store.Add(pvBadMode) // expected exact match on size volume, _ := index.findBestMatchForClaim(claim) @@ -636,6 +641,22 @@ func TestFindingPreboundVolumes(t *testing.T) { if volume.Name != pv8.Name { t.Errorf("Expected %s but got volume %s instead", pv8.Name, volume.Name) } + + // pretend the volume with too small a size is pre-bound to the claim. should get the exact match. + pv8.Spec.ClaimRef = nil + pvBadSize.Spec.ClaimRef = claimRef + volume, _ = index.findBestMatchForClaim(claim) + if volume.Name != pv1.Name { + t.Errorf("Expected %s but got volume %s instead", pv1.Name, volume.Name) + } + + // pretend the volume without the right access mode is pre-bound to the claim. should get the exact match. + pvBadSize.Spec.ClaimRef = nil + pvBadMode.Spec.ClaimRef = claimRef + volume, _ = index.findBestMatchForClaim(claim) + if volume.Name != pv1.Name { + t.Errorf("Expected %s but got volume %s instead", pv1.Name, volume.Name) + } } // byCapacity is used to order volumes by ascending storage size