exclude unnecessary resources after services have been selected

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2024-07-09 09:33:27 +02:00 committed by Guillaume Lours
parent cacbca859d
commit 25f85938bb
4 changed files with 37 additions and 4 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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
pkg/e2e/fixtures/external/compose.yaml vendored Normal file
View File

@ -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