Skip to content

Commit

Permalink
Merge pull request kata-containers#1085 from bergwolf/containerd
Browse files Browse the repository at this point in the history
cli: allow to kill a stopped container and sandbox
  • Loading branch information
jodh-intel authored Jan 8, 2019
2 parents 38c9cd2 + bf2813f commit 36c267a
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 @@ -1591,6 +1591,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 36c267a

Please sign in to comment.