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
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
@ -204,7 +205,6 @@ func (s *Server) createSandboxContainer(containerID string, containerName string
|
|||
specgen.AddAnnotation(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
if containerConfig.GetPrivileged() {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -321,7 +332,7 @@ func (s *Server) createSandboxContainer(containerID string, containerName string
|
|||
|
||||
// TODO: copy the rootfs into the bundle.
|
||||
// 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
|
||||
}
|
||||
|
||||
|
|
|
@ -173,6 +173,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g.SetProcessSelinuxLabel(processLabel)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if err := label.UnreserveLabel(sb.processLabel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Remove the files related to the sandbox
|
||||
podSandboxDir := filepath.Join(s.sandboxDir, sb.id)
|
||||
if err := os.RemoveAll(podSandboxDir); err != nil {
|
||||
|
|
|
@ -38,6 +38,49 @@ type Server struct {
|
|||
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 {
|
||||
config, err := ioutil.ReadFile(filepath.Join(s.sandboxDir, id, "config.json"))
|
||||
if err != nil {
|
||||
|
@ -72,13 +115,24 @@ func (s *Server) loadSandbox(id string) error {
|
|||
})
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
s.addContainer(scontainer)
|
||||
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 {
|
||||
return err
|
||||
|
@ -86,17 +140,32 @@ func (s *Server) loadSandbox(id string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) restore() error {
|
||||
dir, err := ioutil.ReadDir(s.sandboxDir)
|
||||
func (s *Server) restore() {
|
||||
sandboxDir, err := ioutil.ReadDir(s.sandboxDir)
|
||||
if err != nil {
|
||||
return err
|
||||
logrus.Warnf("could not read sandbox directory %s: %v", sandboxDir, err)
|
||||
}
|
||||
for _, v := range dir {
|
||||
if err := s.loadSandbox(v.Name()); err != nil {
|
||||
return err
|
||||
for _, v := range sandboxDir {
|
||||
if !v.IsDir() {
|
||||
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) {
|
||||
|
@ -182,9 +251,8 @@ func New(runtimePath, root, sandboxDir, containerDir, conmonPath, pausePath stri
|
|||
s.ctrIDIndex = truncindex.NewTruncIndex([]string{})
|
||||
s.ctrNameIndex = registrar.NewRegistrar()
|
||||
|
||||
if err := s.restore(); err != nil {
|
||||
logrus.Warnf("couldn't restore: %v", err)
|
||||
}
|
||||
s.restore()
|
||||
|
||||
logrus.Debugf("sandboxes: %v", s.state.sandboxes)
|
||||
logrus.Debugf("containers: %v", s.state.containers)
|
||||
return s, nil
|
||||
|
|
Loading…
Reference in a new issue