From effe1c4281e3929a866eeab18403a3df38ce504d Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Thu, 11 Jan 2018 16:31:50 -0800 Subject: [PATCH] Add support for stracing containers Signed-off-by: Mrunal Patel --- README.md | 1 + oci/oci.go | 24 ++++++++++++++++++++++++ pkg/annotations/annotations.go | 3 +++ 3 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 14cc3014..d79bc424 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ For sync communication we have an IRC channel #CRI-O, on chat.freenode.net, that - socat - iproute - iptables +- strace (required for debugging using cri-o strace annotation) Latest version of `runc` is expected to be installed on the system. It is picked up as the default runtime by CRI-O. diff --git a/oci/oci.go b/oci/oci.go index 658079a3..64092b50 100644 --- a/oci/oci.go +++ b/oci/oci.go @@ -14,6 +14,7 @@ import ( "time" "github.com/containerd/cgroups" + "github.com/kubernetes-incubator/cri-o/pkg/annotations" "github.com/kubernetes-incubator/cri-o/utils" rspec "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" @@ -284,6 +285,7 @@ func (r *Runtime) CreateContainer(c *Container, cgroupParent string) (err error) if ss.err != nil { return fmt.Errorf("error reading container (probably exited) json message: %v", ss.err) } + c.state.Pid = ss.si.Pid logrus.Debugf("Received container pid: %d", ss.si.Pid) if ss.si.Pid == -1 { if ss.si.Message != "" { @@ -297,6 +299,28 @@ func (r *Runtime) CreateContainer(c *Container, cgroupParent string) (err error) logrus.Errorf("Container creation timeout (%v)", ContainerCreateTimeout) return fmt.Errorf("create container timeout") } + + enableStrace := false + if _, ok := c.annotations[annotations.Strace]; ok { + logrus.Debugf("Enabling strace from annotation") + enableStrace = true + } + if !enableStrace { + if _, ok := c.labels[annotations.Strace]; ok { + logrus.Debugf("Enabling strace from label") + enableStrace = true + } + } + + if enableStrace { + go func() { + straceCmd := exec.Command("strace", "-f", "-o", fmt.Sprintf("/tmp/%v", c.id), "-p", fmt.Sprintf("%d", c.state.Pid)) + _, err := straceCmd.CombinedOutput() + if err != nil { + logrus.Errorf("Failed to execute strace: %v", err) + } + }() + } return nil } diff --git a/pkg/annotations/annotations.go b/pkg/annotations/annotations.go index 9b5b1352..966952a4 100644 --- a/pkg/annotations/annotations.go +++ b/pkg/annotations/annotations.go @@ -84,6 +84,9 @@ const ( // Volumes is the volumes annotatoin Volumes = "io.kubernetes.cri-o.Volumes" + + // Strace is enable strace debug annotation. + Strace = "io.kubernetes.cri-o.Strace" ) // ContainerType values