Skip to content

Commit 730609b

Browse files
authored
Merge pull request docker#9280 from ndeloof/kill
kill only need project name
2 parents 283f7a1 + cd8074d commit 730609b

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

cmd/compose/kill.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,44 @@ package compose
1919
import (
2020
"context"
2121

22-
"github.com/compose-spec/compose-go/types"
2322
"github.com/spf13/cobra"
2423

2524
"github.com/docker/compose/v2/pkg/api"
2625
)
2726

27+
type killOptions struct {
28+
*projectOptions
29+
signal string
30+
}
31+
2832
func killCommand(p *projectOptions, backend api.Service) *cobra.Command {
29-
var opts api.KillOptions
33+
opts := killOptions{
34+
projectOptions: p,
35+
}
3036
cmd := &cobra.Command{
3137
Use: "kill [options] [SERVICE...]",
3238
Short: "Force stop service containers.",
33-
RunE: p.WithProject(func(ctx context.Context, project *types.Project) error {
34-
return backend.Kill(ctx, project, opts)
39+
RunE: Adapt(func(ctx context.Context, args []string) error {
40+
return runKill(ctx, backend, opts, args)
3541
}),
3642
ValidArgsFunction: serviceCompletion(p),
3743
}
3844

3945
flags := cmd.Flags()
40-
flags.StringVarP(&opts.Signal, "signal", "s", "SIGKILL", "SIGNAL to send to the container.")
46+
flags.StringVarP(&opts.signal, "signal", "s", "SIGKILL", "SIGNAL to send to the container.")
4147

4248
return cmd
4349
}
50+
51+
func runKill(ctx context.Context, backend api.Service, opts killOptions, services []string) error {
52+
projectName, err := opts.toProjectName()
53+
if err != nil {
54+
return err
55+
}
56+
57+
return backend.Kill(ctx, projectName, api.KillOptions{
58+
Services: services,
59+
Signal: opts.signal,
60+
})
61+
62+
}

pkg/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Service interface {
5454
// Convert translate compose model into backend's native format
5555
Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
5656
// Kill executes the equivalent to a `compose kill`
57-
Kill(ctx context.Context, project *types.Project, options KillOptions) error
57+
Kill(ctx context.Context, project string, options KillOptions) error
5858
// RunOneOffContainer creates a service oneoff container and starts its dependencies
5959
RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (int, error)
6060
// Remove executes the equivalent to a `compose rm`

pkg/api/proxy.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type ServiceProxy struct {
3737
PsFn func(ctx context.Context, projectName string, options PsOptions) ([]ContainerSummary, error)
3838
ListFn func(ctx context.Context, options ListOptions) ([]Stack, error)
3939
ConvertFn func(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
40-
KillFn func(ctx context.Context, project *types.Project, options KillOptions) error
40+
KillFn func(ctx context.Context, project string, options KillOptions) error
4141
RunOneOffContainerFn func(ctx context.Context, project *types.Project, opts RunOptions) (int, error)
4242
RemoveFn func(ctx context.Context, project string, options RemoveOptions) error
4343
ExecFn func(ctx context.Context, project string, opts RunOptions) (int, error)
@@ -219,13 +219,10 @@ func (s *ServiceProxy) Convert(ctx context.Context, project *types.Project, opti
219219
}
220220

221221
// Kill implements Service interface
222-
func (s *ServiceProxy) Kill(ctx context.Context, project *types.Project, options KillOptions) error {
222+
func (s *ServiceProxy) Kill(ctx context.Context, project string, options KillOptions) error {
223223
if s.KillFn == nil {
224224
return ErrNotImplemented
225225
}
226-
for _, i := range s.interceptors {
227-
i(ctx, project)
228-
}
229226
return s.KillFn(ctx, project, options)
230227
}
231228

pkg/compose/kill.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,38 @@ package compose
1818

1919
import (
2020
"context"
21+
"fmt"
2122

22-
"github.com/compose-spec/compose-go/types"
2323
moby "github.com/docker/docker/api/types"
2424
"golang.org/x/sync/errgroup"
2525

2626
"github.com/docker/compose/v2/pkg/api"
2727
"github.com/docker/compose/v2/pkg/progress"
2828
)
2929

30-
func (s *composeService) Kill(ctx context.Context, project *types.Project, options api.KillOptions) error {
30+
func (s *composeService) Kill(ctx context.Context, project string, options api.KillOptions) error {
3131
return progress.Run(ctx, func(ctx context.Context) error {
3232
return s.kill(ctx, project, options)
3333
})
3434
}
3535

36-
func (s *composeService) kill(ctx context.Context, project *types.Project, options api.KillOptions) error {
36+
func (s *composeService) kill(ctx context.Context, project string, options api.KillOptions) error {
3737
w := progress.ContextWriter(ctx)
3838

3939
services := options.Services
40-
if len(services) == 0 {
41-
services = project.ServiceNames()
42-
}
4340

4441
var containers Containers
45-
containers, err := s.getContainers(ctx, project.Name, oneOffInclude, false, services...)
42+
containers, err := s.getContainers(ctx, project, oneOffInclude, false, services...)
4643
if err != nil {
4744
return err
4845
}
4946

47+
if len(containers) == 0 {
48+
fmt.Fprintf(s.stderr(), "no container to kill")
49+
}
50+
5051
eg, ctx := errgroup.WithContext(ctx)
5152
containers.
52-
filter(isService(project.ServiceNames()...)).
5353
forEach(func(container moby.Container) {
5454
eg.Go(func() error {
5555
eventName := getContainerProgressName(container)

pkg/compose/kill_test.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"strings"
2323
"testing"
2424

25-
"github.com/compose-spec/compose-go/types"
2625
moby "github.com/docker/docker/api/types"
2726
"github.com/docker/docker/api/types/filters"
2827
"github.com/golang/mock/gomock"
@@ -45,18 +44,18 @@ func TestKillAll(t *testing.T) {
4544
tested.dockerCli = cli
4645
cli.EXPECT().Client().Return(api).AnyTimes()
4746

48-
project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{testService("service1"), testService("service2")}}
47+
name := strings.ToLower(testProject)
4948

5049
ctx := context.Background()
5150
api.EXPECT().ContainerList(ctx, moby.ContainerListOptions{
52-
Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject))),
51+
Filters: filters.NewArgs(projectFilter(name)),
5352
}).Return(
5453
[]moby.Container{testContainer("service1", "123", false), testContainer("service1", "456", false), testContainer("service2", "789", false)}, nil)
5554
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "").Return(nil)
5655
api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
5756
api.EXPECT().ContainerKill(anyCancellableContext(), "789", "").Return(nil)
5857

59-
err := tested.kill(ctx, &project, compose.KillOptions{})
58+
err := tested.kill(ctx, name, compose.KillOptions{})
6059
assert.NilError(t, err)
6160
}
6261

@@ -70,23 +69,19 @@ func TestKillSignal(t *testing.T) {
7069
tested.dockerCli = cli
7170
cli.EXPECT().Client().Return(api).AnyTimes()
7271

73-
project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{testService(serviceName)}}
72+
name := strings.ToLower(testProject)
7473
listOptions := moby.ContainerListOptions{
75-
Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject)), serviceFilter(serviceName)),
74+
Filters: filters.NewArgs(projectFilter(name), serviceFilter(serviceName)),
7675
}
7776

7877
ctx := context.Background()
7978
api.EXPECT().ContainerList(ctx, listOptions).Return([]moby.Container{testContainer(serviceName, "123", false)}, nil)
8079
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
8180

82-
err := tested.kill(ctx, &project, compose.KillOptions{Services: []string{serviceName}, Signal: "SIGTERM"})
81+
err := tested.kill(ctx, name, compose.KillOptions{Services: []string{serviceName}, Signal: "SIGTERM"})
8382
assert.NilError(t, err)
8483
}
8584

86-
func testService(name string) types.ServiceConfig {
87-
return types.ServiceConfig{Name: name}
88-
}
89-
9085
func testContainer(service string, id string, oneOff bool) moby.Container {
9186
return moby.Container{
9287
ID: id,

pkg/compose/up.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
6060
return progress.Run(ctx, func(ctx context.Context) error {
6161
go func() {
6262
<-signalChan
63-
s.Kill(ctx, project, api.KillOptions{ // nolint:errcheck
63+
s.Kill(ctx, project.Name, api.KillOptions{ // nolint:errcheck
6464
Services: options.Create.Services,
6565
})
6666
}()

0 commit comments

Comments
 (0)