Skip to content

Commit

Permalink
Output logs to stderr, default log level now warn, global --verbose f…
Browse files Browse the repository at this point in the history
…lag (k0sproject#1333)

Signed-off-by: Kimmo Lehto <[email protected]>

- Changes default output level to WARN to reduce verbosity 
- All logging now goes to stderr except from k0s controller, k0s worker and k0s api which are still defaulted to INFO level and output to stdout because they stay in the foreground
- Adds --verbose|-v for displaying progress information (aka, change loglevel to INFO)

Rationale:

- Almost all k0s output is progress information, not "payload". Progress information and diagnostics should not be included in the output when redirecting or piping.
- Warnings and errors belong to stderr, not stdout.
  • Loading branch information
kke authored Dec 22, 2021
1 parent 6d1e12c commit 987491a
Show file tree
Hide file tree
Showing 26 changed files with 54 additions and 64 deletions.
3 changes: 0 additions & 3 deletions cmd/airgap/listimages.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package airgap
import (
"fmt"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/k0sproject/k0s/pkg/airgap"
Expand All @@ -33,8 +32,6 @@ func NewAirgapListImagesCmd() *cobra.Command {
Short: "List image names and version needed for air-gap install",
Example: `k0s airgap list-images`,
RunE: func(cmd *cobra.Command, args []string) error {
// we don't need warning messages in case of default config
logrus.SetLevel(logrus.ErrorLevel)
c := CmdOpts(config.GetCmdOpts())
cfg, err := config.GetYamlFromFile(c.CfgFile, c.K0sVars)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func NewAPICmd() *cobra.Command {
Use: "api",
Short: "Run the controller api",
RunE: func(cmd *cobra.Command, args []string) error {
logrus.SetLevel(logrus.InfoLevel)
logrus.SetOutput(os.Stdout)

c := CmdOpts(config.GetCmdOpts())
cfg, err := config.GetNodeConfig(c.CfgFile, c.K0sVars)
if err != nil {
Expand Down
9 changes: 1 addition & 8 deletions cmd/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,8 @@ func NewBackupCmd() *cobra.Command {
}

func (c *CmdOpts) backup() error {
logger := logrus.New()
textFormatter := new(logrus.TextFormatter)
textFormatter.ForceColors = true
textFormatter.DisableTimestamp = true

logger.SetFormatter(textFormatter)

if os.Geteuid() != 0 {
logger.Fatal("this command must be run as root!")
logrus.Fatal("this command must be run as root!")
}

if !dir.IsDirectory(savePath) {
Expand Down
3 changes: 3 additions & 0 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func NewControllerCmd() *cobra.Command {
$ k0s controller --token-file [path_to_file]
Note: Token can be passed either as a CLI argument or as a flag`,
RunE: func(cmd *cobra.Command, args []string) error {
logrus.SetLevel(logrus.InfoLevel)
logrus.SetOutput(os.Stdout)

c := CmdOpts(config.GetCmdOpts())
if len(args) > 0 {
c.TokenArg = args[0]
Expand Down
10 changes: 3 additions & 7 deletions cmd/etcd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package etcd

import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/k0sproject/k0s/pkg/config"
Expand All @@ -41,12 +42,7 @@ func etcdListCmd() *cobra.Command {
if err != nil {
return fmt.Errorf("can't list etcd cluster members: %v", err)
}
l := logrus.New()
l.SetFormatter(&logrus.JSONFormatter{})

l.WithField("members", members).
Info("done")
return nil
return json.NewEncoder(os.Stdout).Encode(map[string]interface{}{"members": members})
},
}
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubeconfig/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Note: A certificate once signed cannot be revoked for a particular user`,

func (c *CmdOpts) getAPIURL() (string, error) {
// Disable logrus
logrus.SetLevel(logrus.FatalLevel)
logrus.SetLevel(logrus.WarnLevel)

cfg, err := config.GetNodeConfig(c.CfgFile, c.K0sVars)
if err != nil {
Expand Down
13 changes: 4 additions & 9 deletions cmd/reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,13 @@ func NewResetCmd() *cobra.Command {
}

func (c *CmdOpts) reset() error {
logger := logrus.New()
textFormatter := new(logrus.TextFormatter)
textFormatter.DisableTimestamp = true

logger.SetFormatter(textFormatter)

if os.Geteuid() != 0 {
logger.Fatal("this command must be run as root!")
logrus.Fatal("this command must be run as root!")
}

k0sStatus, _ := install.GetStatusInfo(config.StatusSocket)
if k0sStatus != nil && k0sStatus.Pid != 0 {
logger.Fatal("k0s seems to be running! please stop k0s before reset.")
logrus.Fatal("k0s seems to be running! please stop k0s before reset.")
}

// Get Cleanup Config
Expand All @@ -72,8 +66,9 @@ func (c *CmdOpts) reset() error {
}

err = cfg.Cleanup()
logrus.Info("k0s cleanup operations done.")
logrus.Warn("To ensure a full reset, a node reboot is recommended.")

logger.Info("k0s cleanup operations done. To ensure a full reset, a node reboot is recommended.")
return err
}

Expand Down
9 changes: 1 addition & 8 deletions cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,13 @@ func NewRestoreCmd() *cobra.Command {
}

func (c *CmdOpts) restore(path string) error {
logger := logrus.New()
textFormatter := new(logrus.TextFormatter)
textFormatter.ForceColors = true
textFormatter.DisableTimestamp = true

logger.SetFormatter(textFormatter)

if os.Geteuid() != 0 {
return fmt.Errorf("this command must be run as root")
}

k0sStatus, _ := install.GetStatusInfo(config.StatusSocket)
if k0sStatus != nil && k0sStatus.Pid != 0 {
logger.Fatal("k0s seems to be running! k0s must be down during the restore operation.")
logrus.Fatal("k0s seems to be running! k0s must be down during the restore operation.")
}

if !file.Exists(path) {
Expand Down
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func NewRootCmd() *cobra.Command {
Short: "k0s - Zero Friction Kubernetes",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
c := cliOpts(config.GetCmdOpts())

if c.Verbose {
logrus.SetLevel(logrus.InfoLevel)
}

// set DEBUG from env, or from command flag
if viper.GetString("debug") != "" || c.Debug {
logrus.SetLevel(logrus.DebugLevel)
Expand Down Expand Up @@ -132,6 +137,7 @@ func newDefaultConfigCmd() *cobra.Command {
Use: "default-config",
Short: "Output the default k0s configuration yaml to stdout",
RunE: func(cmd *cobra.Command, args []string) error {
logrus.SetLevel(logrus.ErrorLevel)
c := cliOpts(config.GetCmdOpts())
if err := c.buildConfig(); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ k0s token create --role worker --expiry 10m //sets expiration time to 10 minute
PreRunE: checkCreateTokenRole,
RunE: func(cmd *cobra.Command, args []string) error {
// Disable logrus for token commands
logrus.SetLevel(logrus.FatalLevel)
logrus.SetLevel(logrus.WarnLevel)
c := CmdOpts(config.GetCmdOpts())
cfg, err := config.GetNodeConfig(c.CfgFile, c.K0sVars)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func NewWorkerCmd() *cobra.Command {
$ k0s worker --token-file [path_to_file]
Note: Token can be passed either as a CLI argument or as a flag`,
RunE: func(cmd *cobra.Command, args []string) error {
logrus.SetLevel(logrus.InfoLevel)
logrus.SetOutput(os.Stdout)

c := CmdOpts(config.GetCmdOpts())
if len(args) > 0 {
c.TokenArg = args[0]
Expand Down
2 changes: 1 addition & 1 deletion inttest/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (s *CliSuite) TestK0sCliKubectlAndResetCommand() {
err = s.WaitForKubeAPI(s.ControllerNode(0))
s.Require().NoError(err)

output, err := ssh.ExecWithOutput("k0s kubectl get namespaces -o json")
output, err := ssh.ExecWithOutput("k0s kubectl get namespaces -o json 2>/dev/null")
s.Require().NoError(err)

namespaces := &K8sNamespaces{}
Expand Down
6 changes: 3 additions & 3 deletions inttest/common/footloosesuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (s *FootlooseSuite) GetJoinToken(role string, extraArgs ...string) (string,
return "", err
}
defer ssh.Disconnect()
token, err := ssh.ExecWithOutput(fmt.Sprintf("%s token create --role=%s %s", s.K0sFullPath, role, strings.Join(extraArgs, " ")))
token, err := ssh.ExecWithOutput(fmt.Sprintf("%s token create --role=%s %s 2>/dev/null", s.K0sFullPath, role, strings.Join(extraArgs, " ")))
if err != nil {
return "", fmt.Errorf("can't get join token: %v", err)
}
Expand Down Expand Up @@ -535,7 +535,7 @@ func (s *FootlooseSuite) GetKubeClientConfig(node string, k0sKubeconfigArgs ...s
}
defer ssh.Disconnect()

kubeConfigCmd := fmt.Sprintf("%s kubeconfig admin %s", s.K0sFullPath, strings.Join(k0sKubeconfigArgs, " "))
kubeConfigCmd := fmt.Sprintf("%s kubeconfig admin %s 2>/dev/null", s.K0sFullPath, strings.Join(k0sKubeconfigArgs, " "))
kubeConf, err := ssh.ExecWithOutput(kubeConfigCmd)
if err != nil {
return nil, err
Expand Down Expand Up @@ -571,7 +571,7 @@ func (s *FootlooseSuite) GetKubeConfig(node string, k0sKubeconfigArgs ...string)
}
defer ssh.Disconnect()

kubeConfigCmd := fmt.Sprintf("%s kubeconfig admin %s", s.K0sFullPath, strings.Join(k0sKubeconfigArgs, " "))
kubeConfigCmd := fmt.Sprintf("%s kubeconfig admin %s 2>/dev/null", s.K0sFullPath, strings.Join(k0sKubeconfigArgs, " "))
kubeConf, err := ssh.ExecWithOutput(kubeConfigCmd)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion inttest/ctr/ctr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *CtrSuite) TestK0sCtrCommand() {
err = s.WaitForNodeReady(s.ControllerNode(0), kc)
s.NoError(err)

output, err := ssh.ExecWithOutput("k0s ctr namespaces list")
output, err := ssh.ExecWithOutput("k0s ctr namespaces list 2>/dev/null")
s.Require().NoError(err)

flatOutput := removeRedundantSpaces(output)
Expand Down
4 changes: 2 additions & 2 deletions inttest/hacontrolplane/hacontrolplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (s *HAControlplaneSuite) getMembers(fromControllerIdx int) map[string]strin
sshCon, err := s.SSH(s.ControllerNode(fromControllerIdx))
s.NoError(err)
defer sshCon.Disconnect()
output, err := sshCon.ExecWithOutput("k0s etcd member-list")
output = lastLine(output)
output, err := sshCon.ExecWithOutput("k0s etcd member-list 2>/dev/null")
s.T().Logf("k0s etcd member-list output: %s", output)
s.NoError(err)

members := struct {
Expand Down
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ import (
//go:generate make generate-bindata

func init() {

logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.InfoLevel)

logrus.SetOutput(os.Stderr)
logrus.SetLevel(logrus.WarnLevel)
customFormatter := new(logrus.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
customFormatter.FullTimestamp = true
Expand Down
4 changes: 2 additions & 2 deletions pkg/backup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c configurationStep) Name() string {
func (c configurationStep) Backup() (StepResult, error) {
_, err := os.Stat(c.path)
if os.IsNotExist(err) {
logrus.Info("default k0s.yaml is used, do not back it up")
logrus.Warn("default k0s.yaml is used, do not back it up")
return StepResult{}, nil
}
if err != nil {
Expand All @@ -45,7 +45,7 @@ func (c configurationStep) Restore(restoreFrom, restoreTo string) error {
objectPathInArchive := path.Join(restoreFrom, "k0s.yaml")

if !file.Exists(objectPathInArchive) {
logrus.Infof("%s does not exist in the backup file", objectPathInArchive)
logrus.Debugf("%s does not exist in the backup file", objectPathInArchive)
return nil
}
logrus.Infof("Previously used k0s.yaml saved under the data directory `%s`", restoreTo)
Expand Down
4 changes: 2 additions & 2 deletions pkg/backup/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (d FileSystemStep) Name() string {
func (d FileSystemStep) Backup() (StepResult, error) {
s, err := os.Stat(d.path)
if os.IsNotExist(err) {
logrus.Infof("Path `%s` does not exist, skipping...", d.path)
logrus.Debugf("Path `%s` does not exist, skipping...", d.path)
return StepResult{}, nil
}
if err != nil {
Expand Down Expand Up @@ -58,7 +58,7 @@ func (d FileSystemStep) Restore(restoreFrom, restoreTo string) error {
objectPathInRestored := path.Join(restoreTo, childName)
stat, err := os.Stat(objectPathInArchive)
if os.IsNotExist(err) {
logrus.Infof("Path `%s` not found in the archive, skipping...", objectPathInArchive)
logrus.Debugf("Path `%s` not found in the archive, skipping...", objectPathInArchive)
return nil
}
logrus.Infof("restoring from `%s` to `%s`", objectPathInArchive, objectPathInRestored)
Expand Down
5 changes: 2 additions & 3 deletions pkg/cleanup/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ func (u *users) Name() string {

// Run removes all controller users that are present on the host
func (u *users) Run() error {
logger := logrus.New()
cfg, err := config.GetNodeConfig(u.Config.cfgFile, u.Config.k0sVars)
if err != nil {
logger.Errorf("failed to get cluster setup: %v", err)
logrus.Errorf("failed to get cluster setup: %v", err)
return nil
}
if err := install.DeleteControllerUsers(cfg); err != nil {
// don't fail, just notify on delete error
logger.Infof("failed to delete controller users: %v", err)
logrus.Warnf("failed to delete controller users: %v", err)
}
return nil
}
6 changes: 3 additions & 3 deletions pkg/component/controller/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (e *Etcd) syncEtcdConfig(peerURL, etcdCaCert, etcdCaCertKey string) ([]stri
var etcdResponse v1beta1.EtcdResponse
var err error
for i := 0; i < 20; i++ {
logrus.Infof("trying to sync etcd config")
logrus.Debugf("trying to sync etcd config")
etcdResponse, err = e.JoinClient.JoinEtcd(peerURL)
if err == nil {
break
Expand All @@ -101,7 +101,7 @@ func (e *Etcd) syncEtcdConfig(peerURL, etcdCaCert, etcdCaCertKey string) ([]stri
return nil, err
}

logrus.Infof("got cluster info: %v", etcdResponse.InitialCluster)
logrus.Debugf("got cluster info: %v", etcdResponse.InitialCluster)
// Write etcd ca cert&key
if file.Exists(etcdCaCert) && file.Exists(etcdCaCertKey) {
logrus.Warnf("etcd ca certs already exists, not gonna overwrite. If you wish to re-sync them, delete the existing ones.")
Expand Down Expand Up @@ -184,7 +184,7 @@ func (e *Etcd) Run(ctx context.Context) error {
args["--auth-token"] = auth
}

logrus.Infof("starting etcd with args: %v", args)
logrus.Debugf("starting etcd with args: %v", args)

e.supervisor = supervisor.Supervisor{
Name: "etcd",
Expand Down
2 changes: 1 addition & 1 deletion pkg/component/worker/calico_installer_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func getSourceVip() (string, error) {
err := retry.Do(func() error {
ep, err := hcsshim.GetHNSEndpointByName("Calico_ep")
if err != nil {
logrus.WithError(err).Warning("can't get Calico_ep endpoint")
logrus.WithError(err).Warn("can't get Calico_ep endpoint")
return err
}
vip = ep.IPAddress.String()
Expand Down
2 changes: 1 addition & 1 deletion pkg/component/worker/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (k *Kubelet) Run(ctx context.Context) error {
args.Merge(extras)
}

logrus.Infof("starting kubelet with args: %v", args)
logrus.Debugf("starting kubelet with args: %v", args)
k.supervisor = supervisor.Supervisor{
Name: cmd,
BinPath: assets.BinPath(cmd, k.K0sVars.BinDir),
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
StatusSocket string
K0sVars constant.CfgVars
workerOpts WorkerOptions
Verbose bool
controllerOpts ControllerOptions
)

Expand All @@ -54,6 +55,7 @@ type CLIOptions struct {
K0sVars constant.CfgVars
KubeClient k8s.Interface
Logging map[string]string // merged outcome of default log levels and cmdLoglevels
Verbose bool
}

// Shared controller cli flags
Expand Down Expand Up @@ -104,6 +106,7 @@ func GetPersistentFlagSet() *pflag.FlagSet {
flagset := &pflag.FlagSet{}
flagset.StringVarP(&CfgFile, "config", "c", "", "config file, use '-' to read the config from stdin")
flagset.BoolVarP(&Debug, "debug", "d", false, "Debug logging (default: false)")
flagset.BoolVarP(&Verbose, "verbose", "v", false, "Verbose logging (default: false)")
flagset.StringVar(&DataDir, "data-dir", "", "Data Directory for k0s (default: /var/lib/k0s). DO NOT CHANGE for an existing setup, things will break!")
flagset.StringVar(&StatusSocket, "status-socket", filepath.Join(K0sVars.RunDir, "status.sock"), "Full file path to the socket file.")
flagset.StringVar(&DebugListenOn, "debugListenOn", ":6060", "Http listenOn for Debug pprof handler")
Expand Down Expand Up @@ -189,6 +192,7 @@ func GetCmdOpts() CLIOptions {

CfgFile: CfgFile,
Debug: Debug,
Verbose: Verbose,
DefaultLogLevels: DefaultLogLevels(),
K0sVars: K0sVars,
DebugListenOn: DebugListenOn,
Expand Down
Loading

0 comments on commit 987491a

Please sign in to comment.