Skip to content

Commit 362405f

Browse files
authored
Merge pull request #2495 from jterry75/runtime_v2_windows
Adds runtime v2 support for Windows shim's
2 parents c8017d0 + af1b6a0 commit 362405f

12 files changed

+259
-147
lines changed

Makefile.windows

+7
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ BINARY_SUFFIX=".exe"
2323
ifeq ($(GOARCH),amd64)
2424
TESTFLAGS_RACE= -race
2525
endif
26+
27+
# add support for building the Windows v2 runtime
28+
# based on the containerd-shim-runhcs-v1 shim rather
29+
# than the existing runtime on hcsshim
30+
ifeq (${BUILD_WINDOWS_V2},1)
31+
BUILDTAGS += windows_v2
32+
endif

cmd/containerd/builtins_windows.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !windows_v2
2+
13
/*
24
Copyright The containerd Authors.
35
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build windows_v2
2+
13
/*
24
Copyright The containerd Authors.
35
@@ -14,21 +16,10 @@
1416
limitations under the License.
1517
*/
1618

17-
package shim
19+
package main
1820

1921
import (
20-
"syscall"
21-
22-
"github.com/containerd/containerd/sys"
22+
_ "github.com/containerd/containerd/diff/windows"
23+
_ "github.com/containerd/containerd/runtime/v2"
24+
_ "github.com/containerd/containerd/snapshots/windows"
2325
)
24-
25-
func getSysProcAttr() *syscall.SysProcAttr {
26-
return &syscall.SysProcAttr{
27-
Setpgid: true,
28-
}
29-
}
30-
31-
// SetScore sets the oom score for a process
32-
func SetScore(pid int) error {
33-
return sys.SetOOMScore(pid, sys.OOMScoreMaxKillable)
34-
}

runtime/v2/runc/service.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (s *service) StartShim(ctx context.Context, id, containerdBinary, container
136136
if err != nil {
137137
return "", err
138138
}
139-
address, err := shim.AbstractAddress(ctx, id)
139+
address, err := shim.SocketAddress(ctx, id)
140140
if err != nil {
141141
return "", err
142142
}

runtime/v2/shim/shim.go

+2-66
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build !windows
2-
31
/*
42
Copyright The containerd Authors.
53
@@ -19,29 +17,22 @@
1917
package shim
2018

2119
import (
22-
"bytes"
2320
"context"
2421
"flag"
2522
"fmt"
26-
"net"
2723
"os"
28-
"os/exec"
29-
"os/signal"
3024
"runtime"
3125
"runtime/debug"
3226
"strings"
33-
"syscall"
3427
"time"
3528

3629
"github.com/containerd/containerd/events"
3730
"github.com/containerd/containerd/namespaces"
3831
shimapi "github.com/containerd/containerd/runtime/v2/task"
3932
"github.com/containerd/ttrpc"
40-
"github.com/containerd/typeurl"
4133
"github.com/gogo/protobuf/proto"
4234
"github.com/pkg/errors"
4335
"github.com/sirupsen/logrus"
44-
"golang.org/x/sys/unix"
4536
)
4637

4738
// Client for a shim server
@@ -178,7 +169,7 @@ func NewShimClient(ctx context.Context, svc shimapi.TaskService, signals chan os
178169
// Serve the shim server
179170
func (s *Client) Serve() error {
180171
dump := make(chan os.Signal, 32)
181-
signal.Notify(dump, syscall.SIGUSR1)
172+
setupDumpStacks(dump)
182173

183174
path, err := os.Getwd()
184175
if err != nil {
@@ -211,23 +202,10 @@ func (s *Client) Serve() error {
211202
// serve serves the ttrpc API over a unix socket at the provided path
212203
// this function does not block
213204
func serve(ctx context.Context, server *ttrpc.Server, path string) error {
214-
var (
215-
l net.Listener
216-
err error
217-
)
218-
if path == "" {
219-
l, err = net.FileListener(os.NewFile(3, "socket"))
220-
path = "[inherited from parent]"
221-
} else {
222-
if len(path) > 106 {
223-
return errors.Errorf("%q: unix socket path too long (> 106)", path)
224-
}
225-
l, err = net.Listen("unix", "\x00"+path)
226-
}
205+
l, err := serveListener(path)
227206
if err != nil {
228207
return err
229208
}
230-
logrus.WithField("socket", path).Debug("serving api on unix socket")
231209
go func() {
232210
defer l.Close()
233211
if err := server.Serve(ctx, l); err != nil &&
@@ -238,22 +216,6 @@ func serve(ctx context.Context, server *ttrpc.Server, path string) error {
238216
return nil
239217
}
240218

241-
func handleSignals(logger *logrus.Entry, signals chan os.Signal) error {
242-
logger.Info("starting signal loop")
243-
for {
244-
select {
245-
case s := <-signals:
246-
switch s {
247-
case unix.SIGCHLD:
248-
if err := Reap(); err != nil {
249-
logger.WithError(err).Error("reap exit status")
250-
}
251-
case unix.SIGPIPE:
252-
}
253-
}
254-
}
255-
}
256-
257219
func dumpStacks(logger *logrus.Entry) {
258220
var (
259221
buf []byte
@@ -273,29 +235,3 @@ type remoteEventsPublisher struct {
273235
address string
274236
containerdBinaryPath string
275237
}
276-
277-
func (l *remoteEventsPublisher) Publish(ctx context.Context, topic string, event events.Event) error {
278-
ns, _ := namespaces.Namespace(ctx)
279-
encoded, err := typeurl.MarshalAny(event)
280-
if err != nil {
281-
return err
282-
}
283-
data, err := encoded.Marshal()
284-
if err != nil {
285-
return err
286-
}
287-
cmd := exec.CommandContext(ctx, l.containerdBinaryPath, "--address", l.address, "publish", "--topic", topic, "--namespace", ns)
288-
cmd.Stdin = bytes.NewReader(data)
289-
c, err := Default.Start(cmd)
290-
if err != nil {
291-
return err
292-
}
293-
status, err := Default.Wait(cmd, c)
294-
if err != nil {
295-
return err
296-
}
297-
if status != 0 {
298-
return errors.New("failed to publish event")
299-
}
300-
return nil
301-
}

runtime/v2/shim/shim_darwin.go

+1-17
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,9 @@
1818

1919
package shim
2020

21-
import (
22-
"os"
23-
"os/signal"
24-
25-
"github.com/containerd/ttrpc"
26-
)
27-
28-
// setupSignals creates a new signal handler for all signals and sets the shim as a
29-
// sub-reaper so that the container processes are reparented
30-
func setupSignals() (chan os.Signal, error) {
31-
signals := make(chan os.Signal, 2048)
32-
signal.Notify(signals)
33-
return signals, nil
34-
}
21+
import "github.com/containerd/ttrpc"
3522

3623
func newServer() (*ttrpc.Server, error) {
37-
// for darwin, we omit the socket credentials because these syscalls are
38-
// slightly different. since we don't have darwin support yet, this can be
39-
// implemented later and the build can continue without issue.
4024
return ttrpc.NewServer()
4125
}
4226

runtime/v2/shim/shim_linux.go

-12
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,10 @@
1717
package shim
1818

1919
import (
20-
"os"
21-
"os/signal"
22-
2320
"github.com/containerd/containerd/sys"
2421
"github.com/containerd/ttrpc"
25-
"golang.org/x/sys/unix"
2622
)
2723

28-
// setupSignals creates a new signal handler for all signals and sets the shim as a
29-
// sub-reaper so that the container processes are reparented
30-
func setupSignals() (chan os.Signal, error) {
31-
signals := make(chan os.Signal, 32)
32-
signal.Notify(signals, unix.SIGTERM, unix.SIGINT, unix.SIGCHLD, unix.SIGPIPE)
33-
return signals, nil
34-
}
35-
3624
func newServer() (*ttrpc.Server, error) {
3725
return ttrpc.NewServer(ttrpc.WithServerHandshaker(ttrpc.UnixSocketRequireSameUser()))
3826
}

runtime/v2/shim/shim_unix.go

+76-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !linux,!windows,!darwin
1+
// +build !windows
22

33
/*
44
Copyright The containerd Authors.
@@ -19,24 +19,93 @@
1919
package shim
2020

2121
import (
22+
"bytes"
23+
"context"
24+
"net"
2225
"os"
26+
"os/exec"
2327
"os/signal"
28+
"syscall"
2429

25-
"github.com/containerd/ttrpc"
30+
"github.com/containerd/containerd/events"
31+
"github.com/containerd/containerd/namespaces"
32+
"github.com/containerd/typeurl"
33+
"github.com/pkg/errors"
34+
"github.com/sirupsen/logrus"
35+
"golang.org/x/sys/unix"
2636
)
2737

2838
// setupSignals creates a new signal handler for all signals and sets the shim as a
2939
// sub-reaper so that the container processes are reparented
3040
func setupSignals() (chan os.Signal, error) {
31-
signals := make(chan os.Signal, 2048)
32-
signal.Notify(signals)
41+
signals := make(chan os.Signal, 32)
42+
signal.Notify(signals, unix.SIGTERM, unix.SIGINT, unix.SIGCHLD, unix.SIGPIPE)
3343
return signals, nil
3444
}
3545

36-
func newServer() (*ttrpc.Server, error) {
37-
return ttrpc.NewServer(ttrpc.WithServerHandshaker(ttrpc.UnixSocketRequireSameUser()))
46+
func setupDumpStacks(dump chan<- os.Signal) {
47+
signal.Notify(dump, syscall.SIGUSR1)
3848
}
3949

40-
func subreaper() error {
50+
func serveListener(path string) (net.Listener, error) {
51+
var (
52+
l net.Listener
53+
err error
54+
)
55+
if path == "" {
56+
l, err = net.FileListener(os.NewFile(3, "socket"))
57+
path = "[inherited from parent]"
58+
} else {
59+
if len(path) > 106 {
60+
return nil, errors.Errorf("%q: unix socket path too long (> 106)", path)
61+
}
62+
l, err = net.Listen("unix", "\x00"+path)
63+
}
64+
if err != nil {
65+
return nil, err
66+
}
67+
logrus.WithField("socket", path).Debug("serving api on abstract socket")
68+
return l, nil
69+
}
70+
71+
func handleSignals(logger *logrus.Entry, signals chan os.Signal) error {
72+
logger.Info("starting signal loop")
73+
for {
74+
select {
75+
case s := <-signals:
76+
switch s {
77+
case unix.SIGCHLD:
78+
if err := Reap(); err != nil {
79+
logger.WithError(err).Error("reap exit status")
80+
}
81+
case unix.SIGPIPE:
82+
}
83+
}
84+
}
85+
}
86+
87+
func (l *remoteEventsPublisher) Publish(ctx context.Context, topic string, event events.Event) error {
88+
ns, _ := namespaces.Namespace(ctx)
89+
encoded, err := typeurl.MarshalAny(event)
90+
if err != nil {
91+
return err
92+
}
93+
data, err := encoded.Marshal()
94+
if err != nil {
95+
return err
96+
}
97+
cmd := exec.CommandContext(ctx, l.containerdBinaryPath, "--address", l.address, "publish", "--topic", topic, "--namespace", ns)
98+
cmd.Stdin = bytes.NewReader(data)
99+
c, err := Default.Start(cmd)
100+
if err != nil {
101+
return err
102+
}
103+
status, err := Default.Wait(cmd, c)
104+
if err != nil {
105+
return err
106+
}
107+
if status != 0 {
108+
return errors.New("failed to publish event")
109+
}
41110
return nil
42111
}

0 commit comments

Comments
 (0)