8000 api/types/container: move StateStatus, NewStateStatus internal again · moby/moby@ec3e83a · GitHub
[go: up one dir, main page]

Skip to content

Commit ec3e83a

Browse files
committed
api/types/container: move StateStatus, NewStateStatus internal again
These types used to be internal to the container package, but were moved to the API in 1001021. However, the `StateStatus` type is only used internally; it's used as an intermediate type because [`container.State`] contains a sync.Mutex field which would make copying it unsafe (see [2998945]). This moves the type and re-introduces an internal type in the original location, effectively reverting 1001021 [`container.State`]: https://github.com/moby/moby/blob/19e79906cb347ab12deaade16bf9d82c41d777c4/container/state.go#L15-L23 [2998945]: 2998945 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 25e2b4d commit ec3e83a

File tree

9 files changed

+50
-72
lines changed

9 files changed

+50
-72
lines changed

api/types/container/state.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,3 @@ func ValidateContainerState(s ContainerState) error {
3434
return errInvalidParameter{error: fmt.Errorf("invalid value for state (%s): must be one of %s", s, strings.Join(validStates, ", "))}
3535
}
3636
}
37-
38-
// StateStatus is used to return container wait results.
39-
// Implements exec.ExitCode interface.
40-
// This type is needed as State include a sync.Mutex field which make
41-
// copying it unsafe.
42-
type StateStatus struct {
43-
exitCode int
44-
err error
45-
}
46-
47-
// ExitCode returns current exitcode for the state.
48-
func (s StateStatus) ExitCode() int {
49-
return s.exitCode
50-
}
51-
52-
// Err returns current error for the state. Returns nil if the container had
53-
// exited on its own.
54-
func (s StateStatus) Err() error {
55-
return s.err
56-
}
57-
58-
// NewStateStatus returns a new StateStatus with the given exit code and error.
59-
func NewStateStatus(exitCode int, err error) StateStatus {
60-
return StateStatus{
61-
exitCode: exitCode,
62-
err: err,
63-
}
64-
}

daemon/builder/builder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"io"
1010

11+
containerpkg "github.com/docker/docker/daemon/container"
1112
"github.com/docker/docker/daemon/internal/image"
1213
"github.com/docker/docker/daemon/internal/layer"
1314
"github.com/docker/docker/daemon/server/backend"
@@ -65,7 +66,7 @@ type ExecBackend interface {
6566
// ContainerStart starts a new container
6667
ContainerStart(ctx context.Context, containerID string, checkpoint string, checkpointDir string) error
6768
// ContainerWait stops processing until the given container is stopped.
68-
ContainerWait(ctx context.Context, name string, condition container.WaitCondition) (<-chan container.StateStatus, error)
69+
ContainerWait(ctx context.Context, name string, condition container.WaitCondition) (<-chan containerpkg.StateStatus, error)
6970
}
7071

7172
// Result is the output produced by a Builder

daemon/builder/dockerfile/mockbackend_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"runtime"
88

99
"github.com/docker/docker/daemon/builder"
10+
containerpkg "github.com/docker/docker/daemon/container"
1011
"github.com/docker/docker/daemon/internal/image"
1112
"github.com/docker/docker/daemon/internal/layer"
1213
"github.com/docker/docker/daemon/server/backend"
@@ -49,7 +50,7 @@ func (m *MockBackend) ContainerStart(ctx context.Context, containerID string, ch
4950
return nil
5051
}
5152

52-
func (m *MockBackend) ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.StateStatus, error) {
53+
func (m *MockBackend) ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan containerpkg.StateStatus, error) {
5354
return nil, nil
5455
}
5556

daemon/cluster/executor/backend.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/distribution/reference"
99
"github.com/docker/distribution"
1010
clustertypes "github.com/docker/docker/daemon/cluster/provider"
11+
containerpkg "github.com/docker/docker/daemon/container"
1112
"github.com/docker/docker/daemon/internal/image"
1213
"github.com/docker/docker/daemon/libnetwork"
1314
"github.com/docker/docker/daemon/libnetwork/cluster"
@@ -44,7 +45,7 @@ type Backend interface {
4445
DeactivateContainerServiceBinding(containerName string) error
4546
UpdateContainerServiceConfig(containerName string, serviceConfig *clustertypes.ServiceConfig) error
4647
ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (*container.InspectResponse, error)
47-
ContainerWait(ctx context.Context, name string, condition container.WaitCondition) (<-chan container.StateStatus, error)
48+
ContainerWait(ctx context.Context, name string, condition container.WaitCondition) (<-chan containerpkg.StateStatus, error)
4849
ContainerRm(name string, config *backend.ContainerRmConfig) error
4950
ContainerKill(name string, sig string) error
5051
SetContainerDependencyStore(name string, store exec.DependencyGetter) error

daemon/cluster/executor/container/adapter.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/docker/docker/daemon"
1717
"github.com/docker/docker/daemon/cluster/convert"
1818
executorpkg "github.com/docker/docker/daemon/cluster/executor"
19+
containerpkg "github.com/docker/docker/daemon/container"
1920
"github.com/docker/docker/daemon/libnetwork"
2021
networkSettings "github.com/docker/docker/daemon/network"
2122
"github.com/docker/docker/daemon/server/backend"
@@ -415,7 +416,7 @@ func (c *containerAdapter) events(ctx context.Context) <-chan events.Message {
415416
return eventsq
416417
}
417418

418-
func (c *containerAdapter) wait(ctx context.Context) (<-chan containertypes.StateStatus, error) {
419+
func (c *containerAdapter) wait(ctx context.Context) (<-chan containerpkg.StateStatus, error) {
419420
return c.backend.ContainerWait(ctx, c.container.nameOrID(), containertypes.WaitConditionNotRunning)
420421
}
421422

daemon/container/state.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ type State struct {
4646
Health *Health
4747
Removed bool `json:"-"`
4848

49-
stopWaiters []chan<- container.StateStatus
50-
removeOnlyWaiters []chan<- container.StateStatus
49+
stopWaiters []chan<- StateStatus
50+
removeOnlyWaiters []chan<- StateStatus
5151

5252
// The libcontainerd reference fields are unexported to force consumers
5353
// to access them through the getter methods with multi-valued returns
@@ -58,6 +58,26 @@ type State struct {
5858
task libcontainerdtypes.Task
5959
}
6060

61+
// StateStatus is used to return container wait results.
62+
// Implements exec.ExitCode interface.
63+
// This type is needed as State include a sync.Mutex field which make
64+
// copying it unsafe.
65+
type StateStatus struct {
66+
exitCode int
67+
err error
68+
}
69+
70+
// ExitCode returns current exitcode for the state.
71+
func (s StateStatus) ExitCode() int {
72+
return s.exitCode
73+
}
74+
75+
// Err returns current error for the state. Returns nil if the container had
76+
// exited on its own.
77+
func (s StateStatus) Err() error {
78+
return s.err
79+
}
80+
6181
// NewState creates a default state object.
6282
func NewState() *State {
6383
return &State{}
@@ -138,20 +158,23 @@ func (s *State) StateString() container.ContainerState {
138158
// be nil and its ExitCode() method will return the container's exit code,
139159
// otherwise, the results Err() method will return an error indicating why the
140160
// wait operation failed.
141-
func (s *State) Wait(ctx context.Context, condition container.WaitCondition) <-chan container.StateStatus {
161+
func (s *State) Wait(ctx context.Context, condition container.WaitCondition) <-chan StateStatus {
142162
s.Lock()
143163
defer s.Unlock()
144164

145165
// Buffer so we can put status and finish even nobody receives it.
146-
resultC := make(chan container.StateStatus, 1)
166+
resultC := make(chan StateStatus, 1)
147167

148168
if s.conditionAlreadyMet(condition) {
149-
resultC <- container.NewStateStatus(s.ExitCode(), s.Err())
169+
resultC <- StateStatus{
170+
exitCode: s.ExitCodeValue,
171+
err: s.Err(),
172+
}
150173

151174
return resultC
152175
}
153176

154-
waitC := make(chan container.StateStatus, 1)
177+
waitC := make(chan StateStatus, 1)
155178

156179
// Removal wakes up both removeOnlyWaiters and stopWaiters
157180
// Container could be removed while still in "created" state
@@ -166,8 +189,10 @@ func (s *State) Wait(ctx context.Context, condition container.WaitCondition) <-c
166189
select {
167190
case <-ctx.Done():
168191
// Context timeout or cancellation.
169-
resultC <- container.NewStateStatus(-1, ctx.Err())
170-
192+
resultC <- StateStatus{
193+
exitCode: -1,
194+
err: ctx.Err(),
195+
}
171196
return
172197
case status := <-waitC:
173198
resultC <- status
@@ -397,8 +422,11 @@ func (s *State) Err() error {
397422
return nil
398423
}
399424

400-
func (s *State) notifyAndClear(waiters *[]chan<- container.StateStatus) {
401-
result := container.NewStateStatus(s.ExitCodeValue, s.Err())
425+
func (s *State) notifyAndClear(waiters *[]chan<- StateStatus) {
426+
result := StateStatus{
427+
exitCode: s.ExitCodeValue,
428+
err: s.Err(),
429+
}
402430

403431
for _, c := range *waiters {
404432
c <- result

daemon/server/router/container/backend.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"io"
66

7+
containerpkg "github.com/docker/docker/daemon/container"
78
"github.com/docker/docker/daemon/server/backend"
89
"github.com/moby/go-archive"
910
"github.com/moby/moby/api/types/container"
@@ -40,7 +41,7 @@ type stateBackend interface {
4041
ContainerStop(ctx context.Context, name string, options container.StopOptions) error
4142
ContainerUnpause(name string) error
4243
ContainerUpdate(name string, hostConfig *container.HostConfig) (container.UpdateResponse, error)
43-
ContainerWait(ctx context.Context, name string, condition container.WaitCondition) (<-chan container.StateStatus, error)
44+
ContainerWait(ctx context.Context, name string, condition container.WaitCondition) (<-chan containerpkg.StateStatus, error)
4445
}
4546

4647
// monitorBackend includes functions to implement to provide containers monitoring functionality.

daemon/wait.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package daemon
33
import (
44
"context"
55

6+
"github.com/docker/docker/daemon/container"
67
containertypes "github.com/moby/moby/api/types/container"
78
)
89

@@ -13,7 +14,7 @@ import (
1314
// condition is met or if an error occurs waiting for the container (such as a
1415
// context timeout or cancellation). On a successful wait, the exit code of the
1516
// container is returned in the status with a non-nil Err() value.
16-
func (daemon *Daemon) ContainerWait(ctx context.Context, name string, condition containertypes.WaitCondition) (<-chan containertypes.StateStatus, error) {
17+
func (daemon *Daemon) ContainerWait(ctx context.Context, name string, condition containertypes.WaitCondition) (<-chan container.StateStatus, error) {
1718
cntr, err := daemon.GetContainer(name)
1819
if err != nil {
1920
return nil, err

vendor/github.com/moby/moby/api/types/container/state.go

Lines changed: 0 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0