sandboxes restore on server start

Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
Antonio Murdaca 2016-09-18 16:35:17 +02:00
parent 87a15ecd42
commit 4fd1c583ad
No known key found for this signature in database
GPG key ID: B2BEAD150DE936B9
2 changed files with 82 additions and 2 deletions

View file

@ -1,7 +1,9 @@
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -177,6 +179,24 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
s.addContainer(container) s.addContainer(container)
meta := &metadata{
LogDir: logDir,
ContainerName: containerName,
Labels: labels,
}
b, err := json.Marshal(meta)
if err != nil {
return nil, err
}
// TODO: eventually we would track all containers in this pod so on server start
// we can repopulate the structs in memory properly...
// e.g. each container can write itself in podSandboxDir
if err := ioutil.WriteFile(filepath.Join(podSandboxDir, "metadata.json"), b, 0644); err != nil {
return nil, err
}
if err = s.runtime.UpdateStatus(container); err != nil { if err = s.runtime.UpdateStatus(container); err != nil {
return nil, err return nil, err
} }

View file

@ -1,10 +1,14 @@
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath"
"sync" "sync"
"github.com/Sirupsen/logrus"
"github.com/kubernetes-incubator/ocid/oci" "github.com/kubernetes-incubator/ocid/oci"
"github.com/kubernetes-incubator/ocid/utils" "github.com/kubernetes-incubator/ocid/utils"
"github.com/rajatchopra/ocicni" "github.com/rajatchopra/ocicni"
@ -24,6 +28,50 @@ type Server struct {
netPlugin ocicni.CNIPlugin netPlugin ocicni.CNIPlugin
} }
func (s *Server) loadSandboxes() error {
if err := filepath.Walk(s.sandboxDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
if path == s.sandboxDir {
return nil
}
metaJSON, err := ioutil.ReadFile(filepath.Join(path, "metadata.json"))
if err != nil {
return err
}
var m metadata
if err := json.Unmarshal(metaJSON, &m); err != nil {
return err
}
sname, err := filepath.Rel(s.sandboxDir, path)
if err != nil {
return err
}
s.addSandbox(&sandbox{
name: sname,
logDir: m.LogDir,
labels: m.Labels,
containers: make(map[string]*oci.Container),
})
scontainer, err := oci.NewContainer(m.ContainerName, path, path, m.Labels, sname, false)
if err != nil {
return err
}
s.addContainer(scontainer)
if err = s.runtime.UpdateStatus(scontainer); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}
// New creates a new Server with options provided // New creates a new Server with options provided
func New(runtimePath, sandboxDir, containerDir string) (*Server, error) { func New(runtimePath, sandboxDir, containerDir string) (*Server, error) {
// TODO: This will go away later when we have wrapper process or systemd acting as // TODO: This will go away later when we have wrapper process or systemd acting as
@ -48,7 +96,7 @@ func New(runtimePath, sandboxDir, containerDir string) (*Server, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Server{ s := &Server{
runtime: r, runtime: r,
netPlugin: netPlugin, netPlugin: netPlugin,
sandboxDir: sandboxDir, sandboxDir: sandboxDir,
@ -56,7 +104,13 @@ func New(runtimePath, sandboxDir, containerDir string) (*Server, error) {
sandboxes: sandboxes, sandboxes: sandboxes,
containers: containers, containers: containers,
}, },
}, nil }
if err := s.loadSandboxes(); err != nil {
logrus.Warnf("couldn't get sandboxes: %v", err)
}
logrus.Debugf("sandboxes: %v", s.state.sandboxes)
logrus.Debugf("containers: %v", s.state.containers)
return s, nil
} }
type serverState struct { type serverState struct {
@ -71,6 +125,12 @@ type sandbox struct {
containers oci.Store containers oci.Store
} }
type metadata struct {
LogDir string `json:"log_dir"`
ContainerName string `json:"container_name"`
Labels map[string]string `json:"labels"`
}
func (s *sandbox) addContainer(c *oci.Container) { func (s *sandbox) addContainer(c *oci.Container) {
s.containers.Add(c.Name(), c) s.containers.Add(c.Name(), c)
} }