update for containerd vendor changes

Signed-off-by: Mike Brown <brownwm@us.ibm.com>
This commit is contained in:
Mike Brown
2020-01-06 19:58:04 -06:00
parent 55566f9682
commit 2da1ced9a1
42 changed files with 7883 additions and 602 deletions

View File

@@ -24,10 +24,17 @@ import (
var ErrNotAConsole = errors.New("provided file is not a console")
type File interface {
io.ReadWriteCloser
// Fd returns its file descriptor
Fd() uintptr
// Name returns its file name
Name() string
}
type Console interface {
io.Reader
io.Writer
io.Closer
File
// Resize resizes the console to the provided window size
Resize(WinSize) error
@@ -42,10 +49,6 @@ type Console interface {
Reset() error
// Size returns the window size of the console
Size() (WinSize, error)
// Fd returns the console's file descriptor
Fd() uintptr
// Name returns the console's file name
Name() string
}
// WinSize specifies the window size of the console
@@ -70,7 +73,7 @@ func Current() Console {
}
// ConsoleFromFile returns a console using the provided file
func ConsoleFromFile(f *os.File) (Console, error) {
func ConsoleFromFile(f File) (Console, error) {
if err := checkConsole(f); err != nil {
return nil, err
}

View File

@@ -58,6 +58,7 @@ type Epoller struct {
efd int
mu sync.Mutex
fdMapping map[int]*EpollConsole
closeOnce sync.Once
}
// NewEpoller returns an instance of epoller with a valid epoll fd.
@@ -151,7 +152,11 @@ func (e *Epoller) getConsole(sysfd int) *EpollConsole {
// Close closes the epoll fd
func (e *Epoller) Close() error {
return unix.Close(e.efd)
closeErr := os.ErrClosed // default to "file already closed"
e.closeOnce.Do(func() {
closeErr = unix.Close(e.efd)
})
return closeErr
}
// EpollConsole acts like a console but registers its file descriptor with an

View File

@@ -47,7 +47,7 @@ func NewPty() (Console, string, error) {
}
type master struct {
f *os.File
f File
original *unix.Termios
}
@@ -122,7 +122,7 @@ func (m *master) Name() string {
}
// checkConsole checks if the provided file is a console
func checkConsole(f *os.File) error {
func checkConsole(f File) error {
var termios unix.Termios
if tcget(f.Fd(), &termios) != nil {
return ErrNotAConsole
@@ -130,7 +130,7 @@ func checkConsole(f *os.File) error {
return nil
}
func newMaster(f *os.File) (Console, error) {
func newMaster(f File) (Console, error) {
m := &master{
f: f,
}

View File

@@ -198,7 +198,7 @@ func makeInputRaw(fd windows.Handle, mode uint32) error {
return nil
}
func checkConsole(f *os.File) error {
func checkConsole(f File) error {
var mode uint32
if err := windows.GetConsoleMode(windows.Handle(f.Fd()), &mode); err != nil {
return err
@@ -206,7 +206,7 @@ func checkConsole(f *os.File) error {
return nil
}
func newMaster(f *os.File) (Console, error) {
func newMaster(f File) (Console, error) {
if f != os.Stdin && f != os.Stdout && f != os.Stderr {
return nil, errors.New("creating a console from a file is not supported on windows")
}

8
vendor/github.com/containerd/console/go.mod generated vendored Normal file
View File

@@ -0,0 +1,8 @@
module github.com/containerd/console
go 1.13
require (
github.com/pkg/errors v0.8.1
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e
)

View File

@@ -26,6 +26,7 @@ import (
"github.com/containerd/containerd/events/exchange"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/runtime"
client "github.com/containerd/containerd/runtime/v2/shim"
"github.com/containerd/containerd/runtime/v2/task"
@@ -74,7 +75,9 @@ func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_
if err != nil {
return nil, err
}
f, err := openShimLog(context.Background(), b.bundle, client.AnonDialer)
// Windows needs a namespace when openShimLog
ns, _ := namespaces.Namespace(ctx)
f, err := openShimLog(namespaces.WithNamespace(context.Background(), ns), b.bundle, client.AnonDialer)
if err != nil {
return nil, errors.Wrap(err, "open shim log pipe")
}

View File

@@ -20,6 +20,7 @@ import (
"context"
"os"
"os/exec"
"strings"
"syscall"
)
@@ -32,10 +33,24 @@ func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: r.Setpgid,
}
cmd.Env = os.Environ()
cmd.Env = filterEnv(os.Environ(), "NOTIFY_SOCKET") // NOTIFY_SOCKET introduces a special behavior in runc but should only be set if invoked from systemd
if r.PdeathSignal != 0 {
cmd.SysProcAttr.Pdeathsig = r.PdeathSignal
}
return cmd
}
func filterEnv(in []string, names ...string) []string {
out := make([]string, 0, len(in))
loop0:
for _, v := range in {
for _, k := range names {
if strings.HasPrefix(v, k+"=") {
continue loop0
}
}
out = append(out, v)
}
return out
}

View File

@@ -17,6 +17,7 @@
package runc
import (
"bytes"
"context"
"encoding/json"
"errors"
@@ -72,11 +73,12 @@ type Runc struct {
// List returns all containers created inside the provided runc root directory
func (r *Runc) List(context context.Context) ([]*Container, error) {
data, err := cmdOutput(r.command(context, "list", "--format=json"), false)
defer putBuf(data)
if err != nil {
return nil, err
}
var out []*Container
if err := json.Unmarshal(data, &out); err != nil {
if err := json.Unmarshal(data.Bytes(), &out); err != nil {
return nil, err
}
return out, nil
@@ -85,11 +87,12 @@ func (r *Runc) List(context context.Context) ([]*Container, error) {
// State returns the state for the container provided by id
func (r *Runc) State(context context.Context, id string) (*Container, error) {
data, err := cmdOutput(r.command(context, "state", id), true)
defer putBuf(data)
if err != nil {
return nil, fmt.Errorf("%s: %s", err, data)
return nil, fmt.Errorf("%s: %s", err, data.String())
}
var c Container
if err := json.Unmarshal(data, &c); err != nil {
if err := json.Unmarshal(data.Bytes(), &c); err != nil {
return nil, err
}
return &c, nil
@@ -154,8 +157,9 @@ func (r *Runc) Create(context context.Context, id, bundle string, opts *CreateOp
if cmd.Stdout == nil && cmd.Stderr == nil {
data, err := cmdOutput(cmd, true)
defer putBuf(data)
if err != nil {
return fmt.Errorf("%s: %s", err, data)
return fmt.Errorf("%s: %s", err, data.String())
}
return nil
}
@@ -172,7 +176,7 @@ func (r *Runc) Create(context context.Context, id, bundle string, opts *CreateOp
}
status, err := Monitor.Wait(cmd, ec)
if err == nil && status != 0 {
err = fmt.Errorf("%s did not terminate sucessfully", cmd.Args[0])
err = fmt.Errorf("%s did not terminate successfully", cmd.Args[0])
}
return err
}
@@ -233,8 +237,9 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
}
if cmd.Stdout == nil && cmd.Stderr == nil {
data, err := cmdOutput(cmd, true)
defer putBuf(data)
if err != nil {
return fmt.Errorf("%s: %s", err, data)
return fmt.Errorf("%s: %s", err, data.String())
}
return nil
}
@@ -251,7 +256,7 @@ func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts
}
status, err := Monitor.Wait(cmd, ec)
if err == nil && status != 0 {
err = fmt.Errorf("%s did not terminate sucessfully", cmd.Args[0])
err = fmt.Errorf("%s did not terminate successfully", cmd.Args[0])
}
return err
}
@@ -277,7 +282,7 @@ func (r *Runc) Run(context context.Context, id, bundle string, opts *CreateOpts)
}
status, err := Monitor.Wait(cmd, ec)
if err == nil && status != 0 {
err = fmt.Errorf("%s did not terminate sucessfully", cmd.Args[0])
err = fmt.Errorf("%s did not terminate successfully", cmd.Args[0])
}
return status, err
}
@@ -399,11 +404,12 @@ func (r *Runc) Resume(context context.Context, id string) error {
// Ps lists all the processes inside the container returning their pids
func (r *Runc) Ps(context context.Context, id string) ([]int, error) {
data, err := cmdOutput(r.command(context, "ps", "--format", "json", id), true)
defer putBuf(data)
if err != nil {
return nil, fmt.Errorf("%s: %s", err, data)
return nil, fmt.Errorf("%s: %s", err, data.String())
}
var pids []int
if err := json.Unmarshal(data, &pids); err != nil {
if err := json.Unmarshal(data.Bytes(), &pids); err != nil {
return nil, err
}
return pids, nil
@@ -412,11 +418,12 @@ func (r *Runc) Ps(context context.Context, id string) ([]int, error) {
// Top lists all the processes inside the container returning the full ps data
func (r *Runc) Top(context context.Context, id string, psOptions string) (*TopResults, error) {
data, err := cmdOutput(r.command(context, "ps", "--format", "table", id, psOptions), true)
defer putBuf(data)
if err != nil {
return nil, fmt.Errorf("%s: %s", err, data)
return nil, fmt.Errorf("%s: %s", err, data.String())
}
topResults, err := ParsePSOutput(data)
topResults, err := ParsePSOutput(data.Bytes())
if err != nil {
return nil, fmt.Errorf("%s: ", err)
}
@@ -576,7 +583,7 @@ func (r *Runc) Restore(context context.Context, id, bundle string, opts *Restore
}
status, err := Monitor.Wait(cmd, ec)
if err == nil && status != 0 {
err = fmt.Errorf("%s did not terminate sucessfully", cmd.Args[0])
err = fmt.Errorf("%s did not terminate successfully", cmd.Args[0])
}
return status, err
}
@@ -606,10 +613,11 @@ type Version struct {
// Version returns the runc and runtime-spec versions
func (r *Runc) Version(context context.Context) (Version, error) {
data, err := cmdOutput(r.command(context, "--version"), false)
defer putBuf(data)
if err != nil {
return Version{}, err
}
return parseVersion(data)
return parseVersion(data.Bytes())
}
func parseVersion(data []byte) (Version, error) {
@@ -682,20 +690,22 @@ func (r *Runc) runOrError(cmd *exec.Cmd) error {
}
status, err := Monitor.Wait(cmd, ec)
if err == nil && status != 0 {
err = fmt.Errorf("%s did not terminate sucessfully", cmd.Args[0])
err = fmt.Errorf("%s did not terminate successfully", cmd.Args[0])
}
return err
}
data, err := cmdOutput(cmd, true)
defer putBuf(data)
if err != nil {
return fmt.Errorf("%s: %s", err, data)
return fmt.Errorf("%s: %s", err, data.String())
}
return nil
}
func cmdOutput(cmd *exec.Cmd, combined bool) ([]byte, error) {
// callers of cmdOutput are expected to call putBuf on the returned Buffer
// to ensure it is released back to the shared pool after use.
func cmdOutput(cmd *exec.Cmd, combined bool) (*bytes.Buffer, error) {
b := getBuf()
defer putBuf(b)
cmd.Stdout = b
if combined {
@@ -708,8 +718,8 @@ func cmdOutput(cmd *exec.Cmd, combined bool) ([]byte, error) {
status, err := Monitor.Wait(cmd, ec)
if err == nil && status != 0 {
err = fmt.Errorf("%s did not terminate sucessfully", cmd.Args[0])
err = fmt.Errorf("%s did not terminate successfully", cmd.Args[0])
}
return b.Bytes(), err
return b, err
}

View File

@@ -57,6 +57,10 @@ func getBuf() *bytes.Buffer {
}
func putBuf(b *bytes.Buffer) {
if b == nil {
return
}
b.Reset()
bytesBufferPool.Put(b)
}