From 7c9aaf34cd23b0aa6ba018930c314d6a9c4de516 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Mon, 26 Sep 2016 12:44:24 -0700 Subject: [PATCH 1/3] Introduce consts for containers states Signed-off-by: Mrunal Patel --- server/container.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/server/container.go b/server/container.go index 8fe17222..0a39a2fa 100644 --- a/server/container.go +++ b/server/container.go @@ -14,6 +14,15 @@ import ( pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" ) +const ( + // ContainerStateCreated represents the created state of a container + ContainerStateCreated = "created" + // ContainerStateRunning represents the running state of a container + ContainerStateRunning = "running" + // ContainerStateStopped represents the stopped state of a container + ContainerStateStopped = "stopped" +) + // CreateContainer creates a new container in specified PodSandbox func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerRequest) (*pb.CreateContainerResponse, error) { // The id of the PodSandbox @@ -380,17 +389,17 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq rStatus := pb.ContainerState_UNKNOWN switch cState.Status { - case "created": + case ContainerStateCreated: rStatus = pb.ContainerState_CREATED created := cState.Created.Unix() csr.Status.CreatedAt = int64Ptr(created) - case "running": + case ContainerStateRunning: rStatus = pb.ContainerState_RUNNING created := cState.Created.Unix() csr.Status.CreatedAt = int64Ptr(created) started := cState.Started.Unix() csr.Status.StartedAt = int64Ptr(started) - case "stopped": + case ContainerStateStopped: rStatus = pb.ContainerState_EXITED created := cState.Created.Unix() csr.Status.CreatedAt = int64Ptr(created) From ecda01c2840d615bcc60fc3c12db630974be5f92 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Mon, 26 Sep 2016 12:20:59 -0700 Subject: [PATCH 2/3] Add server impl of list pod sandboxes Signed-off-by: Mrunal Patel --- server/sandbox.go | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/server/sandbox.go b/server/sandbox.go index 8ee1b6ca..42dbdb46 100644 --- a/server/sandbox.go +++ b/server/sandbox.go @@ -316,6 +316,9 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR podInfraContainerName := sb.name + "-infra" podInfraContainer := sb.getContainer(podInfraContainerName) + if err := s.runtime.UpdateStatus(podInfraContainer); err != nil { + return nil, err + } cState := s.runtime.ContainerStatus(podInfraContainer) created := cState.Created.Unix() @@ -332,7 +335,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR } rStatus := pb.PodSandBoxState_NOTREADY - if cState.Status == "running" { + if cState.Status == ContainerStateRunning { rStatus = pb.PodSandBoxState_READY } @@ -351,7 +354,32 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR }, nil } -// ListPodSandbox returns a list of SandBox. +// ListPodSandbox returns a list of SandBoxes. func (s *Server) ListPodSandbox(context.Context, *pb.ListPodSandboxRequest) (*pb.ListPodSandboxResponse, error) { - return nil, nil + var pods []*pb.PodSandbox + for _, sb := range s.state.sandboxes { + podInfraContainerName := sb.name + "-infra" + podInfraContainer := sb.getContainer(podInfraContainerName) + if err := s.runtime.UpdateStatus(podInfraContainer); err != nil { + return nil, err + } + cState := s.runtime.ContainerStatus(podInfraContainer) + created := cState.Created.Unix() + rStatus := pb.PodSandBoxState_NOTREADY + if cState.Status == ContainerStateRunning { + rStatus = pb.PodSandBoxState_READY + } + + pod := &pb.PodSandbox{ + Id: &sb.id, + CreatedAt: int64Ptr(created), + State: &rStatus, + } + + pods = append(pods, pod) + } + + return &pb.ListPodSandboxResponse{ + Items: pods, + }, nil } From fd3691a0e38f25f008293397010b86acc4d49c06 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Mon, 26 Sep 2016 12:21:37 -0700 Subject: [PATCH 3/3] Add client impl for listing pod sandboxes Signed-off-by: Mrunal Patel --- cmd/client/sandbox.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/cmd/client/sandbox.go b/cmd/client/sandbox.go index b64be46c..318dcf09 100644 --- a/cmd/client/sandbox.go +++ b/cmd/client/sandbox.go @@ -16,6 +16,7 @@ var podSandboxCommand = cli.Command{ stopPodSandboxCommand, removePodSandboxCommand, podSandboxStatusCommand, + listPodSandboxCommand, }, } @@ -128,6 +129,26 @@ var podSandboxStatusCommand = cli.Command{ }, } +var listPodSandboxCommand = cli.Command{ + Name: "list", + Usage: "list pod sandboxes", + Action: func(context *cli.Context) error { + // Set up a connection to the server. + conn, err := getClientConnection(context) + if err != nil { + return fmt.Errorf("failed to connect: %v", err) + } + defer conn.Close() + client := pb.NewRuntimeServiceClient(conn) + + err = ListPodSandboxes(client) + if err != nil { + return fmt.Errorf("listing pod sandboxes failed: %v", err) + } + return nil + }, +} + // RunPodSandbox sends a RunPodSandboxRequest to the server, and parses // the returned RunPodSandboxResponse. func RunPodSandbox(client pb.RuntimeServiceClient, path string) error { @@ -198,3 +219,19 @@ func PodSandboxStatus(client pb.RuntimeServiceClient, ID string) error { } return nil } + +// ListPodSandboxes sends a ListPodSandboxRequest to the server, and parses +// the returned ListPodSandboxResponse. +func ListPodSandboxes(client pb.RuntimeServiceClient) error { + r, err := client.ListPodSandbox(context.Background(), &pb.ListPodSandboxRequest{}) + if err != nil { + return err + } + for _, pod := range r.Items { + fmt.Printf("ID: %s\n", *pod.Id) + fmt.Printf("Status: %s\n", pod.State) + ctm := time.Unix(*pod.CreatedAt, 0) + fmt.Printf("Created: %v\n", ctm) + } + return nil +}