diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index 63662dd2f91c..2259f03889ce 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -17,7 +17,7 @@ limitations under the License. package cmd import ( - goflag "flag" + "flag" "fmt" "os" "path/filepath" @@ -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.") @@ -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) } diff --git a/cmd/minikube/main.go b/cmd/minikube/main.go index 2c18699ec2ac..719e2ffa47f4 100644 --- a/cmd/minikube/main.go +++ b/cmd/minikube/main.go @@ -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" @@ -61,6 +59,8 @@ func main() { bridgeLogMessages() defer klog.Flush() + setFlags() + s := stacklog.MustStartFromEnv("STACKLOG_PATH") defer s.Stop() @@ -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) + } + } +} diff --git a/pkg/initflag/initflag.go b/pkg/initflag/initflag.go deleted file mode 100644 index 806b8ba76b43..000000000000 --- a/pkg/initflag/initflag.go +++ /dev/null @@ -1,29 +0,0 @@ -/* -Copyright 2019 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package initflag - -import ( - "flag" -) - -func init() { - // Workaround for "ERROR: logging before flag.Parse" - // See: https://github.com/kubernetes/kubernetes/issues/17162 - fs := flag.NewFlagSet("", flag.ContinueOnError) - _ = fs.Parse([]string{}) - flag.CommandLine = fs -} diff --git a/pkg/minikube/extract/extract.go b/pkg/minikube/extract/extract.go index 4ae7b08dd7ec..0505f0775f0f 100644 --- a/pkg/minikube/extract/extract.go +++ b/pkg/minikube/extract/extract.go @@ -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" diff --git a/pkg/minikube/translate/translate.go b/pkg/minikube/translate/translate.go index a80b046a8dda..3a69fcaf65a9 100644 --- a/pkg/minikube/translate/translate.go +++ b/pkg/minikube/translate/translate.go @@ -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 }