Skip to content

Commit 7d59ea8

Browse files
committed
Reorder conditions in FindMatchingVolume to avoid checking NodeAffinity
in trivial cases.
1 parent b6b494b commit 7d59ea8

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

pkg/controller/volume/persistentvolume/util/util.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,14 @@ func FindMatchingVolume(
211211
// Skip volumes in the excluded list
212212
continue
213213
}
214+
if volume.Spec.ClaimRef != nil && !IsVolumeBoundToClaim(volume, claim) {
215+
continue
216+
}
214217

215218
volumeQty := volume.Spec.Capacity[v1.ResourceStorage]
216-
219+
if volumeQty.Cmp(requestedQty) < 0 {
220+
continue
221+
}
217222
// filter out mismatching volumeModes
218223
if CheckVolumeModeMismatches(&claim.Spec, &volume.Spec) {
219224
continue
@@ -230,20 +235,15 @@ func FindMatchingVolume(
230235
if node != nil {
231236
// Scheduler path, check that the PV NodeAffinity
232237
// 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.
233240
err := volumeutil.CheckNodeAffinity(volume, node.Labels)
234241
if err != nil {
235242
nodeAffinityValid = false
236243
}
237244
}
238245

239246
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-
247247
// If PV node affinity is invalid, return no match.
248248
// This means the prebound PV (and therefore PVC)
249249
// is not suitable for this node.
@@ -263,7 +263,6 @@ func FindMatchingVolume(
263263

264264
// filter out:
265265
// - volumes in non-available phase
266-
// - volumes bound to another claim
267266
// - volumes whose labels don't match the claim's selector, if specified
268267
// - volumes in Class that is not requested
269268
// - volumes whose NodeAffinity does not match the node
@@ -273,8 +272,6 @@ func FindMatchingVolume(
273272
// them now has high chance of encountering unnecessary failures
274273
// due to API conflicts.
275274
continue
276-
} else if volume.Spec.ClaimRef != nil {
277-
continue
278275
} else if selector != nil && !selector.Matches(labels.Set(volume.Labels)) {
279276
continue
280277
}
@@ -293,11 +290,9 @@ func FindMatchingVolume(
293290
}
294291
}
295292

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
301296
}
302297
}
303298

test/utils/runners.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,12 +1385,11 @@ func CreatePodWithPersistentVolume(client clientset.Interface, namespace string,
13851385
createError = fmt.Errorf("error creating PV: %s", err)
13861386
return
13871387
}
1388-
// We need to update status separately, as creating persistentvolumes resets status to the default one
1389-
// (so with Status.Phase will be equal to PersistentVolumePhase).
1388+
// We need to update statuses separately, as creating pv/pvc resets status to the default one.
13901389
if _, err := client.CoreV1().PersistentVolumes().UpdateStatus(context.TODO(), pv, metav1.UpdateOptions{}); err != nil {
13911390
lock.Lock()
13921391
defer lock.Unlock()
1393-
createError = fmt.Errorf("error creating PV: %s", err)
1392+
createError = fmt.Errorf("error updating PV status: %s", err)
13941393
return
13951394
}
13961395

@@ -1400,6 +1399,12 @@ func CreatePodWithPersistentVolume(client clientset.Interface, namespace string,
14001399
createError = fmt.Errorf("error creating PVC: %s", err)
14011400
return
14021401
}
1402+
if _, err := client.CoreV1().PersistentVolumeClaims(namespace).UpdateStatus(context.TODO(), pvc, metav1.UpdateOptions{}); err != nil {
1403+
lock.Lock()
1404+
defer lock.Unlock()
1405+
createError = fmt.Errorf("error updating PVC status: %s", err)
1406+
return
1407+
}
14031408

14041409
// pod
14051410
pod := podTemplate.DeepCopy()

0 commit comments

Comments
 (0)