Skip to content

Commit

Permalink
get Tty from container to know adequate way to attach to
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Apr 4, 2022
1 parent f2d9acd commit 29eed2b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pkg/compose/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container s
}
}

streamIn, streamOut, err := s.getContainerStreams(ctx, container)
streamIn, streamOut, err := s.getContainerStreams(ctx, container, stdin != nil)
if err != nil {
return restore, detached, err
}
Expand Down Expand Up @@ -134,12 +134,12 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container s
return restore, detached, nil
}

func (s *composeService) getContainerStreams(ctx context.Context, container string) (io.WriteCloser, io.ReadCloser, error) {
func (s *composeService) getContainerStreams(ctx context.Context, container string, attachStdin bool) (io.WriteCloser, io.ReadCloser, error) {
var stdout io.ReadCloser
var stdin io.WriteCloser
cnx, err := s.apiClient().ContainerAttach(ctx, container, moby.ContainerAttachOptions{
Stream: true,
Stdin: true,
Stdin: attachStdin,
Stdout: true,
Stderr: true,
Logs: false,
Expand Down
17 changes: 12 additions & 5 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func escapeDollarSign(marshal []byte) []byte {
}

// projectFromName builds a types.Project based on actual resources with compose labels set
func (s *composeService) projectFromName(containers Containers, projectName string, services ...string) (*types.Project, error) {
func (s *composeService) projectFromName(ctx context.Context, containers Containers, projectName string, services ...string) (*types.Project, error) {
project := &types.Project{
Name: projectName,
}
Expand All @@ -129,13 +129,20 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
}
set := map[string]*types.ServiceConfig{}
for _, c := range containers {
inspect, err := s.dockerCli.Client().ContainerInspect(ctx, c.ID)
if err != nil {
return nil, err
}

serviceLabel := c.Labels[api.ServiceLabel]
_, ok := set[serviceLabel]
if !ok {
set[serviceLabel] = &types.ServiceConfig{
Name: serviceLabel,
Image: c.Image,
Labels: c.Labels,
Name: serviceLabel,
Image: c.Image,
Labels: c.Labels,
Tty: inspect.Config.Tty,
StdinOpen: inspect.Config.OpenStdin,
}
}
set[serviceLabel].Scale++
Expand Down Expand Up @@ -184,7 +191,7 @@ func (s *composeService) actualState(ctx context.Context, projectName string, se
return nil, nil, err
}

project, err := s.projectFromName(containers, projectName, services...)
project, err := s.projectFromName(ctx, containers, projectName, services...)
if err != nil && !api.IsNotFoundError(err) {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer

func (s *composeService) getProjectWithResources(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
containers = containers.filter(isNotOneOff)
project, _ := s.projectFromName(containers, projectName)
project, _ := s.projectFromName(ctx, containers, projectName)

volumes, err := s.actualVolumes(ctx, projectName)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/compose/down_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func TestDown(t *testing.T) {
testContainer("service2", "789", false),
testContainer("service_orphan", "321", true),
}, nil)
api.EXPECT().ContainerInspect(gomock.Any(), "123").Return(moby.ContainerJSON{}, nil)
api.EXPECT().ContainerInspect(gomock.Any(), "456").Return(moby.ContainerJSON{}, nil)
api.EXPECT().ContainerInspect(gomock.Any(), "789").Return(moby.ContainerJSON{}, nil)
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(strings.ToLower(testProject)))).
Return(volume.VolumeListOKBody{}, nil)
api.EXPECT().NetworkList(gomock.Any(), moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject)))}).
Expand Down Expand Up @@ -81,6 +84,8 @@ func TestDownRemoveOrphans(t *testing.T) {
testContainer("service2", "789", false),
testContainer("service_orphan", "321", true),
}, nil)
api.EXPECT().ContainerInspect(gomock.Any(), "123").Return(moby.ContainerJSON{}, nil)
api.EXPECT().ContainerInspect(gomock.Any(), "789").Return(moby.ContainerJSON{}, nil)
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(strings.ToLower(testProject)))).
Return(volume.VolumeListOKBody{}, nil)
api.EXPECT().NetworkList(gomock.Any(), moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject)))}).
Expand Down Expand Up @@ -112,6 +117,7 @@ func TestDownRemoveVolumes(t *testing.T) {

api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return(
[]moby.Container{testContainer("service1", "123", false)}, nil)
api.EXPECT().ContainerInspect(gomock.Any(), "123").Return(moby.ContainerJSON{}, nil)
api.EXPECT().VolumeList(gomock.Any(), filters.NewArgs(projectFilter(strings.ToLower(testProject)))).
Return(volume.VolumeListOKBody{
Volumes: []*moby.Volume{{Name: "myProject_volume"}},
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *composeService) restart(ctx context.Context, projectName string, option
return err
}

project, err := s.projectFromName(observedState, projectName, options.Services...)
project, err := s.projectFromName(ctx, observedState, projectName, options.Services...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *composeService) start(ctx context.Context, projectName string, options
return err
}

project, err := s.projectFromName(containers, projectName, options.AttachTo...)
project, err := s.projectFromName(ctx, containers, projectName, options.AttachTo...)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/compose/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func TestStopTimeout(t *testing.T) {
testContainer("service1", "456", false),
testContainer("service2", "789", false),
}, nil)
api.EXPECT().ContainerInspect(gomock.Any(), "123").Return(moby.ContainerJSON{}, nil)
api.EXPECT().ContainerInspect(gomock.Any(), "456").Return(moby.ContainerJSON{}, nil)
api.EXPECT().ContainerInspect(gomock.Any(), "789").Return(moby.ContainerJSON{}, nil)

timeout := time.Duration(2) * time.Second
api.EXPECT().ContainerStop(gomock.Any(), "123", &timeout).Return(nil)
Expand Down

0 comments on commit 29eed2b

Please sign in to comment.