Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

runtime: Get containerd container task status #779

Merged
merged 1 commit into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pkg/runtime/containerd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ func (cc *ctdClient) InspectContainer(container string) (*runtime.ContainerInspe
return nil, err
}

tStatus, err := task.Status(cc.ctx)
if err != nil {
return nil, err
}

info, err := cont.Info(cc.ctx)
if err != nil {
return nil, err
Expand All @@ -289,8 +294,8 @@ func (cc *ctdClient) InspectContainer(container string) (*runtime.ContainerInspe
return &runtime.ContainerInspectResult{
ID: info.ID,
Image: info.Image, // TODO: This may be incorrect
Status: "", // TODO: This
IPAddress: nil, // TODO: This, containerd only supports CNI
Status: string(tStatus.Status),
IPAddress: nil, // TODO: This, containerd only supports CNI
PID: task.Pid(),
}, nil
}
Expand Down
72 changes: 58 additions & 14 deletions pkg/runtime/containerd/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package containerd
import (
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -81,20 +82,8 @@ func TestRunRemove(t *testing.T) {
// ideally, we could pass any tempdir with any permissions here
assert.NilError(t, os.MkdirAll(vmDir, constants.DATA_DIR_PERM))

cfg := &runtime.ContainerConfig{
Cmd: []string{
"/bin/sh",
"-c",
"echo hello",
},
Binds: []*runtime.Bind{
runtime.BindBoth(vmDir),
},
Devices: []*runtime.Bind{
runtime.BindBoth("/dev/kvm"),
},
Labels: map[string]string{},
}
cmds := []string{"/bin/sh", "-c", "echo hello"}
cfg := getContainerConfig(cmds, vmDir)

taskID, err := client.RunContainer(imageName, cfg, cName, cID)
if err != nil {
Expand All @@ -112,11 +101,66 @@ func TestRunRemove(t *testing.T) {
t.Errorf("Error Removing Container: %s", err)
}

// just in case the process is hung -- cleanup
containerCleanup(client, cName)
}

func TestInspectContainer(t *testing.T) {
cName := "ignite-test-foo3"
cID := "test-foo3"
vmDir := filepath.Join(constants.VM_DIR, cID)
assert.NilError(t, os.MkdirAll(vmDir, constants.DATA_DIR_PERM))

cmds := []string{"/bin/sh", "-c", "echo hello"}
cfg := getContainerConfig(cmds, vmDir)

taskID, err := client.RunContainer(imageName, cfg, cName, cID)
if err != nil {
t.Errorf("Error Running Container /w TaskID %q: %s", taskID, err)
} else {
t.Logf("TaskID: %q", taskID)
}

// Run inspect and check the result.
result, err := client.InspectContainer(cName)
assert.NilError(t, err)
assert.Equal(t, taskID, result.ID)
// Returns image URI - docker.io/library/busybox:latest.
assert.Check(t, strings.Contains(result.Image, imageName.String()))
assert.Equal(t, "running", result.Status)

time.Sleep(time.Second / 4)

err = client.RemoveContainer(cName)
if err != nil {
t.Errorf("Error Removing Container: %s", err)
}

// just in case the process is hung -- cleanup
containerCleanup(client, cName)
}

// containerCleanup ensures that the container is cleaned up.
func containerCleanup(client runtime.Interface, cName string) {
// just in case the process is hung -- cleanup
client.KillContainer(cName, "SIGQUIT") //nolint:errcheck // TODO: common constant for SIGQUIT
client.RemoveContainer(cName) //nolint:errcheck
}

// getContainerConfig returns a container config.
func getContainerConfig(cmds []string, vmDir string) *runtime.ContainerConfig {
return &runtime.ContainerConfig{
Cmd: cmds,
Binds: []*runtime.Bind{
runtime.BindBoth(vmDir),
},
Devices: []*runtime.Bind{
runtime.BindBoth("/dev/kvm"),
},
Labels: map[string]string{},
}
}

func TestV2ShimRuntimesHaveBinaryNames(t *testing.T) {
for _, runtime := range v2ShimRuntimes {
if v2shim.BinaryName(runtime) == "" {
Expand Down