Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
agent: add support for logging to a vsock port
Browse files Browse the repository at this point in the history
not all the hypervisors connect a socket to /dev/console, so it's no possible
to get the agent's logs from a unix socket. This patch is to connect the
logger (logrus) to a vsock port, this way the logs can be read in the
host using vsocks or hybrid vsocks.

fixes #652

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Oct 3, 2019
1 parent a03e23b commit 95be1c3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
60 changes: 55 additions & 5 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"os"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/kata-containers/agent/pkg/uevent"
pb "github.com/kata-containers/agent/protocols/grpc"
"github.com/mdlayher/vsock"
"github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/configs"
_ "github.com/opencontainers/runc/libcontainer/nsenter"
Expand Down Expand Up @@ -167,6 +169,9 @@ var crashOnError = false
// if true, a shell (bash or sh) is started only if it's available in the rootfs.
var debugConsole = false

// Specify a vsock port where logs are written.
var logsVSockPort = uint32(0)

// commType is used to denote the communication channel type used.
type commType int

Expand Down Expand Up @@ -941,7 +946,30 @@ func announce() error {
return nil
}

func (s *sandbox) initLogger() error {
func logsToVPort() {
l, err := vsock.Listen(logsVSockPort)
if err != nil {
// no body listening
return
}
c, err := l.Accept()
if err != nil {
l.Close()
// no connection
return
}

r, w := io.Pipe()
agentLog.Logger.Out = w
io.Copy(c, r)

w.Close()
r.Close()
c.Close()
l.Close()
}

func (s *sandbox) initLogger(ctx context.Context) error {
agentLog.Logger.Formatter = &logrus.TextFormatter{DisableColors: true, TimestampFormat: time.RFC3339Nano}

config := newConfig(defaultLogLevel)
Expand All @@ -953,6 +981,28 @@ func (s *sandbox) initLogger() error {

agentLog = agentLog.WithField("debug_console", debugConsole)

if logsVSockPort != 0 {
go func() {
// save original logger's output to restore it when there
// is no process reading the logs in the host
out := agentLog.Logger.Out
for {
select {
case <-ctx.Done():
// stop the thread
return
default:
logsToVPort()
if agentLog.Logger.Out != out {
agentLog.Logger.Out = out
}
// waiting for the logs reader
time.Sleep(time.Millisecond * 500)
}
}
}()
}

return announce()
}

Expand Down Expand Up @@ -1370,15 +1420,15 @@ func realMain() error {
stopServer: make(chan struct{}),
}

if err = s.initLogger(); err != nil {
return fmt.Errorf("failed to setup logger: %v", err)
}

rootSpan, rootContext, err = setupTracing(agentName)
if err != nil {
return fmt.Errorf("failed to setup tracing: %v", err)
}

if err = s.initLogger(rootContext); err != nil {
return fmt.Errorf("failed to setup logger: %v", err)
}

if err := setupDebugConsole(rootContext, debugConsolePath); err != nil {
agentLog.WithError(err).Error("failed to setup debug console")
}
Expand Down
7 changes: 7 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
const (
optionPrefix = "agent."
logLevelFlag = optionPrefix + "log"
logsVSockPortFlag = optionPrefix + "log_vport"
devModeFlag = optionPrefix + "devmode"
traceModeFlag = optionPrefix + "trace"
useVsockFlag = optionPrefix + "use_vsock"
Expand Down Expand Up @@ -106,6 +107,12 @@ func (c *agentConfig) parseCmdlineOption(option string) error {
if level == logrus.DebugLevel {
debug = true
}
case logsVSockPortFlag:
port, err := strconv.ParseUint(split[valuePosition], 10, 32)
if err != nil {
return err
}
logsVSockPort = uint32(port)
case traceModeFlag:
switch split[valuePosition] {
case traceTypeIsolated:
Expand Down

0 comments on commit 95be1c3

Please sign in to comment.