Skip to content

Commit 08d02e5

Browse files
Avesh Agarwaldeads2k
Avesh Agarwal
authored andcommitted
UPSTREAM: 53989: Remove repeated random string generations in scheduler volume predicate
:100644 100644 6131acf48f... d19a0a035e... M plugin/pkg/scheduler/algorithm/predicates/predicates.go
1 parent dfcc111 commit 08d02e5

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

plugin/pkg/scheduler/algorithm/predicates/predicates.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ type MaxPDVolumeCountChecker struct {
194194
maxVolumes int
195195
pvInfo PersistentVolumeInfo
196196
pvcInfo PersistentVolumeClaimInfo
197+
198+
// The string below is generated randomly during the struct's initialization.
199+
// It is used to prefix volumeID generated inside the predicate() method to
200+
// avoid conflicts with any real volume.
201+
randomVolumeIDPrefix string
197202
}
198203

199204
// VolumeFilter contains information on how to filter PD Volumes when checking PD Volume caps
@@ -212,16 +217,17 @@ type VolumeFilter struct {
212217
// the maximum.
213218
func NewMaxPDVolumeCountPredicate(filter VolumeFilter, maxVolumes int, pvInfo PersistentVolumeInfo, pvcInfo PersistentVolumeClaimInfo) algorithm.FitPredicate {
214219
c := &MaxPDVolumeCountChecker{
215-
filter: filter,
216-
maxVolumes: maxVolumes,
217-
pvInfo: pvInfo,
218-
pvcInfo: pvcInfo,
220+
filter: filter,
221+
maxVolumes: maxVolumes,
222+
pvInfo: pvInfo,
223+
pvcInfo: pvcInfo,
224+
randomVolumeIDPrefix: rand.String(32),
219225
}
220226

221227
return c.predicate
222228
}
223229

224-
func (c *MaxPDVolumeCountChecker) filterVolumes(volumes []v1.Volume, namespace string, randomPrefix string, filteredVolumes map[string]bool) error {
230+
func (c *MaxPDVolumeCountChecker) filterVolumes(volumes []v1.Volume, namespace string, filteredVolumes map[string]bool) error {
225231
for i := range volumes {
226232
vol := &volumes[i]
227233
if id, ok := c.filter.FilterVolume(vol); ok {
@@ -233,8 +239,9 @@ func (c *MaxPDVolumeCountChecker) filterVolumes(volumes []v1.Volume, namespace s
233239
}
234240

235241
// Until we know real ID of the volume use namespace/pvcName as substitute
236-
// With a random prefix so it can't conflict with existing volume ID.
237-
pvId := fmt.Sprintf("%s-%s/%s", randomPrefix, namespace, pvcName)
242+
// with a random prefix (calculated and stored inside 'c' during initialization)
243+
// to avoid conflicts with existing volume IDs.
244+
pvId := fmt.Sprintf("%s-%s/%s", c.randomVolumeIDPrefix, namespace, pvcName)
238245

239246
pvc, err := c.pvcInfo.GetPersistentVolumeClaimInfo(namespace, pvcName)
240247
if err != nil || pvc == nil {
@@ -280,15 +287,8 @@ func (c *MaxPDVolumeCountChecker) predicate(pod *v1.Pod, meta interface{}, nodeI
280287
return true, nil, nil
281288
}
282289

283-
// randomPrefix is a prefix of auxiliary volume IDs when we don't know the
284-
// real volume ID, e.g. because the corresponding PV or PVC was deleted. It
285-
// is random to avoid conflicts with real volume IDs and it needs to be
286-
// stable in whole predicate() call so a deleted PVC used by two pods is
287-
// counted as one volume and not as two.
288-
randomPrefix := rand.String(32)
289-
290290
newVolumes := make(map[string]bool)
291-
if err := c.filterVolumes(pod.Spec.Volumes, pod.Namespace, randomPrefix, newVolumes); err != nil {
291+
if err := c.filterVolumes(pod.Spec.Volumes, pod.Namespace, newVolumes); err != nil {
292292
return false, nil, err
293293
}
294294

@@ -300,7 +300,7 @@ func (c *MaxPDVolumeCountChecker) predicate(pod *v1.Pod, meta interface{}, nodeI
300300
// count unique volumes
301301
existingVolumes := make(map[string]bool)
302302
for _, existingPod := range nodeInfo.Pods() {
303-
if err := c.filterVolumes(existingPod.Spec.Volumes, existingPod.Namespace, randomPrefix, existingVolumes); err != nil {
303+
if err := c.filterVolumes(existingPod.Spec.Volumes, existingPod.Namespace, existingVolumes); err != nil {
304304
return false, nil, err
305305
}
306306
}

0 commit comments

Comments
 (0)