From ddb9e73d55daebc3d8a78c58a8ff05233b956191 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Thu, 11 May 2017 12:10:18 +0200 Subject: [PATCH] server: restore containers state from disk on startup Signed-off-by: Antonio Murdaca --- oci/container.go | 14 ++++++++++++++ server/server.go | 29 ++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/oci/container.go b/oci/container.go index 7a5ba81e..4717b9ea 100644 --- a/oci/container.go +++ b/oci/container.go @@ -1,7 +1,9 @@ package oci import ( + "encoding/json" "fmt" + "os" "path/filepath" "sync" "time" @@ -65,6 +67,18 @@ func NewContainer(id string, name string, bundlePath string, logPath string, net return c, nil } +// FromDisk restores container's state from disk +func (c *Container) FromDisk() error { + jsonSource, err := os.Open(c.StatePath()) + if err != nil { + return err + } + defer jsonSource.Close() + + dec := json.NewDecoder(jsonSource) + return dec.Decode(c.state) +} + // StatePath returns the containers state.json path func (c *Container) StatePath() string { return filepath.Join(c.dir, "state.json") diff --git a/server/server.go b/server/server.go index 2b0db7af..533149ae 100644 --- a/server/server.go +++ b/server/server.go @@ -12,6 +12,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/containers/image/types" sstorage "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/oci" @@ -19,7 +20,6 @@ import ( "github.com/kubernetes-incubator/cri-o/pkg/storage" "github.com/kubernetes-incubator/cri-o/server/apparmor" "github.com/kubernetes-incubator/cri-o/server/seccomp" - "github.com/moby/moby/pkg/ioutils" rspec "github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/selinux/go-selinux/label" pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" @@ -122,7 +122,7 @@ func (s *Server) loadContainer(id string) error { return err } - containerDir, err := s.store.GetContainerDirectory(id) + containerDir, err := s.store.ContainerDirectory(id) if err != nil { return err } @@ -149,13 +149,24 @@ func (s *Server) loadContainer(id string) error { if err != nil { return err } - if err = s.runtime.UpdateStatus(ctr); err != nil { - return fmt.Errorf("error updating status for container %s: %v", ctr.ID(), err) - } + + 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 @@ -270,7 +281,7 @@ func (s *Server) loadSandbox(id string) error { return err } - sandboxDir, err := s.store.GetContainerDirectory(id) + sandboxDir, err := s.store.ContainerDirectory(id) if err != nil { return err } @@ -294,9 +305,9 @@ func (s *Server) loadSandbox(id string) error { if err != nil { return err } - if err = s.runtime.UpdateStatus(scontainer); err != nil { - return fmt.Errorf("error updating status for pod sandbox infra container %s: %v", scontainer.ID(), err) - } + + s.containerStateFromDisk(scontainer) + if err = label.ReserveLabel(processLabel); err != nil { return err }