Use event service post for shim events
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// +build linux
|
||||
// +build !windows
|
||||
|
||||
package shim
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
@@ -28,7 +27,7 @@ import (
|
||||
type ClientOpt func(context.Context, Config) (shim.ShimClient, io.Closer, error)
|
||||
|
||||
// WithStart executes a new shim process
|
||||
func WithStart(binary string) ClientOpt {
|
||||
func WithStart(binary, address string) ClientOpt {
|
||||
return func(ctx context.Context, config Config) (shim.ShimClient, io.Closer, error) {
|
||||
socket, err := newSocket(config)
|
||||
if err != nil {
|
||||
@@ -41,7 +40,7 @@ func WithStart(binary string) ClientOpt {
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
cmd := newCommand(binary, config, f)
|
||||
cmd := newCommand(binary, address, config, f)
|
||||
if err := reaper.Default.Start(cmd); err != nil {
|
||||
return nil, nil, errors.Wrapf(err, "failed to start shim")
|
||||
}
|
||||
@@ -56,9 +55,10 @@ func WithStart(binary string) ClientOpt {
|
||||
}
|
||||
}
|
||||
|
||||
func newCommand(binary string, config Config, socket *os.File) *exec.Cmd {
|
||||
func newCommand(binary, address string, config Config, socket *os.File) *exec.Cmd {
|
||||
args := []string{
|
||||
"--namespace", config.Namespace,
|
||||
"--address", address,
|
||||
}
|
||||
if config.Debug {
|
||||
args = append(args, "--debug")
|
||||
@@ -68,11 +68,12 @@ func newCommand(binary string, config Config, socket *os.File) *exec.Cmd {
|
||||
// make sure the shim can be re-parented to system init
|
||||
// and is cloned in a new mount namespace because the overlay/filesystems
|
||||
// will be mounted by the shim
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
Cloneflags: syscall.CLONE_NEWNS,
|
||||
Setpgid: true,
|
||||
}
|
||||
cmd.SysProcAttr = &atter
|
||||
cmd.ExtraFiles = append(cmd.ExtraFiles, socket)
|
||||
if config.Debug {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -88,12 +89,12 @@ func newSocket(config Config) (*net.UnixListener, error) {
|
||||
return l.(*net.UnixListener), nil
|
||||
}
|
||||
|
||||
func connect(address string) (*grpc.ClientConn, error) {
|
||||
func connect(address string, d func(string, time.Duration) (net.Conn, error)) (*grpc.ClientConn, error) {
|
||||
gopts := []grpc.DialOption{
|
||||
grpc.WithBlock(),
|
||||
grpc.WithInsecure(),
|
||||
grpc.WithTimeout(100 * time.Second),
|
||||
grpc.WithDialer(dialer),
|
||||
grpc.WithDialer(d),
|
||||
grpc.FailOnNonTempDialError(true),
|
||||
}
|
||||
conn, err := grpc.Dial(dialAddress(address), gopts...)
|
||||
@@ -104,6 +105,11 @@ func connect(address string) (*grpc.ClientConn, error) {
|
||||
}
|
||||
|
||||
func dialer(address string, timeout time.Duration) (net.Conn, error) {
|
||||
address = strings.TrimPrefix(address, "unix://")
|
||||
return net.DialTimeout("unix", address, timeout)
|
||||
}
|
||||
|
||||
func annonDialer(address string, timeout time.Duration) (net.Conn, error) {
|
||||
address = strings.TrimPrefix(address, "unix://")
|
||||
return net.DialTimeout("unix", "\x00"+address, timeout)
|
||||
}
|
||||
@@ -114,7 +120,7 @@ func dialAddress(address string) string {
|
||||
|
||||
// WithConnect connects to an existing shim
|
||||
func WithConnect(ctx context.Context, config Config) (shim.ShimClient, io.Closer, error) {
|
||||
conn, err := connect(config.Address)
|
||||
conn, err := connect(config.Address, annonDialer)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -123,7 +129,7 @@ func WithConnect(ctx context.Context, config Config) (shim.ShimClient, io.Closer
|
||||
|
||||
// WithLocal uses an in process shim
|
||||
func WithLocal(ctx context.Context, config Config) (shim.ShimClient, io.Closer, error) {
|
||||
service, err := NewService(config.Path, config.Namespace)
|
||||
service, err := NewService(config.Path, config.Namespace, "")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
10
linux/shim/client_linux.go
Normal file
10
linux/shim/client_linux.go
Normal file
@@ -0,0 +1,10 @@
|
||||
// +build linux
|
||||
|
||||
package shim
|
||||
|
||||
import "syscall"
|
||||
|
||||
var atter = syscall.SysProcAttr{
|
||||
Cloneflags: syscall.CLONE_NEWNS,
|
||||
Setpgid: true,
|
||||
}
|
||||
9
linux/shim/client_unix.go
Normal file
9
linux/shim/client_unix.go
Normal file
@@ -0,0 +1,9 @@
|
||||
// +build !linux,!windows
|
||||
|
||||
package shim
|
||||
|
||||
import "syscall"
|
||||
|
||||
var atter = syscall.SysProcAttr{
|
||||
Setpgid: true,
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
events "github.com/containerd/containerd/api/services/events/v1"
|
||||
evt "github.com/containerd/containerd/events"
|
||||
shimapi "github.com/containerd/containerd/linux/shim/v1"
|
||||
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
||||
"golang.org/x/net/context"
|
||||
@@ -129,3 +130,19 @@ func (e *streamEvents) SendMsg(m interface{}) error {
|
||||
func (e *streamEvents) RecvMsg(m interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type poster interface {
|
||||
Post(ctx context.Context, in *events.PostEventRequest, opts ...grpc.CallOption) (*google_protobuf.Empty, error)
|
||||
}
|
||||
|
||||
type localEventsClient struct {
|
||||
emitter evt.Poster
|
||||
}
|
||||
|
||||
func (l *localEventsClient) Post(ctx context.Context, r *events.PostEventRequest, opts ...grpc.CallOption) (*google_protobuf.Empty, error) {
|
||||
ctx = evt.WithTopic(ctx, r.Envelope.Topic)
|
||||
if err := l.emitter.Post(ctx, r.Envelope); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return empty, nil
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
@@ -13,8 +14,12 @@ import (
|
||||
"github.com/containerd/console"
|
||||
events "github.com/containerd/containerd/api/services/events/v1"
|
||||
"github.com/containerd/containerd/api/types/task"
|
||||
evt "github.com/containerd/containerd/events"
|
||||
shimapi "github.com/containerd/containerd/linux/shim/v1"
|
||||
"github.com/containerd/containerd/log"
|
||||
"github.com/containerd/containerd/namespaces"
|
||||
"github.com/containerd/containerd/reaper"
|
||||
"github.com/containerd/containerd/typeurl"
|
||||
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/context"
|
||||
@@ -29,16 +34,32 @@ var empty = &google_protobuf.Empty{}
|
||||
const RuncRoot = "/run/containerd/runc"
|
||||
|
||||
// NewService returns a new shim service that can be used via GRPC
|
||||
func NewService(path, namespace string) (*Service, error) {
|
||||
func NewService(path, namespace, address string) (*Service, error) {
|
||||
if namespace == "" {
|
||||
return nil, fmt.Errorf("shim namespace cannot be empty")
|
||||
}
|
||||
return &Service{
|
||||
context := namespaces.WithNamespace(context.Background(), namespace)
|
||||
var client poster
|
||||
if address != "" {
|
||||
conn, err := connect(address, dialer)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to dial %q", address)
|
||||
}
|
||||
client = events.NewEventsClient(conn)
|
||||
} else {
|
||||
client = &localEventsClient{
|
||||
emitter: evt.GetPoster(context),
|
||||
}
|
||||
}
|
||||
s := &Service{
|
||||
path: path,
|
||||
processes: make(map[string]process),
|
||||
events: make(chan *events.RuntimeEvent, 4096),
|
||||
namespace: namespace,
|
||||
}, nil
|
||||
context: context,
|
||||
}
|
||||
go s.forward(client)
|
||||
return s, nil
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
@@ -52,6 +73,7 @@ type Service struct {
|
||||
eventsMu sync.Mutex
|
||||
deferredEvent *events.RuntimeEvent
|
||||
namespace string
|
||||
context context.Context
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, r *shimapi.CreateTaskRequest) (*shimapi.CreateTaskResponse, error) {
|
||||
@@ -367,3 +389,38 @@ func (s *Service) getContainerPids(ctx context.Context, id string) ([]uint32, er
|
||||
}
|
||||
return pids, nil
|
||||
}
|
||||
|
||||
func (s *Service) forward(client poster) {
|
||||
for e := range s.events {
|
||||
a, err := typeurl.MarshalAny(e)
|
||||
if err != nil {
|
||||
log.G(s.context).WithError(err).Error("marshal event")
|
||||
continue
|
||||
}
|
||||
if _, err := client.Post(s.context, &events.PostEventRequest{
|
||||
Envelope: &events.Envelope{
|
||||
Timestamp: time.Now(),
|
||||
Topic: "/runtime/" + getTopic(e),
|
||||
Event: a,
|
||||
},
|
||||
}); err != nil {
|
||||
log.G(s.context).WithError(err).Error("post event")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getTopic(e *events.RuntimeEvent) string {
|
||||
switch e.Type {
|
||||
case events.RuntimeEvent_CREATE:
|
||||
return "task-create"
|
||||
case events.RuntimeEvent_START:
|
||||
return "task-start"
|
||||
case events.RuntimeEvent_EXEC_ADDED:
|
||||
return "task-execadded"
|
||||
case events.RuntimeEvent_OOM:
|
||||
return "task-oom"
|
||||
case events.RuntimeEvent_EXIT:
|
||||
return "task-exit"
|
||||
}
|
||||
return "?"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user