From edba913f678efab2d65a9bf64ad6abb499b50d4d Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Wed, 5 Oct 2016 11:31:41 -0700 Subject: [PATCH 1/2] Track container IDs in index Signed-off-by: Mrunal Patel --- server/container.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/server/container.go b/server/container.go index 48ad9363..5833d4cc 100644 --- a/server/container.go +++ b/server/container.go @@ -99,6 +99,10 @@ func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerReq s.addContainer(container) + if err := s.ctrIDIndex.Add(containerID); err != nil { + return nil, err + } + return &pb.CreateContainerResponse{ ContainerId: &containerID, }, nil @@ -397,6 +401,10 @@ func (s *Server) RemoveContainer(ctx context.Context, req *pb.RemoveContainerReq s.releaseContainerName(c.Name()) s.removeContainer(c) + if err := s.ctrIDIndex.Delete(c.ID()); err != nil { + return nil, err + } + return &pb.RemoveContainerResponse{}, nil } From 869f85e4bbd4fec3fddbe32efc62725b9c2c6e45 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Wed, 5 Oct 2016 11:55:45 -0700 Subject: [PATCH 2/2] Add support for specifying container short IDs Signed-off-by: Mrunal Patel --- server/container.go | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/server/container.go b/server/container.go index 5833d4cc..c62b2032 100644 --- a/server/container.go +++ b/server/container.go @@ -329,11 +329,16 @@ func (s *Server) createSandboxContainer(containerID string, containerName string // StartContainer starts the container. func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerRequest) (*pb.StartContainerResponse, error) { - containerID := req.GetContainerId() - - if containerID == "" { + ctrID := req.GetContainerId() + if ctrID == "" { return nil, fmt.Errorf("container ID should not be empty") } + + containerID, err := s.ctrIDIndex.Get(ctrID) + if err != nil { + return nil, fmt.Errorf("container with ID starting with %s not found: %v", ctrID, err) + } + c := s.state.containers.Get(containerID) if c == nil { return nil, fmt.Errorf("specified container not found: %s", containerID) @@ -348,11 +353,16 @@ func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerReque // StopContainer stops a running container with a grace period (i.e., timeout). func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest) (*pb.StopContainerResponse, error) { - containerID := req.GetContainerId() - - if containerID == "" { + ctrID := req.GetContainerId() + if ctrID == "" { return nil, fmt.Errorf("container ID should not be empty") } + + containerID, err := s.ctrIDIndex.Get(ctrID) + if err != nil { + return nil, fmt.Errorf("container with ID starting with %s not found: %v", ctrID, err) + } + c := s.state.containers.Get(containerID) if c == nil { return nil, fmt.Errorf("specified container not found: %s", containerID) @@ -368,11 +378,16 @@ func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest // RemoveContainer removes the container. If the container is running, the container // should be force removed. func (s *Server) RemoveContainer(ctx context.Context, req *pb.RemoveContainerRequest) (*pb.RemoveContainerResponse, error) { - containerID := req.GetContainerId() - - if containerID == "" { + ctrID := req.GetContainerId() + if ctrID == "" { return nil, fmt.Errorf("container ID should not be empty") } + + containerID, err := s.ctrIDIndex.Get(ctrID) + if err != nil { + return nil, fmt.Errorf("container with ID starting with %s not found: %v", ctrID, err) + } + c := s.state.containers.Get(containerID) if c == nil { return nil, fmt.Errorf("specified container not found: %s", containerID) @@ -447,11 +462,16 @@ func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersReque // ContainerStatus returns status of the container. func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusRequest) (*pb.ContainerStatusResponse, error) { - containerID := req.GetContainerId() - - if containerID == "" { + ctrID := req.GetContainerId() + if ctrID == "" { return nil, fmt.Errorf("container ID should not be empty") } + + containerID, err := s.ctrIDIndex.Get(ctrID) + if err != nil { + return nil, fmt.Errorf("container with ID starting with %s not found: %v", ctrID, err) + } + c := s.state.containers.Get(containerID) if c == nil {