Browse Source

Cleanup logging

pull/718/head
Erik Wilson 5 years ago
parent
commit
30e050a692
  1. 3
      pkg/cli/agent/agent.go
  2. 4
      pkg/cli/cmds/agent.go
  3. 100
      pkg/cli/cmds/log.go
  4. 10
      pkg/cli/cmds/server.go
  5. 53
      pkg/cli/server/server.go

3
pkg/cli/agent/agent.go

@ -37,6 +37,9 @@ func readToken(path string) (string, error) {
} }
func Run(ctx *cli.Context) error { func Run(ctx *cli.Context) error {
if err := cmds.InitLogging(); err != nil {
return err
}
if os.Getuid() != 0 { if os.Getuid() != 0 {
return fmt.Errorf("agent must be ran as root") return fmt.Errorf("agent must be ran as root")
} }

4
pkg/cli/cmds/agent.go

@ -109,6 +109,10 @@ func NewAgentCommand(action func(ctx *cli.Context) error) cli.Command {
UsageText: appName + " agent [OPTIONS]", UsageText: appName + " agent [OPTIONS]",
Action: action, Action: action,
Flags: []cli.Flag{ Flags: []cli.Flag{
VLevel,
VModule,
LogFile,
AlsoLogToStderr,
cli.StringFlag{ cli.StringFlag{
Name: "token,t", Name: "token,t",
Usage: "Token to use for authentication", Usage: "Token to use for authentication",

100
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))
}

10
pkg/cli/cmds/server.go

@ -5,7 +5,6 @@ import (
) )
type Server struct { type Server struct {
Log string
ClusterCIDR string ClusterCIDR string
ClusterSecret string ClusterSecret string
ServiceCIDR string ServiceCIDR string
@ -43,6 +42,10 @@ func NewServerCommand(action func(*cli.Context) error) cli.Command {
UsageText: appName + " server [OPTIONS]", UsageText: appName + " server [OPTIONS]",
Action: action, Action: action,
Flags: []cli.Flag{ Flags: []cli.Flag{
VLevel,
VModule,
LogFile,
AlsoLogToStderr,
cli.StringFlag{ cli.StringFlag{
Name: "bind-address", Name: "bind-address",
Usage: "k3s bind address (default: localhost)", 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", Usage: "Do not run a local agent and register a local kubelet",
Destination: &ServerConfig.DisableAgent, Destination: &ServerConfig.DisableAgent,
}, },
cli.StringFlag{
Name: "log,l",
Usage: "Log to file",
Destination: &ServerConfig.Log,
},
cli.StringFlag{ cli.StringFlag{
Name: "cluster-cidr", Name: "cluster-cidr",
Usage: "Network CIDR to use for pod IPs", Usage: "Network CIDR to use for pod IPs",

53
pkg/cli/server/server.go

@ -2,19 +2,15 @@ package server
import ( import (
"context" "context"
"flag"
"fmt" "fmt"
net2 "net" net2 "net"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/rancher/k3s/pkg/netutil" "github.com/rancher/k3s/pkg/netutil"
systemd "github.com/coreos/go-systemd/daemon" systemd "github.com/coreos/go-systemd/daemon"
"github.com/docker/docker/pkg/reexec"
"github.com/natefinch/lumberjack"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rancher/k3s/pkg/agent" "github.com/rancher/k3s/pkg/agent"
"github.com/rancher/k3s/pkg/cli/cmds" "github.com/rancher/k3s/pkg/cli/cmds"
@ -33,34 +29,10 @@ import (
_ "github.com/mattn/go-sqlite3" // ensure we have sqlite _ "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 { func Run(app *cli.Context) error {
if err := cmds.InitLogging(); err != nil {
return err
}
return run(app, &cmds.ServerConfig) return run(app, &cmds.ServerConfig)
} }
@ -69,16 +41,6 @@ func run(app *cli.Context, cfg *cmds.Server) error {
err 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 { if !cfg.DisableAgent && os.Getuid() != 0 && !cfg.Rootless {
return fmt.Errorf("must run as root unless --disable-agent is specified") return fmt.Errorf("must run as root unless --disable-agent is specified")
} }
@ -227,12 +189,3 @@ func knownIPs(ips []string) []string {
} }
return ips 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
}

Loading…
Cancel
Save