diff --git a/libkpod/containerserver.go b/libkpod/containerserver.go index c9580b78..f2abfe2f 100644 --- a/libkpod/containerserver.go +++ b/libkpod/containerserver.go @@ -1,6 +1,7 @@ package libkpod import ( + "encoding/json" "sync" "github.com/containers/image/types" @@ -9,6 +10,7 @@ import ( "github.com/docker/docker/pkg/truncindex" "github.com/kubernetes-incubator/cri-o/oci" "github.com/kubernetes-incubator/cri-o/pkg/storage" + "github.com/moby/moby/pkg/ioutils" ) // ContainerServer implements the ImageServer @@ -70,6 +72,35 @@ func New(runtime *oci.Runtime, store cstorage.Store, storageImageServer storage. } } +// ContainerStateFromDisk retrieves information on the state of a running container +// from the disk +func (c *ContainerServer) ContainerStateFromDisk(ctr *oci.Container) error { + if err := ctr.FromDisk(); err != nil { + return err + } + // ignore errors, this is a best effort to have up-to-date info about + // a given container before its state gets stored + c.runtime.UpdateStatus(ctr) + + return nil +} + +// ContainerStateToDisk writes the container's state information to a JSON file +// on disk +func (c *ContainerServer) ContainerStateToDisk(ctr *oci.Container) error { + // ignore errors, this is a best effort to have up-to-date info about + // a given container before its state gets stored + c.Runtime().UpdateStatus(ctr) + + jsonSource, err := ioutils.NewAtomicFileWriter(ctr.StatePath(), 0644) + if err != nil { + return err + } + defer jsonSource.Close() + enc := json.NewEncoder(jsonSource) + return enc.Encode(c.runtime.ContainerStatus(ctr)) +} + type containerServerState struct { containers oci.ContainerStorer } diff --git a/server/container_create.go b/server/container_create.go index ebcd607f..c2d4abc7 100644 --- a/server/container_create.go +++ b/server/container_create.go @@ -335,7 +335,7 @@ func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerReq return nil, err } - s.containerStateToDisk(container) + s.ContainerStateToDisk(container) resp := &pb.CreateContainerResponse{ ContainerId: containerID, diff --git a/server/container_start.go b/server/container_start.go index d3d97bc5..b8c7fdb0 100644 --- a/server/container_start.go +++ b/server/container_start.go @@ -29,7 +29,7 @@ func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerReque if err != nil { s.Runtime().SetStartFailed(c, err) } - s.containerStateToDisk(c) + s.ContainerStateToDisk(c) }() err = s.Runtime().StartContainer(c) diff --git a/server/container_status.go b/server/container_status.go index ffa315bb..ece493eb 100644 --- a/server/container_status.go +++ b/server/container_status.go @@ -29,7 +29,7 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq if err = s.Runtime().UpdateStatus(c); err != nil { return nil, err } - s.containerStateToDisk(c) + s.ContainerStateToDisk(c) containerID := c.ID() resp := &pb.ContainerStatusResponse{ diff --git a/server/container_stop.go b/server/container_stop.go index d069e603..ccbde3cc 100644 --- a/server/container_stop.go +++ b/server/container_stop.go @@ -30,7 +30,7 @@ func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest } } - s.containerStateToDisk(c) + s.ContainerStateToDisk(c) resp := &pb.StopContainerResponse{} logrus.Debugf("StopContainerResponse: %+v", resp) diff --git a/server/sandbox_run.go b/server/sandbox_run.go index bfff47d2..8f8abe8b 100644 --- a/server/sandbox_run.go +++ b/server/sandbox_run.go @@ -500,7 +500,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest return nil, err } - s.containerStateToDisk(container) + s.ContainerStateToDisk(container) resp = &pb.RunPodSandboxResponse{PodSandboxId: id} logrus.Debugf("RunPodSandboxResponse: %+v", resp) diff --git a/server/sandbox_status.go b/server/sandbox_status.go index ba00563d..48eb6695 100644 --- a/server/sandbox_status.go +++ b/server/sandbox_status.go @@ -19,7 +19,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR if err = s.Runtime().UpdateStatus(podInfraContainer); err != nil { return nil, err } - s.containerStateToDisk(podInfraContainer) + s.ContainerStateToDisk(podInfraContainer) cState := s.Runtime().ContainerStatus(podInfraContainer) diff --git a/server/sandbox_stop.go b/server/sandbox_stop.go index 1f863fc2..1689d87b 100644 --- a/server/sandbox_stop.go +++ b/server/sandbox_stop.go @@ -85,7 +85,7 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque logrus.Warnf("failed to stop container %s in pod sandbox %s: %v", c.Name(), sb.ID(), err) } } - s.containerStateToDisk(c) + s.ContainerStateToDisk(c) } if err := label.ReleaseLabel(sb.ProcessLabel()); err != nil { diff --git a/server/server.go b/server/server.go index 71cb960b..28ce262f 100644 --- a/server/server.go +++ b/server/server.go @@ -13,7 +13,6 @@ import ( "github.com/Sirupsen/logrus" "github.com/containers/image/types" cstorage "github.com/containers/storage" - "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/registrar" "github.com/docker/docker/pkg/truncindex" "github.com/kubernetes-incubator/cri-o/libkpod" @@ -158,37 +157,12 @@ func (s *Server) loadContainer(id string) error { return err } - s.containerStateFromDisk(ctr) + s.ContainerStateFromDisk(ctr) s.addContainer(ctr) return s.CtrIDIndex().Add(id) } -func (s *Server) containerStateFromDisk(c *oci.Container) error { - if err := c.FromDisk(); err != nil { - return err - } - // ignore errors, this is a best effort to have up-to-date info about - // a given container before its state gets stored - s.Runtime().UpdateStatus(c) - - return nil -} - -func (s *Server) containerStateToDisk(c *oci.Container) error { - // ignore errors, this is a best effort to have up-to-date info about - // a given container before its state gets stored - s.Runtime().UpdateStatus(c) - - jsonSource, err := ioutils.NewAtomicFileWriter(c.StatePath(), 0644) - if err != nil { - return err - } - defer jsonSource.Close() - enc := json.NewEncoder(jsonSource) - return enc.Encode(s.Runtime().ContainerStatus(c)) -} - func configNetNsPath(spec rspec.Spec) (string, error) { for _, ns := range spec.Linux.Namespaces { if ns.Type != rspec.NetworkNamespace { @@ -302,7 +276,7 @@ func (s *Server) loadSandbox(id string) error { return err } - s.containerStateFromDisk(scontainer) + s.ContainerStateFromDisk(scontainer) if err = label.ReserveLabel(processLabel); err != nil { return err