Skip to content

Commit

Permalink
resolveRelativePaths can run with non-canonical model
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Jul 22, 2024
1 parent c69a570 commit cf5a3d4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion loader/extends.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func getExtendsBaseFromFile(
)
}

// Attempt to make a canonical model so ResolveRelativePaths can operate on source:target short syntaxes
// Attempt to make a canonical model so ResolveRelativePaths can operate on source:target short syntax
source, err = transform.Canonical(source, true)
if err != nil {
return nil, nil, err
Expand Down
17 changes: 17 additions & 0 deletions loader/extends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,20 @@ services:
assert.NilError(t, err)
assert.Check(t, p.Services["test"].Command == nil)
}

func TestExtendsWithInterpolation(t *testing.T) {
yaml := `
name: test-extends-with-interpolation
services:
test:
extends: { file: testdata/extends/interpolated.yaml, service: foo }
`
p, err := Load(types.ConfigDetails{
ConfigFiles: []types.ConfigFile{{
Content: []byte(yaml),
Filename: "-",
}},
})
assert.NilError(t, err)
assert.Check(t, p.Services["test"].Volumes[0].Source == "/dev/null")
}
5 changes: 5 additions & 0 deletions loader/testdata/extends/interpolated.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
foo:
image: bash
volumes:
- ${SOURCE:-/dev/null}:/tmp/foo:ro
30 changes: 18 additions & 12 deletions paths/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,30 @@ func (r *relativePathsResolver) absPath(value any) (any, error) {
}
return v, nil
}

return nil, fmt.Errorf("unexpected type %T", value)
}

func (r *relativePathsResolver) absVolumeMount(a any) (any, error) {
vol := a.(map[string]any)
if vol["type"] != types.VolumeTypeBind {
switch vol := a.(type) {
case map[string]any:
if vol["type"] != types.VolumeTypeBind {
return vol, nil
}
src, ok := vol["source"]
if !ok {
return nil, errors.New(`invalid mount config for type "bind": field Source must not be empty`)
}
abs, err := r.maybeUnixPath(src.(string))
if err != nil {
return nil, err
}
vol["source"] = abs
return vol, nil
default:
// not using canonical format, skip
return a, nil
}
src, ok := vol["source"]
if !ok {
return nil, errors.New(`invalid mount config for type "bind": field Source must not be empty`)
}
abs, err := r.maybeUnixPath(src.(string))
if err != nil {
return nil, err
}
vol["source"] = abs
return vol, nil
}

func (r *relativePathsResolver) volumeDriverOpts(a any) (any, error) {
Expand Down

0 comments on commit cf5a3d4

Please sign in to comment.