Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

normalize implicit dependencies set by service:xx references #409

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions loader/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func Normalize(project *types.Project, resolvePaths bool) error {
return err
}

inferImplicitDependencies(&s)

project.Services[i] = s
}

Expand All @@ -171,6 +173,61 @@ func Normalize(project *types.Project, resolvePaths bool) error {
return nil
}

// IsServiceDependency check the relation set by ref refers to a service
func IsServiceDependency(ref string) (string, bool) {
if strings.HasPrefix(
ref,
types.ServicePrefix,
) {
return ref[len(types.ServicePrefix):], true
}
return "", false
}

func inferImplicitDependencies(service *types.ServiceConfig) {
var dependencies []string

maybeReferences := []string{
service.NetworkMode,
service.Ipc,
service.Pid,
service.Uts,
service.Cgroup,
}
for _, ref := range maybeReferences {
if dep, ok := IsServiceDependency(ref); ok {
dependencies = append(dependencies, dep)
}
}

for _, vol := range service.VolumesFrom {
spec := strings.Split(vol, ":")
if len(spec) == 0 {
continue
}
if spec[0] == "container" {
continue
}
dependencies = append(dependencies, spec[0])
}

for _, link := range service.Links {
dependencies = append(dependencies, strings.Split(link, ":")[0])
}

if len(dependencies) > 0 && service.DependsOn == nil {
service.DependsOn = make(types.DependsOnConfig)
}

for _, d := range dependencies {
if _, ok := service.DependsOn[d]; !ok {
service.DependsOn[d] = types.ServiceDependency{
Condition: types.ServiceConditionStarted,
}
}
}
}

// setIfMissing adds a ServiceDependency for service if not already defined
func setIfMissing(d types.DependsOnConfig, service string, dep types.ServiceDependency) types.DependsOnConfig {
if d == nil {
Expand Down
33 changes: 33 additions & 0 deletions loader/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,36 @@ func TestNormalizeAdditionalContexts(t *testing.T) {
assert.NilError(t, err)
assert.DeepEqual(t, expected, project)
}

func TestNormalizeImplicitDependencies(t *testing.T) {
project := types.Project{
Name: "myProject",
Services: types.Services{
types.ServiceConfig{
Name: "test",
Ipc: "service:foo",
Cgroup: "service:bar",
Uts: "service:baz",
Pid: "service:qux",
VolumesFrom: []string{"quux"},
Links: []string{"corge"},
DependsOn: map[string]types.ServiceDependency{
// explicit dependency MUST not be overridden
"foo": {Condition: types.ServiceConditionHealthy, Restart: false},
},
},
},
}

expected := types.DependsOnConfig{
"foo": {Condition: types.ServiceConditionHealthy, Restart: false},
"bar": {Condition: types.ServiceConditionStarted, Restart: true},
"baz": {Condition: types.ServiceConditionStarted, Restart: true},
"qux": {Condition: types.ServiceConditionStarted, Restart: true},
"quux": {Condition: types.ServiceConditionStarted},
"corge": {Condition: types.ServiceConditionStarted, Restart: true},
}
err := Normalize(&project, true)
assert.NilError(t, err)
assert.DeepEqual(t, expected, project.Services[0].DependsOn)
}