From 9ada0eb4e525f3ba4a43cba4d4b4b4bcb8e57808 Mon Sep 17 00:00:00 2001 From: Ryan Cole Date: Mon, 31 Jul 2017 19:23:20 -0400 Subject: [PATCH] Allow containerserver to match containers by id prefix or name Signed-off-by: Ryan Cole --- libkpod/container.go | 76 ++++++++++++++++++++++++++++++++++++ server/container.go | 24 ------------ server/container_execsync.go | 2 +- server/container_remove.go | 2 +- server/container_start.go | 2 +- server/container_status.go | 2 +- server/container_stop.go | 2 +- 7 files changed, 81 insertions(+), 29 deletions(-) delete mode 100644 server/container.go diff --git a/libkpod/container.go b/libkpod/container.go index 0de14df1..0fb20d4b 100644 --- a/libkpod/container.go +++ b/libkpod/container.go @@ -1,7 +1,12 @@ package libkpod import ( + "fmt" + cstorage "github.com/containers/storage" + "github.com/docker/docker/pkg/registrar" + "github.com/kubernetes-incubator/cri-o/libkpod/sandbox" + "github.com/kubernetes-incubator/cri-o/oci" "github.com/pkg/errors" ) @@ -75,3 +80,74 @@ func GetContainerRootFsSize(store cstorage.Store, containerID string) (int64, er layerSize, err := store.DiffSize(layer.Parent, layer.ID) return size + layerSize, err } + +// GetContainerFromRequest gets an oci container matching the specified full or partial id +func (c *ContainerServer) GetContainerFromRequest(cid string) (*oci.Container, error) { + if cid == "" { + return nil, fmt.Errorf("container ID should not be empty") + } + + containerID, err := c.ctrIDIndex.Get(cid) + if err != nil { + return nil, fmt.Errorf("container with ID starting with %s not found: %v", cid, err) + } + + ctr := c.GetContainer(containerID) + if ctr == nil { + return nil, fmt.Errorf("specified container not found: %s", containerID) + } + return ctr, nil +} + +func (c *ContainerServer) getSandboxFromRequest(pid string) (*sandbox.Sandbox, error) { + if pid == "" { + return nil, fmt.Errorf("pod ID should not be empty") + } + + podID, err := c.podIDIndex.Get(pid) + if err != nil { + return nil, fmt.Errorf("pod with ID starting with %s not found: %v", pid, err) + } + + sb := c.GetSandbox(podID) + if sb == nil { + return nil, fmt.Errorf("specified pod not found: %s", podID) + } + return sb, nil +} + +// LookupContainer returns the container with the given name or full or partial id +func (c *ContainerServer) LookupContainer(idOrName string) (*oci.Container, error) { + if idOrName == "" { + return nil, fmt.Errorf("container ID or name should not be empty") + } + + ctrID, err := c.ctrNameIndex.Get(idOrName) + if err != nil { + if err == registrar.ErrNameNotReserved { + ctrID = idOrName + } else { + return nil, err + } + } + + return c.GetContainerFromRequest(ctrID) +} + +// LookupSandbox returns the pod sandbox with the given name or full or partial id +func (c *ContainerServer) LookupSandbox(idOrName string) (*sandbox.Sandbox, error) { + if idOrName == "" { + return nil, fmt.Errorf("container ID or name should not be empty") + } + + podID, err := c.podNameIndex.Get(idOrName) + if err != nil { + if err == registrar.ErrNameNotReserved { + podID = idOrName + } else { + return nil, err + } + } + + return c.getSandboxFromRequest(podID) +} diff --git a/server/container.go b/server/container.go deleted file mode 100644 index 6a8f8957..00000000 --- a/server/container.go +++ /dev/null @@ -1,24 +0,0 @@ -package server - -import ( - "fmt" - - "github.com/kubernetes-incubator/cri-o/oci" -) - -func (s *Server) getContainerFromRequest(cid string) (*oci.Container, error) { - if cid == "" { - return nil, fmt.Errorf("container ID should not be empty") - } - - containerID, err := s.CtrIDIndex().Get(cid) - if err != nil { - return nil, fmt.Errorf("container with ID starting with %s not found: %v", cid, err) - } - - c := s.ContainerServer.GetContainer(containerID) - if c == nil { - return nil, fmt.Errorf("specified container not found: %s", containerID) - } - return c, nil -} diff --git a/server/container_execsync.go b/server/container_execsync.go index a3055acb..3ae28088 100644 --- a/server/container_execsync.go +++ b/server/container_execsync.go @@ -12,7 +12,7 @@ import ( // ExecSync runs a command in a container synchronously. func (s *Server) ExecSync(ctx context.Context, req *pb.ExecSyncRequest) (*pb.ExecSyncResponse, error) { logrus.Debugf("ExecSyncRequest %+v", req) - c, err := s.getContainerFromRequest(req.ContainerId) + c, err := s.GetContainerFromRequest(req.ContainerId) if err != nil { return nil, err } diff --git a/server/container_remove.go b/server/container_remove.go index 8e330e60..6b2aa8a9 100644 --- a/server/container_remove.go +++ b/server/container_remove.go @@ -13,7 +13,7 @@ import ( // should be force removed. func (s *Server) RemoveContainer(ctx context.Context, req *pb.RemoveContainerRequest) (*pb.RemoveContainerResponse, error) { logrus.Debugf("RemoveContainerRequest %+v", req) - c, err := s.getContainerFromRequest(req.ContainerId) + c, err := s.GetContainerFromRequest(req.ContainerId) if err != nil { return nil, err } diff --git a/server/container_start.go b/server/container_start.go index b8c7fdb0..020d76c7 100644 --- a/server/container_start.go +++ b/server/container_start.go @@ -12,7 +12,7 @@ import ( // StartContainer starts the container. func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerRequest) (*pb.StartContainerResponse, error) { logrus.Debugf("StartContainerRequest %+v", req) - c, err := s.getContainerFromRequest(req.ContainerId) + c, err := s.GetContainerFromRequest(req.ContainerId) if err != nil { return nil, err } diff --git a/server/container_status.go b/server/container_status.go index ece493eb..f985a89a 100644 --- a/server/container_status.go +++ b/server/container_status.go @@ -21,7 +21,7 @@ const ( // ContainerStatus returns status of the container. func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusRequest) (*pb.ContainerStatusResponse, error) { logrus.Debugf("ContainerStatusRequest %+v", req) - c, err := s.getContainerFromRequest(req.ContainerId) + c, err := s.GetContainerFromRequest(req.ContainerId) if err != nil { return nil, err } diff --git a/server/container_stop.go b/server/container_stop.go index ccbde3cc..a07c28aa 100644 --- a/server/container_stop.go +++ b/server/container_stop.go @@ -12,7 +12,7 @@ import ( // 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) { logrus.Debugf("StopContainerRequest %+v", req) - c, err := s.getContainerFromRequest(req.ContainerId) + c, err := s.GetContainerFromRequest(req.ContainerId) if err != nil { return nil, err }