Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/server/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.C
}
meta.ImageRef = image.ID

// Get container log path.
if config.GetLogPath() != "" {
meta.LogPath = filepath.Join(sandbox.Config.GetLogDirectory(), config.GetLogPath())
}

containerIO, err := cio.NewContainerIO(id,
cio.WithStdin(config.GetStdin()),
cio.WithTerminal(config.GetTty()),
Expand Down
49 changes: 29 additions & 20 deletions pkg/server/container_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package server
import (
"fmt"
"io"
"path/filepath"
"time"

"github.com/containerd/containerd"
Expand Down Expand Up @@ -88,7 +87,6 @@ func (c *criContainerdService) startContainer(ctx context.Context,
if err != nil {
return fmt.Errorf("sandbox %q not found: %v", meta.SandboxID, err)
}
sandboxConfig := sandbox.Config
sandboxID := meta.SandboxID
// Make sure sandbox is running.
s, err := sandbox.Container.Task(ctx, nil)
Expand All @@ -106,7 +104,10 @@ func (c *criContainerdService) startContainer(ctx context.Context,
}

ioCreation := func(id string) (_ containerd.IO, err error) {
var stdoutWC, stderrWC io.WriteCloser
stdoutWC, stderrWC, err := createContainerLoggers(meta.LogPath, config.GetTty())
if err != nil {
return nil, fmt.Errorf("failed to create container loggers: %v", err)
}
defer func() {
if err != nil {
if stdoutWC != nil {
Expand All @@ -117,23 +118,6 @@ func (c *criContainerdService) startContainer(ctx context.Context,
}
}
}()
if config.GetLogPath() != "" {
// Only generate container log when log path is specified.
logPath := filepath.Join(sandboxConfig.GetLogDirectory(), config.GetLogPath())
if stdoutWC, err = cio.NewCRILogger(logPath, cio.Stdout); err != nil {
return nil, fmt.Errorf("failed to start container stdout logger: %v", err)
}
// Only redirect stderr when there is no tty.
if !config.GetTty() {
if stderrWC, err = cio.NewCRILogger(logPath, cio.Stderr); err != nil {
return nil, fmt.Errorf("failed to start container stderr logger: %v", err)
}
}
} else {
stdoutWC = cio.NewDiscardLogger()
stderrWC = cio.NewDiscardLogger()
}

if err := cio.WithOutput("log", stdoutWC, stderrWC)(cntr.IO); err != nil {
return nil, fmt.Errorf("failed to add container log: %v", err)
}
Expand Down Expand Up @@ -165,3 +149,28 @@ func (c *criContainerdService) startContainer(ctx context.Context,
status.StartedAt = time.Now().UnixNano()
return nil
}

// Create container loggers and return write closer for stdout and stderr.
func createContainerLoggers(logPath string, tty bool) (stdout io.WriteCloser, stderr io.WriteCloser, err error) {
if logPath != "" {
// Only generate container log when log path is specified.
if stdout, err = cio.NewCRILogger(logPath, cio.Stdout); err != nil {
return nil, nil, fmt.Errorf("failed to start container stdout logger: %v", err)
}
defer func() {
if err != nil {
stdout.Close()
}
}()
// Only redirect stderr when there is no tty.
if !tty {
if stderr, err = cio.NewCRILogger(logPath, cio.Stderr); err != nil {
return nil, nil, fmt.Errorf("failed to start container stderr logger: %v", err)
}
}
} else {
stdout = cio.NewDiscardLogger()
stderr = cio.NewDiscardLogger()
}
return
}
6 changes: 6 additions & 0 deletions pkg/server/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,9 @@ func getSourceMount(source string, mountInfos []*mount.Info) (string, string, er
// If we are here, we did not find parent mount. Something is wrong.
return "", "", fmt.Errorf("Could not find source mount of %s", source)
}

// filterLabel returns a label filter. Use `%q` here because containerd
// filter needs extra quote to work properly.
func filterLabel(k, v string) string {
return fmt.Sprintf("labels.%q==%q", k, v)
}
15 changes: 15 additions & 0 deletions pkg/server/io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ func WithRootDir(root string) Opts {
}
}

// WithFIFOs specifies existing fifos for the container io.
func WithFIFOs(dir, stdin, stdout, stderr string) Opts {
return func(c *ContainerIO) error {
c.dir = dir
c.stdinPath = stdin
c.stdoutPath = stdout
c.stderrPath = stderr
return nil
}
}

// NewContainerIO creates container io.
func NewContainerIO(id string, opts ...Opts) (*ContainerIO, error) {
c := &ContainerIO{
Expand All @@ -149,6 +160,10 @@ func NewContainerIO(id string, opts ...Opts) (*ContainerIO, error) {
return nil, err
}
}
if c.dir != "" {
// Return if fifos are already set.
return c, nil
}
fifos, err := newFifos(c.root, id)
if err != nil {
return nil, err
Expand Down
Loading