From f61fa28d39298def261dded2644b8dcf45366415 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Sat, 18 May 2019 19:39:11 -0400 Subject: Added --log-driver and journald logging Signed-off-by: Peter Hunt --- libpod/runtime_ctr.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'libpod/runtime_ctr.go') diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index c7758055f..25db10d33 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -196,7 +196,8 @@ func (r *Runtime) newContainer(ctx context.Context, rSpec *spec.Spec, options .. } } - if ctr.config.LogPath == "" { + // TODO magic string + if ctr.config.LogPath == "" && ctr.config.LogDriver != "journald" { ctr.config.LogPath = filepath.Join(ctr.config.StaticDir, "ctr.log") } -- cgit v1.2.3-54-g00ecf From 51bdf29f0493827ce3eb278a193d0a7402add896 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Mon, 20 May 2019 13:58:31 -0400 Subject: Address comments Signed-off-by: Peter Hunt --- libpod/container.go | 9 +++++++++ libpod/container_log.go | 5 +---- libpod/container_log_linux.go | 7 ++++--- libpod/container_log_unsupported.go | 8 ++++++-- libpod/oci.go | 7 ------- libpod/oci_linux.go | 1 + libpod/options.go | 8 +++++--- libpod/runtime_ctr.go | 3 +-- test/e2e/.logs_test.go.swp | Bin 0 -> 20480 bytes 9 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 test/e2e/.logs_test.go.swp (limited to 'libpod/runtime_ctr.go') diff --git a/libpod/container.go b/libpod/container.go index 9ac08cba0..c8ab42fc3 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -51,6 +51,15 @@ const CgroupfsDefaultCgroupParent = "/libpod_parent" // manager in libpod const SystemdDefaultCgroupParent = "machine.slice" +// JournaldLogging is the string conmon expects to specify journald logging +const JournaldLogging = "journald" + +// KubernetesLogging is the string conmon expects when specifying to use the kubernetes logging format +const KubernetesLogging = "k8s-file" + +// JSONLogging is the string conmon expects when specifying to use the json logging format +const JSONLogging = "json-file" + // DefaultWaitInterval is the default interval between container status checks // while waiting. const DefaultWaitInterval = 250 * time.Millisecond diff --git a/libpod/container_log.go b/libpod/container_log.go index 9276b52f4..c893ccad9 100644 --- a/libpod/container_log.go +++ b/libpod/container_log.go @@ -20,9 +20,6 @@ const ( // https://github.com/golang/go/issues/19635 logTimeFormat = "2006-01-02T15:04:05.000000000Z07:00" - // readLogTimeFormat is the format the log lines will be read in - readLogTimeFormat = time.RFC3339Nano - // partialLogType signifies a log line that exceeded the buffer // length and needed to spill into a new line partialLogType = "P" @@ -213,7 +210,7 @@ func newLogLine(line string) (*LogLine, error) { if len(splitLine) < 4 { return nil, errors.Errorf("'%s' is not a valid container log line", line) } - logTime, err := time.Parse(readLogTimeFormat, splitLine[0]) + logTime, err := time.Parse(logTimeFormat, splitLine[0]) if err != nil { return nil, errors.Wrapf(err, "unable to convert time %s from container log", splitLine[0]) } diff --git a/libpod/container_log_linux.go b/libpod/container_log_linux.go index 47dc44b8f..3b7945e0c 100644 --- a/libpod/container_log_linux.go +++ b/libpod/container_log_linux.go @@ -1,4 +1,5 @@ //+build linux +//+build systemd package libpod @@ -22,8 +23,8 @@ const ( // bufLen is the length of the buffer to read from a k8s-file // formatted log line - // It consists of conmon.TSBUFLEN+2+conmon.STDIOBUFSIZE+'\0' - bufLen = 44 + 2 + 8192 + 1 + // let's set it as 2k just to be safe if k8s-file format ever changes + bufLen = 16384 ) func (c *Container) readFromJournal(options *LogOptions, logChannel chan *LogLine) error { @@ -84,7 +85,7 @@ func (c *Container) readFromJournal(options *LogOptions, logChannel chan *LogLin func journalFormatter(entry *journal.JournalEntry) (string, error) { usec := entry.RealtimeTimestamp timestamp := time.Unix(0, int64(usec)*int64(time.Microsecond)) - output := timestamp.Format(readLogTimeFormat) + " " + output := timestamp.Format(logTimeFormat) + " " priority, ok := entry.Fields["PRIORITY"] if !ok { return "", errors.Errorf("no PRIORITY field present in journal entry") diff --git a/libpod/container_log_unsupported.go b/libpod/container_log_unsupported.go index 0c3d41620..0ec5740e2 100644 --- a/libpod/container_log_unsupported.go +++ b/libpod/container_log_unsupported.go @@ -1,7 +1,11 @@ -//+build !linux +//+build !linux !systemd package libpod +import ( + "github.com/pkg/errors" +) + func (c *Container) readFromJournal(options *LogOptions, logChannel chan *LogLine) error { - return ErrOSNotSupported + return errors.Wrapf(ErrOSNotSupported, "Journald logging only enabled with systemd on linux") } diff --git a/libpod/oci.go b/libpod/oci.go index a3e44bcce..7138108c5 100644 --- a/libpod/oci.go +++ b/libpod/oci.go @@ -42,12 +42,6 @@ const ( // NsRunDir is the default directory in which running network namespaces // are stored NsRunDir = "/var/run/netns" - - // JournaldLogging is the string conmon expects to specify journald logging - JournaldLogging = "journald" - - // KubernetesLogging is the string conmon expects when specifying to use the kubernetes logging format - KubernetesLogging = "k8s-file" ) // OCIRuntime represents an OCI-compatible runtime that libpod can call into @@ -409,7 +403,6 @@ func (r *OCIRuntime) execContainer(c *Container, cmd, capAdd, env []string, tty // Append container ID, name and command args = append(args, c.ID()) args = append(args, cmd...) - args = append(args, c.Name()) logrus.Debugf("Starting runtime %s with following arguments: %v", r.path, args) diff --git a/libpod/oci_linux.go b/libpod/oci_linux.go index 5d66e6d82..7c1c18052 100644 --- a/libpod/oci_linux.go +++ b/libpod/oci_linux.go @@ -214,6 +214,7 @@ func (r *OCIRuntime) createOCIContainer(ctr *Container, cgroupParent string, res } args = append(args, "-c", ctr.ID()) args = append(args, "-u", ctr.ID()) + args = append(args, "-n", ctr.Name()) args = append(args, "-r", r.path) args = append(args, "-b", ctr.bundlePath()) args = append(args, "-p", filepath.Join(ctr.state.RunDir, "pidfile")) diff --git a/libpod/options.go b/libpod/options.go index d6eb97609..20aa51981 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -985,10 +985,12 @@ func WithLogDriver(driver string) CtrCreateOption { if ctr.valid { return ErrCtrFinalized } - if driver == "" { + switch driver { + case "": return errors.Wrapf(ErrInvalidArg, "log driver must be set") - } - if driver != "journald" && driver != "k8s-file" && driver != "json-file" { + case JournaldLogging, KubernetesLogging, JSONLogging: + break + default: return errors.Wrapf(ErrInvalidArg, "invalid log driver") } diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index 25db10d33..db7a5e5c3 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -196,8 +196,7 @@ func (r *Runtime) newContainer(ctx context.Context, rSpec *spec.Spec, options .. } } - // TODO magic string - if ctr.config.LogPath == "" && ctr.config.LogDriver != "journald" { + if ctr.config.LogPath == "" && ctr.config.LogDriver != JournaldLogging { ctr.config.LogPath = filepath.Join(ctr.config.StaticDir, "ctr.log") } diff --git a/test/e2e/.logs_test.go.swp b/test/e2e/.logs_test.go.swp new file mode 100644 index 000000000..4a4788beb Binary files /dev/null and b/test/e2e/.logs_test.go.swp differ -- cgit v1.2.3-54-g00ecf