Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
cli: allow to kill a stopped container and sandbox
Browse files Browse the repository at this point in the history
cri containerd calls kill on stopped sandbox and if we
fail the call, it can cause `cri stopp` command to fail
too.

Fixes: #1084

Signed-off-by: Peng Tao <bergwolf@gmail.com>
  • Loading branch information
bergwolf committed Jan 8, 2019
1 parent a8e158c commit bf2813f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
14 changes: 8 additions & 6 deletions cli/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ func kill(ctx context.Context, containerID, signal string, all bool) error {
return err
}

// container MUST be created, running or paused
if status.State.State != vc.StateReady && status.State.State != vc.StateRunning && status.State.State != vc.StatePaused {
return fmt.Errorf("Container %s not ready, running or paused, cannot send a signal", containerID)
}
kataLog.WithField("signal", signal).WithField("container state", status.State.State).Info("kill")

if err := vci.KillContainer(ctx, sandboxID, containerID, signum, all); err != nil {
return err
// container MUST be created, running or paused
if status.State.State == vc.StateReady || status.State.State == vc.StateRunning || status.State.State == vc.StatePaused {
if err := vci.KillContainer(ctx, sandboxID, containerID, signum, all); err != nil {
return err
}
} else if !all {
return fmt.Errorf("container not running")
}

if signum != syscall.SIGKILL && signum != syscall.SIGTERM {
Expand Down
30 changes: 30 additions & 0 deletions cli/kill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,33 @@ func TestKillCLIFunctionKillContainerFailure(t *testing.T) {

execCLICommandFunc(assert, killCLICommand, set, true)
}

func TestKillCLIFunctionInvalidStateStoppedAllSuccess(t *testing.T) {
assert := assert.New(t)

state := vc.State{
State: vc.StateStopped,
}

testingImpl.KillContainerFunc = testKillContainerFuncReturnNil

path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
assert.NoError(err)
defer os.RemoveAll(path)

testingImpl.StatusContainerFunc = func(ctx context.Context, sandboxID, containerID string) (vc.ContainerStatus, error) {
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil
}

defer func() {
testingImpl.KillContainerFunc = nil
testingImpl.StatusContainerFunc = nil
}()

set := flag.NewFlagSet("", 0)
var all bool
set.BoolVar(&all, "all", false, "")
set.Parse([]string{"-all", testContainerID, "10"})

execCLICommandFunc(assert, killCLICommand, set, false)
}
1 change: 1 addition & 0 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ func (c *Container) setContainerState(state stateString) error {
return errNeedState
}

c.Logger().Debugf("Setting container state from %v to %v", c.state.State, state)
// update in-memory state
c.state.State = state

Expand Down
5 changes: 5 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,11 @@ func (s *Sandbox) Stop() error {
span, _ := s.trace("stop")
defer span.Finish()

if s.state.State == StateStopped {
s.Logger().Info("sandbox already stopped")
return nil
}

if err := s.state.validTransition(s.state.State, StateStopped); err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1754,3 +1754,10 @@ func TestStartNetworkMonitor(t *testing.T) {
err = s.startNetworkMonitor()
assert.Nil(t, err)
}

func TestSandboxStopStopped(t *testing.T) {
s := &Sandbox{state: State{State: StateStopped}}
err := s.Stop()

assert.Nil(t, err)
}

0 comments on commit bf2813f

Please sign in to comment.