Skip to content

Commit 45305bf

Browse files
committed
apply override during extend
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent a91b45c commit 45305bf

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

loader/extends.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@ func applyServiceExtends(ctx context.Context, name string, services map[string]a
8181

8282
var (
8383
base any
84-
processor PostProcessor
84+
processor PostProcessor = NoopPostProcessor{}
8585
)
8686

8787
if file != nil {
8888
refFilename := file.(string)
8989
services, processor, err = getExtendsBaseFromFile(ctx, name, ref, filename, refFilename, opts, tracker)
90-
post = append(post, processor)
9190
if err != nil {
9291
return nil, err
9392
}
@@ -105,7 +104,7 @@ func applyServiceExtends(ctx context.Context, name string, services map[string]a
105104
}
106105

107106
// recursively apply `extends`
108-
base, err = applyServiceExtends(ctx, ref, services, opts, tracker, post...)
107+
base, err = applyServiceExtends(ctx, ref, services, opts, tracker, processor)
109108
if err != nil {
110109
return nil, err
111110
}

loader/loader.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,14 @@ func WithProfiles(profiles []string) func(*Options) {
260260
// PostProcessor is used to tweak compose model based on metadata extracted during yaml Unmarshal phase
261261
// that hardly can be implemented using go-yaml and mapstructure
262262
type PostProcessor interface {
263-
yaml.Unmarshaler
264-
265263
// Apply changes to compose model based on recorder metadata
266264
Apply(interface{}) error
267265
}
268266

267+
type NoopPostProcessor struct{}
268+
269+
func (NoopPostProcessor) Apply(interface{}) error { return nil }
270+
269271
// LoadConfigFiles ingests config files with ResourceLoader and returns config details with paths to local copies
270272
func LoadConfigFiles(ctx context.Context, configFiles []string, workingDir string, options ...func(*Options)) (*types.ConfigDetails, error) {
271273
if len(configFiles) < 1 {

loader/loader_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3917,3 +3917,65 @@ services:
39173917
assert.Equal(t, build.Provenance, "mode=max")
39183918
assert.Equal(t, build.SBOM, "true")
39193919
}
3920+
3921+
func TestOverrideMiddle(t *testing.T) {
3922+
pwd := t.TempDir()
3923+
base := filepath.Join(pwd, "base.yaml")
3924+
err := os.WriteFile(base, []byte(`
3925+
services:
3926+
base:
3927+
volumes:
3928+
- /foo:/foo
3929+
`), 0o700)
3930+
assert.NilError(t, err)
3931+
3932+
override := filepath.Join(pwd, "override.yaml")
3933+
err = os.WriteFile(override, []byte(`
3934+
services:
3935+
override:
3936+
extends:
3937+
file: ./base.yaml
3938+
service: base
3939+
volumes: !override
3940+
- /bar:/bar
3941+
`), 0o700)
3942+
assert.NilError(t, err)
3943+
3944+
compose := filepath.Join(pwd, "compose.yaml")
3945+
err = os.WriteFile(compose, []byte(`
3946+
name: test
3947+
services:
3948+
test:
3949+
image: test
3950+
extends:
3951+
file: ./override.yaml
3952+
service: override
3953+
volumes:
3954+
- /zot:/zot
3955+
`), 0o700)
3956+
assert.NilError(t, err)
3957+
3958+
project, err := LoadWithContext(context.TODO(), types.ConfigDetails{
3959+
WorkingDir: pwd,
3960+
ConfigFiles: []types.ConfigFile{
3961+
{Filename: compose},
3962+
},
3963+
})
3964+
assert.NilError(t, err)
3965+
test := project.Services["test"]
3966+
assert.Equal(t, len(test.Volumes), 2)
3967+
assert.DeepEqual(t, test.Volumes, []types.ServiceVolumeConfig{
3968+
{
3969+
Type: "bind",
3970+
Source: "/bar",
3971+
Target: "/bar",
3972+
Bind: &types.ServiceVolumeBind{CreateHostPath: true},
3973+
},
3974+
{
3975+
Type: "bind",
3976+
Source: "/zot",
3977+
Target: "/zot",
3978+
Bind: &types.ServiceVolumeBind{CreateHostPath: true},
3979+
},
3980+
})
3981+
}

0 commit comments

Comments
 (0)