Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit f426f9f

Browse files
rifelpetPeter Rifel
authored andcommitted
Add support for both image and build in v2 files
Previously one was getting removed due to the v1 logic getting applied. Resolves #471 Signed-off-by: Peter Rifel <[email protected]>
1 parent 01ff892 commit f426f9f

File tree

3 files changed

+89
-41
lines changed

3 files changed

+89
-41
lines changed

config/merge.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,6 @@ func readEnvFile(resourceLookup ResourceLookup, inFile string, serviceData RawSe
236236

237237
func mergeConfig(baseService, serviceData RawService) RawService {
238238
for k, v := range serviceData {
239-
// Image and build are mutually exclusive in merge
240-
if k == "image" {
241-
delete(baseService, "build")
242-
} else if k == "build" {
243-
delete(baseService, "image")
244-
}
245239
existing, ok := baseService[k]
246240
if ok {
247241
baseService[k] = merge(existing, v)

config/merge_test.go

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ services:
116116
}
117117
}
118118

119-
func TestExtendBuildOverImage(t *testing.T) {
120-
_, configV1, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(`
119+
func TestExtendBuildOverImageV1(t *testing.T) {
120+
_, config, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(`
121121
parent:
122122
image: foo
123123
child:
@@ -129,7 +129,24 @@ child:
129129
t.Fatal(err)
130130
}
131131

132-
_, configV2, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(`
132+
parent := config["parent"]
133+
child := config["child"]
134+
135+
if parent.Image != "foo" {
136+
t.Fatal("Invalid image", parent.Image)
137+
}
138+
139+
if child.Build.Context != "." {
140+
t.Fatal("Invalid build", child.Build)
141+
}
142+
143+
if child.Image != "" {
144+
t.Fatal("Invalid image", child.Image)
145+
}
146+
}
147+
148+
func TestExtendBuildOverImageV2(t *testing.T) {
149+
_, config, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(`
133150
version: '2'
134151
services:
135152
parent:
@@ -144,26 +161,24 @@ services:
144161
t.Fatal(err)
145162
}
146163

147-
for _, config := range []map[string]*ServiceConfig{configV1, configV2} {
148-
parent := config["parent"]
149-
child := config["child"]
164+
parent := config["parent"]
165+
child := config["child"]
150166

151-
if parent.Image != "foo" {
152-
t.Fatal("Invalid image", parent.Image)
153-
}
167+
if parent.Image != "foo" {
168+
t.Fatal("Invalid image", parent.Image)
169+
}
154170

155-
if child.Build.Context != "." {
156-
t.Fatal("Invalid build", child.Build)
157-
}
171+
if child.Build.Context != "." {
172+
t.Fatal("Invalid build", child.Build)
173+
}
158174

159-
if child.Image != "" {
160-
t.Fatal("Invalid image", child.Image)
161-
}
175+
if child.Image != "foo" {
176+
t.Fatal("Invalid image", child.Image)
162177
}
163178
}
164179

165-
func TestExtendImageOverBuild(t *testing.T) {
166-
_, configV1, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(`
180+
func TestExtendImageOverBuildV1(t *testing.T) {
181+
_, config, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(`
167182
parent:
168183
build: .
169184
child:
@@ -175,7 +190,29 @@ child:
175190
t.Fatal(err)
176191
}
177192

178-
_, configV2, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(`
193+
parent := config["parent"]
194+
child := config["child"]
195+
196+
if parent.Image != "" {
197+
t.Fatal("Invalid image", parent.Image)
198+
}
199+
200+
if parent.Build.Context != "." {
201+
t.Fatal("Invalid build", parent.Build)
202+
}
203+
204+
if child.Build.Context != "" {
205+
t.Fatal("Invalid build", child.Build)
206+
}
207+
208+
if child.Image != "foo" {
209+
t.Fatal("Invalid image", child.Image)
210+
}
211+
212+
}
213+
214+
func TestExtendImageOverBuildV2(t *testing.T) {
215+
_, config, _, _, err := Merge(NewServiceConfigs(), nil, &NullLookup{}, "", []byte(`
179216
version: '2'
180217
services:
181218
parent:
@@ -190,25 +227,23 @@ services:
190227
t.Fatal(err)
191228
}
192229

193-
for _, config := range []map[string]*ServiceConfig{configV1, configV2} {
194-
parent := config["parent"]
195-
child := config["child"]
230+
parent := config["parent"]
231+
child := config["child"]
196232

197-
if parent.Image != "" {
198-
t.Fatal("Invalid image", parent.Image)
199-
}
233+
if parent.Image != "" {
234+
t.Fatal("Invalid image", parent.Image)
235+
}
200236

201-
if parent.Build.Context != "." {
202-
t.Fatal("Invalid build", parent.Build)
203-
}
237+
if parent.Build.Context != "." {
238+
t.Fatal("Invalid build", parent.Build)
239+
}
204240

205-
if child.Build.Context != "" {
206-
t.Fatal("Invalid build", child.Build)
207-
}
241+
if child.Build.Context != "." {
242+
t.Fatal("Invalid build", child.Build)
243+
}
208244

209-
if child.Image != "foo" {
210-
t.Fatal("Invalid image", child.Image)
211-
}
245+
if child.Image != "foo" {
246+
t.Fatal("Invalid image", child.Image)
212247
}
213248
}
214249

config/merge_v1.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func MergeServicesV1(existingServices *ServiceConfigs, environmentLookup Environ
2929
return nil, err
3030
}
3131

32-
data = mergeConfig(rawExistingService, data)
32+
data = mergeConfigV1(rawExistingService, data)
3333
}
3434

3535
datas[name] = data
@@ -148,7 +148,7 @@ func parseV1(resourceLookup ResourceLookup, environmentLookup EnvironmentLookup,
148148
}
149149
}
150150

151-
baseService = mergeConfig(baseService, serviceData)
151+
baseService = mergeConfigV1(baseService, serviceData)
152152

153153
logrus.Debugf("Merged result %#v", baseService)
154154

@@ -177,3 +177,22 @@ func resolveContextV1(inFile string, serviceData RawService) RawService {
177177

178178
return serviceData
179179
}
180+
181+
func mergeConfigV1(baseService, serviceData RawService) RawService {
182+
for k, v := range serviceData {
183+
// Image and build are mutually exclusive in merge
184+
if k == "image" {
185+
delete(baseService, "build")
186+
} else if k == "build" {
187+
delete(baseService, "image")
188+
}
189+
existing, ok := baseService[k]
190+
if ok {
191+
baseService[k] = merge(existing, v)
192+
} else {
193+
baseService[k] = v
194+
}
195+
}
196+
197+
return baseService
198+
}

0 commit comments

Comments
 (0)