Skip to content

Commit

Permalink
Merge pull request #1728 from ashishsachdeva/asachdev/logsredirection
Browse files Browse the repository at this point in the history
gcs: Support routing container stdio to sidecar
  • Loading branch information
kevpar committed May 9, 2023
2 parents de0e116 + b1b0768 commit a1d874a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
71 changes: 69 additions & 2 deletions internal/guest/runtime/runc/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package runc

import (
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -326,7 +327,7 @@ func (c *container) runExecCommand(processDef *oci.Process, stdioSet *stdio.Conn

args := []string{"exec"}
args = append(args, "-d", "--process", filepath.Join(tempProcessDir, "process.json"))
return c.startProcess(tempProcessDir, processDef.Terminal, stdioSet, args...)
return c.startProcess(tempProcessDir, processDef.Terminal, stdioSet, nil, args...)
}

// startProcess performs the operations necessary to start a container process
Expand All @@ -337,7 +338,9 @@ func (c *container) runExecCommand(processDef *oci.Process, stdioSet *stdio.Conn
func (c *container) startProcess(
tempProcessDir string,
hasTerminal bool,
stdioSet *stdio.ConnectionSet, initialArgs ...string,
stdioSet *stdio.ConnectionSet,
annotations map[string]string,
initialArgs ...string,
) (p *process, err error) {
args := initialArgs

Expand Down Expand Up @@ -389,6 +392,70 @@ func (c *container) startProcess(
}
}

// This is for enabling container logging via side car feature. This is in experimental stage now and need to be revisited.
var stdoutFifoPipe, stderrFifoPipe *os.File
if annotations != nil {
pipeNameSuffix, exists := annotations["io.microsoft.bmc.logging.pipelocation"]
if exists {
if hasTerminal {
return nil, fmt.Errorf("logging via side car and TTY are not supported together")
}
pipeDirectory := "/run/gcs/containerlogs/"
stdoutPipeName := pipeDirectory + pipeNameSuffix + "-stdout"
stderrPipeName := pipeDirectory + pipeNameSuffix + "-stderr"
err = os.MkdirAll(pipeDirectory, 0755)
if err != nil {
return nil, fmt.Errorf("error creating log directory %s for logging pipe fifo: %w", pipeDirectory, err)
}

_, err = os.Stat(stdoutPipeName)
if err != nil {
// fifo pipe does not exist, create one
err = syscall.Mkfifo(stdoutPipeName, 0666)
if err != nil {
return nil, fmt.Errorf("error creating fifo %s: %w", stdoutPipeName, err)
}
}

_, err = os.Stat(stderrPipeName)
if err != nil {
// fifo pipe does not exist, create one
err = syscall.Mkfifo(stderrPipeName, 0666)
if err != nil {
return nil, fmt.Errorf("error creating fifo %s: %w", stderrPipeName, err)
}
}

// pipe either exist before hand or we have created one above
stdoutFifoPipe, err = os.OpenFile(stdoutPipeName, os.O_RDWR|os.O_APPEND, os.ModeNamedPipe)
if err != nil {
return nil, fmt.Errorf("error opening fifo %s: %w", stdoutPipeName, err)
}

stderrFifoPipe, err = os.OpenFile(stderrPipeName, os.O_RDWR|os.O_APPEND, os.ModeNamedPipe)
if err != nil {
return nil, fmt.Errorf("error opening fifo %s: %w", stderrPipeName, err)
}
}

isLoggingSideCarContainerStr, exists := annotations["io.microsoft.bmc.logging.isLoggingSideCarContainer"]
if exists {
isLoggingSideCarContainer, err := strconv.ParseBool(isLoggingSideCarContainerStr)
if err != nil {
return nil, fmt.Errorf("error parsing flag isLoggingSideCarContainer: %w", err)
}
if !isLoggingSideCarContainer {
// workload container needs to redirect stdout and stderr to fifo pipe.
cmd.Stdout = stdoutFifoPipe
cmd.Stderr = stderrFifoPipe
} else {
// logging side car container needs to know the pipe fd.
cmd.Args = append(cmd.Args, "--preserve-fds", "2")
cmd.ExtraFiles = []*os.File{stdoutFifoPipe, stderrFifoPipe}
}
}
}

if err := cmd.Run(); err != nil {
runcErr := getRuncLogError(logPath)
return nil, errors.Wrapf(runcErr, "failed to run runc create/exec call for container %s with %v", c.id, err)
Expand Down
3 changes: 2 additions & 1 deletion internal/guest/runtime/runc/runc.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ func (r *runcRuntime) runCreateCommand(id string, bundlePath string, stdioSet *s
}

args := []string{"create", "-b", bundlePath, "--no-pivot"}
p, err := c.startProcess(tempProcessDir, spec.Process.Terminal, stdioSet, args...)

p, err := c.startProcess(tempProcessDir, spec.Process.Terminal, stdioSet, spec.Annotations, args...)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit a1d874a

Please sign in to comment.