Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup container and sandbox state. #316

Merged
merged 1 commit into from
May 27, 2018
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
29 changes: 24 additions & 5 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,18 +565,21 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
}
if opts.state != "" {
st.State = pb.ContainerState_CONTAINER_UNKNOWN
switch opts.state {
switch strings.ToLower(opts.state) {
case "created":
st.State = pb.ContainerState_CONTAINER_CREATED
filter.State = st
case "running":
st.State = pb.ContainerState_CONTAINER_RUNNING
filter.State = st
case "stopped":
case "exited":
st.State = pb.ContainerState_CONTAINER_EXITED
filter.State = st
case "unknown":
st.State = pb.ContainerState_CONTAINER_UNKNOWN
filter.State = st
default:
log.Fatalf("--state should be one of created, running or stopped")
log.Fatalf("--state should be one of created, running, exited or unknown")
}
}
if opts.latest || opts.last > 0 {
Expand Down Expand Up @@ -625,7 +628,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
}
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\n",
id, c.Image.Image, ctm, c.State, c.Metadata.Name, c.Metadata.Attempt)
id, c.Image.Image, ctm, convertContainerState(c.State), c.Metadata.Name, c.Metadata.Attempt)
continue
}

Expand All @@ -637,7 +640,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
}
fmt.Printf("Attempt: %v\n", c.Metadata.Attempt)
}
fmt.Printf("State: %s\n", c.State)
fmt.Printf("State: %s\n", convertContainerState(c.State))
if c.Image != nil {
fmt.Printf("Image: %s\n", c.Image.Image)
}
Expand All @@ -661,6 +664,22 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
return nil
}

func convertContainerState(state pb.ContainerState) string {
switch state {
case pb.ContainerState_CONTAINER_CREATED:
return "Created"
case pb.ContainerState_CONTAINER_RUNNING:
return "Running"
case pb.ContainerState_CONTAINER_EXITED:
return "Exited"
case pb.ContainerState_CONTAINER_UNKNOWN:
return "Unknown"
default:
log.Fatalf("Unknown container state %q", state)
return ""
}
}

func getContainersList(containersList []*pb.Container, opts listOptions) []*pb.Container {
sort.Sort(containerByCreated(containersList))
n := len(containersList)
Expand Down
18 changes: 15 additions & 3 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
if opts.state != "" {
st := &pb.PodSandboxStateValue{}
st.State = pb.PodSandboxState_SANDBOX_NOTREADY
switch opts.state {
switch strings.ToLower(opts.state) {
case "ready":
st.State = pb.PodSandboxState_SANDBOX_READY
filter.State = st
Expand Down Expand Up @@ -439,7 +439,7 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
}
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\n",
id, ctm, pod.State, pod.Metadata.Name, pod.Metadata.Namespace, pod.Metadata.Attempt)
id, ctm, convertPodState(pod.State), pod.Metadata.Name, pod.Metadata.Namespace, pod.Metadata.Attempt)
continue
}

Expand All @@ -458,7 +458,7 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
fmt.Printf("Attempt: %v\n", pod.Metadata.Attempt)
}
}
fmt.Printf("Status: %s\n", pod.State)
fmt.Printf("Status: %s\n", convertPodState(pod.State))
ctm := time.Unix(0, pod.CreatedAt)
fmt.Printf("Created: %v\n", ctm)
if pod.Labels != nil {
Expand All @@ -480,6 +480,18 @@ func ListPodSandboxes(client pb.RuntimeServiceClient, opts listOptions) error {
return nil
}

func convertPodState(state pb.PodSandboxState) string {
switch state {
case pb.PodSandboxState_SANDBOX_READY:
return "Ready"
case pb.PodSandboxState_SANDBOX_NOTREADY:
return "NotReady"
default:
log.Fatalf("Unknown pod state %q", state)
return ""
}
}

func getSandboxesList(sandboxesList []*pb.PodSandbox, opts listOptions) []*pb.PodSandbox {
sort.Sort(sandboxByCreated(sandboxesList))
n := len(sandboxesList)
Expand Down