diff --git a/pkg/cli/agent/agent.go b/pkg/cli/agent/agent.go index 7afe5ec996..c7aff6829c 100644 --- a/pkg/cli/agent/agent.go +++ b/pkg/cli/agent/agent.go @@ -37,6 +37,9 @@ func readToken(path string) (string, error) { } func Run(ctx *cli.Context) error { + if err := cmds.InitLogging(); err != nil { + return err + } if os.Getuid() != 0 { return fmt.Errorf("agent must be ran as root") } diff --git a/pkg/cli/cmds/agent.go b/pkg/cli/cmds/agent.go index e1364dda12..0368e13609 100644 --- a/pkg/cli/cmds/agent.go +++ b/pkg/cli/cmds/agent.go @@ -109,6 +109,10 @@ func NewAgentCommand(action func(ctx *cli.Context) error) cli.Command { UsageText: appName + " agent [OPTIONS]", Action: action, Flags: []cli.Flag{ + VLevel, + VModule, + LogFile, + AlsoLogToStderr, cli.StringFlag{ Name: "token,t", Usage: "Token to use for authentication", diff --git a/pkg/cli/cmds/log.go b/pkg/cli/cmds/log.go new file mode 100644 index 0000000000..cf86687dd3 --- /dev/null +++ b/pkg/cli/cmds/log.go @@ -0,0 +1,100 @@ +package cmds + +import ( + "flag" + "fmt" + "io" + "os" + "strconv" + "time" + + "github.com/docker/docker/pkg/reexec" + "github.com/natefinch/lumberjack" + "github.com/urfave/cli" +) + +type Log struct { + VLevel int + VModule string + LogFile string + AlsoLogToStderr bool +} + +var ( + LogConfig Log + + VLevel = cli.IntFlag{ + Name: "v", + Usage: "Number for the log level verbosity", + Destination: &LogConfig.VLevel, + } + VModule = cli.StringFlag{ + Name: "vmodule", + Usage: "Comma-separated list of pattern=N settings for file-filtered logging", + Destination: &LogConfig.VModule, + } + LogFile = cli.StringFlag{ + Name: "log,l", + Usage: "Log to file", + Destination: &LogConfig.LogFile, + } + AlsoLogToStderr = cli.BoolFlag{ + Name: "alsologtostderr", + Usage: "Log to standard error as well as file (if set)", + Destination: &LogConfig.AlsoLogToStderr, + } +) + +func InitLogging() error { + if LogConfig.LogFile != "" && os.Getenv("_K3S_LOG_REEXEC_") == "" { + return runWithLogging() + } + + if err := checkUnixTimestamp(); err != nil { + return err + } + + setupLogging() + return nil +} + +func checkUnixTimestamp() error { + timeNow := time.Now() + // check if time before 01/01/1980 + if timeNow.Before(time.Unix(315532800, 0)) { + return fmt.Errorf("server time isn't set properly: %v", timeNow) + } + return nil +} + +func runWithLogging() error { + var ( + l io.Writer + ) + l = &lumberjack.Logger{ + Filename: LogConfig.LogFile, + MaxSize: 50, + MaxBackups: 3, + MaxAge: 28, + Compress: true, + } + if LogConfig.AlsoLogToStderr { + l = io.MultiWriter(l, os.Stderr) + } + + args := append([]string{"k3s"}, os.Args[1:]...) + cmd := reexec.Command(args...) + cmd.Env = os.Environ() + cmd.Env = append(cmd.Env, "_K3S_LOG_REEXEC_=true") + cmd.Stderr = l + cmd.Stdout = l + cmd.Stdin = os.Stdin + return cmd.Run() +} + +func setupLogging() { + flag.Set("v", strconv.Itoa(LogConfig.VLevel)) + flag.Set("vmodule", LogConfig.VModule) + flag.Set("alsologtostderr", strconv.FormatBool(debug)) + flag.Set("logtostderr", strconv.FormatBool(!debug)) +} diff --git a/pkg/cli/cmds/server.go b/pkg/cli/cmds/server.go index d0279529b8..43616fb7d8 100644 --- a/pkg/cli/cmds/server.go +++ b/pkg/cli/cmds/server.go @@ -5,7 +5,6 @@ import ( ) type Server struct { - Log string ClusterCIDR string ClusterSecret string ServiceCIDR string @@ -43,6 +42,10 @@ func NewServerCommand(action func(*cli.Context) error) cli.Command { UsageText: appName + " server [OPTIONS]", Action: action, Flags: []cli.Flag{ + VLevel, + VModule, + LogFile, + AlsoLogToStderr, cli.StringFlag{ Name: "bind-address", Usage: "k3s bind address (default: localhost)", @@ -70,11 +73,6 @@ func NewServerCommand(action func(*cli.Context) error) cli.Command { Usage: "Do not run a local agent and register a local kubelet", Destination: &ServerConfig.DisableAgent, }, - cli.StringFlag{ - Name: "log,l", - Usage: "Log to file", - Destination: &ServerConfig.Log, - }, cli.StringFlag{ Name: "cluster-cidr", Usage: "Network CIDR to use for pod IPs", diff --git a/pkg/cli/server/server.go b/pkg/cli/server/server.go index fe4d39b1ca..ded7c8be4a 100644 --- a/pkg/cli/server/server.go +++ b/pkg/cli/server/server.go @@ -2,19 +2,15 @@ package server import ( "context" - "flag" "fmt" net2 "net" "os" "path/filepath" "strings" - "time" "github.com/rancher/k3s/pkg/netutil" systemd "github.com/coreos/go-systemd/daemon" - "github.com/docker/docker/pkg/reexec" - "github.com/natefinch/lumberjack" "github.com/pkg/errors" "github.com/rancher/k3s/pkg/agent" "github.com/rancher/k3s/pkg/cli/cmds" @@ -33,34 +29,10 @@ import ( _ "github.com/mattn/go-sqlite3" // ensure we have sqlite ) -func setupLogging(app *cli.Context) { - if !app.GlobalBool("debug") { - flag.Set("stderrthreshold", "WARNING") - flag.Set("alsologtostderr", "false") - flag.Set("logtostderr", "false") - } -} - -func runWithLogging(app *cli.Context, cfg *cmds.Server) error { - l := &lumberjack.Logger{ - Filename: cfg.Log, - MaxSize: 50, - MaxBackups: 3, - MaxAge: 28, - Compress: true, - } - - args := append([]string{"k3s"}, os.Args[1:]...) - cmd := reexec.Command(args...) - cmd.Env = os.Environ() - cmd.Env = append(cmd.Env, "_RIO_REEXEC_=true") - cmd.Stderr = l - cmd.Stdout = l - cmd.Stdin = os.Stdin - return cmd.Run() -} - func Run(app *cli.Context) error { + if err := cmds.InitLogging(); err != nil { + return err + } return run(app, &cmds.ServerConfig) } @@ -69,16 +41,6 @@ func run(app *cli.Context, cfg *cmds.Server) error { err error ) - if cfg.Log != "" && os.Getenv("_RIO_REEXEC_") == "" { - return runWithLogging(app, cfg) - } - - if err := checkUnixTimestamp(); err != nil { - return err - } - - setupLogging(app) - if !cfg.DisableAgent && os.Getuid() != 0 && !cfg.Rootless { return fmt.Errorf("must run as root unless --disable-agent is specified") } @@ -227,12 +189,3 @@ func knownIPs(ips []string) []string { } return ips } - -func checkUnixTimestamp() error { - timeNow := time.Now() - // check if time before 01/01/1980 - if timeNow.Before(time.Unix(315532800, 0)) { - return fmt.Errorf("server time isn't set properly: %v", timeNow) - } - return nil -}