Skip to content

Commit

Permalink
exclude unnecessary resources after services have been selected
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof authored and glours committed Jul 9, 2024
1 parent cacbca8 commit 25f8593
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
7 changes: 5 additions & 2 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,14 @@ func (o *ProjectOptions) ToProject(ctx context.Context, dockerCli command.Cli, s
project.Services[name] = s
}

project, err = project.WithSelectedServices(services)
if err != nil {
return nil, tracing.Metrics{}, err
}

if !o.All {
project = project.WithoutUnnecessaryResources()
}

project, err = project.WithSelectedServices(services)
return project, metrics, err
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -1307,11 +1307,10 @@ func (s *composeService) resolveExternalNetwork(ctx context.Context, n *types.Ne
if len(networks) == 0 {
// in this instance, n.Name is really an ID
sn, err := s.apiClient().NetworkInspect(ctx, n.Name, network.InspectOptions{})
if err != nil {
if err != nil && !errdefs.IsNotFound(err) {
return err
}
networks = append(networks, sn)

}

// NetworkList API doesn't return the exact name match, so we can retrieve more than one network with a request
Expand Down
17 changes: 17 additions & 0 deletions pkg/e2e/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,20 @@ func TestNestedDotEnv(t *testing.T) {
})

}

func TestUnnecesaryResources(t *testing.T) {
const projectName = "compose-e2e-unnecessary-resources"
c := NewParallelCLI(t)
t.Cleanup(func() {
c.RunDockerComposeCmd(t, "-p", projectName, "down", "-t=0")
})

res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/external/compose.yaml", "-p", projectName, "up", "-d")
res.Assert(t, icmd.Expected{
ExitCode: 1,
Err: "network foo_bar declared as external, but could not be found",
})

c.RunDockerComposeCmd(t, "-f", "./fixtures/external/compose.yaml", "-p", projectName, "up", "-d", "test")
// Should not fail as missing external network is not used
}
14 changes: 14 additions & 0 deletions pkg/e2e/fixtures/external/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
test:
image: nginx:alpine

other:
image: nginx:alpine
networks:
test_network:
ipv4_address: 8.8.8.8

networks:
test_network:
external: true
name: foo_bar

0 comments on commit 25f8593

Please sign in to comment.