mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-27 07:48:55 +00:00
runtime: change un-structured log to structured log
Change some logger from directly calling logrus to use a logrus instance to use structured log. Fixes: #458 Signed-off-by: bin liu <bin@hyper.sh>
This commit is contained in:
parent
2fef265701
commit
61d133f941
@ -19,7 +19,6 @@ import (
|
|||||||
"github.com/containerd/typeurl"
|
"github.com/containerd/typeurl"
|
||||||
"github.com/opencontainers/runtime-spec/specs-go"
|
"github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
// only register the proto type
|
// only register the proto type
|
||||||
_ "github.com/containerd/containerd/runtime/linux/runctypes"
|
_ "github.com/containerd/containerd/runtime/linux/runctypes"
|
||||||
@ -72,7 +71,7 @@ func create(ctx context.Context, s *service, r *taskAPI.CreateTaskRequest) (*con
|
|||||||
defer func() {
|
defer func() {
|
||||||
if err != nil && rootFs.Mounted {
|
if err != nil && rootFs.Mounted {
|
||||||
if err2 := mount.UnmountAll(rootfs, 0); err2 != nil {
|
if err2 := mount.UnmountAll(rootfs, 0); err2 != nil {
|
||||||
logrus.WithError(err2).Warn("failed to cleanup rootfs mount")
|
shimLog.WithField("container-type", containerType).WithError(err2).Warn("failed to cleanup rootfs mount")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -102,7 +101,7 @@ func create(ctx context.Context, s *service, r *taskAPI.CreateTaskRequest) (*con
|
|||||||
defer func() {
|
defer func() {
|
||||||
if err != nil && rootFs.Mounted {
|
if err != nil && rootFs.Mounted {
|
||||||
if err2 := mount.UnmountAll(rootfs, 0); err2 != nil {
|
if err2 := mount.UnmountAll(rootfs, 0); err2 != nil {
|
||||||
logrus.WithError(err2).Warn("failed to cleanup rootfs mount")
|
shimLog.WithField("container-type", containerType).WithError(err2).Warn("failed to cleanup rootfs mount")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -12,8 +12,6 @@ import (
|
|||||||
"github.com/containerd/containerd/mount"
|
"github.com/containerd/containerd/mount"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils"
|
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func deleteContainer(ctx context.Context, s *service, c *container) error {
|
func deleteContainer(ctx context.Context, s *service, c *container) error {
|
||||||
@ -42,7 +40,7 @@ func deleteContainer(ctx context.Context, s *service, c *container) error {
|
|||||||
if c.mounted {
|
if c.mounted {
|
||||||
rootfs := path.Join(c.bundle, "rootfs")
|
rootfs := path.Join(c.bundle, "rootfs")
|
||||||
if err := mount.UnmountAll(rootfs, 0); err != nil {
|
if err := mount.UnmountAll(rootfs, 0); err != nil {
|
||||||
logrus.WithError(err).Warn("failed to cleanup rootfs mount")
|
shimLog.WithError(err).Warn("failed to cleanup rootfs mount")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,9 +58,15 @@ var (
|
|||||||
// concrete virtcontainer implementation
|
// concrete virtcontainer implementation
|
||||||
var vci vc.VC = &vc.VCImpl{}
|
var vci vc.VC = &vc.VCImpl{}
|
||||||
|
|
||||||
|
// shimLog is logger for shim package
|
||||||
|
var shimLog = logrus.WithField("source", "containerd-kata-shim-v2")
|
||||||
|
|
||||||
// New returns a new shim service that can be used via GRPC
|
// New returns a new shim service that can be used via GRPC
|
||||||
func New(ctx context.Context, id string, publisher events.Publisher) (cdshim.Shim, error) {
|
func New(ctx context.Context, id string, publisher events.Publisher) (cdshim.Shim, error) {
|
||||||
logger := logrus.WithField("ID", id)
|
shimLog = shimLog.WithFields(logrus.Fields{
|
||||||
|
"sandbox": id,
|
||||||
|
"pid": os.Getpid(),
|
||||||
|
})
|
||||||
// Discard the log before shim init its log output. Otherwise
|
// Discard the log before shim init its log output. Otherwise
|
||||||
// it will output into stdio, from which containerd would like
|
// it will output into stdio, from which containerd would like
|
||||||
// to get the shim's socket address.
|
// to get the shim's socket address.
|
||||||
@ -69,8 +75,8 @@ func New(ctx context.Context, id string, publisher events.Publisher) (cdshim.Shi
|
|||||||
if !opts.Debug {
|
if !opts.Debug {
|
||||||
logrus.SetLevel(logrus.WarnLevel)
|
logrus.SetLevel(logrus.WarnLevel)
|
||||||
}
|
}
|
||||||
vci.SetLogger(ctx, logger)
|
vci.SetLogger(ctx, shimLog)
|
||||||
katautils.SetLogger(ctx, logger, logger.Logger.Level)
|
katautils.SetLogger(ctx, shimLog, shimLog.Logger.Level)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
@ -226,7 +232,7 @@ func (s *service) forward(publisher events.Publisher) {
|
|||||||
err := publisher.Publish(ctx, getTopic(e), e)
|
err := publisher.Publish(ctx, getTopic(e), e)
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Error("post event")
|
shimLog.WithError(err).Error("post event")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -269,7 +275,7 @@ func getTopic(e interface{}) string {
|
|||||||
case *eventstypes.TaskCheckpointed:
|
case *eventstypes.TaskCheckpointed:
|
||||||
return cdruntime.TaskCheckpointedEventTopic
|
return cdruntime.TaskCheckpointedEventTopic
|
||||||
default:
|
default:
|
||||||
logrus.Warnf("no topic for type %#v", e)
|
shimLog.WithField("event-type", e).Warn("no topic for event type")
|
||||||
}
|
}
|
||||||
return cdruntime.TaskUnknownTopic
|
return cdruntime.TaskUnknownTopic
|
||||||
}
|
}
|
||||||
@ -684,7 +690,7 @@ func (s *service) Kill(ctx context.Context, r *taskAPI.KillRequest) (_ *ptypes.E
|
|||||||
// and return directly.
|
// and return directly.
|
||||||
if signum == syscall.SIGKILL || signum == syscall.SIGTERM {
|
if signum == syscall.SIGKILL || signum == syscall.SIGTERM {
|
||||||
if c.status == task.StatusStopped {
|
if c.status == task.StatusStopped {
|
||||||
logrus.WithField("sandbox", s.sandbox.ID()).WithField("Container", c.id).Debug("Container has already been stopped")
|
shimLog.WithField("sandbox", s.sandbox.ID()).WithField("container", c.id).Debug("Container has already been stopped")
|
||||||
return empty, nil
|
return empty, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -697,10 +703,10 @@ func (s *service) Kill(ctx context.Context, r *taskAPI.KillRequest) (_ *ptypes.E
|
|||||||
}
|
}
|
||||||
processID = execs.id
|
processID = execs.id
|
||||||
if processID == "" {
|
if processID == "" {
|
||||||
logrus.WithFields(logrus.Fields{
|
shimLog.WithFields(logrus.Fields{
|
||||||
"sandbox": s.sandbox.ID(),
|
"sandbox": s.sandbox.ID(),
|
||||||
"Container": c.id,
|
"container": c.id,
|
||||||
"ExecID": r.ExecID,
|
"exec-id": r.ExecID,
|
||||||
}).Debug("Id of exec process to be signalled is empty")
|
}).Debug("Id of exec process to be signalled is empty")
|
||||||
return empty, errors.New("The exec process does not exist")
|
return empty, errors.New("The exec process does not exist")
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,6 @@ import (
|
|||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
"github.com/prometheus/common/expfmt"
|
"github.com/prometheus/common/expfmt"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
|
|
||||||
mutils "github.com/kata-containers/kata-containers/src/runtime/pkg/utils"
|
mutils "github.com/kata-containers/kata-containers/src/runtime/pkg/utils"
|
||||||
@ -33,6 +31,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
ifSupportAgentMetricsAPI = true
|
ifSupportAgentMetricsAPI = true
|
||||||
|
shimMgtLog = shimLog.WithField("subsystem", "shim-management")
|
||||||
)
|
)
|
||||||
|
|
||||||
// serveMetrics handle /metrics requests
|
// serveMetrics handle /metrics requests
|
||||||
@ -65,9 +64,9 @@ func (s *service) serveMetrics(w http.ResponseWriter, r *http.Request) {
|
|||||||
// get metrics from agent
|
// get metrics from agent
|
||||||
agentMetrics, err := s.sandbox.GetAgentMetrics()
|
agentMetrics, err := s.sandbox.GetAgentMetrics()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Error("failed GetAgentMetrics")
|
shimMgtLog.WithError(err).Error("failed GetAgentMetrics")
|
||||||
if isGRPCErrorCode(codes.NotFound, err) {
|
if isGRPCErrorCode(codes.NotFound, err) {
|
||||||
logrus.Warn("metrics API not supportted by this agent.")
|
shimMgtLog.Warn("metrics API not supportted by this agent.")
|
||||||
ifSupportAgentMetricsAPI = false
|
ifSupportAgentMetricsAPI = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -119,23 +118,23 @@ func (s *service) startManagementServer(ctx context.Context, ociSpec *specs.Spec
|
|||||||
// metrics socket will under sandbox's bundle path
|
// metrics socket will under sandbox's bundle path
|
||||||
metricsAddress, err := socketAddress(ctx, s.id)
|
metricsAddress, err := socketAddress(ctx, s.id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("failed to create socket address: %s", err.Error())
|
shimMgtLog.WithError(err).Error("failed to create socket address")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
listener, err := cdshim.NewSocket(metricsAddress)
|
listener, err := cdshim.NewSocket(metricsAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("failed to create listener: %s", err.Error())
|
shimMgtLog.WithError(err).Error("failed to create listener")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// write metrics address to filesystem
|
// write metrics address to filesystem
|
||||||
if err := cdshim.WriteAddress("monitor_address", metricsAddress); err != nil {
|
if err := cdshim.WriteAddress("monitor_address", metricsAddress); err != nil {
|
||||||
logrus.Errorf("failed to write metrics address: %s", err.Error())
|
shimMgtLog.WithError(err).Errorf("failed to write metrics address")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Info("kata monitor inited")
|
shimMgtLog.Info("kata management inited")
|
||||||
|
|
||||||
// bind hanlder
|
// bind hanlder
|
||||||
m := http.NewServeMux()
|
m := http.NewServeMux()
|
||||||
|
@ -19,7 +19,6 @@ import (
|
|||||||
vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers"
|
vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/compatoci"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/compatoci"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/oci"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/oci"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func cReap(s *service, status int, id, execid string, exitat time.Time) {
|
func cReap(s *service, status int, id, execid string, exitat time.Time) {
|
||||||
@ -33,18 +32,18 @@ func cReap(s *service, status int, id, execid string, exitat time.Time) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func cleanupContainer(ctx context.Context, sid, cid, bundlePath string) error {
|
func cleanupContainer(ctx context.Context, sid, cid, bundlePath string) error {
|
||||||
logrus.WithField("Service", "Cleanup").WithField("container", cid).Info("Cleanup container")
|
shimLog.WithField("service", "cleanup").WithField("container", cid).Info("Cleanup container")
|
||||||
|
|
||||||
err := vci.CleanupContainer(ctx, sid, cid, true)
|
err := vci.CleanupContainer(ctx, sid, cid, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).WithField("container", cid).Warn("failed to cleanup container")
|
shimLog.WithError(err).WithField("container", cid).Warn("failed to cleanup container")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
rootfs := filepath.Join(bundlePath, "rootfs")
|
rootfs := filepath.Join(bundlePath, "rootfs")
|
||||||
|
|
||||||
if err := mount.UnmountAll(rootfs, 0); err != nil {
|
if err := mount.UnmountAll(rootfs, 0); err != nil {
|
||||||
logrus.WithError(err).WithField("container", cid).Warn("failed to cleanup container rootfs")
|
shimLog.WithError(err).WithField("container", cid).Warn("failed to cleanup container rootfs")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ func wait(s *service, c *container, execID string) (int32, error) {
|
|||||||
|
|
||||||
ret, err := s.sandbox.WaitProcess(c.id, processID)
|
ret, err := s.sandbox.WaitProcess(c.id, processID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).WithFields(logrus.Fields{
|
shimLog.WithError(err).WithFields(logrus.Fields{
|
||||||
"container": c.id,
|
"container": c.id,
|
||||||
"pid": processID,
|
"pid": processID,
|
||||||
}).Error("Wait for process failed")
|
}).Error("Wait for process failed")
|
||||||
@ -61,15 +61,15 @@ func wait(s *service, c *container, execID string) (int32, error) {
|
|||||||
s.monitor <- nil
|
s.monitor <- nil
|
||||||
}
|
}
|
||||||
if err = s.sandbox.Stop(true); err != nil {
|
if err = s.sandbox.Stop(true); err != nil {
|
||||||
logrus.WithField("sandbox", s.sandbox.ID()).Error("failed to stop sandbox")
|
shimLog.WithField("sandbox", s.sandbox.ID()).Error("failed to stop sandbox")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.sandbox.Delete(); err != nil {
|
if err = s.sandbox.Delete(); err != nil {
|
||||||
logrus.WithField("sandbox", s.sandbox.ID()).Error("failed to delete sandbox")
|
shimLog.WithField("sandbox", s.sandbox.ID()).Error("failed to delete sandbox")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if _, err = s.sandbox.StopContainer(c.id, false); err != nil {
|
if _, err = s.sandbox.StopContainer(c.id, false); err != nil {
|
||||||
logrus.WithError(err).WithField("container", c.id).Warn("stop container failed")
|
shimLog.WithError(err).WithField("container", c.id).Warn("stop container failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.status = task.StatusStopped
|
c.status = task.StatusStopped
|
||||||
@ -105,14 +105,14 @@ func watchSandbox(s *service) {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
// sandbox malfunctioning, cleanup as much as we can
|
// sandbox malfunctioning, cleanup as much as we can
|
||||||
logrus.WithError(err).Warn("sandbox stopped unexpectedly")
|
shimLog.WithError(err).Warn("sandbox stopped unexpectedly")
|
||||||
err = s.sandbox.Stop(true)
|
err = s.sandbox.Stop(true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Warn("stop sandbox failed")
|
shimLog.WithError(err).Warn("stop sandbox failed")
|
||||||
}
|
}
|
||||||
err = s.sandbox.Delete()
|
err = s.sandbox.Delete()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Warn("delete sandbox failed")
|
shimLog.WithError(err).Warn("delete sandbox failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range s.containers {
|
for _, c := range s.containers {
|
||||||
@ -120,9 +120,9 @@ func watchSandbox(s *service) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
rootfs := path.Join(c.bundle, "rootfs")
|
rootfs := path.Join(c.bundle, "rootfs")
|
||||||
logrus.WithField("rootfs", rootfs).WithField("id", c.id).Debug("container umount rootfs")
|
shimLog.WithField("rootfs", rootfs).WithField("container", c.id).Debug("container umount rootfs")
|
||||||
if err := mount.UnmountAll(rootfs, 0); err != nil {
|
if err := mount.UnmountAll(rootfs, 0); err != nil {
|
||||||
logrus.WithError(err).Warn("failed to cleanup rootfs mount")
|
shimLog.WithError(err).Warn("failed to cleanup rootfs mount")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,7 +142,7 @@ func watchOOMEvents(ctx context.Context, s *service) {
|
|||||||
default:
|
default:
|
||||||
containerID, err := s.sandbox.GetOOMEvent()
|
containerID, err := s.sandbox.GetOOMEvent()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithField("sandbox", s.sandbox.ID()).WithError(err).Warn("failed to get OOM event from sandbox")
|
shimLog.WithError(err).Warn("failed to get OOM event from sandbox")
|
||||||
// If the GetOOMEvent call is not implemented, then the agent is most likely an older version,
|
// If the GetOOMEvent call is not implemented, then the agent is most likely an older version,
|
||||||
// stop attempting to get OOM events.
|
// stop attempting to get OOM events.
|
||||||
// for rust agent, the response code is not found
|
// for rust agent, the response code is not found
|
||||||
|
@ -30,6 +30,10 @@ var rootfsDir = "rootfs"
|
|||||||
|
|
||||||
var systemMountPrefixes = []string{"/proc", "/sys"}
|
var systemMountPrefixes = []string{"/proc", "/sys"}
|
||||||
|
|
||||||
|
func mountLogger() *logrus.Entry {
|
||||||
|
return virtLog.WithField("subsystem", "mount")
|
||||||
|
}
|
||||||
|
|
||||||
var propagationTypes = map[string]uintptr{
|
var propagationTypes = map[string]uintptr{
|
||||||
"shared": syscall.MS_SHARED,
|
"shared": syscall.MS_SHARED,
|
||||||
"private": syscall.MS_PRIVATE,
|
"private": syscall.MS_PRIVATE,
|
||||||
@ -321,17 +325,17 @@ func bindUnmountContainerRootfs(ctx context.Context, sharedDir, cID string) erro
|
|||||||
|
|
||||||
rootfsDest := filepath.Join(sharedDir, cID, rootfsDir)
|
rootfsDest := filepath.Join(sharedDir, cID, rootfsDir)
|
||||||
if isSymlink(filepath.Join(sharedDir, cID)) || isSymlink(rootfsDest) {
|
if isSymlink(filepath.Join(sharedDir, cID)) || isSymlink(rootfsDest) {
|
||||||
logrus.Warnf("container dir %s is a symlink, malicious guest?", cID)
|
mountLogger().WithField("container", cID).Warnf("container dir is a symlink, malicious guest?")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := syscall.Unmount(rootfsDest, syscall.MNT_DETACH|UmountNoFollow)
|
err := syscall.Unmount(rootfsDest, syscall.MNT_DETACH|UmountNoFollow)
|
||||||
if err == syscall.ENOENT {
|
if err == syscall.ENOENT {
|
||||||
logrus.Warnf("%s: %s", err, rootfsDest)
|
mountLogger().WithError(err).WithField("rootfs-dir", rootfsDest).Warn()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := syscall.Rmdir(rootfsDest); err != nil {
|
if err := syscall.Rmdir(rootfsDest); err != nil {
|
||||||
logrus.WithError(err).WithField("rootfs-dir", rootfsDest).Warn("Could not remove container rootfs dir")
|
mountLogger().WithError(err).WithField("rootfs-dir", rootfsDest).Warn("Could not remove container rootfs dir")
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
@ -344,7 +348,7 @@ func bindUnmountAllRootfs(ctx context.Context, sharedDir string, sandbox *Sandbo
|
|||||||
var errors *merr.Error
|
var errors *merr.Error
|
||||||
for _, c := range sandbox.containers {
|
for _, c := range sandbox.containers {
|
||||||
if isSymlink(filepath.Join(sharedDir, c.id)) {
|
if isSymlink(filepath.Join(sharedDir, c.id)) {
|
||||||
logrus.Warnf("container dir %s is a symlink, malicious guest?", c.id)
|
mountLogger().WithField("container", c.id).Warnf("container dir is a symlink, malicious guest?")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
c.unmountHostMounts()
|
c.unmountHostMounts()
|
||||||
|
Loading…
Reference in New Issue
Block a user