server: restore containers state from disk on startup

Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
Antonio Murdaca 2017-05-11 12:10:18 +02:00
parent da0b8a6157
commit a41ca975c1
No known key found for this signature in database
GPG key ID: B2BEAD150DE936B9
2 changed files with 34 additions and 9 deletions

View file

@ -1,7 +1,9 @@
package oci package oci
import ( import (
"encoding/json"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"sync" "sync"
"time" "time"
@ -65,6 +67,18 @@ func NewContainer(id string, name string, bundlePath string, logPath string, net
return c, nil 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 // StatePath returns the containers state.json path
func (c *Container) StatePath() string { func (c *Container) StatePath() string {
return filepath.Join(c.dir, "state.json") return filepath.Join(c.dir, "state.json")

View file

@ -12,6 +12,7 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/containers/image/types" "github.com/containers/image/types"
sstorage "github.com/containers/storage" sstorage "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/oci" "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/pkg/storage"
"github.com/kubernetes-incubator/cri-o/server/apparmor" "github.com/kubernetes-incubator/cri-o/server/apparmor"
"github.com/kubernetes-incubator/cri-o/server/seccomp" "github.com/kubernetes-incubator/cri-o/server/seccomp"
"github.com/moby/moby/pkg/ioutils"
rspec "github.com/opencontainers/runtime-spec/specs-go" rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/selinux/go-selinux/label" "github.com/opencontainers/selinux/go-selinux/label"
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
@ -122,7 +122,7 @@ func (s *Server) loadContainer(id string) error {
return err return err
} }
containerDir, err := s.store.GetContainerDirectory(id) containerDir, err := s.store.ContainerDirectory(id)
if err != nil { if err != nil {
return err return err
} }
@ -149,13 +149,24 @@ func (s *Server) loadContainer(id string) error {
if err != nil { if err != nil {
return err 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) 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 { func (s *Server) containerStateToDisk(c *oci.Container) error {
// ignore errors, this is a best effort to have up-to-date info about // ignore errors, this is a best effort to have up-to-date info about
// a given container before its state gets stored // a given container before its state gets stored
@ -270,7 +281,7 @@ func (s *Server) loadSandbox(id string) error {
return err return err
} }
sandboxDir, err := s.store.GetContainerDirectory(id) sandboxDir, err := s.store.ContainerDirectory(id)
if err != nil { if err != nil {
return err return err
} }
@ -294,9 +305,9 @@ func (s *Server) loadSandbox(id string) error {
if err != nil { if err != nil {
return err 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 { if err = label.ReserveLabel(processLabel); err != nil {
return err return err
} }