Skip to content

Commit 2dacef0

Browse files
committed
Add shim skeleton code
This allows runtime authors to quickly bootstrap new shim implementations. Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
1 parent b94b99d commit 2dacef0

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

runtime/v2/example/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Example Shim
2+
3+
This directory provides skeleton code as the starting point for creating a Runtime v2 shim.
4+
This allows runtime authors to quickly bootstrap new shim implementations.
5+
6+
For full documentation on building a shim for containerd, see the [Shim Documentation](../README.md) file.

runtime/v2/example/cmd/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// +build linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package main
20+
21+
import (
22+
"github.com/containerd/containerd/runtime/v2/example"
23+
"github.com/containerd/containerd/runtime/v2/shim"
24+
)
25+
26+
func main() {
27+
// init and execute the shim
28+
shim.Run("io.containerd.example.v1", example.New)
29+
}

runtime/v2/example/example.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// +build linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package example
20+
21+
import (
22+
"context"
23+
"os"
24+
25+
"github.com/containerd/containerd/errdefs"
26+
"github.com/containerd/containerd/events"
27+
"github.com/containerd/containerd/runtime/v2/shim"
28+
taskAPI "github.com/containerd/containerd/runtime/v2/task"
29+
ptypes "github.com/gogo/protobuf/types"
30+
)
31+
32+
var (
33+
// check to make sure the *service implements the GRPC API
34+
_ = (taskAPI.TaskService)(&service{})
35+
// common response type
36+
empty = &ptypes.Empty{}
37+
)
38+
39+
// New returns a new shim service
40+
func New(ctx context.Context, id string, publisher events.Publisher) (shim.Shim, error) {
41+
return &service{}, nil
42+
}
43+
44+
type service struct {
45+
}
46+
47+
// StartShim is a binary call that executes a new shim returning the address
48+
func (s *service) StartShim(ctx context.Context, id, containerdBinary, containerdAddress string) (string, error) {
49+
return "", nil
50+
}
51+
52+
// Cleanup is a binary call that cleans up any resources used by the shim when the service crashes
53+
func (s *service) Cleanup(ctx context.Context) (*taskAPI.DeleteResponse, error) {
54+
return nil, errdefs.ErrNotImplemented
55+
}
56+
57+
// Create a new container
58+
func (s *service) Create(ctx context.Context, r *taskAPI.CreateTaskRequest) (_ *taskAPI.CreateTaskResponse, err error) {
59+
return nil, errdefs.ErrNotImplemented
60+
}
61+
62+
// Start the primary user process inside the container
63+
func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (*taskAPI.StartResponse, error) {
64+
return nil, errdefs.ErrNotImplemented
65+
}
66+
67+
// Delete a process or container
68+
func (s *service) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (*taskAPI.DeleteResponse, error) {
69+
return nil, errdefs.ErrNotImplemented
70+
}
71+
72+
// Exec an additional process inside the container
73+
func (s *service) Exec(ctx context.Context, r *taskAPI.ExecProcessRequest) (*ptypes.Empty, error) {
74+
return nil, errdefs.ErrNotImplemented
75+
}
76+
77+
// ResizePty of a process
78+
func (s *service) ResizePty(ctx context.Context, r *taskAPI.ResizePtyRequest) (*ptypes.Empty, error) {
79+
return nil, errdefs.ErrNotImplemented
80+
}
81+
82+
// State returns runtime state of a process
83+
func (s *service) State(ctx context.Context, r *taskAPI.StateRequest) (*taskAPI.StateResponse, error) {
84+
return nil, errdefs.ErrNotImplemented
85+
}
86+
87+
// Pause the container
88+
func (s *service) Pause(ctx context.Context, r *taskAPI.PauseRequest) (*ptypes.Empty, error) {
89+
return nil, errdefs.ErrNotImplemented
90+
}
91+
92+
// Resume the container
93+
func (s *service) Resume(ctx context.Context, r *taskAPI.ResumeRequest) (*ptypes.Empty, error) {
94+
return nil, errdefs.ErrNotImplemented
95+
}
96+
97+
// Kill a process
98+
func (s *service) Kill(ctx context.Context, r *taskAPI.KillRequest) (*ptypes.Empty, error) {
99+
return nil, errdefs.ErrNotImplemented
100+
}
101+
102+
// Pids returns all pids inside the container
103+
func (s *service) Pids(ctx context.Context, r *taskAPI.PidsRequest) (*taskAPI.PidsResponse, error) {
104+
return nil, errdefs.ErrNotImplemented
105+
}
106+
107+
// CloseIO of a process
108+
func (s *service) CloseIO(ctx context.Context, r *taskAPI.CloseIORequest) (*ptypes.Empty, error) {
109+
return nil, errdefs.ErrNotImplemented
110+
}
111+
112+
// Checkpoint the container
113+
func (s *service) Checkpoint(ctx context.Context, r *taskAPI.CheckpointTaskRequest) (*ptypes.Empty, error) {
114+
return nil, errdefs.ErrNotImplemented
115+
}
116+
117+
// Connect returns shim information of the underlying service
118+
func (s *service) Connect(ctx context.Context, r *taskAPI.ConnectRequest) (*taskAPI.ConnectResponse, error) {
119+
return nil, errdefs.ErrNotImplemented
120+
}
121+
122+
// Shutdown is called after the underlying resources of the shim are cleaned up and the service can be stopped
123+
func (s *service) Shutdown(ctx context.Context, r *taskAPI.ShutdownRequest) (*ptypes.Empty, error) {
124+
os.Exit(0)
125+
return empty, nil
126+
}
127+
128+
// Stats returns container level system stats for a container and its processes
129+
func (s *service) Stats(ctx context.Context, r *taskAPI.StatsRequest) (*taskAPI.StatsResponse, error) {
130+
return nil, errdefs.ErrNotImplemented
131+
}
132+
133+
// Update the live container
134+
func (s *service) Update(ctx context.Context, r *taskAPI.UpdateTaskRequest) (*ptypes.Empty, error) {
135+
return nil, errdefs.ErrNotImplemented
136+
}
137+
138+
// Wait for a process to exit
139+
func (s *service) Wait(ctx context.Context, r *taskAPI.WaitRequest) (*taskAPI.WaitResponse, error) {
140+
return nil, errdefs.ErrNotImplemented
141+
}

0 commit comments

Comments
 (0)