8000 Use container status values from api · moby/moby@1001021 · GitHub
[go: up one dir, main page]

Skip to content
  • Pricing
  • Search code, repositories, users, issues, pull requests...

    Provide feedback

    We read every piece of feedback, and take your input very seriously.

    Saved searches

    Use saved searches to filter your results more quickly

    Appearance settings

    Commit 1001021

    Browse files
    committed
    Use container status values from api
    Alias and deprecate the status types and constants from the root container package. The root container package is intended for use within the daemon and no the api package. Signed-off-by: Derek McGowan <derek@mcg.dev>
    1 parent 19e7990 commit 1001021

    File tree

    15 files changed

    +86
    -77
    lines changed

    15 files changed

    +86
    -77
    lines changed

    api/server/router/container/backend.go

    Lines changed: 1 addition & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -7,7 +7,6 @@ import (
    77
    "github.com/docker/docker/api/types/backend"
    88
    "github.com/docker/docker/api/types/container"
    99
    "github.com/docker/docker/api/types/filters"
    10-
    containerpkg "github.com/docker/docker/container"
    1110
    "github.com/moby/go-archive"
    1211
    )
    1312

    @@ -41,7 +40,7 @@ type stateBackend interface {
    4140
    ContainerStop(ctx context.Context, name string, options container.StopOptions) error
    4241
    ContainerUnpause(name string) error
    4342
    ContainerUpdate(name string, hostConfig *container.HostConfig) (container.UpdateResponse, error)
    44-
    ContainerWait(ctx context.Context, name string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error)
    43+
    ContainerWait(ctx context.Context, name string, condition container.WaitCondition) (<-chan container.StateStatus, error)
    4544
    }
    4645

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

    api/server/router/container/container_routes.go

    Lines changed: 4 additions & 5 deletions
    Original file line numberDiff line numberDiff line change
    @@ -21,7 +21,6 @@ import (
    2121
    "github.com/docker/docker/api/types/mount"
    2222
    "github.com/docker/docker/api/types/network"
    2323
    "github.com/docker/docker/api/types/versions"
    24-
    containerpkg "github.com/docker/docker/container"
    2524
    networkSettings "github.com/docker/docker/daemon/network"
    2625
    "github.com/docker/docker/errdefs"
    2726
    "github.com/docker/docker/libnetwork/netlabel"
    @@ -337,19 +336,19 @@ func (c *containerRouter) postContainersWait(ctx context.Context, w http.Respons
    337336
    legacyRemovalWaitPre134 := false
    338337

    339338
    // The wait condition defaults to "not-running".
    340-
    waitCondition := containerpkg.WaitConditionNotRunning
    339+
    waitCondition := container.WaitConditionNotRunning
    341340
    if !legacyBehaviorPre130 {
    342341
    if err := httputils.ParseForm(r); err != nil {
    343342
    return err
    344343
    }
    345344
    if v := r.Form.Get("condition"); v != "" {
    346345
    switch container.WaitCondition(v) {
    347346
    case container.WaitConditionNotRunning:
    348-
    waitCondition = containerpkg.WaitConditionNotRunning
    347+
    waitCondition = container.WaitConditionNotRunning
    349348
    case container.WaitConditionNextExit:
    350-
    waitCondition = containerpkg.WaitConditionNextExit
    349+
    waitCondition = container.WaitConditionNextExit
    351350
    case container.WaitConditionRemoved:
    352-
    waitCondition = containerpkg.WaitConditionRemoved
    351+
    waitCondition = container.WaitConditionRemoved
    353352
    legacyRemovalWaitPre134 = versions.LessThan(version, "1.34")
    354353
    default:
    355354
    return errdefs.InvalidParameter(errors.Errorf("invalid condition: %q", v))

    api/types/container/state.go

    Lines changed: 29 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,29 @@
    1+
    package container
    2+
    3+
    // StateStatus is used to return container wait results.
    4+
    // Implements exec.ExitCode interface.
    5+
    // This type is needed as State include a sync.Mutex field which make
    6+
    // copying it unsafe.
    7+
    type StateStatus struct {
    8+
    exitCode int
    9+
    err error
    10+
    }
    11+
    12+
    // ExitCode returns current exitcode for the state.
    13+
    func (s StateStatus) ExitCode() int {
    14+
    return s.exitCode
    15+
    }
    16+
    17+
    // Err returns current error for the state. Returns nil if the container had
    18+
    // exited on its own.
    19+
    func (s StateStatus) Err() error {
    20+
    return s.err
    21+
    }
    22+
    23+
    // NewStateStatus returns a new StateStatus with the given exit code and error.
    24+
    func NewStateStatus(exitCode int, err error) StateStatus {
    25+
    return StateStatus{
    26+
    exitCode: exitCode,
    27+
    err: err,
    28+
    }
    29+
    }

    builder/builder.go

    Lines changed: 1 addition & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -10,7 +10,6 @@ import (
    1010

    1111
    "github.com/docker/docker/api/types/backend"
    1212
    "github.com/docker/docker/api/types/container"
    13-
    containerpkg "github.com/docker/docker/container"
    1413
    "github.com/docker/docker/image"
    1514
    "github.com/docker/docker/layer"
    1615
    "github.com/opencontainers/go-digest"
    @@ -66,7 +65,7 @@ type ExecBackend interface {
    6665
    // ContainerStart starts a new container
    6766
    ContainerStart(ctx context.Context, containerID string, checkpoint string, checkpointDir string) error
    6867
    // ContainerWait stops processing until the given container is stopped.
    69-
    ContainerWait(ctx context.Context, name string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error)
    68+
    ContainerWait(ctx context.Context, name string, condition container.WaitCondition) (<-chan container.StateStatus, error)
    7069
    }
    7170

    7271
    // Result is the output produced by a Builder

    builder/dockerfile/containerbackend.go

    Lines changed: 1 addition & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -9,7 +9,6 @@ import (
    99
    "github.com/docker/docker/api/types/backend"
    1010
    "github.com/docker/docker/api/types/container"
    1111
    "github.com/docker/docker/builder"
    12-
    containerpkg "github.com/docker/docker/container"
    1312
    "github.com/docker/docker/errdefs"
    1413
    "github.com/docker/docker/pkg/stringid"
    1514
    "github.com/pkg/errors"
    @@ -85,7 +84,7 @@ func (c *containerManager) Run(ctx context.Context, cID string, stdout, stderr i
    8584
    return err
    8685
    }
    8786

    88-
    waitC, err := c.backend.ContainerWait(ctx, cID, containerpkg.WaitConditionNotRunning)
    87+
    waitC, err := c.backend.ContainerWait(ctx, cID, container.WaitConditionNotRunning)
    8988
    if err != nil {
    9089
    close(finished)
    9190
    logCancellationError(cancelErrCh, fmt.Sprintf("unable to begin ContainerWait: %s", err))

    builder/dockerfile/mockbackend_test.go

    Lines changed: 1 addition & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -9,7 +9,6 @@ import (
    99
    "github.com/docker/docker/api/types/backend"
    1010
    "github.com/docker/docker/api/types/container"
    1111
    "github.com/docker/docker/builder"
    12-
    containerpkg "github.com/docker/docker/container"
    1312
    "github.com/docker/docker/image"
    1413
    "github.com/docker/docker/layer"
    1514
    "github.com/opencontainers/go-digest"
    @@ -50,7 +49,7 @@ func (m *MockBackend) ContainerStart(ctx context.Context, containerID string, ch
    5049
    return nil
    5150
    }
    5251

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

    container/state.go

    Lines changed: 26 additions & 41 deletions
    Original file line numberDiff line numberDiff line change
    @@ -39,8 +39,8 @@ type State struct {
    3939
    Health *Health
    4040
    Removed bool `json:"-"`
    4141

    42-
    stopWaiters []chan<- StateStatus
    43-
    removeOnlyWaiters []chan<- StateStatus
    42+
    stopWaiters []chan<- container.StateStatus
    43+
    removeOnlyWaiters []chan<- container.StateStatus
    4444

    4545
    // The libcontainerd reference fields are unexported to force consumers
    4646
    // to access them through the getter methods with multi-valued returns
    @@ -55,21 +55,9 @@ type State struct {
    5555
    // Implements exec.ExitCode interface.
    5656
    // This type is needed as State include a sync.Mutex field which make
    5757
    // copying it unsafe.
    58-
    type StateStatus struct {
    59-
    exitCode int
    60-
    err error
    61-
    }
    62-
    63-
    // ExitCode returns current exitcode for the state.
    64-
    func (s StateStatus) ExitCode() int {
    65-
    return s.exitCode
    66-
    }
    67-
    68-
    // Err returns current error for the state. Returns nil if the container had
    69-
    // exited on its own.
    70-
    func (s StateStatus) Err() error {
    71-
    return s.err
    72-
    }
    58+
    //
    59+
    // Deprecated: use [container.StateStatus] instead.
    60+
    type StateStatus = container.StateStatus
    7361

    7462
    // NewState creates a default state object.
    7563
    func NewState() *State {
    @@ -162,7 +150,9 @@ func IsValidStateString(s string) bool {
    162150
    }
    163151

    164152
    // WaitCondition is an enum type for different states to wait for.
    165-
    type WaitCondition int
    153+
    //
    154+
    // Deprecated: use [container.WaitCondition] instead.
    155+
    type WaitCondition = container.WaitCondition
    166156

    167157
    // Possible WaitCondition Values.
    168158
    //
    @@ -176,9 +166,12 @@ type WaitCondition int
    176166
    //
    177167
    // WaitConditionRemoved is used to wait for the container to be removed.
    178168
    const (
    179-
    WaitConditionNotRunning WaitCondition = iota
    180-
    WaitConditionNextExit
    181-
    WaitConditionRemoved
    169+
    // Deprecated: use [container.WaitConditionNotRunning] instead.
    170+
    WaitConditionNotRunning = container.WaitConditionNotRunning
    171+
    // Deprecated: use [container.WaitConditionNextExit] instead.
    172+
    WaitConditionNextExit = container.WaitConditionNextExit
    173+
    // Deprecated: use [container.WaitConditionRemoved] instead.
    174+
    WaitConditionRemoved = container.WaitConditionRemoved
    182175
    )
    183176

    184177
    // Wait waits until the container is in a certain state indicated by the given
    @@ -189,28 +182,25 @@ const (
    189182
    // be nil and its ExitCode() method will return the container's exit code,
    190183
    // otherwise, the results Err() method will return an error indicating why the
    191184
    // wait operation failed.
    192-
    func (s *State) Wait(ctx context.Context, condition WaitCondition) <-chan StateStatus {
    185+
    func (s *State) Wait(ctx context.Context, condition WaitCondition) <-chan container.StateStatus {
    193186
    s.Lock()
    194187
    defer s.Unlock()
    195188

    196189
    // Buffer so we can put status and finish even nobody receives it.
    197-
    resultC := make(chan StateStatus, 1)
    190+
    resultC := make(chan container.StateStatus, 1)
    198191

    199192
    if s.conditionAlreadyMet(condition) {
    200-
    resultC <- StateStatus{
    201-
    exitCode: s.ExitCode(),
    202-
    err: s.Err(),
    203-
    }
    193+
    resultC <- container.NewStateStatus(s.ExitCode(), s.Err())
    204194

    205195
    return resultC
    206196
    }
    207197

    208-
    waitC := make(chan StateStatus, 1)
    198+
    waitC := make(chan container.StateStatus, 1)
    209199

    210200
    // Removal wakes up both removeOnlyWaiters and stopWaiters
    211201
    // Container could be removed while still in "created" state
    212202
    // in which case it is never actually stopped
    213-
    if condition == WaitConditionRemoved {
    203+
    if condition == container.WaitConditionRemoved {
    214204
    s.removeOnlyWaiters = append(s.removeOnlyWaiters, waitC)
    215205
    } else {
    216206
    s.stopWaiters = append(s.stopWaiters, waitC)
    @@ -220,10 +210,8 @@ func (s *State) Wait(ctx context.Context, condition WaitCondition) <-chan StateS
    220210
    select {
    221211
    case <-ctx.Done():
    222212
    // Context timeout or cancellation.
    223-
    resultC <- StateStatus{
    224-
    exitCode: -1,
    225-
    err: ctx.Err(),
    226-
    }
    213+
    resultC <- container.NewStateStatus(-1, ctx.Err())
    214+
    227215
    return
    228216
    case status := <-waitC:
    229217
    resultC <- status
    @@ -233,11 +221,11 @@ func (s *State) Wait(ctx context.Context, condition WaitCondition) <-chan StateS
    233221
    return resultC
    234222
    }
    235223

    236-
    func (s *State) conditionAlreadyMet(condition WaitCondition) bool {
    224+
    func (s *State) conditionAlreadyMet(condition container.WaitCondition) bool {
    237225
    switch condition {
    238-
    case WaitConditionNotRunning:
    226+
    case container.WaitConditionNotRunning:
    239227
    return !s.Running
    240-
    case WaitConditionRemoved:
    228+
    case container.WaitConditionRemoved:
    241229
    return s.Removed
    242230
    default:
    243231
    // TODO(thaJeztah): how do we want to handle "WaitConditionNextExit"?
    @@ -423,11 +411,8 @@ func (s *State) Err() error {
    423411
    return nil
    424412
    }
    425413

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

    432417
    for _, c := range *waiters {
    433418
    c <- result

    container/state_test.go

    Lines changed: 9 additions & 9 deletions
    Original file line numberDiff line numberDiff line change
    @@ -43,7 +43,7 @@ func TestStateRunStop(t *testing.T) {
    4343
    // within 200 milliseconds.
    4444
    ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
    4545
    defer cancel()
    46-
    removalWait := s.Wait(ctx, WaitConditionRemoved)
    46+
    removalWait := s.Wait(ctx, container.WaitConditionRemoved)
    4747

    4848
    // Full lifecycle two times.
    4949
    for i := 1; i <= 2; i++ {
    @@ -55,7 +55,7 @@ func TestStateRunStop(t *testing.T) {
    5555
    defer cancel()
    5656
    // Expectx exit code to be i-1 since it should be the exit
    5757
    // code from the previous loop or 0 for the created state.
    58-
    if status := <-s.Wait(ctx, WaitConditionNotRunning); status.ExitCode() != i-1 {
    58+
    if status := <-s.Wait(ctx, container.WaitConditionNotRunning); status.ExitCode() != i-1 {
    5959
    t.Fatalf("ExitCode %v, expected %v, err %q", status.ExitCode(), i-1, status.Err())
    6060
    }
    6161

    @@ -64,7 +64,7 @@ func TestStateRunStop(t *testing.T) {
    6464
    // than 100 milliseconds.
    6565
    ctx, cancel = context.WithTimeout(context.Background(), 100*time.Millisecond)
    6666
    defer cancel()
    67-
    initialWait := s.Wait(ctx, WaitConditionNextExit)
    67+
    initialWait := s.Wait(ctx, container.WaitConditionNextExit)
    6868

    6969
    // Set the state to "Running".
    7070
    s.Lock()
    @@ -87,7 +87,7 @@ func TestStateRunStop(t *testing.T) {
    8787
    // more than 100 milliseconds.
    8888
    ctx, cancel = context.WithTimeout(context.Background(), 100*time.Millisecond)
    8989
    defer cancel()
    90-
    exitWait := s.Wait(ctx, WaitConditionNotRunning)
    90+
    exitWait := s.Wait(ctx, container.WaitConditionNotRunning)
    9191

    9292
    // Set the state to "Exited".
    9393
    s.Lock()
    @@ -139,7 +139,7 @@ func TestStateTimeoutWait(t *testing.T) {
    139139
    // Start a wait with a timeout.
    140140
    ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
    141141
    defer cancel()
    142-
    waitC := s.Wait(ctx, WaitConditionNotRunning)
    142+
    waitC := s.Wait(ctx, container.WaitConditionNotRunning)
    143143

    144144
    // It should timeout *before* this 200ms timer does.
    145145
    select {
    @@ -164,7 +164,7 @@ func TestStateTimeoutWait(t *testing.T) {
    164164
    // immediately.
    165165
    ctx, cancel = context.WithTimeout(context.Background(), 100*time.Millisecond)
    166166
    defer cancel()
    167-
    waitC = s.Wait(ctx, WaitConditionNotRunning)
    167+
    waitC = s.Wait(ctx, container.WaitConditionNotRunning)
    168168

    169169
    select {
    170170
    case <-time.After(200 * time.Millisecond):
    @@ -185,7 +185,7 @@ func TestCorrectStateWaitResultAfterRestart(t *testing.T) {
    185185
    s.SetRunning(nil, nil, time.Now())
    186186
    s.Unlock()
    187187

    188-
    waitC := s.Wait(context.Background(), WaitConditionNotRunning)
    188+
    waitC := s.Wait(context.Background(), container.WaitConditionNotRunning)
    189189
    want := ExitStatus{ExitCode: 10, ExitedAt: time.Now()}
    190190

    191191
    s.Lock()
    @@ -197,8 +197,8 @@ func TestCorrectStateWaitResultAfterRestart(t *testing.T) {
    197197
    s.Unlock()
    198198

    199199
    got := <-waitC
    200-
    if got.exitCode != want.ExitCode {
    201-
    t.Fatalf("expected exit code %v, got %v", want.ExitCode, got.exitCode)
    200+
    if got.ExitCode() != want.ExitCode {
    201+
    t.Fatalf("expected exit code %v, got %v", want.ExitCode, got.ExitCode())
    202202
    }
    203203
    }
    204204

    daemon/attach.go

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

    88
    "github.com/containerd/log"
    99
    "github.com/docker/docker/api/types/backend"
    10+
    containertypes "github.com/docker/docker/api/types/container"
    1011
    "github.com/docker/docker/api/types/events"
    1112
    "github.com/docker/docker/container"
    1213
    "github.com/docker/docker/container/stream"
    @@ -191,7 +192,7 @@ func (daemon *Daemon) containerAttach(ctr *container.Container, cfg *stream.Atta
    191192

    192193
    if ctr.Config.StdinOnce && !ctr.Config.Tty {
    193194
    // Wait for the container to stop before returning.
    194-
    waitChan := ctr.Wait(context.Background(), container.WaitConditionNotRunning)
    195+
    waitChan := ctr.Wait(context.Background(), containertypes.WaitConditionNotRunning)
    195196
    defer func() {
    196197
    <-waitChan // Ignore returned exit code.
    197198
    }()

    daemon/cluster/executor/backend.go

    Lines changed: 1 addition & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -16,7 +16,6 @@ import (
    1616
    "github.com/docker/docker/api/types/swarm"
    1717
    "github.com/docker/docker/api/types/system"
    1818
    "github.com/docker/docker/api/types/volume"
    19-
    containerpkg "github.com/docker/docker/container"
    2019
    clustertypes "github.com/docker/docker/daemon/cluster/provider"
    2120
    networkSettings "github.com/docker/docker/daemon/network"
    2221
    "github.com/docker/docker/image"
    @@ -45,7 +44,7 @@ type Backend interface {
    4544
    DeactivateContainerServiceBinding(containerName string) error
    4645
    UpdateContainerServiceConfig(containerName string, serviceConfig *clustertypes.ServiceConfig) error
    4746
    ContainerInspect(ctx context.Context, name string, options backend.ContainerInspectOptions) (*container.InspectResponse, error)
    48-
    ContainerWait(ctx context.Context, name string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error)
    47+
    ContainerWait(ctx context.Context, name string, condition container.WaitCondition) (<-chan container.StateStatus, error)
    4948
    ContainerRm(name string, config *backend.ContainerRmConfig) error
    5049
    ContainerKill(name string, sig string) error
    5150
    SetContainerDependencyStore(name string, store exec.DependencyGetter) error

    0 commit comments

    Comments
     (0)
    0