Skip to content

Commit

Permalink
Merge pull request #2588 from norio-nomura/support-template-in-mounts
Browse files Browse the repository at this point in the history
limayaml: support template expansion in `.mounts[].location` and `.mounts[].mountPoint`
  • Loading branch information
jandubois authored Sep 5, 2024
2 parents 2eb8b2f + 59e7c0e commit 536f375
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ memory: null
disk: null

# Expose host directories to the guest, the mount point might be accessible from all UIDs in the guest
# "location" can use these template variables: {{.Home}}, {{.Dir}}, {{.Name}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
# "mountPoint" can use these template variables: {{.Home}}, {{.Name}}, {{.UID}}, {{.User}}, and {{.Param.Key}}
# 🟢 Builtin default: null (Mount nothing)
# 🔵 This file: Mount the home as read-only, /tmp/lima as writable
mounts:
Expand Down
10 changes: 10 additions & 0 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,16 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
mounts := make([]Mount, 0, len(d.Mounts)+len(y.Mounts)+len(o.Mounts))
location := make(map[string]int)
for _, mount := range append(append(d.Mounts, y.Mounts...), o.Mounts...) {
if out, err := executeHostTemplate(mount.Location, instDir, y.Param); err == nil {
mount.Location = out.String()
} else {
logrus.WithError(err).Warnf("Couldn't process mount location %q as a template", mount.Location)
}
if out, err := executeGuestTemplate(mount.MountPoint, instDir, y.Param); err == nil {
mount.MountPoint = out.String()
} else {
logrus.WithError(err).Warnf("Couldn't process mount point %q as a template", mount.MountPoint)
}
if i, ok := location[mount.Location]; ok {
if mount.SSHFS.Cache != nil {
mounts[i].SSHFS.Cache = mount.SSHFS.Cache
Expand Down
12 changes: 12 additions & 0 deletions pkg/limayaml/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func TestFillDefault(t *testing.T) {
},
Mounts: []Mount{
{Location: "/tmp"},
{Location: "{{.Dir}}/{{.Param.ONE}}", MountPoint: "/mnt/{{.Param.ONE}}"},
},
MountType: ptr.Of(NINEP),
Provision: []Provision{
Expand Down Expand Up @@ -214,6 +215,17 @@ func TestFillDefault(t *testing.T) {
expect.Mounts[0].NineP.Cache = ptr.Of(Default9pCacheForRO)
expect.Mounts[0].Virtiofs.QueueSize = nil
// Only missing Mounts field is Writable, and the default value is also the null value: false
expect.Mounts[1].Location = fmt.Sprintf("%s/%s", instDir, y.Param["ONE"])
expect.Mounts[1].MountPoint = fmt.Sprintf("/mnt/%s", y.Param["ONE"])
expect.Mounts[1].Writable = ptr.Of(false)
expect.Mounts[1].SSHFS.Cache = ptr.Of(true)
expect.Mounts[1].SSHFS.FollowSymlinks = ptr.Of(false)
expect.Mounts[1].SSHFS.SFTPDriver = ptr.Of("")
expect.Mounts[1].NineP.SecurityModel = ptr.Of(Default9pSecurityModel)
expect.Mounts[1].NineP.ProtocolVersion = ptr.Of(Default9pProtocolVersion)
expect.Mounts[1].NineP.Msize = ptr.Of(Default9pMsize)
expect.Mounts[1].NineP.Cache = ptr.Of(Default9pCacheForRO)
expect.Mounts[1].Virtiofs.QueueSize = nil

expect.MountType = ptr.Of(NINEP)

Expand Down
10 changes: 10 additions & 0 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,16 @@ func ValidateParamIsUsed(y *LimaYAML) error {
break
}
}
for _, p := range y.Mounts {
if re.MatchString(p.Location) {
keyIsUsed = true
break
}
if re.MatchString(p.MountPoint) {
keyIsUsed = true
break
}
}
if !keyIsUsed {
return fmt.Errorf("field `param` key %q is not used in any provision, probe, copyToHost, or portForward", key)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/limayaml/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func TestValidateParamIsUsed(t *testing.T) {
assert.Error(t, err, "field `param` key \"name\" is not used in any provision, probe, copyToHost, or portForward")

fieldsUsingParam := []string{
`mounts: [{"location": "/tmp/{{ .Param.name }}"}]`,
`mounts: [{"location": "/tmp", mountPoint: "/tmp/{{ .Param.name }}"}]`,
`provision: [{"script": "echo {{ .Param.name }}"}]`,
`probes: [{"script": "echo {{ .Param.name }}"}]`,
`copyToHost: [{"guest": "/tmp/{{ .Param.name }}", "host": "/tmp"}]`,
Expand All @@ -55,6 +57,8 @@ func TestValidateParamIsUsed(t *testing.T) {
rootfulYaml := `param:
rootful: true`
fieldsUsingIfParamRootfulTrue := []string{
`mounts: [{"location": "/tmp/{{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}", "mountPoint": "/tmp"}]`,
`mounts: [{"location": "/tmp", "mountPoint": "/tmp/{{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}"}]`,
`provision: [{"script": "echo {{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}"}]`,
`probes: [{"script": "echo {{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}"}]`,
`copyToHost: [{"guest": "/tmp/{{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}", "host": "/tmp"}]`,
Expand Down

0 comments on commit 536f375

Please sign in to comment.