Files
kata-containers/src/runtime/pkg/containerd-shim-v2/shim_io_pipe.go
Bin Liu 4e30e11b31 shim: support shim v2 logging plugin
Now kata shim only supports stdout/stderr of fifo from
containerd/CRI-O, but shim v2 supports logging plugins,
and nerdctl default will use the binary schema for logs.

This commit will add the others type of log plugins:

- file
- binary

In case of binary, kata shim will receive a stdout/stderr like:

binary:///nerdctl?_NERDCTL_INTERNAL_LOGGING=/var/lib/nerdctl/1935db59

That means the nerdctl process will handle the logs(stdout/stderr)

Fixes: #4420

Signed-off-by: Bin Liu <bin@hyper.sh>
2022-06-28 13:54:22 +08:00

96 lines
1.7 KiB
Go

// Copyright (c) 2022 Ant Group
//
// SPDX-License-Identifier: Apache-2.0
//
package containerdshim
import (
"context"
"fmt"
"io"
"syscall"
"github.com/containerd/fifo"
"github.com/hashicorp/go-multierror"
)
var (
_ IO = &pipeIO{}
)
type pipeIO struct {
in io.ReadCloser
outw io.WriteCloser
errw io.WriteCloser
}
func newPipeIO(ctx context.Context, stdio *stdio) (*pipeIO, error) {
var in io.ReadCloser
var outw io.WriteCloser
var errw io.WriteCloser
var err error
if stdio.Stdin != "" {
in, err = fifo.OpenFifo(ctx, stdio.Stdin, syscall.O_RDONLY|syscall.O_NONBLOCK, 0)
if err != nil {
return nil, err
}
}
if stdio.Stdout != "" {
outw, err = fifo.OpenFifo(ctx, stdio.Stdout, syscall.O_RDWR, 0)
if err != nil {
return nil, err
}
}
if !stdio.Console && stdio.Stderr != "" {
errw, err = fifo.OpenFifo(ctx, stdio.Stderr, syscall.O_RDWR, 0)
if err != nil {
return nil, err
}
}
pipeIO := &pipeIO{
in: in,
outw: outw,
errw: errw,
}
return pipeIO, nil
}
func (pi *pipeIO) Stdin() io.ReadCloser {
return pi.in
}
func (pi *pipeIO) Stdout() io.Writer {
return pi.outw
}
func (pi *pipeIO) Stderr() io.Writer {
return pi.errw
}
func (pi *pipeIO) Close() error {
var result *multierror.Error
if pi.in != nil {
if err := pi.in.Close(); err != nil {
result = multierror.Append(result, fmt.Errorf("failed to close stdin: %w", err))
}
pi.in = nil
}
if err := wc(pi.outw); err != nil {
result = multierror.Append(result, fmt.Errorf("failed to close stdout: %w", err))
}
if err := wc(pi.errw); err != nil {
result = multierror.Append(result, fmt.Errorf("failed to close stderr: %w", err))
}
return result.ErrorOrNil()
}