Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement "--log_file" and "--log_dir" for klog #9592

Merged
merged 1 commit into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions cmd/minikube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package cmd

import (
goflag "flag"
"flag"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -156,6 +156,18 @@ func usageTemplate() string {
}

func init() {
klog.InitFlags(nil)
// preset logtostderr and alsologtostderr only for test runs, for normal runs consider flags in main()
if strings.HasPrefix(filepath.Base(os.Args[0]), "e2e-") || strings.HasSuffix(os.Args[0], "test") {
if err := flag.Set("logtostderr", "false"); err != nil {
klog.Warningf("Unable to set default flag value for logtostderr: %v", err)
}
if err := flag.Set("alsologtostderr", "false"); err != nil {
klog.Warningf("Unable to set default flag value for alsologtostderr: %v", err)
}
}
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) // avoid `generate-docs_test.go` complaining about "Docs are not updated"

RootCmd.PersistentFlags().StringP(config.ProfileName, "p", constants.DefaultClusterName, `The name of the minikube VM being used. This can be set to allow having multiple instances of minikube independently.`)
RootCmd.PersistentFlags().StringP(configCmd.Bootstrapper, "b", "kubeadm", "The name of the cluster bootstrapper that will set up the Kubernetes cluster.")

Expand Down Expand Up @@ -223,15 +235,6 @@ func init() {
RootCmd.AddCommand(completionCmd)
templates.ActsAsRootCommand(RootCmd, []string{"options"}, groups...)

klog.InitFlags(nil)
if err := goflag.Set("logtostderr", "false"); err != nil {
klog.Warningf("Unable to set default flag value for logtostderr: %v", err)
}
if err := goflag.Set("alsologtostderr", "false"); err != nil {
klog.Warningf("Unable to set default flag value for alsologtostderr: %v", err)
}

pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
if err := viper.BindPFlags(RootCmd.PersistentFlags()); err != nil {
exit.Error(reason.InternalBindFlags, "Unable to bind flags", err)
}
Expand Down
38 changes: 34 additions & 4 deletions cmd/minikube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ package main

import (
"bytes"
"flag"
"fmt"
"log"
"os"
"regexp"
"strconv"

// initflag must be imported before any other minikube pkg.
// Fix for https://github.com/kubernetes/minikube/issues/4866

"github.com/spf13/pflag"
"k8s.io/klog/v2"
_ "k8s.io/minikube/pkg/initflag"

// Register drivers
_ "k8s.io/minikube/pkg/minikube/registry/drvs"
Expand Down Expand Up @@ -61,6 +59,8 @@ func main() {
bridgeLogMessages()
defer klog.Flush()

setFlags()

s := stacklog.MustStartFromEnv("STACKLOG_PATH")
defer s.Stop()

Expand Down Expand Up @@ -120,3 +120,33 @@ func (lb machineLogBridge) Write(b []byte) (n int, err error) {
}
return len(b), nil
}

// setFlags sets the flags
func setFlags() {
// parse flags beyond subcommand - get aroung go flag 'limitations':
// "Flag parsing stops just before the first non-flag argument" (ref: https://pkg.go.dev/flag#hdr-Command_line_flag_syntax)
pflag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

// set default flag value for logtostderr and alsologtostderr but don't override user's preferences
if !pflag.CommandLine.Changed("logtostderr") {
if err := pflag.Set("logtostderr", "false"); err != nil {
klog.Warningf("Unable to set default flag value for logtostderr: %v", err)
}
}
if !pflag.CommandLine.Changed("alsologtostderr") {
if err := pflag.Set("alsologtostderr", "false"); err != nil {
klog.Warningf("Unable to set default flag value for alsologtostderr: %v", err)
}
}

// make sure log_dir exists if log_file is not also set - the log_dir is mutually exclusive with the log_file option
// ref: https://github.com/kubernetes/klog/blob/52c62e3b70a9a46101f33ebaf0b100ec55099975/klog.go#L491
if pflag.Lookup("log_file") != nil && pflag.Lookup("log_file").Value.String() == "" &&
pflag.Lookup("log_dir") != nil && pflag.Lookup("log_dir").Value.String() != "" {
if err := os.MkdirAll(pflag.Lookup("log_dir").Value.String(), 0755); err != nil {
klog.Warningf("unable to create log directory: %v", err)
}
}
}
29 changes: 0 additions & 29 deletions pkg/initflag/initflag.go

This file was deleted.

4 changes: 0 additions & 4 deletions pkg/minikube/extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ import (
"strconv"
"strings"

// initflag must be imported before any other minikube pkg.
// Fix for https://github.com/kubernetes/minikube/issues/4866
_ "k8s.io/minikube/pkg/initflag"

"github.com/golang-collections/collections/stack"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/util/lock"
Expand Down
3 changes: 2 additions & 1 deletion pkg/minikube/translate/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ func DetermineLocale() {

// setPreferredLanguageTag configures which language future messages should use.
func setPreferredLanguageTag(l language.Tag) {
klog.Infof("Setting Language to %s ...", l)
// output message only if verbosity level is set and we still haven't got all the flags parsed in main()
klog.V(1).Infof("Setting Language to %s ...", l)
preferredLanguage = l
}

Expand Down