Skip to content

Commit e831ea8

Browse files
committed
add support for restart for depends_on
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 8d56db5 commit e831ea8

File tree

7 files changed

+32
-16
lines changed

7 files changed

+32
-16
lines changed

cmd/compose/compose.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ type ProjectOptions struct {
9999
ConfigPaths []string
100100
WorkDir string
101101
ProjectDir string
102-
EnvFile string
102+
EnvFiles []string
103103
Compatibility bool
104104
}
105105

@@ -132,7 +132,7 @@ func (o *ProjectOptions) addProjectFlags(f *pflag.FlagSet) {
132132
f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable")
133133
f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
134134
f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
135-
f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.")
135+
f.StringArrayVar(&o.EnvFiles, "env-file", nil, "Specify an alternate environment file.")
136136
f.StringVar(&o.ProjectDir, "project-directory", "", "Specify an alternate working directory\n(default: the path of the, first specified, Compose file)")
137137
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)")
138138
f.BoolVar(&o.Compatibility, "compatibility", false, "Run compose in backward compatibility mode")
@@ -198,8 +198,8 @@ func (o *ProjectOptions) ToProject(services []string, po ...cli.ProjectOptionsFn
198198
api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","),
199199
api.OneoffLabel: "False", // default, will be overridden by `run` command
200200
}
201-
if o.EnvFile != "" {
202-
s.CustomLabels[api.EnvironmentFileLabel] = o.EnvFile
201+
if len(o.EnvFiles) != 0 {
202+
s.CustomLabels[api.EnvironmentFileLabel] = strings.Join(o.EnvFiles, ",")
203203
}
204204
project.Services[i] = s
205205
}
@@ -229,7 +229,7 @@ func (o *ProjectOptions) toProjectOptions(po ...cli.ProjectOptionsFn) (*cli.Proj
229229
append(po,
230230
cli.WithWorkingDirectory(o.ProjectDir),
231231
cli.WithOsEnv,
232-
cli.WithEnvFile(o.EnvFile),
232+
cli.WithEnvFiles(o.EnvFiles...),
233233
cli.WithDotEnv,
234234
cli.WithConfigFileEnv,
235235
cli.WithDefaultConfigPath,
@@ -322,10 +322,13 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
322322
opts.ProjectDir = opts.WorkDir
323323
fmt.Fprint(os.Stderr, aec.Apply("option '--workdir' is DEPRECATED at root level! Please use '--project-directory' instead.\n", aec.RedF))
324324
}
325-
if opts.EnvFile != "" && !filepath.IsAbs(opts.EnvFile) {
326-
opts.EnvFile, err = filepath.Abs(opts.EnvFile)
327-
if err != nil {
328-
return err
325+
for i, file := range opts.EnvFiles {
326+
if !filepath.IsAbs(file) {
327+
file, err = filepath.Abs(file)
328+
if err != nil {
329+
return err
330+
}
331+
opts.EnvFiles[i] = file
329332
}
330333
}
331334
if v, ok := os.LookupEnv("COMPOSE_PARALLEL_LIMIT"); ok && !cmd.Flags().Changed("parallel") {

cmd/compose/compose_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ func TestFilterServices(t *testing.T) {
3131
Links: []string{"bar"},
3232
},
3333
{
34-
Name: "bar",
35-
NetworkMode: types.NetworkModeServicePrefix + "zot",
34+
Name: "bar",
35+
DependsOn: map[string]types.ServiceDependency{
36+
"zot": {},
37+
},
3638
},
3739
{
3840
Name: "zot",

docs/reference/compose.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Docker Compose
4141
|:-----------------------|:--------------|:--------|:----------------------------------------------------------------------------------------------------|
4242
| `--ansi` | `string` | `auto` | Control when to print ANSI control characters ("never"\|"always"\|"auto") |
4343
| `--compatibility` | | | Run compose in backward compatibility mode |
44-
| `--env-file` | `string` | | Specify an alternate environment file. |
44+
| `--env-file` | `stringArray` | | Specify an alternate environment file. |
4545
| `-f`, `--file` | `stringArray` | | Compose configuration files |
4646
| `--parallel` | `int` | `-1` | Control max parallelism, -1 for unlimited |
4747
| `--profile` | `stringArray` | | Specify a profile to enable |

docs/reference/docker_compose.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ options:
198198
kubernetes: false
199199
swarm: false
200200
- option: env-file
201-
value_type: string
201+
value_type: stringArray
202+
default_value: '[]'
202203
description: Specify an alternate environment file.
203204
deprecated: false
204205
hidden: false

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.20
55
require (
66
github.com/AlecAivazis/survey/v2 v2.3.6
77
github.com/buger/goterm v1.0.4
8-
github.com/compose-spec/compose-go v1.10.0
8+
github.com/compose-spec/compose-go v1.11.0
99
github.com/containerd/console v1.0.3
1010
github.com/containerd/containerd v1.6.18
1111
github.com/cucumber/godog v0.0.0-00010101000000-000000000000

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWH
165165
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
166166
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
167167
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
168-
github.com/compose-spec/compose-go v1.10.0 h1:MGrEv+WyETQWB4ARKTHRTvoZ0CZGi8lyFlveGNMej40=
169-
github.com/compose-spec/compose-go v1.10.0/go.mod h1:Tb5Ae2PsYN3GTqYqzl2IRbTPiJtPZZjMw8UKUvmehFk=
168+
github.com/compose-spec/compose-go v1.11.0 h1:YLl0wf4YU9ZVei6mqLxAfI2gWOrqnTsPBAcIe9cO9Zk=
169+
github.com/compose-spec/compose-go v1.11.0/go.mod h1:huuiqxbQTZLkagcN9D/1tEKZwMXVetYeIWtjCAVsoXw=
170170
github.com/containerd/cgroups v1.0.4 h1:jN/mbWBEaz+T1pi5OFtnkQ+8qnmEbAr1Oo1FRm5B0dA=
171171
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
172172
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=

pkg/compose/restart.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ func (s *composeService) restart(ctx context.Context, projectName string, option
4747
}
4848
}
4949

50+
// ignore depends_on relations which are not impacted by restarting service
51+
for i, service := range project.Services {
52+
for name, r := range service.DependsOn {
53+
if !r.Restart {
54+
delete(service.DependsOn, name)
55+
}
56+
}
57+
project.Services[i] = service
58+
}
59+
5060
if len(options.Services) == 0 {
5161
err = project.ForServices(options.Services)
5262
if err != nil {

0 commit comments

Comments
 (0)