Add Status in the runtime Container type
This is necessary for the generic PLEG to distinguish container events.
This commit is contained in:
@@ -198,6 +198,15 @@ func (c *ContainerID) UnmarshalJSON(data []byte) error {
|
|||||||
return c.ParseString(string(data))
|
return c.ParseString(string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ContainerStatus string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ContainerStatusRunning ContainerStatus = "running"
|
||||||
|
ContainerStatusExited ContainerStatus = "exited"
|
||||||
|
// This unknown encompasses all the statuses that we currently don't care.
|
||||||
|
ContainerStatusUnknown ContainerStatus = "unknown"
|
||||||
|
)
|
||||||
|
|
||||||
// Container provides the runtime information for a container, such as ID, hash,
|
// Container provides the runtime information for a container, such as ID, hash,
|
||||||
// status of the container.
|
// status of the container.
|
||||||
type Container struct {
|
type Container struct {
|
||||||
@@ -215,6 +224,8 @@ type Container struct {
|
|||||||
// The timestamp of the creation time of the container.
|
// The timestamp of the creation time of the container.
|
||||||
// TODO(yifan): Consider to move it to api.ContainerStatus.
|
// TODO(yifan): Consider to move it to api.ContainerStatus.
|
||||||
Created int64
|
Created int64
|
||||||
|
// Status is the status of the container.
|
||||||
|
Status ContainerStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
// Basic information about a container image.
|
// Basic information about a container image.
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package dockertools
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
docker "github.com/fsouza/go-dockerclient"
|
docker "github.com/fsouza/go-dockerclient"
|
||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
@@ -27,6 +28,19 @@ import (
|
|||||||
// This file contains helper functions to convert docker API types to runtime
|
// This file contains helper functions to convert docker API types to runtime
|
||||||
// (kubecontainer) types.
|
// (kubecontainer) types.
|
||||||
|
|
||||||
|
func mapStatus(status string) kubecontainer.ContainerStatus {
|
||||||
|
// Parse the status string in docker.APIContainers. This could break when
|
||||||
|
// we upgrade docker.
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(status, "Up"):
|
||||||
|
return kubecontainer.ContainerStatusRunning
|
||||||
|
case strings.HasPrefix(status, "Exited"):
|
||||||
|
return kubecontainer.ContainerStatusExited
|
||||||
|
default:
|
||||||
|
return kubecontainer.ContainerStatusUnknown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Converts docker.APIContainers to kubecontainer.Container.
|
// Converts docker.APIContainers to kubecontainer.Container.
|
||||||
func toRuntimeContainer(c *docker.APIContainers) (*kubecontainer.Container, error) {
|
func toRuntimeContainer(c *docker.APIContainers) (*kubecontainer.Container, error) {
|
||||||
if c == nil {
|
if c == nil {
|
||||||
@@ -37,12 +51,14 @@ func toRuntimeContainer(c *docker.APIContainers) (*kubecontainer.Container, erro
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &kubecontainer.Container{
|
return &kubecontainer.Container{
|
||||||
ID: kubetypes.DockerID(c.ID).ContainerID(),
|
ID: kubetypes.DockerID(c.ID).ContainerID(),
|
||||||
Name: dockerName.ContainerName,
|
Name: dockerName.ContainerName,
|
||||||
Image: c.Image,
|
Image: c.Image,
|
||||||
Hash: hash,
|
Hash: hash,
|
||||||
Created: c.Created,
|
Created: c.Created,
|
||||||
|
Status: mapStatus(c.Status),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,12 +24,31 @@ import (
|
|||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestMapStatus(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
input string
|
||||||
|
expected kubecontainer.ContainerStatus
|
||||||
|
}{
|
||||||
|
{input: "Up 5 hours", expected: kubecontainer.ContainerStatusRunning},
|
||||||
|
{input: "Exited (0) 2 hours ago", expected: kubecontainer.ContainerStatusExited},
|
||||||
|
{input: "Created", expected: kubecontainer.ContainerStatusUnknown},
|
||||||
|
{input: "Random string", expected: kubecontainer.ContainerStatusUnknown},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range testCases {
|
||||||
|
if actual := mapStatus(test.input); actual != test.expected {
|
||||||
|
t.Errorf("Test[%d]: expected %q, got %q", i, test.expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestToRuntimeContainer(t *testing.T) {
|
func TestToRuntimeContainer(t *testing.T) {
|
||||||
original := &docker.APIContainers{
|
original := &docker.APIContainers{
|
||||||
ID: "ab2cdf",
|
ID: "ab2cdf",
|
||||||
Image: "bar_image",
|
Image: "bar_image",
|
||||||
Created: 12345,
|
Created: 12345,
|
||||||
Names: []string{"/k8s_bar.5678_foo_ns_1234_42"},
|
Names: []string{"/k8s_bar.5678_foo_ns_1234_42"},
|
||||||
|
Status: "Up 5 hours",
|
||||||
}
|
}
|
||||||
expected := &kubecontainer.Container{
|
expected := &kubecontainer.Container{
|
||||||
ID: kubecontainer.ContainerID{"docker", "ab2cdf"},
|
ID: kubecontainer.ContainerID{"docker", "ab2cdf"},
|
||||||
@@ -37,6 +56,7 @@ func TestToRuntimeContainer(t *testing.T) {
|
|||||||
Image: "bar_image",
|
Image: "bar_image",
|
||||||
Hash: 0x5678,
|
Hash: 0x5678,
|
||||||
Created: 12345,
|
Created: 12345,
|
||||||
|
Status: kubecontainer.ContainerStatusRunning,
|
||||||
}
|
}
|
||||||
|
|
||||||
actual, err := toRuntimeContainer(original)
|
actual, err := toRuntimeContainer(original)
|
||||||
|
|||||||
@@ -580,14 +580,16 @@ func TestFindContainersByPod(t *testing.T) {
|
|||||||
Namespace: "ns",
|
Namespace: "ns",
|
||||||
Containers: []*kubecontainer.Container{
|
Containers: []*kubecontainer.Container{
|
||||||
{
|
{
|
||||||
ID: kubetypes.DockerID("foobar").ContainerID(),
|
ID: kubetypes.DockerID("foobar").ContainerID(),
|
||||||
Name: "foobar",
|
Name: "foobar",
|
||||||
Hash: 0x1234,
|
Hash: 0x1234,
|
||||||
|
Status: kubecontainer.ContainerStatusUnknown,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: kubetypes.DockerID("baz").ContainerID(),
|
ID: kubetypes.DockerID("baz").ContainerID(),
|
||||||
Name: "baz",
|
Name: "baz",
|
||||||
Hash: 0x1234,
|
Hash: 0x1234,
|
||||||
|
Status: kubecontainer.ContainerStatusUnknown,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -597,9 +599,10 @@ func TestFindContainersByPod(t *testing.T) {
|
|||||||
Namespace: "ns",
|
Namespace: "ns",
|
||||||
Containers: []*kubecontainer.Container{
|
Containers: []*kubecontainer.Container{
|
||||||
{
|
{
|
||||||
ID: kubetypes.DockerID("barbar").ContainerID(),
|
ID: kubetypes.DockerID("barbar").ContainerID(),
|
||||||
Name: "barbar",
|
Name: "barbar",
|
||||||
Hash: 0x1234,
|
Hash: 0x1234,
|
||||||
|
Status: kubecontainer.ContainerStatusUnknown,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -638,19 +641,22 @@ func TestFindContainersByPod(t *testing.T) {
|
|||||||
Namespace: "ns",
|
Namespace: "ns",
|
||||||
Containers: []*kubecontainer.Container{
|
Containers: []*kubecontainer.Container{
|
||||||
{
|
{
|
||||||
ID: kubetypes.DockerID("foobar").ContainerID(),
|
ID: kubetypes.DockerID("foobar").ContainerID(),
|
||||||
Name: "foobar",
|
Name: "foobar",
|
||||||
Hash: 0x1234,
|
Hash: 0x1234,
|
||||||
|
Status: kubecontainer.ContainerStatusUnknown,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: kubetypes.DockerID("barfoo").ContainerID(),
|
ID: kubetypes.DockerID("barfoo").ContainerID(),
|
||||||
Name: "barfoo",
|
Name: "barfoo",
|
||||||
Hash: 0x1234,
|
Hash: 0x1234,
|
||||||
|
Status: kubecontainer.ContainerStatusUnknown,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: kubetypes.DockerID("baz").ContainerID(),
|
ID: kubetypes.DockerID("baz").ContainerID(),
|
||||||
Name: "baz",
|
Name: "baz",
|
||||||
Hash: 0x1234,
|
Hash: 0x1234,
|
||||||
|
Status: kubecontainer.ContainerStatusUnknown,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -660,9 +666,10 @@ func TestFindContainersByPod(t *testing.T) {
|
|||||||
Namespace: "ns",
|
Namespace: "ns",
|
||||||
Containers: []*kubecontainer.Container{
|
Containers: []*kubecontainer.Container{
|
||||||
{
|
{
|
||||||
ID: kubetypes.DockerID("barbar").ContainerID(),
|
ID: kubetypes.DockerID("barbar").ContainerID(),
|
||||||
Name: "barbar",
|
Name: "barbar",
|
||||||
Hash: 0x1234,
|
Hash: 0x1234,
|
||||||
|
Status: kubecontainer.ContainerStatusUnknown,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -672,9 +679,10 @@ func TestFindContainersByPod(t *testing.T) {
|
|||||||
Namespace: "ns",
|
Namespace: "ns",
|
||||||
Containers: []*kubecontainer.Container{
|
Containers: []*kubecontainer.Container{
|
||||||
{
|
{
|
||||||
ID: kubetypes.DockerID("bazbaz").ContainerID(),
|
ID: kubetypes.DockerID("bazbaz").ContainerID(),
|
||||||
Name: "bazbaz",
|
Name: "bazbaz",
|
||||||
Hash: 0x1234,
|
Hash: 0x1234,
|
||||||
|
Status: kubecontainer.ContainerStatusUnknown,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -793,7 +793,14 @@ func (r *Runtime) GetPods(all bool) ([]*kubecontainer.Pod, error) {
|
|||||||
var pods []*kubecontainer.Pod
|
var pods []*kubecontainer.Pod
|
||||||
for _, u := range units {
|
for _, u := range units {
|
||||||
if strings.HasPrefix(u.Name, kubernetesUnitPrefix) {
|
if strings.HasPrefix(u.Name, kubernetesUnitPrefix) {
|
||||||
if !all && u.SubState != "running" {
|
var status kubecontainer.ContainerStatus
|
||||||
|
switch {
|
||||||
|
case u.SubState == "running":
|
||||||
|
status = kubecontainer.ContainerStatusRunning
|
||||||
|
default:
|
||||||
|
status = kubecontainer.ContainerStatusExited
|
||||||
|
}
|
||||||
|
if !all && status != kubecontainer.ContainerStatusRunning {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
pod, _, err := r.readServiceFile(u.Name)
|
pod, _, err := r.readServiceFile(u.Name)
|
||||||
|
|||||||
Reference in New Issue
Block a user