Skip to content

Commit

Permalink
extending a service, dependencies must be excluded
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Dec 11, 2024
1 parent 4afb43c commit 20738c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 7 additions & 1 deletion loader/extends.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import (
"github.com/compose-spec/compose-go/v2/types"
)

// as we use another service definition by `extends`, we must exclude attributes which creates dependency to another service
// see https://github.com/compose-spec/compose-spec/blob/main/05-services.md#restrictions
var exclusions = []string{"extends", "depends_on", "volumes_from"}

func ApplyExtends(ctx context.Context, dict map[string]any, opts *Options, tracker *cycleTracker, post ...PostProcessor) error {
a, ok := dict["services"]
if !ok {
Expand Down Expand Up @@ -123,7 +127,9 @@ func applyServiceExtends(ctx context.Context, name string, services map[string]a
if err != nil {
return nil, err
}
delete(merged, "extends")
for _, exclusion := range exclusions {
delete(merged, exclusion)
}
services[name] = merged
return merged, nil
}
Expand Down
7 changes: 4 additions & 3 deletions loader/extends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ services:
file: ./testdata/extends/depends_on.yaml
service: with_volumes_from
`,
wantErr: `service "bar" depends on undefined service "zot"`,
},
{
name: "depends_on",
Expand All @@ -334,7 +333,6 @@ services:
file: ./testdata/extends/depends_on.yaml
service: with_depends_on
`,
wantErr: `service "bar" depends on undefined service "zot"`,
},
{
name: "shared ipc",
Expand Down Expand Up @@ -369,8 +367,11 @@ services:
Content: []byte(tt.yaml),
}},
})
if tt.wantErr == "" {
assert.NilError(t, err)
return
}
assert.ErrorContains(t, err, tt.wantErr)

// Do the same but with a local `zot` service matching the imported reference
_, err = LoadWithContext(context.Background(), types.ConfigDetails{
ConfigFiles: []types.ConfigFile{{
Expand Down

0 comments on commit 20738c5

Please sign in to comment.