mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-10 21:53:57 +08:00
f944ccecb2
In case early stage of runc init (nsenter) fails for some reason, it logs error(s) with FATAL log level, via bail(). The runc init log is read by a parent (runc create/run/exec) and is logged via normal logrus mechanism, which is all fine and dandy, except when `runc init` fails, we return the error from the parent (which is usually not too helpful, for example): runc run failed: unable to start container process: can't get final child's PID from pipe: EOF Now, the actual underlying error is from runc init and it was logged earlier; here's how full runc output looks like: FATA[0000] nsexec-1[3247792]: failed to unshare remaining namespaces: No space left on device FATA[0000] nsexec-0[3247790]: failed to sync with stage-1: next state ERRO[0000] runc run failed: unable to start container process: can't get final child's PID from pipe: EOF The problem is, upper level runtimes tend to ignore everything except the last line from runc, and thus error reported by e.g. docker is not very helpful. This patch tries to improve the situation by collecting FATAL errors from runc init and appending those to the error returned (instead of logging). With it, the above error will look like this: ERRO[0000] runc run failed: unable to start container process: can't get final child's PID from pipe: EOF; runc init error(s): nsexec-1[141549]: failed to unshare remaining namespaces: No space left on device; nsexec-0[141547]: failed to sync with stage-1: next state Yes, it is long and ugly, but at least the upper level runtime will report it. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
// Package logs provides helpers for logging used within runc (specifically for
|
|
// forwarding logs from "runc init" to the main runc process).
|
|
package logs
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var fatalsSep = []byte("; ")
|
|
|
|
func ForwardLogs(logPipe io.ReadCloser) chan error {
|
|
done := make(chan error, 1)
|
|
s := bufio.NewScanner(logPipe)
|
|
|
|
logger := logrus.StandardLogger()
|
|
if logger.ReportCaller {
|
|
// Need a copy of the standard logger, but with ReportCaller
|
|
// turned off, as the logs are merely forwarded and their
|
|
// true source is not this file/line/function.
|
|
logNoCaller := *logrus.StandardLogger()
|
|
logNoCaller.ReportCaller = false
|
|
logger = &logNoCaller
|
|
}
|
|
|
|
go func() {
|
|
fatals := []byte{}
|
|
for s.Scan() {
|
|
fatals = processEntry(s.Bytes(), logger, fatals)
|
|
}
|
|
if err := s.Err(); err != nil {
|
|
logrus.Errorf("error reading from log source: %v", err)
|
|
}
|
|
if err := logPipe.Close(); err != nil {
|
|
logrus.Errorf("error closing log source: %v", err)
|
|
}
|
|
// The only error we return is fatal messages from runc init.
|
|
var err error
|
|
if len(fatals) > 0 {
|
|
err = errors.New(string(bytes.TrimSuffix(fatals, fatalsSep)))
|
|
}
|
|
done <- err
|
|
close(done)
|
|
}()
|
|
|
|
return done
|
|
}
|
|
|
|
// processEntry parses the error and either logs it via the standard logger or,
|
|
// if this is a fatal error, appends its text to fatals.
|
|
func processEntry(text []byte, logger *logrus.Logger, fatals []byte) []byte {
|
|
if len(text) == 0 {
|
|
return fatals
|
|
}
|
|
|
|
var jl struct {
|
|
Level logrus.Level `json:"level"`
|
|
Msg string `json:"msg"`
|
|
}
|
|
if err := json.Unmarshal(text, &jl); err != nil {
|
|
logrus.Errorf("failed to decode %q to json: %v", text, err)
|
|
return fatals
|
|
}
|
|
|
|
if jl.Level == logrus.FatalLevel {
|
|
fatals = append(fatals, jl.Msg...)
|
|
fatals = append(fatals, fatalsSep...)
|
|
} else {
|
|
logger.Log(jl.Level, jl.Msg)
|
|
}
|
|
return fatals
|
|
}
|