server: containers restore
Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
parent
cc732301ac
commit
54d6ddb5af
3 changed files with 99 additions and 15 deletions
|
@ -1,6 +1,7 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
@ -204,7 +205,6 @@ func (s *Server) createSandboxContainer(containerID string, containerName string
|
||||||
specgen.AddAnnotation(k, v)
|
specgen.AddAnnotation(k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if containerConfig.GetPrivileged() {
|
if containerConfig.GetPrivileged() {
|
||||||
specgen.SetupPrivileged(true)
|
specgen.SetupPrivileged(true)
|
||||||
}
|
}
|
||||||
|
@ -305,7 +305,18 @@ func (s *Server) createSandboxContainer(containerID string, containerName string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := specgen.SaveToFile(filepath.Join(containerDir, "config.json")); err != nil {
|
specgen.AddAnnotation("ocid/name", containerName)
|
||||||
|
specgen.AddAnnotation("ocid/sandbox_id", sb.id)
|
||||||
|
specgen.AddAnnotation("ocid/log_path", logPath)
|
||||||
|
specgen.AddAnnotation("ocid/tty", fmt.Sprintf("%v", containerConfig.GetTty()))
|
||||||
|
labelsJSON, err := json.Marshal(labels)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
specgen.AddAnnotation("ocid/labels", string(labelsJSON))
|
||||||
|
|
||||||
|
if err = specgen.SaveToFile(filepath.Join(containerDir, "config.json")); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,7 +332,7 @@ func (s *Server) createSandboxContainer(containerID string, containerName string
|
||||||
|
|
||||||
// TODO: copy the rootfs into the bundle.
|
// TODO: copy the rootfs into the bundle.
|
||||||
// Currently, utils.CreateFakeRootfs is used to populate the rootfs.
|
// Currently, utils.CreateFakeRootfs is used to populate the rootfs.
|
||||||
if err := utils.CreateFakeRootfs(containerDir, image); err != nil {
|
if err = utils.CreateFakeRootfs(containerDir, image); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -173,6 +173,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
g.SetProcessSelinuxLabel(processLabel)
|
||||||
}
|
}
|
||||||
|
|
||||||
containerID, containerName, err := s.generateContainerIDandName(name, "infra", 0)
|
containerID, containerName, err := s.generateContainerIDandName(name, "infra", 0)
|
||||||
|
@ -355,6 +356,10 @@ func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxR
|
||||||
s.removeContainer(c)
|
s.removeContainer(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := label.UnreserveLabel(sb.processLabel); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// Remove the files related to the sandbox
|
// Remove the files related to the sandbox
|
||||||
podSandboxDir := filepath.Join(s.sandboxDir, sb.id)
|
podSandboxDir := filepath.Join(s.sandboxDir, sb.id)
|
||||||
if err := os.RemoveAll(podSandboxDir); err != nil {
|
if err := os.RemoveAll(podSandboxDir); err != nil {
|
||||||
|
|
|
@ -38,6 +38,49 @@ type Server struct {
|
||||||
ctrIDIndex *truncindex.TruncIndex
|
ctrIDIndex *truncindex.TruncIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) loadContainer(id string) error {
|
||||||
|
config, err := ioutil.ReadFile(filepath.Join(s.runtime.ContainerDir(), id, "config.json"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var m rspec.Spec
|
||||||
|
if err = json.Unmarshal(config, &m); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
labels := make(map[string]string)
|
||||||
|
if err = json.Unmarshal([]byte(m.Annotations["ocid/labels"]), &labels); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
name := m.Annotations["ocid/name"]
|
||||||
|
name, err = s.reserveContainerName(id, name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sb := s.getSandbox(m.Annotations["ocid/sandbox_id"])
|
||||||
|
if sb == nil {
|
||||||
|
logrus.Warnf("could not get sandbox with id %s, skipping", m.Annotations["ocid/sandbox_id"])
|
||||||
|
}
|
||||||
|
|
||||||
|
var tty bool
|
||||||
|
if v := m.Annotations["ocid/tty"]; v == "true" {
|
||||||
|
tty = true
|
||||||
|
}
|
||||||
|
containerPath := filepath.Join(s.runtime.ContainerDir(), id)
|
||||||
|
|
||||||
|
ctr, err := oci.NewContainer(id, name, containerPath, m.Annotations["ocid/log_path"], labels, sb.id, tty)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.addContainer(ctr)
|
||||||
|
if err = s.runtime.UpdateStatus(ctr); err != nil {
|
||||||
|
logrus.Warnf("error updating status for container %s: %v", ctr.ID(), err)
|
||||||
|
}
|
||||||
|
if err = s.ctrIDIndex.Add(id); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) loadSandbox(id string) error {
|
func (s *Server) loadSandbox(id string) error {
|
||||||
config, err := ioutil.ReadFile(filepath.Join(s.sandboxDir, id, "config.json"))
|
config, err := ioutil.ReadFile(filepath.Join(s.sandboxDir, id, "config.json"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -72,13 +115,24 @@ func (s *Server) loadSandbox(id string) error {
|
||||||
})
|
})
|
||||||
sandboxPath := filepath.Join(s.sandboxDir, id)
|
sandboxPath := filepath.Join(s.sandboxDir, id)
|
||||||
|
|
||||||
scontainer, err := oci.NewContainer(m.Annotations["ocid/container_id"], m.Annotations["ocid/container_name"], sandboxPath, sandboxPath, labels, id, false)
|
if err := label.ReserveLabel(processLabel); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cname, err := s.reserveContainerName(m.Annotations["ocid/container_id"], m.Annotations["ocid/container_name"])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
scontainer, err := oci.NewContainer(m.Annotations["ocid/container_id"], cname, sandboxPath, sandboxPath, labels, id, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.addContainer(scontainer)
|
s.addContainer(scontainer)
|
||||||
if err = s.runtime.UpdateStatus(scontainer); err != nil {
|
if err = s.runtime.UpdateStatus(scontainer); err != nil {
|
||||||
logrus.Warnf("error updating status for container %s: %v", scontainer, err)
|
logrus.Warnf("error updating status for container %s: %v", scontainer.ID(), err)
|
||||||
|
}
|
||||||
|
if err = s.ctrIDIndex.Add(scontainer.ID()); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
if err = s.podIDIndex.Add(id); err != nil {
|
if err = s.podIDIndex.Add(id); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -86,17 +140,32 @@ func (s *Server) loadSandbox(id string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) restore() error {
|
func (s *Server) restore() {
|
||||||
dir, err := ioutil.ReadDir(s.sandboxDir)
|
sandboxDir, err := ioutil.ReadDir(s.sandboxDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.Warnf("could not read sandbox directory %s: %v", sandboxDir, err)
|
||||||
}
|
}
|
||||||
for _, v := range dir {
|
for _, v := range sandboxDir {
|
||||||
if err := s.loadSandbox(v.Name()); err != nil {
|
if !v.IsDir() {
|
||||||
return err
|
continue
|
||||||
|
}
|
||||||
|
if err = s.loadSandbox(v.Name()); err != nil {
|
||||||
|
logrus.Warnf("could not restore sandbox %s: %v", v.Name(), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
containerDir, err := ioutil.ReadDir(s.runtime.ContainerDir())
|
||||||
|
if err != nil {
|
||||||
|
logrus.Warnf("could not read container directory %s: %v", s.runtime.ContainerDir(), err)
|
||||||
|
}
|
||||||
|
for _, v := range containerDir {
|
||||||
|
if !v.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := s.loadContainer(v.Name()); err != nil {
|
||||||
|
logrus.Warnf("could not restore container %s: %v", v.Name(), err)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) reservePodName(id, name string) (string, error) {
|
func (s *Server) reservePodName(id, name string) (string, error) {
|
||||||
|
@ -182,9 +251,8 @@ func New(runtimePath, root, sandboxDir, containerDir, conmonPath, pausePath stri
|
||||||
s.ctrIDIndex = truncindex.NewTruncIndex([]string{})
|
s.ctrIDIndex = truncindex.NewTruncIndex([]string{})
|
||||||
s.ctrNameIndex = registrar.NewRegistrar()
|
s.ctrNameIndex = registrar.NewRegistrar()
|
||||||
|
|
||||||
if err := s.restore(); err != nil {
|
s.restore()
|
||||||
logrus.Warnf("couldn't restore: %v", err)
|
|
||||||
}
|
|
||||||
logrus.Debugf("sandboxes: %v", s.state.sandboxes)
|
logrus.Debugf("sandboxes: %v", s.state.sandboxes)
|
||||||
logrus.Debugf("containers: %v", s.state.containers)
|
logrus.Debugf("containers: %v", s.state.containers)
|
||||||
return s, nil
|
return s, nil
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue