Files
kata-containers/src/runtime/virtcontainers/iostream.go
Chelsea Mafrica 6b0dc60dda runtime: Fix ordering of trace spans
A significant number of trace calls did not use a parent context that
would create proper span ordering in trace output. Add local context to
functions for use in trace calls to facilitate proper span ordering.
Additionally, change whether trace function returns context in some
functions in virtcontainers and use existing context rather than
background context in bindMount() so that span exists as a child of a
parent span.

Fixes #1355

Signed-off-by: Chelsea Mafrica <chelsea.e.mafrica@intel.com>
2021-03-16 17:39:28 -07:00

97 lines
1.9 KiB
Go

// Copyright (c) 2018 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"context"
"errors"
"io"
)
type iostream struct {
sandbox *Sandbox
container *Container
process string
closed bool
}
// io.WriteCloser
type stdinStream struct {
*iostream
}
// io.Reader
type stdoutStream struct {
*iostream
}
// io.Reader
type stderrStream struct {
*iostream
}
func newIOStream(s *Sandbox, c *Container, proc string) *iostream {
return &iostream{
sandbox: s,
container: c,
process: proc,
closed: false, // needed to workaround buggy structcheck
}
}
func (s *iostream) stdin() io.WriteCloser {
return &stdinStream{s}
}
func (s *iostream) stdout() io.Reader {
return &stdoutStream{s}
}
func (s *iostream) stderr() io.Reader {
return &stderrStream{s}
}
func (s *stdinStream) Write(data []byte) (n int, err error) {
if s.closed {
return 0, errors.New("stream closed")
}
// can not pass context to Write(), so use background context
return s.sandbox.agent.writeProcessStdin(context.Background(), s.container, s.process, data)
}
func (s *stdinStream) Close() error {
if s.closed {
return errors.New("stream closed")
}
// can not pass context to Close(), so use background context
err := s.sandbox.agent.closeProcessStdin(context.Background(), s.container, s.process)
if err == nil {
s.closed = true
}
return err
}
func (s *stdoutStream) Read(data []byte) (n int, err error) {
if s.closed {
return 0, errors.New("stream closed")
}
// can not pass context to Read(), so use background context
return s.sandbox.agent.readProcessStdout(context.Background(), s.container, s.process, data)
}
func (s *stderrStream) Read(data []byte) (n int, err error) {
if s.closed {
return 0, errors.New("stream closed")
}
// can not pass context to Read(), so use background context
return s.sandbox.agent.readProcessStderr(context.Background(), s.container, s.process, data)
}