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

Commit

Permalink
api: add sandbox StatusContainer API
Browse files Browse the repository at this point in the history
It retrieves container status from sandbox.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Apr 24, 2018
1 parent 4b30446 commit b3d9683
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions virtcontainers/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
1 change: 1 addition & 0 deletions virtcontainers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions virtcontainers/pkg/vcmock/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
23 changes: 23 additions & 0 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit b3d9683

Please sign in to comment.