Skip to content

Commit 1592cda

Browse files
committed
expand additional context relative paths
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 298fa92 commit 1592cda

File tree

4 files changed

+69
-8
lines changed

4 files changed

+69
-8
lines changed

loader/full-struct_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
5757
Target: "foo",
5858
Network: "foo",
5959
CacheFrom: []string{"foo", "bar"},
60-
AdditionalContexts: map[string]*string{"foo": strPtr("/bar")},
60+
AdditionalContexts: types.Mapping{"foo": "/bar"},
6161
Labels: map[string]string{"FOO": "BAR"},
6262
Secrets: []types.ServiceSecretConfig{
6363
{

loader/normalize.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,23 @@ func Normalize(project *types.Project, resolvePaths bool) error {
7575
if s.Build.Dockerfile == "" && s.Build.DockerfileInline == "" {
7676
s.Build.Dockerfile = "Dockerfile"
7777
}
78-
localContext := absPath(project.WorkingDir, s.Build.Context)
79-
if _, err := os.Stat(localContext); err == nil {
80-
if resolvePaths {
78+
if resolvePaths {
79+
// Build context might be a remote http/git context. Unfortunately supported "remote"
80+
// syntax is highly ambiguous in moby/moby and not defined by compose-spec,
81+
// so let's assume runtime will check
82+
localContext := absPath(project.WorkingDir, s.Build.Context)
83+
if _, err := os.Stat(localContext); err == nil {
8184
s.Build.Context = localContext
8285
}
83-
// } else {
84-
// might be a remote http/git context. Unfortunately supported "remote" syntax is highly ambiguous
85-
// in moby/moby and not defined by compose-spec, so let's assume runtime will check
86+
for name, path := range s.Build.AdditionalContexts {
87+
if strings.Contains(path, "://") { // `docker-image://` or any builder specific context type
88+
continue
89+
}
90+
path = absPath(project.WorkingDir, path)
91+
if _, err := os.Stat(path); err == nil {
92+
s.Build.AdditionalContexts[name] = path
93+
}
94+
}
8695
}
8796
s.Build.Args = s.Build.Args.Resolve(fn)
8897
}

loader/normalize_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,55 @@ networks:
241241
assert.NilError(t, err)
242242
assert.Equal(t, expected, string(marshal))
243243
}
244+
245+
func TestNormalizeAdditionalContexts(t *testing.T) {
246+
project := types.Project{
247+
Name: "myProject",
248+
Services: types.Services{
249+
types.ServiceConfig{
250+
Name: "test",
251+
Build: &types.BuildConfig{
252+
Context: ".",
253+
Dockerfile: "Dockerfile",
254+
AdditionalContexts: map[string]string{
255+
"image": "docker-image://foo",
256+
"oci": "oci-layout://foo",
257+
"abs_path": "/tmp",
258+
"rel_path": "./testdata",
259+
},
260+
},
261+
},
262+
},
263+
}
264+
265+
absCwd, _ := filepath.Abs(".")
266+
expected := types.Project{
267+
Name: "myProject",
268+
Services: types.Services{
269+
types.ServiceConfig{
270+
Name: "test",
271+
Build: &types.BuildConfig{
272+
Context: absCwd,
273+
Dockerfile: "Dockerfile",
274+
AdditionalContexts: map[string]string{
275+
"image": "docker-image://foo",
276+
"oci": "oci-layout://foo",
277+
"abs_path": "/tmp",
278+
"rel_path": filepath.Join(absCwd, "testdata"),
279+
},
280+
},
281+
Networks: map[string]*types.ServiceNetworkConfig{
282+
"default": nil,
283+
},
284+
},
285+
},
286+
Networks: types.Networks{"default": types.NetworkConfig{
287+
Name: "myProject_default",
288+
}},
289+
WorkingDir: absCwd,
290+
ComposeFiles: []string{},
291+
}
292+
err := Normalize(&project, true)
293+
assert.NilError(t, err)
294+
assert.DeepEqual(t, expected, project)
295+
}

types/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ type BuildConfig struct {
303303
CacheFrom StringList `mapstructure:"cache_from" yaml:"cache_from,omitempty" json:"cache_from,omitempty"`
304304
CacheTo StringList `mapstructure:"cache_to" yaml:"cache_to,omitempty" json:"cache_to,omitempty"`
305305
NoCache bool `mapstructure:"no_cache" yaml:"no_cache,omitempty" json:"no_cache,omitempty"`
306-
AdditionalContexts MappingWithEquals `mapstructure:"additional_contexts" yaml:"additional_contexts,omitempty" json:"additional_contexts,omitempty"`
306+
AdditionalContexts Mapping `mapstructure:"additional_contexts" yaml:"additional_contexts,omitempty" json:"additional_contexts,omitempty"`
307307
Pull bool `mapstructure:"pull" yaml:"pull,omitempty" json:"pull,omitempty"`
308308
ExtraHosts HostsList `mapstructure:"extra_hosts" yaml:"extra_hosts,omitempty" json:"extra_hosts,omitempty"`
309309
Isolation string `yaml:",omitempty" json:"isolation,omitempty"`

0 commit comments

Comments
 (0)