diff --git a/pkg/agent/config/config.go b/pkg/agent/config/config.go index 43533415cb..4b3d7af05e 100644 --- a/pkg/agent/config/config.go +++ b/pkg/agent/config/config.go @@ -171,6 +171,9 @@ func get(envInfo *cmds.Agent) (*config.Node, error) { nodeConfig.Containerd.Config = filepath.Join(envInfo.DataDir, "etc/containerd/config.toml") nodeConfig.Containerd.Root = filepath.Join(envInfo.DataDir, "containerd") nodeConfig.Containerd.Opt = filepath.Join(envInfo.DataDir, "containerd") + if !envInfo.Debug { + nodeConfig.Containerd.Log = filepath.Join(envInfo.DataDir, "containerd/containerd.log") + } nodeConfig.Containerd.State = "/run/k3s/containerd" nodeConfig.Containerd.Address = filepath.Join(nodeConfig.Containerd.State, "containerd.sock") nodeConfig.ServerAddress = serverURLParsed.Host diff --git a/pkg/agent/containerd/containerd.go b/pkg/agent/containerd/containerd.go index 156eb4fe50..5b5b8ca948 100644 --- a/pkg/agent/containerd/containerd.go +++ b/pkg/agent/containerd/containerd.go @@ -3,12 +3,14 @@ package containerd import ( "context" "fmt" + "io" "os" "os/exec" "strings" "syscall" "time" + "github.com/natefinch/lumberjack" util2 "github.com/rancher/k3s/pkg/agent/util" "github.com/rancher/k3s/pkg/daemons/config" "github.com/sirupsen/logrus" @@ -56,15 +58,30 @@ func Run(ctx context.Context, cfg *config.Node) error { return err } - if logrus.GetLevel() >= logrus.DebugLevel { - args = append(args, "-l", "debug") + if os.Getenv("CONTAINERD_LOG_LEVEL") != "" { + args = append(args, "-l", "CONTAINERD_LOG_LEVEL") + } + + stdOut := io.Writer(os.Stdout) + stdErr := io.Writer(os.Stderr) + + if cfg.Containerd.Log != "" { + logrus.Infof("Logging containerd to %s", cfg.Containerd.Log) + stdOut = &lumberjack.Logger{ + Filename: cfg.Containerd.Log, + MaxSize: 50, + MaxBackups: 3, + MaxAge: 28, + Compress: true, + } + stdErr = stdOut } go func() { logrus.Infof("Running containerd %s", config.ArgString(args[1:])) cmd := exec.Command(args[0], args[1:]...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stdout = stdOut + cmd.Stderr = stdErr cmd.SysProcAttr = &syscall.SysProcAttr{ Pdeathsig: syscall.SIGKILL, } diff --git a/pkg/cli/agent/agent.go b/pkg/cli/agent/agent.go index 1d515e8f5d..83e8632763 100644 --- a/pkg/cli/agent/agent.go +++ b/pkg/cli/agent/agent.go @@ -31,6 +31,7 @@ func Run(ctx *cli.Context) error { } cfg := cmds.AgentConfig + cfg.Debug = ctx.GlobalBool("debug") cfg.DataDir = dataDir contextCtx := signal.SigTermCancelContext(context.Background()) diff --git a/pkg/cli/server/server.go b/pkg/cli/server/server.go index 2af3badc1c..237cf49d80 100644 --- a/pkg/cli/server/server.go +++ b/pkg/cli/server/server.go @@ -8,8 +8,6 @@ import ( "path/filepath" "strings" - "k8s.io/apimachinery/pkg/util/net" - "github.com/docker/docker/pkg/reexec" "github.com/natefinch/lumberjack" "github.com/rancher/k3s/pkg/agent" @@ -18,6 +16,7 @@ import ( "github.com/rancher/norman/signal" "github.com/sirupsen/logrus" "github.com/urfave/cli" + "k8s.io/apimachinery/pkg/util/net" _ "github.com/mattn/go-sqlite3" // ensure we have sqlite ) @@ -95,17 +94,18 @@ func run(app *cli.Context, cfg *cmds.Server) error { return err } + logrus.Info("k3s is up and running") + if cfg.DisableAgent { <-ctx.Done() return nil } - logFile := filepath.Join(serverConfig.ControlConfig.DataDir, "agent/agent.log") url := fmt.Sprintf("https://localhost:%d", serverConfig.TLSConfig.HTTPSPort) - logrus.Infof("Agent starting, logging to %s", logFile) token := server.FormatToken(serverConfig.ControlConfig.Runtime.NodeToken, certs) agentConfig := cmds.AgentConfig + agentConfig.Debug = app.GlobalBool("bool") agentConfig.DataDir = filepath.Dir(serverConfig.ControlConfig.DataDir) agentConfig.ServerURL = url agentConfig.Token = token diff --git a/pkg/daemons/config/types.go b/pkg/daemons/config/types.go index 2297d1d80c..7c7cfad6fd 100644 --- a/pkg/daemons/config/types.go +++ b/pkg/daemons/config/types.go @@ -23,6 +23,7 @@ type Node struct { type Containerd struct { Address string + Log string Root string State string Config string