Skip to content

Commit d8ca21a

Browse files
ndeloofglours
authored andcommitted
service.networks must be merged as a mapping
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 00be3c8 commit d8ca21a

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

loader/override_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2020 The Compose Specification Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package loader
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/compose-spec/compose-go/v2/types"
24+
"gotest.tools/v3/assert"
25+
)
26+
27+
func TestOverrideNetworks(t *testing.T) {
28+
yaml := `
29+
name: test-override-networks
30+
services:
31+
test:
32+
image: test
33+
networks:
34+
- test_network
35+
36+
networks:
37+
test_network: {}
38+
`
39+
40+
override := `
41+
services:
42+
test:
43+
image: test
44+
networks:
45+
test_network:
46+
aliases:
47+
- alias1
48+
- alias2
49+
`
50+
_, err := LoadWithContext(context.Background(), types.ConfigDetails{
51+
ConfigFiles: []types.ConfigFile{
52+
{
53+
Filename: "base",
54+
Content: []byte(yaml),
55+
},
56+
{
57+
Filename: "override",
58+
Content: []byte(override),
59+
},
60+
},
61+
})
62+
assert.NilError(t, err)
63+
}

override/merge.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var mergeSpecials = map[tree.Path]merger{}
4040

4141
func init() {
4242
mergeSpecials["services.*.logging"] = mergeLogging
43+
mergeSpecials["services.*.networks"] = mergeNetworks
4344
mergeSpecials["services.*.command"] = override
4445
mergeSpecials["services.*.entrypoint"] = override
4546
mergeSpecials["services.*.healthcheck.test"] = override
@@ -106,6 +107,12 @@ func mergeLogging(c any, o any, p tree.Path) (any, error) {
106107
return other, nil
107108
}
108109

110+
func mergeNetworks(c any, o any, path tree.Path) (any, error) {
111+
right := convertIntoMapping(c)
112+
left := convertIntoMapping(o)
113+
return mergeMappings(right, left, path)
114+
}
115+
109116
// environment must be first converted into yaml sequence syntax so we can append
110117
func mergeEnvironment(c any, o any, _ tree.Path) (any, error) {
111118
right := convertIntoSequence(c)
@@ -144,6 +151,20 @@ func mergeUlimit(_ any, o any, p tree.Path) (any, error) {
144151
return o, nil
145152
}
146153

154+
func convertIntoMapping(a any) map[string]any {
155+
switch v := a.(type) {
156+
case map[string]any:
157+
return v
158+
case []any:
159+
converted := map[string]any{}
160+
for _, s := range v {
161+
converted[s.(string)] = nil
162+
}
163+
return converted
164+
}
165+
return nil
166+
}
167+
147168
func override(_ any, other any, _ tree.Path) (any, error) {
148169
return other, nil
149170
}

0 commit comments

Comments
 (0)