Skip to content

Commit

Permalink
allow use of x-* service names as depends_on keys
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Oct 2, 2024
1 parent 2821e2c commit e18ae4d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
7 changes: 4 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ func NormalizeProjectName(s string) string {

var userDefinedKeys = []tree.Path{
"services",
"services.*.depends_on",
"volumes",
"networks",
"secrets",
Expand All @@ -687,7 +688,7 @@ func processExtensions(dict map[string]any, p tree.Path, extensions map[string]a
for key, value := range dict {
skip := false
for _, uk := range userDefinedKeys {
if uk.Matches(p) {
if p.Matches(uk) {
skip = true
break
}
Expand Down Expand Up @@ -770,14 +771,14 @@ func secretConfigDecoderHook(from, to reflect.Type, data interface{}) (interface
// Check if the input is a map and we're decoding into a SecretConfig
if from.Kind() == reflect.Map && to == reflect.TypeOf(types.SecretConfig{}) {
if v, ok := data.(map[string]interface{}); ok {
if ext, ok := v["#extensions"].(map[string]interface{}); ok {
if ext, ok := v[consts.Extensions].(map[string]interface{}); ok {
if val, ok := ext[types.SecretConfigXValue].(string); ok {
// Return a map with the Content field populated
v["Content"] = val
delete(ext, types.SecretConfigXValue)

if len(ext) == 0 {
delete(v, "#extensions")
delete(v, consts.Extensions)
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3409,3 +3409,24 @@ services:
"myhost": []string{"0.0.0.1", "0.0.0.2"},
})
}

func TestLoadDependsOnX(t *testing.T) {
p, err := loadYAML(`
name: load-depends-on-x
services:
test:
image: test
depends_on:
- x-foo
x-foo:
image: foo
`)
assert.NilError(t, err)
test := p.Services["test"]
assert.DeepEqual(t, test.DependsOn, types.DependsOnConfig{
"x-foo": types.ServiceDependency{
Condition: types.ServiceConditionStarted,
Required: true,
},
})
}
7 changes: 7 additions & 0 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func TestValidateAllowsXFields(t *testing.T) {
"bar": dict{
"x-extra-stuff": dict{},
},
"foo": dict{
"depends_on": dict{
"x-dependency": dict{
"condition": "service_started",
},
},
},
},
"volumes": dict{
"bar": dict{
Expand Down

0 comments on commit e18ae4d

Please sign in to comment.