@@ -277,18 +277,6 @@ func makeMounts(pod *v1.Pod, podDir string, container *v1.Container, hostName, h
277
277
mounts := []kubecontainer.Mount {}
278
278
var cleanupAction func ()
279
279
for i , mount := range container .VolumeMounts {
280
- // Check if the mount is referencing an OCI volume
281
- if imageVolumes != nil && utilfeature .DefaultFeatureGate .Enabled (features .ImageVolume ) {
282
- if image , ok := imageVolumes [mount .Name ]; ok {
283
- mounts = append (mounts , kubecontainer.Mount {
284
- Name : mount .Name ,
285
- ContainerPath : mount .MountPath ,
286
- Image : image ,
287
- })
288
- continue
289
- }
290
- }
291
-
292
280
// do not mount /etc/hosts if container is already mounting on the path
293
281
mountEtcHostsFile = mountEtcHostsFile && (mount .MountPath != etcHostsPath )
294
282
vol , ok := podVolumes [mount .Name ]
@@ -306,69 +294,82 @@ func makeMounts(pod *v1.Pod, podDir string, container *v1.Container, hostName, h
306
294
vol .SELinuxLabeled = true
307
295
relabelVolume = true
308
296
}
309
- hostPath , err := volumeutil .GetPath (vol .Mounter )
310
- if err != nil {
311
- return nil , cleanupAction , err
312
- }
313
297
314
- subPath := mount .SubPath
315
- if mount .SubPathExpr != "" {
316
- subPath , err = kubecontainer .ExpandContainerVolumeMounts (mount , expandEnvs )
298
+ var (
299
+ hostPath string
300
+ image * runtimeapi.ImageSpec
301
+ err error
302
+ )
317
303
304
+ if imageVolumes != nil && utilfeature .DefaultFeatureGate .Enabled (features .ImageVolume ) {
305
+ image = imageVolumes [mount .Name ]
306
+ }
307
+
308
+ if image == nil {
309
+ hostPath , err = volumeutil .GetPath (vol .Mounter )
318
310
if err != nil {
319
311
return nil , cleanupAction , err
320
312
}
321
- }
322
313
323
- if subPath != "" {
324
- if utilfs .IsAbs (subPath ) {
325
- return nil , cleanupAction , fmt .Errorf ("error SubPath `%s` must not be an absolute path" , subPath )
326
- }
314
+ subPath := mount .SubPath
315
+ if mount .SubPathExpr != "" {
316
+ subPath , err = kubecontainer .ExpandContainerVolumeMounts (mount , expandEnvs )
327
317
328
- err = volumevalidation . ValidatePathNoBacksteps ( subPath )
329
- if err != nil {
330
- return nil , cleanupAction , fmt . Errorf ( "unable to provision SubPath `%s`: %v" , subPath , err )
318
+ if err != nil {
319
+ return nil , cleanupAction , err
320
+ }
331
321
}
332
322
333
- volumePath := hostPath
334
- hostPath = filepath .Join (volumePath , subPath )
335
-
336
- if subPathExists , err := hu .PathExists (hostPath ); err != nil {
337
- klog .ErrorS (nil , "Could not determine if subPath exists, will not attempt to change its permissions" , "path" , hostPath )
338
- } else if ! subPathExists {
339
- // Create the sub path now because if it's auto-created later when referenced, it may have an
340
- // incorrect ownership and mode. For example, the sub path directory must have at least g+rwx
341
- // when the pod specifies an fsGroup, and if the directory is not created here, Docker will
342
- // later auto-create it with the incorrect mode 0750
343
- // Make extra care not to escape the volume!
344
- perm , err := hu .GetMode (volumePath )
323
+ if subPath != "" {
324
+ if utilfs .IsAbs (subPath ) {
325
+ return nil , cleanupAction , fmt .Errorf ("error SubPath `%s` must not be an absolute path" , subPath )
326
+ }
327
+
328
+ err = volumevalidation .ValidatePathNoBacksteps (subPath )
345
329
if err != nil {
346
- return nil , cleanupAction , err
330
+ return nil , cleanupAction , fmt .Errorf ("unable to provision SubPath `%s`: %w" , subPath , err )
331
+ }
332
+
333
+ volumePath := hostPath
334
+ hostPath = filepath .Join (volumePath , subPath )
335
+
336
+ if subPathExists , err := hu .PathExists (hostPath ); err != nil {
337
+ klog .ErrorS (nil , "Could not determine if subPath exists, will not attempt to change its permissions" , "path" , hostPath )
338
+ } else if ! subPathExists {
339
+ // Create the sub path now because if it's auto-created later when referenced, it may have an
340
+ // incorrect ownership and mode. For example, the sub path directory must have at least g+rwx
341
+ // when the pod specifies an fsGroup, and if the directory is not created here, Docker will
342
+ // later auto-create it with the incorrect mode 0750
343
+ // Make extra care not to escape the volume!
344
+ perm , err := hu .GetMode (volumePath )
345
+ if err != nil {
346
+ return nil , cleanupAction , err
347
+ }
348
+ if err := subpather .SafeMakeDir (subPath , volumePath , perm ); err != nil {
349
+ // Don't pass detailed error back to the user because it could give information about host filesystem
350
+ klog .ErrorS (err , "Failed to create subPath directory for volumeMount of the container" , "containerName" , container .Name , "volumeMountName" , mount .Name )
351
+ return nil , cleanupAction , fmt .Errorf ("failed to create subPath directory for volumeMount %q of container %q" , mount .Name , container .Name )
352
+ }
347
353
}
348
- if err := subpather .SafeMakeDir (subPath , volumePath , perm ); err != nil {
354
+ hostPath , cleanupAction , err = subpather .PrepareSafeSubpath (subpath.Subpath {
355
+ VolumeMountIndex : i ,
356
+ Path : hostPath ,
357
+ VolumeName : vol .InnerVolumeSpecName ,
358
+ VolumePath : volumePath ,
359
+ PodDir : podDir ,
360
+ ContainerName : container .Name ,
361
+ })
362
+ if err != nil {
349
363
// Don't pass detailed error back to the user because it could give information about host filesystem
350
- klog .ErrorS (err , "Failed to create subPath directory for volumeMount of the container" , "containerName" , container .Name , "volumeMountName" , mount .Name )
351
- return nil , cleanupAction , fmt .Errorf ("failed to create subPath directory for volumeMount %q of container %q" , mount .Name , container .Name )
364
+ klog .ErrorS (err , "Failed to prepare subPath for volumeMount of the container" , "containerName" , container .Name , "volumeMountName" , mount .Name )
365
+ return nil , cleanupAction , fmt .Errorf ("failed to prepare subPath for volumeMount %q of container %q" , mount .Name , container .Name )
352
366
}
353
367
}
354
- hostPath , cleanupAction , err = subpather .PrepareSafeSubpath (subpath.Subpath {
355
- VolumeMountIndex : i ,
356
- Path : hostPath ,
357
- VolumeName : vol .InnerVolumeSpecName ,
358
- VolumePath : volumePath ,
359
- PodDir : podDir ,
360
- ContainerName : container .Name ,
361
- })
362
- if err != nil {
363
- // Don't pass detailed error back to the user because it could give information about host filesystem
364
- klog .ErrorS (err , "Failed to prepare subPath for volumeMount of the container" , "containerName" , container .Name , "volumeMountName" , mount .Name )
365
- return nil , cleanupAction , fmt .Errorf ("failed to prepare subPath for volumeMount %q of container %q" , mount .Name , container .Name )
366
- }
367
- }
368
368
369
- // Docker Volume Mounts fail on Windows if it is not of the form C:/
370
- if volumeutil .IsWindowsLocalPath (runtime .GOOS , hostPath ) {
371
- hostPath = volumeutil .MakeAbsolutePath (runtime .GOOS , hostPath )
369
+ // Docker Volume Mounts fail on Windows if it is not of the form C:/
370
+ if hostPath != "" && volumeutil .IsWindowsLocalPath (runtime .GOOS , hostPath ) {
371
+ hostPath = volumeutil .MakeAbsolutePath (runtime .GOOS , hostPath )
372
+ }
372
373
}
373
374
374
375
containerPath := mount .MountPath
@@ -396,6 +397,7 @@ func makeMounts(pod *v1.Pod, podDir string, container *v1.Container, hostName, h
396
397
Name : mount .Name ,
397
398
ContainerPath : containerPath ,
398
399
HostPath : hostPath ,
400
+ Image : image ,
399
401
ReadOnly : mount .ReadOnly || mustMountRO ,
400
402
RecursiveReadOnly : rro ,
401
403
SELinuxRelabel : relabelVolume ,
0 commit comments