Skip to content

Commit 9150dc8

Browse files
committed
Migrate task directory
Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent 80492ce commit 9150dc8

6 files changed

Lines changed: 48 additions & 39 deletions

File tree

integration/client/container_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ func getLogDirPath(runtimeVersion, id string) string {
610610
case "v1":
611611
return filepath.Join(defaultRoot, plugin.RuntimeLinuxV1, testNamespace, id)
612612
case "v2":
613-
return filepath.Join(defaultState, "io.containerd.runtime.v2.task", testNamespace, id)
613+
return filepath.Join(defaultState, "io.containerd.runtime-shim.v2.shim", testNamespace, id)
614614
default:
615615
panic(fmt.Errorf("Unsupported runtime version %s", runtimeVersion))
616616
}

runtime/task_list.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,16 @@ func (l *TaskList) Delete(ctx context.Context, id string) {
128128
delete(tasks, id)
129129
}
130130
}
131+
132+
func (l *TaskList) IsEmpty() bool {
133+
l.mu.Lock()
134+
defer l.mu.Unlock()
135+
136+
for ns := range l.tasks {
137+
if len(l.tasks[ns]) > 0 {
138+
return false
139+
}
140+
}
141+
142+
return true
143+
}

runtime/v2/manager.go

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -92,49 +92,22 @@ func init() {
9292
ID: "task",
9393
Requires: []plugin.Type{
9494
plugin.RuntimeShimPlugin,
95-
plugin.EventPlugin,
96-
plugin.MetadataPlugin,
9795
},
9896
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
99-
m, err := ic.Get(plugin.MetadataPlugin)
100-
if err != nil {
101-
return nil, err
102-
}
103-
ep, err := ic.GetByID(plugin.EventPlugin, "exchange")
97+
shimManagerInterface, err := ic.Get(plugin.RuntimeShimPlugin)
10498
if err != nil {
10599
return nil, err
106100
}
107-
cs := metadata.NewContainerStore(m.(*metadata.DB))
108-
events := ep.(*exchange.Exchange)
109101

110-
shimManager, err := NewShimManager(ic.Context, &ManagerConfig{
111-
Root: ic.Root,
112-
State: ic.State,
113-
Address: ic.Address,
114-
TTRPCAddress: ic.TTRPCAddress,
115-
Events: events,
116-
Store: cs,
117-
SchedCore: false,
118-
})
119-
if err != nil {
120-
return nil, err
121-
}
102+
shimManager := shimManagerInterface.(*ShimManager)
122103

123-
if err := shimManager.loadExistingTasks(ic.Context); err != nil {
104+
// From now on task manager works via shim manager, which has different home directory.
105+
// Check if there are any leftovers from previous containerd versions and migrate home directory,
106+
// so we can properly restore existing tasks as well.
107+
if err := migrateTasks(ic, shimManager); err != nil {
124108
return nil, err
125109
}
126110

127-
// Internally task manager relies on shim manager to launch task shims.
128-
// It's also possible to use shim manager independently and launch other types of shims.
129-
//
130-
// Ideally task manager should depend on shim instance we registered above, however it'll use
131-
// different home directory (`io.containerd.runtime.v2.task` vs `io.containerd.runtime.v2.shim`),
132-
// which will break backward compatibility when upgrading containerd to the new version.
133-
//
134-
// For now, we create another instance of shim manager with the "old" home directory, so shim tasks
135-
// are properly restored, but will work independently.
136-
//
137-
// See more context https://github.com/containerd/containerd/pull/5918#discussion_r705434412
138111
return NewTaskManager(shimManager), nil
139112
},
140113
})
@@ -150,6 +123,32 @@ type ManagerConfig struct {
150123
SchedCore bool
151124
}
152125

126+
func migrateTasks(ic *plugin.InitContext, shimManager *ShimManager) error {
127+
if !shimManager.list.IsEmpty() {
128+
return nil
129+
}
130+
131+
if err := os.Rename(ic.Root, shimManager.root); err != nil {
132+
if os.IsNotExist(err) {
133+
return nil
134+
}
135+
return fmt.Errorf("failed to migrate task `root` directory")
136+
}
137+
138+
if err := os.Rename(ic.State, shimManager.state); err != nil {
139+
if os.IsNotExist(err) {
140+
return nil
141+
}
142+
return fmt.Errorf("failed to migrate task `state` directory")
143+
}
144+
145+
if err := shimManager.loadExistingTasks(ic.Context); err != nil {
146+
return fmt.Errorf("failed to load tasks after migration")
147+
}
148+
149+
return nil
150+
}
151+
153152
// NewShimManager creates a manager for v2 shims
154153
func NewShimManager(ctx context.Context, config *ManagerConfig) (*ShimManager, error) {
155154
for _, d := range []string{config.Root, config.State} {
@@ -192,7 +191,7 @@ type ShimManager struct {
192191

193192
// ID of the shim manager
194193
func (m *ShimManager) ID() string {
195-
return fmt.Sprintf("%s.%s", plugin.RuntimePluginV2, "shim")
194+
return fmt.Sprintf("%s.%s", plugin.RuntimeShimPlugin, "shim")
196195
}
197196

198197
// Start launches a new shim instance
@@ -323,7 +322,7 @@ func NewTaskManager(shims *ShimManager) *TaskManager {
323322

324323
// ID of the task manager
325324
func (m *TaskManager) ID() string {
326-
return fmt.Sprintf("%s.%s", plugin.RuntimeShimPlugin, "task")
325+
return fmt.Sprintf("%s.%s", plugin.RuntimePluginV2, "task")
327326
}
328327

329328
// Create launches new shim instance and creates new task

services/tasks/local_freebsd.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
var tasksServiceRequires = []plugin.Type{
2525
plugin.EventPlugin,
2626
plugin.RuntimePluginV2,
27-
plugin.RuntimeShimPlugin,
2827
plugin.MetadataPlugin,
2928
plugin.TaskMonitorPlugin,
3029
}

services/tasks/local_unix.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ var tasksServiceRequires = []plugin.Type{
3030
plugin.EventPlugin,
3131
plugin.RuntimePlugin,
3232
plugin.RuntimePluginV2,
33-
plugin.RuntimeShimPlugin,
3433
plugin.MetadataPlugin,
3534
plugin.TaskMonitorPlugin,
3635
}

services/tasks/local_windows.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
var tasksServiceRequires = []plugin.Type{
2525
plugin.EventPlugin,
2626
plugin.RuntimePluginV2,
27-
plugin.RuntimeShimPlugin,
2827
plugin.MetadataPlugin,
2928
plugin.TaskMonitorPlugin,
3029
}

0 commit comments

Comments
 (0)