@@ -211,9 +211,14 @@ func FindMatchingVolume(
211
211
// Skip volumes in the excluded list
212
212
continue
213
213
}
214
+ if volume .Spec .ClaimRef != nil && ! IsVolumeBoundToClaim (volume , claim ) {
215
+ continue
216
+ }
214
217
215
218
volumeQty := volume .Spec .Capacity [v1 .ResourceStorage ]
216
-
219
+ if volumeQty .Cmp (requestedQty ) < 0 {
220
+ continue
221
+ }
217
222
// filter out mismatching volumeModes
218
223
if CheckVolumeModeMismatches (& claim .Spec , & volume .Spec ) {
219
224
continue
@@ -230,20 +235,15 @@ func FindMatchingVolume(
230
235
if node != nil {
231
236
// Scheduler path, check that the PV NodeAffinity
232
237
// is satisfied by the node
238
+ // volumeutil.CheckNodeAffinity is the most expensive call in this loop.
239
+ // We should check cheaper conditions first or consider optimizing this function.
233
240
err := volumeutil .CheckNodeAffinity (volume , node .Labels )
234
241
if err != nil {
235
242
nodeAffinityValid = false
236
243
}
237
244
}
238
245
239
246
if IsVolumeBoundToClaim (volume , claim ) {
240
- // this claim and volume are pre-bound; return
241
- // the volume if the size request is satisfied,
242
- // otherwise continue searching for a match
243
- if volumeQty .Cmp (requestedQty ) < 0 {
244
- continue
245
- }
246
-
247
247
// If PV node affinity is invalid, return no match.
248
248
// This means the prebound PV (and therefore PVC)
249
249
// is not suitable for this node.
@@ -263,7 +263,6 @@ func FindMatchingVolume(
263
263
264
264
// filter out:
265
265
// - volumes in non-available phase
266
- // - volumes bound to another claim
267
266
// - volumes whose labels don't match the claim's selector, if specified
268
267
// - volumes in Class that is not requested
269
268
// - volumes whose NodeAffinity does not match the node
@@ -273,8 +272,6 @@ func FindMatchingVolume(
273
272
// them now has high chance of encountering unnecessary failures
274
273
// due to API conflicts.
275
274
continue
276
- } else if volume .Spec .ClaimRef != nil {
277
- continue
278
275
} else if selector != nil && ! selector .Matches (labels .Set (volume .Labels )) {
279
276
continue
280
277
}
@@ -293,11 +290,9 @@ func FindMatchingVolume(
293
290
}
294
291
}
295
292
296
- if volumeQty .Cmp (requestedQty ) >= 0 {
297
- if smallestVolume == nil || smallestVolumeQty .Cmp (volumeQty ) > 0 {
298
- smallestVolume = volume
299
- smallestVolumeQty = volumeQty
300
- }
293
+ if smallestVolume == nil || smallestVolumeQty .Cmp (volumeQty ) > 0 {
294
+ smallestVolume = volume
295
+ smallestVolumeQty = volumeQty
301
296
}
302
297
}
303
298
0 commit comments