Skip to content

Commit 23fbdba

Browse files
committed
Cleanup workdirs on manager load
This cleans up persistent work dirs on TaskManager boot. These dirs can be left behind in a machine reboot. The state in /run will not exist but the work dir in the root does, we should cleanup work dirs when tasks are not loaded. This also improves error handling that would prevent the task manager from loading when a single task fails to load or cleanup. Signed-off-by: Michael Crosby <[email protected]>
1 parent 2ebfba5 commit 23fbdba

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

runtime/restart/monitor/monitor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ func (m *monitor) reconcile(ctx context.Context) error {
168168
ctx = namespaces.WithNamespace(ctx, name)
169169
changes, err := m.monitor(ctx)
170170
if err != nil {
171-
return err
171+
logrus.WithError(err).Error("monitor for changes")
172+
continue
172173
}
173174
for _, c := range changes {
174175
if err := c.apply(ctx, m.client); err != nil {

runtime/v2/manager.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/containerd/containerd/plugin"
3535
"github.com/containerd/containerd/runtime"
3636
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
37-
"github.com/pkg/errors"
3837
)
3938

4039
func init() {
@@ -150,7 +149,12 @@ func (m *TaskManager) loadExistingTasks(ctx context.Context) error {
150149
ns := nsd.Name()
151150
log.G(ctx).WithField("namespace", ns).Debug("loading tasks in namespace")
152151
if err := m.loadTasks(namespaces.WithNamespace(ctx, ns)); err != nil {
153-
return err
152+
log.G(ctx).WithField("namespace", ns).WithError(err).Error("loading tasks in namespace")
153+
continue
154+
}
155+
if err := m.cleanupWorkDirs(namespaces.WithNamespace(ctx, ns)); err != nil {
156+
log.G(ctx).WithField("namespace", ns).WithError(err).Error("cleanup working directory in namespace")
157+
continue
154158
}
155159
}
156160
return nil
@@ -172,12 +176,16 @@ func (m *TaskManager) loadTasks(ctx context.Context) error {
172176
id := sd.Name()
173177
bundle, err := LoadBundle(ctx, m.state, id)
174178
if err != nil {
179+
// fine to return error here, it is a programmer error if the context
180+
// does not have a namespace
175181
return err
176182
}
177183
// fast path
178184
bf, err := ioutil.ReadDir(bundle.Path)
179185
if err != nil {
180-
return err
186+
bundle.Delete()
187+
log.G(ctx).WithError(err).Errorf("fast path read bundle path for %s", bundle.Path)
188+
continue
181189
}
182190
if len(bf) == 0 {
183191
bundle.Delete()
@@ -197,7 +205,8 @@ func (m *TaskManager) loadTasks(ctx context.Context) error {
197205
}
198206
binaryCall := shimBinary(ctx, bundle, container.Runtime.Name, m.containerdAddress, m.events, m.tasks)
199207
if _, err := binaryCall.Delete(ctx); err != nil {
200-
return errors.Wrapf(err, "remove disk state %s", id)
208+
log.G(ctx).WithError(err).Errorf("binary call to delete for %s", id)
209+
continue
201210
}
202211
continue
203212
}
@@ -218,3 +227,26 @@ func (m *TaskManager) container(ctx context.Context, id string) (*containers.Con
218227
}
219228
return &container, nil
220229
}
230+
231+
func (m *TaskManager) cleanupWorkDirs(ctx context.Context) error {
232+
ns, err := namespaces.NamespaceRequired(ctx)
233+
if err != nil {
234+
return err
235+
}
236+
dirs, err := ioutil.ReadDir(filepath.Join(m.root, ns))
237+
if err != nil {
238+
return err
239+
}
240+
for _, d := range dirs {
241+
// if the task was not loaded, cleanup and empty working directory
242+
// this can happen on a reboot where /run for the bundle state is cleaned up
243+
// but that persistent working dir is left
244+
if _, err := m.tasks.Get(ctx, d.Name()); err != nil {
245+
path := filepath.Join(m.root, ns, d.Name())
246+
if err := os.RemoveAll(path); err != nil {
247+
log.G(ctx).WithError(err).Errorf("cleanup working dir %s", path)
248+
}
249+
}
250+
}
251+
return nil
252+
}

0 commit comments

Comments
 (0)