Skip to content

Commit 87ef292

Browse files
authored
Merge pull request #428 from glours/depends_on-required-attribut
2 parents 09318c2 + e582172 commit 87ef292

File tree

6 files changed

+71
-15
lines changed

6 files changed

+71
-15
lines changed

loader/full-struct_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
9595
},
9696
ContainerName: "my-web-container",
9797
DependsOn: types.DependsOnConfig{
98-
"db": {Condition: types.ServiceConditionStarted},
99-
"redis": {Condition: types.ServiceConditionStarted},
98+
"db": {Condition: types.ServiceConditionStarted, Required: true},
99+
"redis": {Condition: types.ServiceConditionStarted, Required: true},
100100
},
101101
Deploy: &types.DeployConfig{
102102
Mode: "replicated",
@@ -657,8 +657,10 @@ services:
657657
depends_on:
658658
db:
659659
condition: service_started
660+
required: true
660661
redis:
661662
condition: service_started
663+
required: true
662664
deploy:
663665
mode: replicated
664666
replicas: 6
@@ -1228,10 +1230,12 @@ func fullExampleJSON(workingDir, homeDir string) string {
12281230
"container_name": "my-web-container",
12291231
"depends_on": {
12301232
"db": {
1231-
"condition": "service_started"
1233+
"condition": "service_started",
1234+
"required": true
12321235
},
12331236
"redis": {
1234-
"condition": "service_started"
1237+
"condition": "service_started",
1238+
"required": true
12351239
}
12361240
},
12371241
"deploy": {

loader/loader.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,13 +1088,24 @@ var transformDependsOnConfig TransformerFunc = func(data interface{}) (interface
10881088
for _, serviceIntf := range value {
10891089
service, ok := serviceIntf.(string)
10901090
if !ok {
1091-
return data, errors.Errorf("invalid type %T for service depends_on elementn, expected string", value)
1091+
return data, errors.Errorf("invalid type %T for service depends_on element, expected string", value)
10921092
}
1093-
transformed[service] = map[string]interface{}{"condition": types.ServiceConditionStarted}
1093+
transformed[service] = map[string]interface{}{"condition": types.ServiceConditionStarted, "required": true}
10941094
}
10951095
return transformed, nil
10961096
case map[string]interface{}:
1097-
return groupXFieldsIntoExtensions(data.(map[string]interface{})), nil
1097+
transformed := map[string]interface{}{}
1098+
for service, val := range value {
1099+
dependsConfigIntf, ok := val.(map[string]interface{})
1100+
if !ok {
1101+
return data, errors.Errorf("invalid type %T for service depends_on element", value)
1102+
}
1103+
if _, ok := dependsConfigIntf["required"]; !ok {
1104+
dependsConfigIntf["required"] = true
1105+
}
1106+
transformed[service] = dependsConfigIntf
1107+
}
1108+
return groupXFieldsIntoExtensions(transformed), nil
10981109
default:
10991110
return data, errors.Errorf("invalid type %T for service depends_on", value)
11001111
}

loader/loader_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2428,7 +2428,7 @@ services:
24282428
Image: "busybox",
24292429
Environment: types.MappingWithEquals{},
24302430
Scale: 1,
2431-
DependsOn: types.DependsOnConfig{"imported": {Condition: "service_started"}},
2431+
DependsOn: types.DependsOnConfig{"imported": {Condition: "service_started", Required: true}},
24322432
},
24332433
{
24342434
Name: "imported",
@@ -2465,3 +2465,35 @@ func TestLoadWithIncludeCycle(t *testing.T) {
24652465
})
24662466
assert.Check(t, strings.HasPrefix(err.Error(), "include cycle detected"))
24672467
}
2468+
2469+
func TestLoadWithDependsOn(t *testing.T) {
2470+
p, err := loadYAML(`
2471+
name: test-depends-on
2472+
services:
2473+
foo:
2474+
image: nginx
2475+
depends_on:
2476+
bar:
2477+
condition: service_started
2478+
baz:
2479+
condition: service_healthy
2480+
required: false
2481+
qux:
2482+
condition: service_completed_successfully
2483+
required: true
2484+
`)
2485+
assert.NilError(t, err)
2486+
assert.DeepEqual(t, p.Services, types.Services{
2487+
{
2488+
Name: "foo",
2489+
Image: "nginx",
2490+
Environment: types.MappingWithEquals{},
2491+
Scale: 1,
2492+
DependsOn: types.DependsOnConfig{
2493+
"bar": {Condition: types.ServiceConditionStarted, Required: true},
2494+
"baz": {Condition: types.ServiceConditionHealthy, Required: false},
2495+
"qux": {Condition: types.ServiceConditionCompletedSuccessfully, Required: true},
2496+
},
2497+
},
2498+
})
2499+
}

loader/normalize.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func Normalize(project *types.Project) error {
8383
s.DependsOn = setIfMissing(s.DependsOn, link, types.ServiceDependency{
8484
Condition: types.ServiceConditionStarted,
8585
Restart: true,
86+
Required: true,
8687
})
8788
}
8889

@@ -92,6 +93,7 @@ func Normalize(project *types.Project) error {
9293
s.DependsOn = setIfMissing(s.DependsOn, name, types.ServiceDependency{
9394
Condition: types.ServiceConditionStarted,
9495
Restart: true,
96+
Required: true,
9597
})
9698
}
9799
}
@@ -102,6 +104,7 @@ func Normalize(project *types.Project) error {
102104
s.DependsOn = setIfMissing(s.DependsOn, spec[0], types.ServiceDependency{
103105
Condition: types.ServiceConditionStarted,
104106
Restart: false,
107+
Required: true,
105108
})
106109
}
107110
}
@@ -194,6 +197,7 @@ func inferImplicitDependencies(service *types.ServiceConfig) {
194197
if _, ok := service.DependsOn[d]; !ok {
195198
service.DependsOn[d] = types.ServiceDependency{
196199
Condition: types.ServiceConditionStarted,
200+
Required: true,
197201
}
198202
}
199203
}

loader/normalize_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ func TestNormalizeDependsOn(t *testing.T) {
136136
"bar": { // explicit depends_on never should be overridden
137137
Condition: types.ServiceConditionHealthy,
138138
Restart: false,
139+
Required: true,
139140
},
140141
},
141142
NetworkMode: "service:zot",
@@ -159,6 +160,7 @@ services:
159160
depends_on:
160161
zot:
161162
condition: service_started
163+
required: true
162164
networks:
163165
default: null
164166
volumes_from:
@@ -168,9 +170,11 @@ services:
168170
depends_on:
169171
bar:
170172
condition: service_healthy
173+
required: true
171174
zot:
172175
condition: service_started
173176
restart: true
177+
required: true
174178
network_mode: service:zot
175179
zot:
176180
networks:
@@ -200,19 +204,19 @@ func TestNormalizeImplicitDependencies(t *testing.T) {
200204
Links: []string{"corge"},
201205
DependsOn: map[string]types.ServiceDependency{
202206
// explicit dependency MUST not be overridden
203-
"foo": {Condition: types.ServiceConditionHealthy, Restart: false},
207+
"foo": {Condition: types.ServiceConditionHealthy, Restart: false, Required: true},
204208
},
205209
},
206210
},
207211
}
208212

209213
expected := types.DependsOnConfig{
210-
"foo": {Condition: types.ServiceConditionHealthy, Restart: false},
211-
"bar": {Condition: types.ServiceConditionStarted, Restart: true},
212-
"baz": {Condition: types.ServiceConditionStarted, Restart: true},
213-
"qux": {Condition: types.ServiceConditionStarted, Restart: true},
214-
"quux": {Condition: types.ServiceConditionStarted},
215-
"corge": {Condition: types.ServiceConditionStarted, Restart: true},
214+
"foo": {Condition: types.ServiceConditionHealthy, Restart: false, Required: true},
215+
"bar": {Condition: types.ServiceConditionStarted, Restart: true, Required: true},
216+
"baz": {Condition: types.ServiceConditionStarted, Restart: true, Required: true},
217+
"qux": {Condition: types.ServiceConditionStarted, Restart: true, Required: true},
218+
"quux": {Condition: types.ServiceConditionStarted, Required: true},
219+
"corge": {Condition: types.ServiceConditionStarted, Restart: true, Required: true},
216220
}
217221
err := Normalize(&project)
218222
assert.NilError(t, err)

types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ type ServiceDependency struct {
10251025
Condition string `yaml:"condition,omitempty" json:"condition,omitempty"`
10261026
Restart bool `yaml:"restart,omitempty" json:"restart,omitempty"`
10271027
Extensions Extensions `yaml:"#extensions,inline" json:"-"`
1028+
Required bool `yaml:"required" json:"required"`
10281029
}
10291030

10301031
type ExtendsConfig struct {

0 commit comments

Comments
 (0)