Skip to content

Commit 7b84f2c

Browse files
authored
Merge pull request docker#8974 from ulyssessouza/fix-links-resolution3
Return an error when failing to list containers
2 parents 32005b0 + cf7b144 commit 7b84f2c

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

pkg/compose/convergence.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ func getContainerProgressName(container moby.Container) string {
261261
return "Container " + getCanonicalContainerName(container)
262262
}
263263

264+
// ServiceConditionRunningOrHealthy is a service condition on statys running or healthy
264265
const ServiceConditionRunningOrHealthy = "running_or_healthy"
265266

266267
func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, dependencies types.DependsOnConfig) error {
@@ -448,7 +449,10 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
448449
Networks: inspectedContainer.NetworkSettings.Networks,
449450
},
450451
}
451-
links := s.getLinks(ctx, project.Name, service, number)
452+
links, err := s.getLinks(ctx, project.Name, service, number)
453+
if err != nil {
454+
return created, err
455+
}
452456
for _, netName := range service.NetworksByPriority() {
453457
netwrk := project.Networks[netName]
454458
cfg := service.Networks[netName]
@@ -477,7 +481,7 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
477481
}
478482

479483
// getLinks mimics V1 compose/service.py::Service::_get_links()
480-
func (s composeService) getLinks(ctx context.Context, projectName string, service types.ServiceConfig, number int) []string {
484+
func (s composeService) getLinks(ctx context.Context, projectName string, service types.ServiceConfig, number int) ([]string, error) {
481485
var links []string
482486
format := func(k, v string) string {
483487
return fmt.Sprintf("%s:%s", k, v)
@@ -495,7 +499,7 @@ func (s composeService) getLinks(ctx context.Context, projectName string, servic
495499
}
496500
cnts, err := getServiceContainers(linkServiceName)
497501
if err != nil {
498-
return nil
502+
return nil, err
499503
}
500504
for _, c := range cnts {
501505
containerName := getCanonicalContainerName(c)
@@ -510,7 +514,7 @@ func (s composeService) getLinks(ctx context.Context, projectName string, servic
510514
if service.Labels[api.OneoffLabel] == "True" {
511515
cnts, err := getServiceContainers(service.Name)
512516
if err != nil {
513-
return nil
517+
return nil, err
514518
}
515519
for _, c := range cnts {
516520
containerName := getCanonicalContainerName(c)
@@ -531,7 +535,7 @@ func (s composeService) getLinks(ctx context.Context, projectName string, servic
531535
}
532536
links = append(links, format(externalLink, linkName))
533537
}
534-
return links
538+
return links, nil
535539
}
536540

537541
func shortIDAliasExists(containerID string, aliases ...string) bool {

pkg/compose/convergence_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ func TestServiceLinks(t *testing.T) {
8181
c := testContainer("db", dbContainerName, false)
8282
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
8383

84-
links := tested.getLinks(context.Background(), testProject, s, 1)
84+
links, err := tested.getLinks(context.Background(), testProject, s, 1)
85+
assert.NilError(t, err)
8586

8687
assert.Equal(t, len(links), 3)
8788
assert.Equal(t, links[0], "testProject-db-1:db")
@@ -100,7 +101,8 @@ func TestServiceLinks(t *testing.T) {
100101
c := testContainer("db", dbContainerName, false)
101102

102103
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
103-
links := tested.getLinks(context.Background(), testProject, s, 1)
104+
links, err := tested.getLinks(context.Background(), testProject, s, 1)
105+
assert.NilError(t, err)
104106

105107
assert.Equal(t, len(links), 3)
106108
assert.Equal(t, links[0], "testProject-db-1:db")
@@ -119,7 +121,8 @@ func TestServiceLinks(t *testing.T) {
119121
c := testContainer("db", dbContainerName, false)
120122
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
121123

122-
links := tested.getLinks(context.Background(), testProject, s, 1)
124+
links, err := tested.getLinks(context.Background(), testProject, s, 1)
125+
assert.NilError(t, err)
123126

124127
assert.Equal(t, len(links), 3)
125128
assert.Equal(t, links[0], "testProject-db-1:dbname")
@@ -139,7 +142,9 @@ func TestServiceLinks(t *testing.T) {
139142
c := testContainer("db", dbContainerName, false)
140143
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
141144

142-
links := tested.getLinks(context.Background(), testProject, s, 1)
145+
links, err := tested.getLinks(context.Background(), testProject, s, 1)
146+
assert.NilError(t, err)
147+
143148
assert.Equal(t, len(links), 4)
144149
assert.Equal(t, links[0], "testProject-db-1:dbname")
145150
assert.Equal(t, links[1], "testProject-db-1:db-1")
@@ -170,7 +175,9 @@ func TestServiceLinks(t *testing.T) {
170175
}
171176
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptionsOneOff).Return([]moby.Container{c}, nil)
172177

173-
links := tested.getLinks(context.Background(), testProject, s, 1)
178+
links, err := tested.getLinks(context.Background(), testProject, s, 1)
179+
assert.NilError(t, err)
180+
174181
assert.Equal(t, len(links), 3)
175182
assert.Equal(t, links[0], "testProject-web-1:web")
176183
assert.Equal(t, links[1], "testProject-web-1:web-1")

0 commit comments

Comments
 (0)