Merge pull request #904 from sboeuf/export_sandbox_api

sandbox: Extend sandbox API
This commit is contained in:
Sebastien Boeuf 2018-11-14 09:01:21 -08:00 committed by GitHub
commit 0911331974
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 132 additions and 66 deletions

View File

@ -232,7 +232,7 @@ func StartSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
func startSandbox(s *Sandbox) (*Sandbox, error) { func startSandbox(s *Sandbox) (*Sandbox, error) {
// Start it // Start it
err := s.start() err := s.Start()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -264,16 +264,11 @@ func StopSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) {
defer s.releaseStatelessSandbox() defer s.releaseStatelessSandbox()
// Stop it. // Stop it.
err = s.stop() err = s.Stop()
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Remove the network.
if err := s.removeNetwork(); err != nil {
return nil, err
}
return s, nil return s, nil
} }
@ -473,12 +468,7 @@ func StartContainer(ctx context.Context, sandboxID, containerID string) (VCConta
} }
defer s.releaseStatelessSandbox() defer s.releaseStatelessSandbox()
c, err := s.StartContainer(containerID) return s.StartContainer(containerID)
if err != nil {
return nil, err
}
return c, nil
} }
// StopContainer is the virtcontainers container stopping entry point. // StopContainer is the virtcontainers container stopping entry point.
@ -507,19 +497,7 @@ func StopContainer(ctx context.Context, sandboxID, containerID string) (VCContai
} }
defer s.releaseStatelessSandbox() defer s.releaseStatelessSandbox()
// Fetch the container. return s.StopContainer(containerID)
c, err := s.findContainer(containerID)
if err != nil {
return nil, err
}
// Stop it.
err = c.stop()
if err != nil {
return nil, err
}
return c, nil
} }
// EnterContainer is the virtcontainers container command execution entry point. // EnterContainer is the virtcontainers container command execution entry point.
@ -672,19 +650,7 @@ func KillContainer(ctx context.Context, sandboxID, containerID string, signal sy
} }
defer s.releaseStatelessSandbox() defer s.releaseStatelessSandbox()
// Fetch the container. return s.KillContainer(containerID, signal, all)
c, err := s.findContainer(containerID)
if err != nil {
return err
}
// Send a signal to the process.
err = c.kill(signal, all)
if err != nil {
return err
}
return nil
} }
// PauseSandbox is the virtcontainers pausing entry point which pauses an // PauseSandbox is the virtcontainers pausing entry point which pauses an
@ -731,13 +697,7 @@ func ProcessListContainer(ctx context.Context, sandboxID, containerID string, op
} }
defer s.releaseStatelessSandbox() defer s.releaseStatelessSandbox()
// Fetch the container. return s.ProcessListContainer(containerID, options)
c, err := s.findContainer(containerID)
if err != nil {
return nil, err
}
return c.processList(options)
} }
// UpdateContainer is the virtcontainers entry point to update // UpdateContainer is the virtcontainers entry point to update
@ -819,17 +779,11 @@ func togglePauseContainer(ctx context.Context, sandboxID, containerID string, pa
} }
defer s.releaseStatelessSandbox() defer s.releaseStatelessSandbox()
// Fetch the container.
c, err := s.findContainer(containerID)
if err != nil {
return err
}
if pause { if pause {
return c.pause() return s.PauseContainer(containerID)
} }
return c.resume() return s.ResumeContainer(containerID)
} }
// PauseContainer is the virtcontainers container pause entry point. // PauseContainer is the virtcontainers container pause entry point.

View File

@ -66,6 +66,8 @@ type VCSandbox interface {
ID() string ID() string
SetAnnotations(annotations map[string]string) error SetAnnotations(annotations map[string]string) error
Start() error
Stop() error
Pause() error Pause() error
Resume() error Resume() error
Release() error Release() error
@ -75,10 +77,15 @@ type VCSandbox interface {
CreateContainer(contConfig ContainerConfig) (VCContainer, error) CreateContainer(contConfig ContainerConfig) (VCContainer, error)
DeleteContainer(contID string) (VCContainer, error) DeleteContainer(contID string) (VCContainer, error)
StartContainer(containerID string) (VCContainer, error) StartContainer(containerID string) (VCContainer, error)
StopContainer(containerID string) (VCContainer, error)
KillContainer(containerID string, signal syscall.Signal, all bool) error
StatusContainer(containerID string) (ContainerStatus, error) StatusContainer(containerID string) (ContainerStatus, error)
StatsContainer(containerID string) (ContainerStats, error) StatsContainer(containerID string) (ContainerStats, error)
PauseContainer(containerID string) error
ResumeContainer(containerID string) error
EnterContainer(containerID string, cmd Cmd) (VCContainer, *Process, error) EnterContainer(containerID string, cmd Cmd) (VCContainer, *Process, error)
UpdateContainer(containerID string, resources specs.LinuxResources) error UpdateContainer(containerID string, resources specs.LinuxResources) error
ProcessListContainer(containerID string, options ProcessListOptions) (ProcessList, error)
WaitProcess(containerID, processID string) (int32, error) WaitProcess(containerID, processID string) (int32, error)
SignalProcess(containerID, processID string, signal syscall.Signal, all bool) error SignalProcess(containerID, processID string, signal syscall.Signal, all bool) error
WinsizeProcess(containerID, processID string, height, width uint32) error WinsizeProcess(containerID, processID string, height, width uint32) error

View File

@ -67,6 +67,16 @@ func (s *Sandbox) Release() error {
return nil return nil
} }
// Start implements the VCSandbox function of the same name.
func (s *Sandbox) Start() error {
return nil
}
// Stop implements the VCSandbox function of the same name.
func (s *Sandbox) Stop() error {
return nil
}
// Pause implements the VCSandbox function of the same name. // Pause implements the VCSandbox function of the same name.
func (s *Sandbox) Pause() error { func (s *Sandbox) Pause() error {
return nil return nil
@ -97,6 +107,16 @@ func (s *Sandbox) StartContainer(contID string) (vc.VCContainer, error) {
return &Container{}, nil return &Container{}, nil
} }
// StopContainer implements the VCSandbox function of the same name.
func (s *Sandbox) StopContainer(contID string) (vc.VCContainer, error) {
return &Container{}, nil
}
// KillContainer implements the VCSandbox function of the same name.
func (s *Sandbox) KillContainer(contID string, signal syscall.Signal, all bool) error {
return nil
}
// StatusContainer implements the VCSandbox function of the same name. // StatusContainer implements the VCSandbox function of the same name.
func (s *Sandbox) StatusContainer(contID string) (vc.ContainerStatus, error) { func (s *Sandbox) StatusContainer(contID string) (vc.ContainerStatus, error) {
return vc.ContainerStatus{}, nil return vc.ContainerStatus{}, nil
@ -107,6 +127,16 @@ func (s *Sandbox) StatsContainer(contID string) (vc.ContainerStats, error) {
return vc.ContainerStats{}, nil return vc.ContainerStats{}, nil
} }
// PauseContainer implements the VCSandbox function of the same name.
func (s *Sandbox) PauseContainer(contID string) error {
return nil
}
// ResumeContainer implements the VCSandbox function of the same name.
func (s *Sandbox) ResumeContainer(contID string) error {
return nil
}
// Status implements the VCSandbox function of the same name. // Status implements the VCSandbox function of the same name.
func (s *Sandbox) Status() vc.SandboxStatus { func (s *Sandbox) Status() vc.SandboxStatus {
return vc.SandboxStatus{} return vc.SandboxStatus{}
@ -127,6 +157,11 @@ func (s *Sandbox) UpdateContainer(containerID string, resources specs.LinuxResou
return nil return nil
} }
// ProcessListContainer implements the VCSandbox function of the same name.
func (s *Sandbox) ProcessListContainer(containerID string, options vc.ProcessListOptions) (vc.ProcessList, error) {
return nil, nil
}
// WaitProcess implements the VCSandbox function of the same name. // WaitProcess implements the VCSandbox function of the same name.
func (s *Sandbox) WaitProcess(containerID, processID string) (int32, error) { func (s *Sandbox) WaitProcess(containerID, processID string) (int32, error) {
return 0, nil return 0, nil

View File

@ -1320,6 +1320,34 @@ func (s *Sandbox) StartContainer(containerID string) (VCContainer, error) {
return c, nil return c, nil
} }
// StopContainer stops a container in the sandbox
func (s *Sandbox) StopContainer(containerID string) (VCContainer, error) {
// Fetch the container.
c, err := s.findContainer(containerID)
if err != nil {
return nil, err
}
// Stop it.
if err := c.stop(); err != nil {
return nil, err
}
return c, nil
}
// KillContainer signals a container in the sandbox
func (s *Sandbox) KillContainer(containerID string, signal syscall.Signal, all bool) error {
// Fetch the container.
c, err := s.findContainer(containerID)
if err != nil {
return err
}
// Send a signal to the process.
return c.kill(signal, all)
}
// DeleteContainer deletes a container from the sandbox // DeleteContainer deletes a container from the sandbox
func (s *Sandbox) DeleteContainer(containerID string) (VCContainer, error) { func (s *Sandbox) DeleteContainer(containerID string) (VCContainer, error) {
if containerID == "" { if containerID == "" {
@ -1355,6 +1383,19 @@ func (s *Sandbox) DeleteContainer(containerID string) (VCContainer, error) {
return c, nil return c, nil
} }
// ProcessListContainer lists every process running inside a specific
// container in the sandbox.
func (s *Sandbox) ProcessListContainer(containerID string, options ProcessListOptions) (ProcessList, error) {
// Fetch the container.
c, err := s.findContainer(containerID)
if err != nil {
return nil, err
}
// Get the process list related to the container.
return c.processList(options)
}
// StatusContainer gets the status of a container // StatusContainer gets the status of a container
// TODO: update container status properly, see kata-containers/runtime#253 // TODO: update container status properly, see kata-containers/runtime#253
func (s *Sandbox) StatusContainer(containerID string) (ContainerStatus, error) { func (s *Sandbox) StatusContainer(containerID string) (ContainerStatus, error) {
@ -1422,6 +1463,30 @@ func (s *Sandbox) StatsContainer(containerID string) (ContainerStats, error) {
return *stats, nil return *stats, nil
} }
// PauseContainer pauses a running container.
func (s *Sandbox) PauseContainer(containerID string) error {
// Fetch the container.
c, err := s.findContainer(containerID)
if err != nil {
return err
}
// Pause the container.
return c.pause()
}
// ResumeContainer resumes a paused container.
func (s *Sandbox) ResumeContainer(containerID string) error {
// Fetch the container.
c, err := s.findContainer(containerID)
if err != nil {
return err
}
// Resume the container.
return c.resume()
}
// createContainers registers all containers to the proxy, create the // createContainers registers all containers to the proxy, create the
// containers in the guest and starts one shim per container. // containers in the guest and starts one shim per container.
func (s *Sandbox) createContainers() error { func (s *Sandbox) createContainers() error {
@ -1442,9 +1507,9 @@ func (s *Sandbox) createContainers() error {
return nil return nil
} }
// start starts a sandbox. The containers that are making the sandbox // Start starts a sandbox. The containers that are making the sandbox
// will be started. // will be started.
func (s *Sandbox) start() error { func (s *Sandbox) Start() error {
if err := s.state.validTransition(s.state.State, StateRunning); err != nil { if err := s.state.validTransition(s.state.State, StateRunning); err != nil {
return err return err
} }
@ -1464,9 +1529,9 @@ func (s *Sandbox) start() error {
return nil return nil
} }
// stop stops a sandbox. The containers that are making the sandbox // Stop stops a sandbox. The containers that are making the sandbox
// will be destroyed. // will be destroyed.
func (s *Sandbox) stop() error { func (s *Sandbox) Stop() error {
span, _ := s.trace("stop") span, _ := s.trace("stop")
defer span.Finish() defer span.Finish()
@ -1489,7 +1554,12 @@ func (s *Sandbox) stop() error {
return err return err
} }
return s.setSandboxState(StateStopped) if err := s.setSandboxState(StateStopped); err != nil {
return err
}
// Remove the network.
return s.removeNetwork()
} }
// Pause pauses the sandbox // Pause pauses the sandbox

View File

@ -1345,7 +1345,7 @@ func TestStartContainer(t *testing.T) {
_, err = s.StartContainer(contID) _, err = s.StartContainer(contID)
assert.NotNil(t, err, "Starting non-existing container should fail") assert.NotNil(t, err, "Starting non-existing container should fail")
err = s.start() err = s.Start()
assert.Nil(t, err, "Failed to start sandbox: %v", err) assert.Nil(t, err, "Failed to start sandbox: %v", err)
contConfig := newTestContainerConfigNoop(contID) contConfig := newTestContainerConfigNoop(contID)
@ -1401,7 +1401,7 @@ func TestEnterContainer(t *testing.T) {
_, _, err = s.EnterContainer(contID, cmd) _, _, err = s.EnterContainer(contID, cmd)
assert.NotNil(t, err, "Entering non-running container should fail") assert.NotNil(t, err, "Entering non-running container should fail")
err = s.start() err = s.Start()
assert.Nil(t, err, "Failed to start sandbox: %v", err) assert.Nil(t, err, "Failed to start sandbox: %v", err)
_, _, err = s.EnterContainer(contID, cmd) _, _, err = s.EnterContainer(contID, cmd)
@ -1416,7 +1416,7 @@ func TestMonitor(t *testing.T) {
_, err = s.Monitor() _, err = s.Monitor()
assert.NotNil(t, err, "Monitoring non-running container should fail") assert.NotNil(t, err, "Monitoring non-running container should fail")
err = s.start() err = s.Start()
assert.Nil(t, err, "Failed to start sandbox: %v", err) assert.Nil(t, err, "Failed to start sandbox: %v", err)
_, err = s.Monitor() _, err = s.Monitor()
@ -1438,7 +1438,7 @@ func TestWaitProcess(t *testing.T) {
_, err = s.WaitProcess(contID, execID) _, err = s.WaitProcess(contID, execID)
assert.NotNil(t, err, "Wait process in stopped sandbox should fail") assert.NotNil(t, err, "Wait process in stopped sandbox should fail")
err = s.start() err = s.Start()
assert.Nil(t, err, "Failed to start sandbox: %v", err) assert.Nil(t, err, "Failed to start sandbox: %v", err)
_, err = s.WaitProcess(contID, execID) _, err = s.WaitProcess(contID, execID)
@ -1468,7 +1468,7 @@ func TestSignalProcess(t *testing.T) {
err = s.SignalProcess(contID, execID, syscall.SIGKILL, true) err = s.SignalProcess(contID, execID, syscall.SIGKILL, true)
assert.NotNil(t, err, "Wait process in stopped sandbox should fail") assert.NotNil(t, err, "Wait process in stopped sandbox should fail")
err = s.start() err = s.Start()
assert.Nil(t, err, "Failed to start sandbox: %v", err) assert.Nil(t, err, "Failed to start sandbox: %v", err)
err = s.SignalProcess(contID, execID, syscall.SIGKILL, false) err = s.SignalProcess(contID, execID, syscall.SIGKILL, false)
@ -1498,7 +1498,7 @@ func TestWinsizeProcess(t *testing.T) {
err = s.WinsizeProcess(contID, execID, 100, 200) err = s.WinsizeProcess(contID, execID, 100, 200)
assert.NotNil(t, err, "Winsize process in stopped sandbox should fail") assert.NotNil(t, err, "Winsize process in stopped sandbox should fail")
err = s.start() err = s.Start()
assert.Nil(t, err, "Failed to start sandbox: %v", err) assert.Nil(t, err, "Failed to start sandbox: %v", err)
err = s.WinsizeProcess(contID, execID, 100, 200) err = s.WinsizeProcess(contID, execID, 100, 200)
@ -1528,7 +1528,7 @@ func TestContainerProcessIOStream(t *testing.T) {
_, _, _, err = s.IOStream(contID, execID) _, _, _, err = s.IOStream(contID, execID)
assert.NotNil(t, err, "Winsize process in stopped sandbox should fail") assert.NotNil(t, err, "Winsize process in stopped sandbox should fail")
err = s.start() err = s.Start()
assert.Nil(t, err, "Failed to start sandbox: %v", err) assert.Nil(t, err, "Failed to start sandbox: %v", err)
_, _, _, err = s.IOStream(contID, execID) _, _, _, err = s.IOStream(contID, execID)