Merge pull request #8974 from ulyssessouza/fix-links-resolution3

Return an error when failing to list containers
This commit is contained in:
Mathieu Champlon 2021-11-29 10:34:00 +01:00 committed by GitHub
commit 7b84f2c2a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 10 deletions

View File

@ -261,6 +261,7 @@ func getContainerProgressName(container moby.Container) string {
return "Container " + getCanonicalContainerName(container)
}
// ServiceConditionRunningOrHealthy is a service condition on statys running or healthy
const ServiceConditionRunningOrHealthy = "running_or_healthy"
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
Networks: inspectedContainer.NetworkSettings.Networks,
},
}
links := s.getLinks(ctx, project.Name, service, number)
links, err := s.getLinks(ctx, project.Name, service, number)
if err != nil {
return created, err
}
for _, netName := range service.NetworksByPriority() {
netwrk := project.Networks[netName]
cfg := service.Networks[netName]
@ -477,7 +481,7 @@ func (s *composeService) createMobyContainer(ctx context.Context, project *types
}
// getLinks mimics V1 compose/service.py::Service::_get_links()
func (s composeService) getLinks(ctx context.Context, projectName string, service types.ServiceConfig, number int) []string {
func (s composeService) getLinks(ctx context.Context, projectName string, service types.ServiceConfig, number int) ([]string, error) {
var links []string
format := func(k, v string) string {
return fmt.Sprintf("%s:%s", k, v)
@ -495,7 +499,7 @@ func (s composeService) getLinks(ctx context.Context, projectName string, servic
}
cnts, err := getServiceContainers(linkServiceName)
if err != nil {
return nil
return nil, err
}
for _, c := range cnts {
containerName := getCanonicalContainerName(c)
@ -510,7 +514,7 @@ func (s composeService) getLinks(ctx context.Context, projectName string, servic
if service.Labels[api.OneoffLabel] == "True" {
cnts, err := getServiceContainers(service.Name)
if err != nil {
return nil
return nil, err
}
for _, c := range cnts {
containerName := getCanonicalContainerName(c)
@ -531,7 +535,7 @@ func (s composeService) getLinks(ctx context.Context, projectName string, servic
}
links = append(links, format(externalLink, linkName))
}
return links
return links, nil
}
func shortIDAliasExists(containerID string, aliases ...string) bool {

View File

@ -81,7 +81,8 @@ func TestServiceLinks(t *testing.T) {
c := testContainer("db", dbContainerName, false)
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
links := tested.getLinks(context.Background(), testProject, s, 1)
links, err := tested.getLinks(context.Background(), testProject, s, 1)
assert.NilError(t, err)
assert.Equal(t, len(links), 3)
assert.Equal(t, links[0], "testProject-db-1:db")
@ -100,7 +101,8 @@ func TestServiceLinks(t *testing.T) {
c := testContainer("db", dbContainerName, false)
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
links := tested.getLinks(context.Background(), testProject, s, 1)
links, err := tested.getLinks(context.Background(), testProject, s, 1)
assert.NilError(t, err)
assert.Equal(t, len(links), 3)
assert.Equal(t, links[0], "testProject-db-1:db")
@ -119,7 +121,8 @@ func TestServiceLinks(t *testing.T) {
c := testContainer("db", dbContainerName, false)
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
links := tested.getLinks(context.Background(), testProject, s, 1)
links, err := tested.getLinks(context.Background(), testProject, s, 1)
assert.NilError(t, err)
assert.Equal(t, len(links), 3)
assert.Equal(t, links[0], "testProject-db-1:dbname")
@ -139,7 +142,9 @@ func TestServiceLinks(t *testing.T) {
c := testContainer("db", dbContainerName, false)
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
links := tested.getLinks(context.Background(), testProject, s, 1)
links, err := tested.getLinks(context.Background(), testProject, s, 1)
assert.NilError(t, err)
assert.Equal(t, len(links), 4)
assert.Equal(t, links[0], "testProject-db-1:dbname")
assert.Equal(t, links[1], "testProject-db-1:db-1")
@ -170,7 +175,9 @@ func TestServiceLinks(t *testing.T) {
}
apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptionsOneOff).Return([]moby.Container{c}, nil)
links := tested.getLinks(context.Background(), testProject, s, 1)
links, err := tested.getLinks(context.Background(), testProject, s, 1)
assert.NilError(t, err)
assert.Equal(t, len(links), 3)
assert.Equal(t, links[0], "testProject-web-1:web")
assert.Equal(t, links[1], "testProject-web-1:web-1")