api: add sandbox StatusContainer API

It retrieves container status from sandbox.

Signed-off-by: Peng Tao <bergwolf@gmail.com>
This commit is contained in:
Peng Tao 2018-04-20 17:01:21 +08:00
parent 4b30446217
commit b3d9683743
5 changed files with 50 additions and 0 deletions

View File

@ -17,4 +17,5 @@ var (
errNeedFile = errors.New("File cannot be empty")
errNeedState = errors.New("State cannot be empty")
errInvalidResource = errors.New("Invalid sandbox resource")
errNoSuchContainer = errors.New("Container does not exist")
)

View File

@ -53,6 +53,7 @@ type VCSandbox interface {
CreateContainer(contConfig ContainerConfig) (VCContainer, error)
DeleteContainer(contID string) (VCContainer, error)
StartContainer(containerID string) (VCContainer, error)
StatusContainer(containerID string) (ContainerStatus, error)
}
// VCContainer is the Container interface

View File

@ -84,3 +84,8 @@ func (p *Sandbox) DeleteContainer(contID string) (vc.VCContainer, error) {
func (p *Sandbox) StartContainer(contID string) (vc.VCContainer, error) {
return &Container{}, nil
}
// StatusContainer implements the VCSandbox function of the same name.
func (p *Sandbox) StatusContainer(contID string) (vc.ContainerStatus, error) {
return vc.ContainerStatus{}, nil
}

View File

@ -946,6 +946,29 @@ func (s *Sandbox) DeleteContainer(containerID string) (VCContainer, error) {
return c, nil
}
// StatusContainer gets the status of a container
// TODO: update container status properly, see kata-containers/runtime#253
func (s *Sandbox) StatusContainer(containerID string) (ContainerStatus, error) {
if containerID == "" {
return ContainerStatus{}, errNeedContainerID
}
for _, c := range s.containers {
if c.id == containerID {
return ContainerStatus{
ID: c.id,
State: c.state,
PID: c.process.Pid,
StartTime: c.process.StartTime,
RootFs: c.config.RootFs,
Annotations: c.config.Annotations,
}, nil
}
}
return ContainerStatus{}, errNoSuchContainer
}
// createContainers registers all containers to the proxy, create the
// containers in the guest and starts one shim per container.
func (s *Sandbox) createContainers() error {

View File

@ -1336,3 +1336,23 @@ func TestStartContainer(t *testing.T) {
_, err = s.StartContainer(contID)
assert.Nil(t, err, "Start container failed: %v", err)
}
func TestStatusContainer(t *testing.T) {
s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NoopNetworkModel, NetworkConfig{}, nil, nil)
assert.Nil(t, err, "VirtContainers should not allow empty sandboxes")
defer cleanUp()
contID := "999"
_, err = s.StatusContainer(contID)
assert.NotNil(t, err, "Status non-existing container should fail")
contConfig := newTestContainerConfigNoop(contID)
_, err = s.CreateContainer(contConfig)
assert.Nil(t, err, "Failed to create container %+v in sandbox %+v: %v", contConfig, s, err)
_, err = s.StatusContainer(contID)
assert.Nil(t, err, "Status container failed: %v", err)
_, err = s.DeleteContainer(contID)
assert.Nil(t, err, "Failed to delete container %s in sandbox %s: %v", contID, s.ID(), err)
}