Skip to content

Commit

Permalink
Add Links in the labels for project reconstruction
Browse files Browse the repository at this point in the history
Signed-off-by: Ulysses Souza <[email protected]>
  • Loading branch information
ulyssessouza committed Apr 7, 2022
1 parent 804ef4a commit c783eb1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/api/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const (
ImageDigestLabel = "com.docker.compose.image"
// DependenciesLabel stores service dependencies
DependenciesLabel = "com.docker.compose.depends_on"
// LinksLabel stores service links
LinksLabel = "com.docker.compose.links"
// VersionLabel stores the compose tool version used to run application
VersionLabel = "com.docker.compose.version"
)
Expand Down
8 changes: 8 additions & 0 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
service.DependsOn[dependency] = types.ServiceDependency{Condition: condition}
}
}

links := service.Labels[api.LinksLabel]
if len(links) > 0 {
for _, link := range strings.Split(links, ",") {
l := strings.Split(link, ":")[0]
service.Links = append(service.Links, l)
}
}
project.Services = append(project.Services, *service)
}
SERVICES:
Expand Down
2 changes: 2 additions & 0 deletions pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ func (s *composeService) prepareLabels(service types.ServiceConfig, number int)
dependencies = append(dependencies, s+":"+d.Condition)
}
labels[api.DependenciesLabel] = strings.Join(dependencies, ",")
labels[api.LinksLabel] = strings.Join(service.Links, ",")

return labels, nil
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/e2e/fixtures/dependencies/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ services:
image: nginx:alpine
depends_on:
- bar
links:
- bar:first_web
- baz:second_web

bar:
image: nginx:alpine

baz:
image: nginx:alpine
6 changes: 6 additions & 0 deletions pkg/e2e/start_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func TestStartStopWithDependencies(t *testing.T) {
res := c.RunDockerComposeCmd("-f", "./fixtures/dependencies/compose.yaml", "--project-name", projectName, "up", "-d")
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-dependencies-foo-1 Started"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-dependencies-bar-1 Started"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-dependencies-baz-1 Started"), res.Combined())
})

t.Run("stop foo", func(t *testing.T) {
Expand All @@ -114,19 +115,24 @@ func TestStartStopWithDependencies(t *testing.T) {
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-dependencies-foo-1 Stopped"), res.Combined())

res = c.RunDockerComposeCmd("--project-name", projectName, "ps", "--status", "running")
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-dependencies-baz-1"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-dependencies-bar-1"), res.Combined())
assert.Assert(t, !strings.Contains(res.Combined(), "e2e-start-stop-with-dependencies-foo-1"), res.Combined())
})

t.Run("start foo", func(t *testing.T) {
res := c.RunDockerComposeCmd("--project-name", projectName, "stop")
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-dependencies-baz-1 Stopped"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-dependencies-bar-1 Stopped"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-dependencies-foo-1 Stopped"), res.Combined())

res = c.RunDockerComposeCmd("--project-name", projectName, "start", "foo")
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-dependencies-baz-1 Started"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-dependencies-bar-1 Started"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-dependencies-foo-1 Started"), res.Combined())

res = c.RunDockerComposeCmd("--project-name", projectName, "ps", "--status", "running")
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-dependencies-baz-1"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-dependencies-bar-1"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-dependencies-foo-1"), res.Combined())
})
Expand Down

0 comments on commit c783eb1

Please sign in to comment.