Skip to content

Commit

Permalink
Cleanup logging
Browse files Browse the repository at this point in the history
  • Loading branch information
erikwilson committed Aug 8, 2019
1 parent 862c7ee commit 30e050a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 56 deletions.
3 changes: 3 additions & 0 deletions pkg/cli/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/cli/cmds/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
100 changes: 100 additions & 0 deletions pkg/cli/cmds/log.go
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 6 deletions pkg/cli/cmds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
)

type Server struct {
Log string
ClusterCIDR string
ClusterSecret string
ServiceCIDR string
Expand Down Expand Up @@ -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)",
Expand Down Expand Up @@ -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",
Expand Down
53 changes: 3 additions & 50 deletions pkg/cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}

Expand All @@ -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")
}
Expand Down Expand Up @@ -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
}

0 comments on commit 30e050a

Please sign in to comment.