sandboxes restore on server start
Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
parent
87a15ecd42
commit
4fd1c583ad
2 changed files with 82 additions and 2 deletions
|
@ -1,7 +1,9 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
|
@ -177,6 +179,24 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
|
|||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/kubernetes-incubator/ocid/oci"
|
||||
"github.com/kubernetes-incubator/ocid/utils"
|
||||
"github.com/rajatchopra/ocicni"
|
||||
|
@ -24,6 +28,50 @@ type Server struct {
|
|||
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
|
||||
func New(runtimePath, sandboxDir, containerDir string) (*Server, error) {
|
||||
// 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 {
|
||||
return nil, err
|
||||
}
|
||||
return &Server{
|
||||
s := &Server{
|
||||
runtime: r,
|
||||
netPlugin: netPlugin,
|
||||
sandboxDir: sandboxDir,
|
||||
|
@ -56,7 +104,13 @@ func New(runtimePath, sandboxDir, containerDir string) (*Server, error) {
|
|||
sandboxes: sandboxes,
|
||||
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 {
|
||||
|
@ -71,6 +125,12 @@ type sandbox struct {
|
|||
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) {
|
||||
s.containers.Add(c.Name(), c)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue