From a3ce12179f21f39c7384e1a1b51da1fe0dfde686 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 28 Jun 2018 10:07:07 +0100 Subject: [PATCH] logging: Add containerID and sandboxID to all log calls Adding cid+sid fields to the log entries generated by most of the CLI commands will make debugging across the system easier. Fixes #452. Signed-off-by: James O. D. Hunt --- cli/create.go | 6 ++++++ cli/delete.go | 6 ++++++ cli/events.go | 5 +++++ cli/exec.go | 6 ++++++ cli/kill.go | 6 ++++++ cli/pause.go | 6 ++++++ cli/ps.go | 6 ++++++ cli/start.go | 6 ++++++ cli/state.go | 2 ++ cli/update.go | 7 +++++++ 10 files changed, 56 insertions(+) diff --git a/cli/create.go b/cli/create.go index 5067d53c4..165bf6b69 100644 --- a/cli/create.go +++ b/cli/create.go @@ -89,6 +89,8 @@ func create(containerID, bundlePath, console, pidFilePath string, detach bool, runtimeConfig oci.RuntimeConfig) error { var err error + kataLog = kataLog.WithField("container", containerID) + // Checks the MUST and MUST NOT from OCI runtime specification if bundlePath, err = validCreateParams(containerID, bundlePath); err != nil { return err @@ -238,6 +240,8 @@ func createSandbox(ociSpec oci.CompatOCISpec, runtimeConfig oci.RuntimeConfig, return vc.Process{}, err } + kataLog = kataLog.WithField("sandbox", sandbox.ID()) + containers := sandbox.GetAllContainers() if len(containers) != 1 { return vc.Process{}, fmt.Errorf("BUG: Container list from sandbox is wrong, expecting only one container, found %d containers", len(containers)) @@ -263,6 +267,8 @@ func createContainer(ociSpec oci.CompatOCISpec, containerID, bundlePath, return vc.Process{}, err } + kataLog = kataLog.WithField("sandbox", sandboxID) + _, c, err := vci.CreateContainer(sandboxID, contConfig) if err != nil { return vc.Process{}, err diff --git a/cli/delete.go b/cli/delete.go index 1ed2fbdd9..0d83314e9 100644 --- a/cli/delete.go +++ b/cli/delete.go @@ -12,6 +12,7 @@ import ( vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" + "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -58,6 +59,11 @@ func delete(containerID string, force bool) error { return err } + kataLog = kataLog.WithFields(logrus.Fields{ + "container": containerID, + "sandbox": sandboxID, + }) + containerID = status.ID containerType, err := oci.GetContainerType(status.Annotations) diff --git a/cli/events.go b/cli/events.go index 553ab0f1c..7da633cc9 100644 --- a/cli/events.go +++ b/cli/events.go @@ -151,6 +151,11 @@ information is displayed once every 5 seconds.`, return err } + kataLog = kataLog.WithFields(logrus.Fields{ + "container": containerID, + "sandbox": sandboxID, + }) + if status.State.State == vc.StateStopped { return fmt.Errorf("container with id %s is not running", status.ID) } diff --git a/cli/exec.go b/cli/exec.go index b1bcc4599..1e71c90d2 100644 --- a/cli/exec.go +++ b/cli/exec.go @@ -16,6 +16,7 @@ import ( vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" specs "github.com/opencontainers/runtime-spec/specs-go" + "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -188,6 +189,11 @@ func execute(context *cli.Context) error { return err } + kataLog = kataLog.WithFields(logrus.Fields{ + "container": containerID, + "sandbox": sandboxID, + }) + // Retrieve OCI spec configuration. ociSpec, err := oci.GetOCIConfig(status) if err != nil { diff --git a/cli/kill.go b/cli/kill.go index 3c5c2a52d..5d93df052 100644 --- a/cli/kill.go +++ b/cli/kill.go @@ -13,6 +13,7 @@ import ( vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" + "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -98,6 +99,11 @@ func kill(containerID, signal string, all bool) error { containerID = status.ID + kataLog = kataLog.WithFields(logrus.Fields{ + "container": containerID, + "sandbox": sandboxID, + }) + signum, err := processSignal(signal) if err != nil { return err diff --git a/cli/pause.go b/cli/pause.go index a2d804ea1..4b9a49087 100644 --- a/cli/pause.go +++ b/cli/pause.go @@ -7,6 +7,7 @@ package main import ( + "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -49,6 +50,11 @@ func toggleContainerPause(containerID string, pause bool) (err error) { containerID = status.ID + kataLog = kataLog.WithFields(logrus.Fields{ + "container": containerID, + "sandbox": sandboxID, + }) + if pause { err = vci.PauseContainer(sandboxID, containerID) } else { diff --git a/cli/ps.go b/cli/ps.go index 73e18005a..4dfab940f 100644 --- a/cli/ps.go +++ b/cli/ps.go @@ -10,6 +10,7 @@ import ( "fmt" vc "github.com/kata-containers/runtime/virtcontainers" + "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -55,6 +56,11 @@ func ps(containerID, format string, args []string) error { containerID = status.ID + kataLog = kataLog.WithFields(logrus.Fields{ + "container": containerID, + "sandbox": sandboxID, + }) + // container MUST be running if status.State.State != vc.StateRunning { return fmt.Errorf("Container %s is not running", containerID) diff --git a/cli/start.go b/cli/start.go index a96e9c1a0..7da8a5a28 100644 --- a/cli/start.go +++ b/cli/start.go @@ -11,6 +11,7 @@ import ( vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" + "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -46,6 +47,11 @@ func start(containerID string) (vc.VCSandbox, error) { return nil, err } + kataLog = kataLog.WithFields(logrus.Fields{ + "container": containerID, + "sandbox": sandboxID, + }) + containerID = status.ID containerType, err := oci.GetContainerType(status.Annotations) diff --git a/cli/state.go b/cli/state.go index c264616dc..57412e618 100644 --- a/cli/state.go +++ b/cli/state.go @@ -34,6 +34,8 @@ instance of a container.`, } func state(containerID string) error { + kataLog = kataLog.WithField("container", containerID) + // Checks the MUST and MUST NOT from OCI runtime specification status, _, err := getExistingContainerInfo(containerID) if err != nil { diff --git a/cli/update.go b/cli/update.go index d1db024cf..fbb718f62 100644 --- a/cli/update.go +++ b/cli/update.go @@ -15,6 +15,7 @@ import ( "github.com/docker/go-units" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/opencontainers/runtime-spec/specs-go" + "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -137,6 +138,12 @@ other options are ignored. } containerID = status.ID + + kataLog = kataLog.WithFields(logrus.Fields{ + "container": containerID, + "sandbox": sandboxID, + }) + // container MUST be running if status.State.State != vc.StateRunning { return fmt.Errorf("Container %s is not running", containerID)