134 lines
3.5 KiB
Diff
134 lines
3.5 KiB
Diff
From 8b9b5e2615b1952a062f09476c53ff0a536df1ac Mon Sep 17 00:00:00 2001
|
|
From: zhongjiawei <zhongjiawei1@huawei.com>
|
|
Date: Mon, 24 Jul 2023 19:15:52 +0800
|
|
Subject: [PATCH] runc:make hooks log more userful and fix syslog hook bug
|
|
|
|
---
|
|
libcontainer/configs/config.go | 17 ++++++++++++++---
|
|
main.go | 26 ++++++++++++++++++++++++--
|
|
2 files changed, 38 insertions(+), 5 deletions(-)
|
|
|
|
diff --git a/libcontainer/configs/config.go b/libcontainer/configs/config.go
|
|
index 9076846..7bf83b9 100644
|
|
--- a/libcontainer/configs/config.go
|
|
+++ b/libcontainer/configs/config.go
|
|
@@ -8,6 +8,7 @@ import (
|
|
"github.com/opencontainers/runc/libcontainer/devices"
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
"os/exec"
|
|
+ "strings"
|
|
"time"
|
|
)
|
|
|
|
@@ -286,11 +287,11 @@ type Capabilities struct {
|
|
|
|
func (hooks HookList) RunHooks(state *specs.State) error {
|
|
for i, h := range hooks {
|
|
- logrus.Infof("run hooks %d:%T, ContainerId: %s", i, h, state.ID)
|
|
+ logrus.Infof("run hooks %d:%s, ContainerId: %s", i, h.Info(), state.ID)
|
|
if err := h.Run(state); err != nil {
|
|
- return fmt.Errorf("error running hook #%d: %w, ContainerId: %s", i, err, state.ID)
|
|
+ return fmt.Errorf("error running hook %d:%s err: %w, ContainerId: %s", i, h.Info(), err, state.ID)
|
|
}
|
|
- logrus.Infof("hooks %d:%T done, ContainerId: %s", i, h, state.ID)
|
|
+ logrus.Infof("hooks %d:%s done, ContainerId: %s", i, h.Info(), state.ID)
|
|
}
|
|
|
|
return nil
|
|
@@ -345,6 +346,7 @@ func (hooks *Hooks) MarshalJSON() ([]byte, error) {
|
|
type Hook interface {
|
|
// Run executes the hook with the provided state.
|
|
Run(*specs.State) error
|
|
+ Info() string
|
|
}
|
|
|
|
// NewFunctionHook will call the provided function when the hook is run.
|
|
@@ -362,6 +364,11 @@ func (f FuncHook) Run(s *specs.State) error {
|
|
return f.run(s)
|
|
}
|
|
|
|
+func (f FuncHook) Info() string {
|
|
+ return "hook function"
|
|
+}
|
|
+
|
|
+
|
|
type Command struct {
|
|
Path string `json:"path"`
|
|
Args []string `json:"args"`
|
|
@@ -381,6 +388,10 @@ type CommandHook struct {
|
|
Command
|
|
}
|
|
|
|
+func (c Command) Info() string {
|
|
+ return c.Path + "," + strings.Join(c.Args, ",")
|
|
+}
|
|
+
|
|
func (c Command) Run(s *specs.State) error {
|
|
b, err := json.Marshal(s)
|
|
if err != nil {
|
|
diff --git a/main.go b/main.go
|
|
index 9e14976..6e9101a 100644
|
|
--- a/main.go
|
|
+++ b/main.go
|
|
@@ -1,19 +1,23 @@
|
|
package main
|
|
|
|
import (
|
|
+ "encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
+ "log/syslog"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
+ "time"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/seccomp"
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
+ logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
@@ -217,9 +221,10 @@ func configLogrus(context *cli.Context) error {
|
|
logrus.SetOutput(f)
|
|
hook, serr := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO|syslog.LOG_USER, "docker-runc")
|
|
if serr != nil {
|
|
- fmt.Fprint(f, fmt.Sprintf("Leo: new syslog hook get %s", serr))
|
|
+ logToFile(f, "error", fmt.Sprintf("Leo: new syslog hook get %s", serr))
|
|
+ } else {
|
|
+ logrus.AddHook(hook)
|
|
}
|
|
- logrus.AddHook(hook)
|
|
}
|
|
if logLevel := context.GlobalString("log-level"); logLevel != "" {
|
|
lvl, err := logrus.ParseLevel(logLevel)
|
|
@@ -235,3 +240,20 @@ func configLogrus(context *cli.Context) error {
|
|
}
|
|
return nil
|
|
}
|
|
+
|
|
+func logToFile(f io.Writer, level string, msg string) {
|
|
+ var (
|
|
+ log struct {
|
|
+ Level string
|
|
+ Msg string
|
|
+ Time time.Time
|
|
+ }
|
|
+ )
|
|
+ log.Level = level
|
|
+ log.Msg = msg
|
|
+ log.Time = time.Now()
|
|
+ s, err := json.Marshal(log)
|
|
+ if err != nil {
|
|
+ fmt.Fprint(f, string(s))
|
|
+ }
|
|
+}
|
|
--
|
|
2.33.0
|
|
|