Skip to content

Commit a7c63c7

Browse files
committed
feat(shim): add urunc task service wrapper
Signed-off-by: sidneychang <2190206983@qq.com>
1 parent 11e894a commit a7c63c7

6 files changed

Lines changed: 153 additions & 5 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ URUNC_SRC += $(wildcard $(CURDIR)/pkg/unikontainers/types/*.go)
7272
URUNC_SRC += $(wildcard $(CURDIR)/pkg/unikontainers/initrd/*.go)
7373
URUNC_SRC += $(wildcard $(CURDIR)/pkg/network/*.go)
7474
SHIM_SRC := $(wildcard $(CURDIR)/cmd/containerd-shim-urunc-v2/*.go)
75+
SHIM_SRC += $(wildcard $(CURDIR)/pkg/containerd-shim/*.go)
7576

7677
#? CNTR_TOOL Tool to run the linter container (default: docker)
7778
CNTR_TOOL ?= docker

cmd/containerd-shim-urunc-v2/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ package main
1717
import (
1818
"context"
1919

20-
"github.com/containerd/containerd/runtime/v2/runc/manager"
21-
_ "github.com/containerd/containerd/runtime/v2/runc/task/plugin"
2220
"github.com/containerd/containerd/runtime/v2/shim"
21+
containerdshim "github.com/urunc-dev/urunc/pkg/containerd-shim"
2322
)
2423

2524
func main() {
26-
shim.RunManager(context.Background(), manager.NewShimManager("io.containerd.urunc.v2"))
25+
shim.RunManager(context.Background(), containerdshim.NewShimManager("io.containerd.urunc.v2"))
2726
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ require (
77
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
88
github.com/cavaliergopher/cpio v1.0.1
99
github.com/containerd/containerd v1.7.30
10+
github.com/containerd/containerd/api v1.10.0
11+
github.com/containerd/ttrpc v1.2.7
1012
github.com/creack/pty v1.1.24
1113
github.com/elastic/go-seccomp-bpf v1.6.0
1214
github.com/hashicorp/go-version v1.9.0
@@ -36,15 +38,13 @@ require (
3638
github.com/cilium/ebpf v0.20.0 // indirect
3739
github.com/containerd/cgroups/v3 v3.1.0 // indirect
3840
github.com/containerd/console v1.0.5 // indirect
39-
github.com/containerd/containerd/api v1.10.0 // indirect
4041
github.com/containerd/continuity v0.4.5 // indirect
4142
github.com/containerd/errdefs v1.0.0 // indirect
4243
github.com/containerd/errdefs/pkg v0.3.0 // indirect
4344
github.com/containerd/fifo v1.1.0 // indirect
4445
github.com/containerd/go-runc v1.0.0 // indirect
4546
github.com/containerd/log v0.1.0 // indirect
4647
github.com/containerd/platforms v0.2.1 // indirect
47-
github.com/containerd/ttrpc v1.2.7 // indirect
4848
github.com/containerd/typeurl/v2 v2.2.3 // indirect
4949
github.com/coreos/go-systemd/v22 v22.7.0 // indirect
5050
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect

pkg/containerd-shim/manager.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2023-2026, Nubificus LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package containerdshim
16+
17+
import (
18+
"context"
19+
20+
runcManager "github.com/containerd/containerd/runtime/v2/runc/manager"
21+
"github.com/containerd/containerd/runtime/v2/shim"
22+
)
23+
24+
// NewShimManager returns urunc's shim manager wrapper around containerd's runc
25+
// shim manager. The wrapper is behavior-preserving today, but gives urunc a
26+
// manager-level hook for cleanup paths that do not go through TaskService.
27+
func NewShimManager(name string) shim.Manager {
28+
return &manager{
29+
Manager: runcManager.NewShimManager(name),
30+
}
31+
}
32+
33+
type manager struct {
34+
shim.Manager
35+
}
36+
37+
// Stop handles the shim binary "delete" action path.
38+
//
39+
// In containerd runtime-v2, normal task deletion reaches the long-running shim
40+
// over ttrpc and therefore goes through taskService.Delete. Dead-shim cleanup is
41+
// different: when containerd cannot reconnect to the shim, it executes the shim
42+
// binary with the "delete" action, and shim.RunManager calls Manager.Stop
43+
// directly before task plugins are initialized. Any urunc resource created by
44+
// the shim but needing dead-shim cleanup must therefore be cleaned from this
45+
// path as well as from taskService.Delete.
46+
func (m *manager) Stop(ctx context.Context, id string) (shim.StopStatus, error) {
47+
return m.Manager.Stop(ctx, id)
48+
}

pkg/containerd-shim/task_plugin.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2023-2026, Nubificus LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package containerdshim
16+
17+
import (
18+
"github.com/containerd/containerd/pkg/shutdown"
19+
"github.com/containerd/containerd/plugin"
20+
runcTask "github.com/containerd/containerd/runtime/v2/runc/task"
21+
"github.com/containerd/containerd/runtime/v2/shim"
22+
)
23+
24+
func init() {
25+
plugin.Register(&plugin.Registration{
26+
Type: plugin.TTRPCPlugin,
27+
ID: "task",
28+
Requires: []plugin.Type{
29+
plugin.EventPlugin,
30+
plugin.InternalPlugin,
31+
},
32+
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
33+
pp, err := ic.GetByID(plugin.EventPlugin, "publisher")
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
ss, err := ic.GetByID(plugin.InternalPlugin, "shutdown")
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
inner, err := runcTask.NewTaskService(ic.Context, pp.(shim.Publisher), ss.(shutdown.Service))
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
return &taskService{
49+
TaskService: inner,
50+
containerdAddress: ic.Address,
51+
}, nil
52+
},
53+
})
54+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2023-2026, Nubificus LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package containerdshim
16+
17+
import (
18+
"context"
19+
20+
taskAPI "github.com/containerd/containerd/api/runtime/task/v2"
21+
"github.com/containerd/ttrpc"
22+
)
23+
24+
// taskService is urunc's shim-side wrapper around containerd's runc task
25+
// service. It is intentionally behavior-preserving for now; feature-specific
26+
// helpers can hook into Create/Delete here without replacing the plugin again.
27+
type taskService struct {
28+
taskAPI.TaskService
29+
30+
containerdAddress string
31+
}
32+
33+
var _ taskAPI.TaskService = (*taskService)(nil)
34+
35+
func (s *taskService) Create(ctx context.Context, r *taskAPI.CreateTaskRequest) (*taskAPI.CreateTaskResponse, error) {
36+
return s.TaskService.Create(ctx, r)
37+
}
38+
39+
func (s *taskService) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (*taskAPI.DeleteResponse, error) {
40+
return s.TaskService.Delete(ctx, r)
41+
}
42+
43+
func (s *taskService) RegisterTTRPC(server *ttrpc.Server) error {
44+
taskAPI.RegisterTaskService(server, s)
45+
return nil
46+
}

0 commit comments

Comments
 (0)