From 20cb595625dcfdf89fdf766028625a7864674dec Mon Sep 17 00:00:00 2001
From: liuzekun <liuzekun@huawei.com>
Date: Mon, 23 Dec 2019 03:10:49 -0500
Subject: [PATCH] containerd: wrap and process return errors

reason: wrap and process return errors

Signed-off-by: liuzekun <liuzekun@huawei.com>
---
 cmd/containerd-shim/main_unix.go              | 2 +-
 events/exit.go                                | 4 ++--
 legacy/legacy.go                              | 8 +++++---
 runtime/v1/linux/leruntime.go                 | 5 ++++-
 runtime/v1/linux/runtime.go                   | 7 +++++--
 runtime/v1/shim/reaper.go                     | 4 ++--
 runtime/v1/shim/service.go                    | 1 +
 vendor/github.com/sirupsen/logrus/exported.go | 5 +++++
 8 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/cmd/containerd-shim/main_unix.go b/cmd/containerd-shim/main_unix.go
index 2228362..e9c1426 100644
--- a/cmd/containerd-shim/main_unix.go
+++ b/cmd/containerd-shim/main_unix.go
@@ -259,7 +259,7 @@ func dumpStacks(logger *logrus.Entry) {
 		bufferLen *= 2
 	}
 	buf = buf[:stackSize]
-	ioutil.WriteFile(fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1)), buf, 0600)
+	logrus.Devour(ioutil.WriteFile(fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1)), buf, 0600))
 	logger.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
 }
 
diff --git a/events/exit.go b/events/exit.go
index 772dc24..c0a3583 100644
--- a/events/exit.go
+++ b/events/exit.go
@@ -48,13 +48,14 @@ func ExitInfo(ef string) (string, uint32, uint32) {
 }
 
 func ExitAddFile(ns string, ef string, reason string) {
-	os.MkdirAll(filepath.Join(ExitDir, ns), 0700)
+	logrus.Devour(os.MkdirAll(filepath.Join(ExitDir, ns), 0700))
 	err := ioutil.WriteFile(filepath.Join(ExitDir, ns, ef), []byte{}, 0600)
 	logrus.Infof("exit-add %s/%s [reason: %s] error=%v", ns, ef, reason, err)
 }
 
 func ExitDelFile(ns string, ef string) {
 	err := os.RemoveAll(filepath.Join(ExitDir, ns, ef))
+	logrus.Devour(err)
 	logrus.Infof("exit-del %s/%s error=%v", ns, ef, err)
 }
 
diff --git a/legacy/legacy.go b/legacy/legacy.go
index fde9f70..219508c 100644
--- a/legacy/legacy.go
+++ b/legacy/legacy.go
@@ -17,8 +17,8 @@ import (
 	"runtime"
 	"strings"
 
-	"github.com/sirupsen/logrus"
 	"github.com/opencontainers/runtime-spec/specs-go"
+	"github.com/sirupsen/logrus"
 )
 
 const (
@@ -107,8 +107,10 @@ func InitBundle(root string, id string) error {
 	if err != nil {
 		return err
 	}
-	CopyFile(Config120+id+"/config.json", Config028+id+"/config.json")
-	CopyFile(Config120+id+"/init.pid", State028+id+"/init/pid")
+	_, err = CopyFile(Config120+id+"/config.json", Config028+id+"/config.json")
+	logrus.Devour(err)
+	_, err = CopyFile(Config120+id+"/init.pid", State028+id+"/init/pid")
+	logrus.Devour(err)
 	return nil
 }
 
diff --git a/runtime/v1/linux/leruntime.go b/runtime/v1/linux/leruntime.go
index 9c793a5..e8fbe61 100644
--- a/runtime/v1/linux/leruntime.go
+++ b/runtime/v1/linux/leruntime.go
@@ -112,7 +112,10 @@ func (r *Runtime) legacyCreate(ctx context.Context, id string, opts runtime.Crea
 			// Task was never started or was already successfully deleted
 			return
 		}
-		lc := t.(*Task)
+		lc, ok := t.(*Task)
+		if !ok {
+			log.G(ctx).WithField("id", id).Errorf("task t's type is %T, cannot convert to a *Task value", t)
+		}
 
 		log.G(ctx).WithFields(logrus.Fields{
 			"id":        id,
diff --git a/runtime/v1/linux/runtime.go b/runtime/v1/linux/runtime.go
index 1b763fb..c334bf4 100644
--- a/runtime/v1/linux/runtime.go
+++ b/runtime/v1/linux/runtime.go
@@ -43,7 +43,7 @@ import (
 	"github.com/containerd/containerd/plugin"
 	"github.com/containerd/containerd/runtime"
 	"github.com/containerd/containerd/runtime/linux/runctypes"
-	"github.com/containerd/containerd/runtime/v1"
+	v1 "github.com/containerd/containerd/runtime/v1"
 	"github.com/containerd/containerd/runtime/v1/linux/proc"
 	shim "github.com/containerd/containerd/runtime/v1/shim/v1"
 	runc "github.com/containerd/go-runc"
@@ -200,7 +200,10 @@ func (r *Runtime) Create(ctx context.Context, id string, opts runtime.CreateOpts
 				// Task was never started or was already successfully deleted
 				return
 			}
-			lc := t.(*Task)
+			lc, ok := t.(*Task)
+			if !ok {
+				log.G(ctx).WithField("id", id).Errorf("task t's type is %T, cannot convert to a *Task value", t)
+			}
 
 			log.G(ctx).WithFields(logrus.Fields{
 				"id":        id,
diff --git a/runtime/v1/shim/reaper.go b/runtime/v1/shim/reaper.go
index 2846152..c657397 100644
--- a/runtime/v1/shim/reaper.go
+++ b/runtime/v1/shim/reaper.go
@@ -95,7 +95,7 @@ func (m *Monitor) Wait(c *exec.Cmd, ec chan runc.Exit) (int, error) {
 	for e := range ec {
 		if e.Pid == c.Process.Pid {
 			// make sure we flush all IO
-			c.Wait()
+			logrus.Devour(c.Wait())
 			m.Unsubscribe(ec)
 			return e.Status, nil
 		}
@@ -123,7 +123,7 @@ func (m *Monitor) WaitTimeout(c *exec.Cmd, ec chan runc.Exit, sec int64) (int, e
 	select {
 	case <-time.After(time.Duration(sec) * time.Second):
 		if SameProcess(c, c.Process.Pid) {
-			syscall.Kill(c.Process.Pid, syscall.SIGKILL)
+			logrus.Devour(syscall.Kill(c.Process.Pid, syscall.SIGKILL))
 		}
 		return 0, errors.Errorf("container did not start before the specified timeout %ds for cmd(pid=%d): %s, %s", sec, c.Process.Pid, c.Path, c.Args)
 	case status := <-sch:
diff --git a/runtime/v1/shim/service.go b/runtime/v1/shim/service.go
index 4025a72..beb0ed8 100644
--- a/runtime/v1/shim/service.go
+++ b/runtime/v1/shim/service.go
@@ -146,6 +146,7 @@ func (s *Service) Create(ctx context.Context, r *shimapi.CreateTaskRequest) (_ *
 			for i := 0; i < 60; i++ {
 				time.Sleep(time.Second)
 				_, err := os.Stat(r.Bundle)
+				logrus.Devour(err)
 				if os.IsNotExist(err) {
 					logrus.Errorf("bundle dir: %v does not exist, containerd-shim exit", r.Bundle)
 					os.Exit(0)
diff --git a/vendor/github.com/sirupsen/logrus/exported.go b/vendor/github.com/sirupsen/logrus/exported.go
index 1aeaa90..46fa7f8 100644
--- a/vendor/github.com/sirupsen/logrus/exported.go
+++ b/vendor/github.com/sirupsen/logrus/exported.go
@@ -191,3 +191,8 @@ func Panicln(args ...interface{}) {
 func Fatalln(args ...interface{}) {
 	std.Fatalln(args...)
 }
+
+// Devour will do nothing and return directly
+func Devour(args ...interface{}) {
+	return
+}
-- 
2.20.1

