Skip to content

Commit

Permalink
Properly handle files and sockets in extra mounts
Browse files Browse the repository at this point in the history
Also allows manually specifying HostPathType, in case an unhandled type is needed

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
brandond committed Apr 12, 2024
1 parent 3fd8fe5 commit 35e0220
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions pkg/staticpod/staticpod.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,7 @@ func addVolumes(p *v1.Pod, src []string, volume typeVolume) {
}

func addExtraMounts(p *v1.Pod, extraMounts []string) {
var (
sourceType = v1.HostPathDirectoryOrCreate
)
var sourceType v1.HostPathType

for i, rawMount := range extraMounts {
mount := strings.Split(rawMount, ":")
Expand All @@ -350,14 +348,37 @@ func addExtraMounts(p *v1.Pod, extraMounts []string) {
case "rw":
ro = false
default:
logrus.Errorf("unknown mount option: %s encountered in extra mount %s for pod %s", mount[2], rawMount, p.Name)
logrus.Errorf("Unknown mount option: %s encountered in extra mount %s for pod %s", mount[2], rawMount, p.Name)
continue
}
case 4:
sourceType = v1.HostPathType(mount[3])
default:
logrus.Errorf("mount for pod %s %s was not valid", p.Name, rawMount)
logrus.Errorf("Extra mount for pod %s %s was not valid", p.Name, rawMount)
continue
}

// If the source type was not specified, try to auto-detect.
// Paths that cannot be stat-ed are handled as DirectoryOrCreate.
// Only sockets, directories, and files are supported for auto-detection.
if sourceType == v1.HostPathUnset {
if info, err := os.Stat(mount[0]); err != nil {
if !os.IsNotExist(err) {
logrus.Warnf("Failed to stat mount for pod %s %s: %v", p.Name, mount[0], err)
}
sourceType = v1.HostPathDirectoryOrCreate
} else {
switch {
case info.Mode().Type() == fs.ModeSocket:
sourceType = v1.HostPathSocket
case info.IsDir():
sourceType = v1.HostPathDirectory
default:
sourceType = v1.HostPathFile
}
}
}

name := fmt.Sprintf("%s-%d", extraMountPrefix, i)
p.Spec.Volumes = append(p.Spec.Volumes, v1.Volume{
Name: name,
Expand Down

0 comments on commit 35e0220

Please sign in to comment.