Skip to content

Commit

Permalink
introduce depends_on.service
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Dec 18, 2024
1 parent 20738c5 commit dfc3a63
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
8 changes: 6 additions & 2 deletions loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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},
},
},
})
Expand Down Expand Up @@ -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,
},
Expand Down
6 changes: 3 additions & 3 deletions loader/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ func checkConsistency(project *types.Project) error {
}
}

for dependedService, cfg := range s.DependsOn {
if _, err := project.GetService(dependedService); err != nil {
for _, cfg := range s.DependsOn {
if _, err := project.GetService(cfg.Service); err != nil {
if errors.Is(err, errdefs.ErrDisabled) && !cfg.Required {
continue
}
return fmt.Errorf("service %q depends on undefined service %q: %w", s.Name, dependedService, errdefs.ErrInvalid)
return fmt.Errorf("service %q depends on undefined service %q: %w", s.Name, cfg.Service, errdefs.ErrInvalid)
}
}

Expand Down
1 change: 1 addition & 0 deletions schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"additionalProperties": false,
"patternProperties": {"^x-": {}},
"properties": {
"service": {"type": "string"},
"restart": {"type": ["boolean", "string"]},
"required": {
"type": "boolean",
Expand Down
8 changes: 6 additions & 2 deletions transform/dependson.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ 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"
}
if _, ok := d["required"]; !ok {
d["required"] = true
}
if _, ok := d["service"]; !ok {
d["service"] = k
}
}
return v, nil
case []any:
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions types/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ const (
// GetDependencies retrieves all services this service depends on
func (s ServiceConfig) GetDependencies() []string {
var dependencies []string
for service := range s.DependsOn {
dependencies = append(dependencies, service)
for _, dependency := range s.DependsOn {
dependencies = append(dependencies, dependency.Service)
}
return dependencies
}
Expand All @@ -259,8 +259,8 @@ func (s ServiceConfig) GetDependencies() []string {
func (s ServiceConfig) GetDependents(p *Project) []string {
var dependent []string
for _, service := range p.Services {
for name := range service.DependsOn {
if name == s.Name {
for _, dependency := range service.DependsOn {
if dependency.Service == s.Name {
dependent = append(dependent, service.Name)
}
}
Expand Down Expand Up @@ -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:"-"`
Expand Down

0 comments on commit dfc3a63

Please sign in to comment.