commit
cb4234b020
3 changed files with 80 additions and 6 deletions
|
@ -16,6 +16,7 @@ var podSandboxCommand = cli.Command{
|
||||||
stopPodSandboxCommand,
|
stopPodSandboxCommand,
|
||||||
removePodSandboxCommand,
|
removePodSandboxCommand,
|
||||||
podSandboxStatusCommand,
|
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
|
// RunPodSandbox sends a RunPodSandboxRequest to the server, and parses
|
||||||
// the returned RunPodSandboxResponse.
|
// the returned RunPodSandboxResponse.
|
||||||
func RunPodSandbox(client pb.RuntimeServiceClient, path string) error {
|
func RunPodSandbox(client pb.RuntimeServiceClient, path string) error {
|
||||||
|
@ -198,3 +219,19 @@ func PodSandboxStatus(client pb.RuntimeServiceClient, ID string) error {
|
||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -14,6 +14,15 @@ import (
|
||||||
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
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
|
// CreateContainer creates a new container in specified PodSandbox
|
||||||
func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerRequest) (*pb.CreateContainerResponse, error) {
|
func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerRequest) (*pb.CreateContainerResponse, error) {
|
||||||
// The id of the PodSandbox
|
// The id of the PodSandbox
|
||||||
|
@ -380,17 +389,17 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq
|
||||||
rStatus := pb.ContainerState_UNKNOWN
|
rStatus := pb.ContainerState_UNKNOWN
|
||||||
|
|
||||||
switch cState.Status {
|
switch cState.Status {
|
||||||
case "created":
|
case ContainerStateCreated:
|
||||||
rStatus = pb.ContainerState_CREATED
|
rStatus = pb.ContainerState_CREATED
|
||||||
created := cState.Created.Unix()
|
created := cState.Created.Unix()
|
||||||
csr.Status.CreatedAt = int64Ptr(created)
|
csr.Status.CreatedAt = int64Ptr(created)
|
||||||
case "running":
|
case ContainerStateRunning:
|
||||||
rStatus = pb.ContainerState_RUNNING
|
rStatus = pb.ContainerState_RUNNING
|
||||||
created := cState.Created.Unix()
|
created := cState.Created.Unix()
|
||||||
csr.Status.CreatedAt = int64Ptr(created)
|
csr.Status.CreatedAt = int64Ptr(created)
|
||||||
started := cState.Started.Unix()
|
started := cState.Started.Unix()
|
||||||
csr.Status.StartedAt = int64Ptr(started)
|
csr.Status.StartedAt = int64Ptr(started)
|
||||||
case "stopped":
|
case ContainerStateStopped:
|
||||||
rStatus = pb.ContainerState_EXITED
|
rStatus = pb.ContainerState_EXITED
|
||||||
created := cState.Created.Unix()
|
created := cState.Created.Unix()
|
||||||
csr.Status.CreatedAt = int64Ptr(created)
|
csr.Status.CreatedAt = int64Ptr(created)
|
||||||
|
|
|
@ -316,6 +316,9 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR
|
||||||
|
|
||||||
podInfraContainerName := sb.name + "-infra"
|
podInfraContainerName := sb.name + "-infra"
|
||||||
podInfraContainer := sb.getContainer(podInfraContainerName)
|
podInfraContainer := sb.getContainer(podInfraContainerName)
|
||||||
|
if err := s.runtime.UpdateStatus(podInfraContainer); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
cState := s.runtime.ContainerStatus(podInfraContainer)
|
cState := s.runtime.ContainerStatus(podInfraContainer)
|
||||||
created := cState.Created.Unix()
|
created := cState.Created.Unix()
|
||||||
|
@ -332,7 +335,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR
|
||||||
}
|
}
|
||||||
|
|
||||||
rStatus := pb.PodSandBoxState_NOTREADY
|
rStatus := pb.PodSandBoxState_NOTREADY
|
||||||
if cState.Status == "running" {
|
if cState.Status == ContainerStateRunning {
|
||||||
rStatus = pb.PodSandBoxState_READY
|
rStatus = pb.PodSandBoxState_READY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -351,7 +354,32 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListPodSandbox returns a list of SandBox.
|
// ListPodSandbox returns a list of SandBoxes.
|
||||||
func (s *Server) ListPodSandbox(context.Context, *pb.ListPodSandboxRequest) (*pb.ListPodSandboxResponse, error) {
|
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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue