libpod: use io.Writer vs io.WriteCloser for attach streams

We never ever close the stream so we do not need the Close() function in
th ebackend, the caller should close when required which may no be the
case, i.e. when os.Stdout/err is used.
This should not be a breaking change as the io.Writer is a subset of
io.WriteCloser, therfore all code should still compile while allowing to
pass in Writers without Close().

This is useful for podman top where we exec ps in the container via
podman exec.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2023-07-10 10:52:44 +02:00
committed by Ashley Cui
parent 574b78291c
commit 598ebe8a1b
4 changed files with 14 additions and 29 deletions

View File

@ -355,16 +355,10 @@ func (c *Container) execPSinContainer(args []string) ([]string, error) {
defer wPipe.Close()
defer rPipe.Close()
rErrPipe, wErrPipe, err := os.Pipe()
if err != nil {
return nil, err
}
defer wErrPipe.Close()
defer rErrPipe.Close()
var errBuf bytes.Buffer
streams := new(define.AttachStreams)
streams.OutputStream = wPipe
streams.ErrorStream = wErrPipe
streams.ErrorStream = &errBuf
streams.AttachOutput = true
streams.AttachError = true
@ -375,13 +369,6 @@ func (c *Container) execPSinContainer(args []string) ([]string, error) {
stdout = append(stdout, scanner.Text())
}
}()
stderr := []string{}
go func() {
scanner := bufio.NewScanner(rErrPipe)
for scanner.Scan() {
stderr = append(stderr, scanner.Text())
}
}()
cmd := append([]string{"ps"}, args...)
config := new(ExecConfig)
@ -390,15 +377,13 @@ func (c *Container) execPSinContainer(args []string) ([]string, error) {
if err != nil {
return nil, err
} else if ec != 0 {
return nil, fmt.Errorf("runtime failed with exit status: %d and output: %s", ec, strings.Join(stderr, " "))
return nil, fmt.Errorf("runtime failed with exit status: %d and output: %s", ec, errBuf.String())
}
if logrus.GetLevel() >= logrus.DebugLevel {
// If we're running in debug mode or higher, we might want to have a
// look at stderr which includes debug logs from conmon.
for _, log := range stderr {
logrus.Debugf("%s", log)
}
logrus.Debugf(errBuf.String())
}
return stdout, nil

View File

@ -51,9 +51,9 @@ const (
// AttachStreams contains streams that will be attached to the container
type AttachStreams struct {
// OutputStream will be attached to container's STDOUT
OutputStream io.WriteCloser
OutputStream io.Writer
// ErrorStream will be attached to container's STDERR
ErrorStream io.WriteCloser
ErrorStream io.Writer
// InputStream will be attached to container's STDIN
InputStream *bufio.Reader
// AttachOutput is whether to attach to STDOUT

View File

@ -295,9 +295,9 @@ type ResizeExecTTYOptions struct {
//go:generate go run ../generator/generator.go ExecStartAndAttachOptions
type ExecStartAndAttachOptions struct {
// OutputStream will be attached to container's STDOUT
OutputStream *io.WriteCloser
OutputStream *io.Writer
// ErrorStream will be attached to container's STDERR
ErrorStream *io.WriteCloser
ErrorStream *io.Writer
// InputStream will be attached to container's STDIN
InputStream *bufio.Reader
// AttachOutput is whether to attach to STDOUT

View File

@ -20,30 +20,30 @@ func (o *ExecStartAndAttachOptions) ToParams() (url.Values, error) {
}
// WithOutputStream set field OutputStream to given value
func (o *ExecStartAndAttachOptions) WithOutputStream(value io.WriteCloser) *ExecStartAndAttachOptions {
func (o *ExecStartAndAttachOptions) WithOutputStream(value io.Writer) *ExecStartAndAttachOptions {
o.OutputStream = &value
return o
}
// GetOutputStream returns value of field OutputStream
func (o *ExecStartAndAttachOptions) GetOutputStream() io.WriteCloser {
func (o *ExecStartAndAttachOptions) GetOutputStream() io.Writer {
if o.OutputStream == nil {
var z io.WriteCloser
var z io.Writer
return z
}
return *o.OutputStream
}
// WithErrorStream set field ErrorStream to given value
func (o *ExecStartAndAttachOptions) WithErrorStream(value io.WriteCloser) *ExecStartAndAttachOptions {
func (o *ExecStartAndAttachOptions) WithErrorStream(value io.Writer) *ExecStartAndAttachOptions {
o.ErrorStream = &value
return o
}
// GetErrorStream returns value of field ErrorStream
func (o *ExecStartAndAttachOptions) GetErrorStream() io.WriteCloser {
func (o *ExecStartAndAttachOptions) GetErrorStream() io.Writer {
if o.ErrorStream == nil {
var z io.WriteCloser
var z io.Writer
return z
}
return *o.ErrorStream