diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index 33e17104be..e6fd350d71 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -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 { @@ -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 } @@ -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) } @@ -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) diff --git a/cmd/crictl/sandbox.go b/cmd/crictl/sandbox.go index 470de34fe5..3250575c78 100644 --- a/cmd/crictl/sandbox.go +++ b/cmd/crictl/sandbox.go @@ -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 @@ -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 } @@ -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 { @@ -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)