Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce --all-resources to _not_ exclude resources not used by services #11702

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ type ProjectOptions struct {
Compatibility bool
Progress string
Offline bool
All bool
}

// ProjectFunc does stuff within a types.Project
Expand Down Expand Up @@ -175,6 +176,7 @@ func (o *ProjectOptions) addProjectFlags(f *pflag.FlagSet) {
f.StringVar(&o.WorkDir, "workdir", "", "DEPRECATED! USE --project-directory INSTEAD.\nSpecify an alternate working directory\n(default: the path of the, first specified, Compose file)")
f.BoolVar(&o.Compatibility, "compatibility", false, "Run compose in backward compatibility mode")
f.StringVar(&o.Progress, "progress", string(buildkit.AutoMode), fmt.Sprintf(`Set type of progress output (%s)`, strings.Join(printerModes, ", ")))
f.BoolVar(&o.All, "all-resources", false, "Include all resources, even those not used by services")
_ = f.MarkHidden("workdir")
}

Expand Down Expand Up @@ -231,9 +233,8 @@ func (o *ProjectOptions) ToModel(ctx context.Context, dockerCli command.Cli, ser
return options.LoadModel(ctx)
}

func (o *ProjectOptions) ToProject(ctx context.Context, dockerCli command.Cli, services []string, po ...cli.ProjectOptionsFn) (*types.Project, tracing.Metrics, error) {
func (o *ProjectOptions) ToProject(ctx context.Context, dockerCli command.Cli, services []string, po ...cli.ProjectOptionsFn) (*types.Project, tracing.Metrics, error) { //nolint:gocyclo
var metrics tracing.Metrics

remotes := o.remoteLoaders(dockerCli)
for _, r := range remotes {
po = append(po, cli.WithResourceLoader(r))
Expand Down Expand Up @@ -300,7 +301,9 @@ func (o *ProjectOptions) ToProject(ctx context.Context, dockerCli command.Cli, s
project.Services[name] = s
}

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

project, err = project.WithSelectedServices(services)
return project, metrics, err
Expand Down
8 changes: 4 additions & 4 deletions cmd/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func upCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service, ex

up.validateNavigationMenu(dockerCli, experiments)

if !p.All && len(project.Services) == 0 {
return fmt.Errorf("no service selected")
}

return runUp(ctx, dockerCli, backend, create, up, build, project, services)
}),
ValidArgsFunction: completeServiceNames(dockerCli, p),
Expand Down Expand Up @@ -205,10 +209,6 @@ func runUp(
project *types.Project,
services []string,
) error {
if len(project.Services) == 0 {
return fmt.Errorf("no service selected")
}

err := createOptions.Apply(project)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions docs/reference/compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Define and run multi-container applications with Docker

| Name | Type | Default | Description |
|:-----------------------|:--------------|:--------|:----------------------------------------------------------------------------------------------------|
| `--all-resources` | | | Include all resources, even those not used by services |
| `--ansi` | `string` | `auto` | Control when to print ANSI control characters ("never"\|"always"\|"auto") |
| `--compatibility` | | | Run compose in backward compatibility mode |
| `--dry-run` | | | Execute command in dry run mode |
Expand Down
10 changes: 10 additions & 0 deletions docs/reference/docker_compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ clink:
- docker_compose_wait.yaml
- docker_compose_watch.yaml
options:
- option: all-resources
value_type: bool
default_value: "false"
description: Include all resources, even those not used by services
deprecated: false
hidden: false
experimental: false
experimentalcli: false
kubernetes: false
swarm: false
- option: ansi
value_type: string
default_value: auto
Expand Down
5 changes: 5 additions & 0 deletions pkg/e2e/fixtures/resources/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
volumes:
my_vol: {}

networks:
my_net: {}
13 changes: 13 additions & 0 deletions pkg/e2e/up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package e2e
import (
"context"
"errors"
"fmt"
"os/exec"
"strings"
"syscall"
Expand Down Expand Up @@ -165,3 +166,15 @@ func TestUpWithDependencyNotRequired(t *testing.T) {
assert.Assert(t, strings.Contains(res.Combined(), "foo"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), " optional dependency \"bar\" failed to start"), res.Combined())
}

func TestUpWithAllResources(t *testing.T) {
c := NewCLI(t)
const projectName = "compose-e2e-all-resources"
t.Cleanup(func() {
c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "-v")
})

res := c.RunDockerComposeCmd(t, "-f", "./fixtures/resources/compose.yaml", "--all-resources", "--project-name", projectName, "up")
assert.Assert(t, strings.Contains(res.Combined(), fmt.Sprintf(`Volume "%s_my_vol" Created`, projectName)), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), fmt.Sprintf(`Network %s_my_net Created`, projectName)), res.Combined())
}
Loading