From 76688d4000a09e590e76b05767c83fd0b8052579 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 18 Dec 2024 17:22:16 +0100 Subject: [PATCH] introduce depends_on.service Signed-off-by: Nicolas De Loof --- loader/full-struct_test.go | 8 ++++++-- loader/loader_test.go | 9 +++++---- schema/compose-spec.json | 1 + transform/dependson.go | 8 ++++++-- types/derived.gen.go | 1 + types/types.go | 1 + 6 files changed, 20 insertions(+), 8 deletions(-) diff --git a/loader/full-struct_test.go b/loader/full-struct_test.go index 187eea59..cda69a04 100644 --- a/loader/full-struct_test.go +++ b/loader/full-struct_test.go @@ -96,8 +96,8 @@ func services(workingDir, homeDir string) types.Services { }, ContainerName: "my-web-container", DependsOn: types.DependsOnConfig{ - "db": {Condition: types.ServiceConditionStarted, Required: true}, - "redis": {Condition: types.ServiceConditionStarted, Required: true}, + "db": {Service: "db", Condition: types.ServiceConditionStarted, Required: true}, + "redis": {Service: "redis", Condition: types.ServiceConditionStarted, Required: true}, }, Deploy: &types.DeployConfig{ Mode: "replicated", @@ -678,9 +678,11 @@ services: container_name: my-web-container depends_on: db: + service: db condition: service_started required: true redis: + service: redis condition: service_started required: true deploy: @@ -1262,10 +1264,12 @@ func fullExampleJSON(workingDir, homeDir string) string { "container_name": "my-web-container", "depends_on": { "db": { + "service": "db", "condition": "service_started", "required": true }, "redis": { + "service": "redis", "condition": "service_started", "required": true } diff --git a/loader/loader_test.go b/loader/loader_test.go index 8f062c00..3e3aefd4 100644 --- a/loader/loader_test.go +++ b/loader/loader_test.go @@ -2702,7 +2702,7 @@ services: Name: "foo", Image: "busybox", Environment: types.MappingWithEquals{}, - DependsOn: types.DependsOnConfig{"imported": {Condition: "service_started", Required: true}}, + DependsOn: types.DependsOnConfig{"imported": {Service: "imported", Condition: "service_started", Required: true}}, }, "imported": { Name: "imported", @@ -2844,9 +2844,9 @@ services: Image: "nginx", Environment: types.MappingWithEquals{}, DependsOn: types.DependsOnConfig{ - "bar": {Condition: types.ServiceConditionStarted, Required: true}, - "baz": {Condition: types.ServiceConditionHealthy, Required: false}, - "qux": {Condition: types.ServiceConditionCompletedSuccessfully, Required: true}, + "bar": {Service: "bar", Condition: types.ServiceConditionStarted, Required: true}, + "baz": {Service: "baz", Condition: types.ServiceConditionHealthy, Required: false}, + "qux": {Service: "qux", Condition: types.ServiceConditionCompletedSuccessfully, Required: true}, }, }, }) @@ -3546,6 +3546,7 @@ services: test := p.Services["test"] assert.DeepEqual(t, test.DependsOn, types.DependsOnConfig{ "x-foo": types.ServiceDependency{ + Service: "x-foo", Condition: types.ServiceConditionStarted, Required: true, }, diff --git a/schema/compose-spec.json b/schema/compose-spec.json index f95a7b9d..583e4015 100644 --- a/schema/compose-spec.json +++ b/schema/compose-spec.json @@ -198,6 +198,7 @@ "additionalProperties": false, "patternProperties": {"^x-": {}}, "properties": { + "service": {"type": "string"}, "restart": {"type": ["boolean", "string"]}, "required": { "type": "boolean", diff --git a/transform/dependson.go b/transform/dependson.go index 0a72ffa4..6b06d694 100644 --- a/transform/dependson.go +++ b/transform/dependson.go @@ -25,10 +25,10 @@ import ( func transformDependsOn(data any, p tree.Path, _ bool) (any, error) { switch v := data.(type) { case map[string]any: - for i, e := range v { + for k, e := range v { d, ok := e.(map[string]any) if !ok { - return nil, fmt.Errorf("%s.%s: unsupported value %s", p, i, v) + return nil, fmt.Errorf("%s.%s: unsupported value %s", p, k, v) } if _, ok := d["condition"]; !ok { d["condition"] = "service_started" @@ -36,6 +36,9 @@ func transformDependsOn(data any, p tree.Path, _ bool) (any, error) { if _, ok := d["required"]; !ok { d["required"] = true } + if _, ok := d["service"]; !ok { + d["service"] = k + } } return v, nil case []any: @@ -44,6 +47,7 @@ func transformDependsOn(data any, p tree.Path, _ bool) (any, error) { d[k.(string)] = map[string]any{ "condition": "service_started", "required": true, + "service": k, } } return d, nil diff --git a/types/derived.gen.go b/types/derived.gen.go index f4f2db56..be285c46 100644 --- a/types/derived.gen.go +++ b/types/derived.gen.go @@ -1612,6 +1612,7 @@ func deriveDeepCopy_31(dst, src *ServiceConfigObjConfig) { // deriveDeepCopy_32 recursively copies the contents of src into dst. func deriveDeepCopy_32(dst, src *ServiceDependency) { + dst.Service = src.Service dst.Condition = src.Condition dst.Restart = src.Restart if src.Extensions != nil { diff --git a/types/types.go b/types/types.go index 5da8b853..6bfb01e4 100644 --- a/types/types.go +++ b/types/types.go @@ -761,6 +761,7 @@ const ( type DependsOnConfig map[string]ServiceDependency type ServiceDependency struct { + Service string `yaml:"service,omitempty" json:"service,omitempty"` Condition string `yaml:"condition,omitempty" json:"condition,omitempty"` Restart bool `yaml:"restart,omitempty" json:"restart,omitempty"` Extensions Extensions `yaml:"#extensions,inline,omitempty" json:"-"`