adds volatile state directory to the fs plan for cntrs/pods/fifo
Signed-off-by: Mike Brown <brownwm@us.ibm.com>
This commit is contained in:
@@ -78,8 +78,9 @@ func (c *criService) recover(ctx context.Context) error {
|
||||
return errors.Wrap(err, "failed to list containers")
|
||||
}
|
||||
for _, container := range containers {
|
||||
containerDir := getContainerRootDir(c.config.RootDir, container.ID())
|
||||
cntr, err := loadContainer(ctx, container, containerDir)
|
||||
containerDir := c.getContainerRootDir(container.ID())
|
||||
volatileContainerDir := c.getVolatileContainerRootDir(container.ID())
|
||||
cntr, err := loadContainer(ctx, container, containerDir, volatileContainerDir)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Errorf("Failed to load container %q", container.ID())
|
||||
continue
|
||||
@@ -113,21 +114,42 @@ func (c *criService) recover(ctx context.Context) error {
|
||||
// we can't even get metadata, we should cleanup orphaned sandbox/container directories
|
||||
// with best effort.
|
||||
|
||||
// Cleanup orphaned sandbox directories without corresponding containerd container.
|
||||
if err := cleanupOrphanedSandboxDirs(sandboxes, filepath.Join(c.config.RootDir, "sandboxes")); err != nil {
|
||||
return errors.Wrap(err, "failed to cleanup orphaned sandbox directories")
|
||||
// Cleanup orphaned sandbox and container directories without corresponding containerd container.
|
||||
for _, cleanup := range []struct {
|
||||
cntrs []containerd.Container
|
||||
base string
|
||||
errMsg string
|
||||
}{
|
||||
{
|
||||
cntrs: sandboxes,
|
||||
base: filepath.Join(c.config.RootDir, sandboxesDir),
|
||||
errMsg: "failed to cleanup orphaned sandbox directories",
|
||||
},
|
||||
{
|
||||
cntrs: sandboxes,
|
||||
base: filepath.Join(c.config.StateDir, sandboxesDir),
|
||||
errMsg: "failed to cleanup orphaned volatile sandbox directories",
|
||||
},
|
||||
{
|
||||
cntrs: containers,
|
||||
base: filepath.Join(c.config.RootDir, containersDir),
|
||||
errMsg: "failed to cleanup orphaned container directories",
|
||||
},
|
||||
{
|
||||
cntrs: containers,
|
||||
base: filepath.Join(c.config.StateDir, containersDir),
|
||||
errMsg: "failed to cleanup orphaned volatile container directories",
|
||||
},
|
||||
} {
|
||||
if err := cleanupOrphanedIDDirs(cleanup.cntrs, cleanup.base); err != nil {
|
||||
return errors.Wrap(err, cleanup.errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup orphaned container directories without corresponding containerd container.
|
||||
if err := cleanupOrphanedContainerDirs(containers, filepath.Join(c.config.RootDir, "containers")); err != nil {
|
||||
return errors.Wrap(err, "failed to cleanup orphaned container directories")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// loadContainer loads container from containerd and status checkpoint.
|
||||
func loadContainer(ctx context.Context, cntr containerd.Container, containerDir string) (containerstore.Container, error) {
|
||||
func loadContainer(ctx context.Context, cntr containerd.Container, containerDir, volatileContainerDir string) (containerstore.Container, error) {
|
||||
id := cntr.ID()
|
||||
var container containerstore.Container
|
||||
// Load container metadata.
|
||||
@@ -197,7 +219,7 @@ func loadContainer(ctx context.Context, cntr containerd.Container, containerDir
|
||||
// containerd got restarted during that. In that case, we still
|
||||
// treat the container as `CREATED`.
|
||||
containerIO, err = cio.NewContainerIO(id,
|
||||
cio.WithNewFIFOs(containerDir, meta.Config.GetTty(), meta.Config.GetStdin()),
|
||||
cio.WithNewFIFOs(volatileContainerDir, meta.Config.GetTty(), meta.Config.GetStdin()),
|
||||
)
|
||||
if err != nil {
|
||||
return container, errors.Wrap(err, "failed to create container io")
|
||||
@@ -448,59 +470,30 @@ func loadImages(ctx context.Context, cImages []containerd.Image,
|
||||
return images, nil
|
||||
}
|
||||
|
||||
func cleanupOrphanedSandboxDirs(cntrs []containerd.Container, sandboxesRoot string) error {
|
||||
// Cleanup orphaned sandbox directories.
|
||||
dirs, err := ioutil.ReadDir(sandboxesRoot)
|
||||
func cleanupOrphanedIDDirs(cntrs []containerd.Container, base string) error {
|
||||
// Cleanup orphaned id directories.
|
||||
dirs, err := ioutil.ReadDir(base)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return errors.Wrapf(err, "failed to read pod sandboxes directory %q", sandboxesRoot)
|
||||
return errors.Wrap(err, "failed to read base directory")
|
||||
}
|
||||
cntrsMap := make(map[string]containerd.Container)
|
||||
idsMap := make(map[string]containerd.Container)
|
||||
for _, cntr := range cntrs {
|
||||
cntrsMap[cntr.ID()] = cntr
|
||||
idsMap[cntr.ID()] = cntr
|
||||
}
|
||||
for _, d := range dirs {
|
||||
if !d.IsDir() {
|
||||
logrus.Warnf("Invalid file %q found in pod sandboxes directory", d.Name())
|
||||
logrus.Warnf("Invalid file %q found in base directory %q", d.Name(), base)
|
||||
continue
|
||||
}
|
||||
if _, ok := cntrsMap[d.Name()]; ok {
|
||||
// Do not remove sandbox directory if corresponding container is found.
|
||||
if _, ok := idsMap[d.Name()]; ok {
|
||||
// Do not remove id directory if corresponding container is found.
|
||||
continue
|
||||
}
|
||||
sandboxDir := filepath.Join(sandboxesRoot, d.Name())
|
||||
if err := system.EnsureRemoveAll(sandboxDir); err != nil {
|
||||
logrus.WithError(err).Warnf("Failed to remove pod sandbox directory %q", sandboxDir)
|
||||
dir := filepath.Join(base, d.Name())
|
||||
if err := system.EnsureRemoveAll(dir); err != nil {
|
||||
logrus.WithError(err).Warnf("Failed to remove id directory %q", dir)
|
||||
} else {
|
||||
logrus.Debugf("Cleanup orphaned pod sandbox directory %q", sandboxDir)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cleanupOrphanedContainerDirs(cntrs []containerd.Container, containersRoot string) error {
|
||||
// Cleanup orphaned container directories.
|
||||
dirs, err := ioutil.ReadDir(containersRoot)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return errors.Wrapf(err, "failed to read containers directory %q", containersRoot)
|
||||
}
|
||||
cntrsMap := make(map[string]containerd.Container)
|
||||
for _, cntr := range cntrs {
|
||||
cntrsMap[cntr.ID()] = cntr
|
||||
}
|
||||
for _, d := range dirs {
|
||||
if !d.IsDir() {
|
||||
logrus.Warnf("Invalid file %q found in containers directory", d.Name())
|
||||
continue
|
||||
}
|
||||
if _, ok := cntrsMap[d.Name()]; ok {
|
||||
// Do not remove container directory if corresponding container is found.
|
||||
continue
|
||||
}
|
||||
containerDir := filepath.Join(containersRoot, d.Name())
|
||||
if err := system.EnsureRemoveAll(containerDir); err != nil {
|
||||
logrus.WithError(err).Warnf("Failed to remove container directory %q", containerDir)
|
||||
} else {
|
||||
logrus.Debugf("Cleanup orphaned container directory %q", containerDir)
|
||||
logrus.Debugf("Cleanup orphaned id directory %q", dir)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user