@@ -194,6 +194,11 @@ type MaxPDVolumeCountChecker struct {
194
194
maxVolumes int
195
195
pvInfo PersistentVolumeInfo
196
196
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
197
202
}
198
203
199
204
// VolumeFilter contains information on how to filter PD Volumes when checking PD Volume caps
@@ -212,16 +217,17 @@ type VolumeFilter struct {
212
217
// the maximum.
213
218
func NewMaxPDVolumeCountPredicate (filter VolumeFilter , maxVolumes int , pvInfo PersistentVolumeInfo , pvcInfo PersistentVolumeClaimInfo ) algorithm.FitPredicate {
214
219
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 ),
219
225
}
220
226
221
227
return c .predicate
222
228
}
223
229
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 {
225
231
for i := range volumes {
226
232
vol := & volumes [i ]
227
233
if id , ok := c .filter .FilterVolume (vol ); ok {
@@ -233,8 +239,9 @@ func (c *MaxPDVolumeCountChecker) filterVolumes(volumes []v1.Volume, namespace s
233
239
}
234
240
235
241
// 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 )
238
245
239
246
pvc , err := c .pvcInfo .GetPersistentVolumeClaimInfo (namespace , pvcName )
240
247
if err != nil || pvc == nil {
@@ -280,15 +287,8 @@ func (c *MaxPDVolumeCountChecker) predicate(pod *v1.Pod, meta interface{}, nodeI
280
287
return true , nil , nil
281
288
}
282
289
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
-
290
290
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 {
292
292
return false , nil , err
293
293
}
294
294
@@ -300,7 +300,7 @@ func (c *MaxPDVolumeCountChecker) predicate(pod *v1.Pod, meta interface{}, nodeI
300
300
// count unique volumes
301
301
existingVolumes := make (map [string ]bool )
302
302
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 {
304
304
return false , nil , err
305
305
}
306
306
}
0 commit comments