move containerState to/from disk to libkpod

Signed-off-by: Ryan Cole <rcyoalne@gmail.com>
This commit is contained in:
Ryan Cole 2017-07-20 13:05:12 -04:00
parent 1aa0d5da86
commit d625e0e468
9 changed files with 40 additions and 35 deletions

View file

@ -1,6 +1,7 @@
package libkpod package libkpod
import ( import (
"encoding/json"
"sync" "sync"
"github.com/containers/image/types" "github.com/containers/image/types"
@ -9,6 +10,7 @@ import (
"github.com/docker/docker/pkg/truncindex" "github.com/docker/docker/pkg/truncindex"
"github.com/kubernetes-incubator/cri-o/oci" "github.com/kubernetes-incubator/cri-o/oci"
"github.com/kubernetes-incubator/cri-o/pkg/storage" "github.com/kubernetes-incubator/cri-o/pkg/storage"
"github.com/moby/moby/pkg/ioutils"
) )
// ContainerServer implements the ImageServer // 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 { type containerServerState struct {
containers oci.ContainerStorer containers oci.ContainerStorer
} }

View file

@ -335,7 +335,7 @@ func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerReq
return nil, err return nil, err
} }
s.containerStateToDisk(container) s.ContainerStateToDisk(container)
resp := &pb.CreateContainerResponse{ resp := &pb.CreateContainerResponse{
ContainerId: containerID, ContainerId: containerID,

View file

@ -29,7 +29,7 @@ func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerReque
if err != nil { if err != nil {
s.Runtime().SetStartFailed(c, err) s.Runtime().SetStartFailed(c, err)
} }
s.containerStateToDisk(c) s.ContainerStateToDisk(c)
}() }()
err = s.Runtime().StartContainer(c) err = s.Runtime().StartContainer(c)

View file

@ -29,7 +29,7 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq
if err = s.Runtime().UpdateStatus(c); err != nil { if err = s.Runtime().UpdateStatus(c); err != nil {
return nil, err return nil, err
} }
s.containerStateToDisk(c) s.ContainerStateToDisk(c)
containerID := c.ID() containerID := c.ID()
resp := &pb.ContainerStatusResponse{ resp := &pb.ContainerStatusResponse{

View file

@ -30,7 +30,7 @@ func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest
} }
} }
s.containerStateToDisk(c) s.ContainerStateToDisk(c)
resp := &pb.StopContainerResponse{} resp := &pb.StopContainerResponse{}
logrus.Debugf("StopContainerResponse: %+v", resp) logrus.Debugf("StopContainerResponse: %+v", resp)

View file

@ -500,7 +500,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
return nil, err return nil, err
} }
s.containerStateToDisk(container) s.ContainerStateToDisk(container)
resp = &pb.RunPodSandboxResponse{PodSandboxId: id} resp = &pb.RunPodSandboxResponse{PodSandboxId: id}
logrus.Debugf("RunPodSandboxResponse: %+v", resp) logrus.Debugf("RunPodSandboxResponse: %+v", resp)

View file

@ -19,7 +19,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR
if err = s.Runtime().UpdateStatus(podInfraContainer); err != nil { if err = s.Runtime().UpdateStatus(podInfraContainer); err != nil {
return nil, err return nil, err
} }
s.containerStateToDisk(podInfraContainer) s.ContainerStateToDisk(podInfraContainer)
cState := s.Runtime().ContainerStatus(podInfraContainer) cState := s.Runtime().ContainerStatus(podInfraContainer)

View file

@ -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) 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 { if err := label.ReleaseLabel(sb.ProcessLabel()); err != nil {

View file

@ -13,7 +13,6 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/containers/image/types" "github.com/containers/image/types"
cstorage "github.com/containers/storage" cstorage "github.com/containers/storage"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/registrar" "github.com/docker/docker/pkg/registrar"
"github.com/docker/docker/pkg/truncindex" "github.com/docker/docker/pkg/truncindex"
"github.com/kubernetes-incubator/cri-o/libkpod" "github.com/kubernetes-incubator/cri-o/libkpod"
@ -158,37 +157,12 @@ func (s *Server) loadContainer(id string) error {
return err return err
} }
s.containerStateFromDisk(ctr) s.ContainerStateFromDisk(ctr)
s.addContainer(ctr) s.addContainer(ctr)
return s.CtrIDIndex().Add(id) 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) { func configNetNsPath(spec rspec.Spec) (string, error) {
for _, ns := range spec.Linux.Namespaces { for _, ns := range spec.Linux.Namespaces {
if ns.Type != rspec.NetworkNamespace { if ns.Type != rspec.NetworkNamespace {
@ -302,7 +276,7 @@ func (s *Server) loadSandbox(id string) error {
return err return err
} }
s.containerStateFromDisk(scontainer) s.ContainerStateFromDisk(scontainer)
if err = label.ReserveLabel(processLabel); err != nil { if err = label.ReserveLabel(processLabel); err != nil {
return err return err