Merge pull request #105076 from pohly/log-flush-frequency-bug

initialize logging after flag parsing + refactor commands

Kubernetes-commit: 82da9bdaab705a1a72e4c5915f6ddd8775592fdf
This commit is contained in:
Kubernetes Publisher 2021-10-01 14:30:18 -07:00
commit 1e432bea02
4 changed files with 19 additions and 86 deletions

12
go.mod
View File

@ -33,9 +33,9 @@ require (
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.0.0-20211001003357-dd4141958dfc
k8s.io/apimachinery v0.0.0-20211001003147-df63df3af3fc
k8s.io/cli-runtime v0.0.0-20211001005839-d250093cf64e
k8s.io/client-go v0.0.0-20211001003700-dbfa30b9d908
k8s.io/component-base v0.0.0-20211001004235-05eaa93d6d25
k8s.io/cli-runtime v0.0.0-20211001205205-cb123082bee1
k8s.io/client-go v0.0.0-20211001163650-f71d2554c374
k8s.io/component-base v0.0.0-20211002003808-8ef22d17745b
k8s.io/component-helpers v0.0.0-20211001004347-11b67914ce96
k8s.io/klog/v2 v2.20.0
k8s.io/kube-openapi v0.0.0-20210817084001-7fbd8d59e5b8
@ -49,10 +49,10 @@ require (
replace (
k8s.io/api => k8s.io/api v0.0.0-20211001003357-dd4141958dfc
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20211001003147-df63df3af3fc
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20211001005839-d250093cf64e
k8s.io/client-go => k8s.io/client-go v0.0.0-20211001003700-dbfa30b9d908
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20211001205205-cb123082bee1
k8s.io/client-go => k8s.io/client-go v0.0.0-20211001163650-f71d2554c374
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20210930223515-ede4574ee351
k8s.io/component-base => k8s.io/component-base v0.0.0-20211001004235-05eaa93d6d25
k8s.io/component-base => k8s.io/component-base v0.0.0-20211002003808-8ef22d17745b
k8s.io/component-helpers => k8s.io/component-helpers v0.0.0-20211001004347-11b67914ce96
k8s.io/metrics => k8s.io/metrics v0.0.0-20211001005724-9be6d708955e
)

12
go.sum
View File

@ -906,13 +906,13 @@ k8s.io/api v0.0.0-20211001003357-dd4141958dfc h1:mEvk8nUryhzsFO3gvz1GOCHKAk9SwmC
k8s.io/api v0.0.0-20211001003357-dd4141958dfc/go.mod h1:X/EFj3T8/b6pT7Vu0CQfs8hKQGD/NpK6nituBPxPypM=
k8s.io/apimachinery v0.0.0-20211001003147-df63df3af3fc h1:xWJbWTw1QtGvoHnsdF1oI10d2gEBl2yqipw/m1DQjLU=
k8s.io/apimachinery v0.0.0-20211001003147-df63df3af3fc/go.mod h1:RAdi3McqM+9tkYHOyceb4XOeJWm9BCAF4BhZki5iiok=
k8s.io/cli-runtime v0.0.0-20211001005839-d250093cf64e h1:44rDupAn9GVHdHmFNQ3GJvbgfhCVA24O/NYbth+XCLM=
k8s.io/cli-runtime v0.0.0-20211001005839-d250093cf64e/go.mod h1:HHTg5hjK2TYMHKJ+0cTcQVTdVfvsDUuLcPBUPCwpHcU=
k8s.io/client-go v0.0.0-20211001003700-dbfa30b9d908 h1:B+OGWjn70mELs6xMjuvbxagQo6vMUCvVXRvLU8E/RbU=
k8s.io/client-go v0.0.0-20211001003700-dbfa30b9d908/go.mod h1:D3oqLmrdamgyGGdeFnlQ2viGosUBar48jDD5c89k3TA=
k8s.io/cli-runtime v0.0.0-20211001205205-cb123082bee1 h1:GvF0XFIO0Cuu5Ly8vnav4vMtzlWGoA5igAh33tUbDkw=
k8s.io/cli-runtime v0.0.0-20211001205205-cb123082bee1/go.mod h1:qYViNs18B5H9dPd0sBerg0p7e5BNc43CFWpK4/etB3Y=
k8s.io/client-go v0.0.0-20211001163650-f71d2554c374 h1:BESUWBMp04vEvrBgB9N9WbDLm5L8T8wGu3CSkVoKFpk=
k8s.io/client-go v0.0.0-20211001163650-f71d2554c374/go.mod h1:D3oqLmrdamgyGGdeFnlQ2viGosUBar48jDD5c89k3TA=
k8s.io/code-generator v0.0.0-20210930223515-ede4574ee351/go.mod h1:pxmv1vqS30f8CieYQvITL/Z2lV5G6+/Ze3wRjS3HXFo=
k8s.io/component-base v0.0.0-20211001004235-05eaa93d6d25 h1:4rKPbaIXyH8ttIz3aM4VdiZ7W3iRhw7Vc+AgR6cMV3w=
k8s.io/component-base v0.0.0-20211001004235-05eaa93d6d25/go.mod h1:hJUQ9S5ONjPiJMk3dsctlNEu+CS+jEyu9W5wVEMAF4g=
k8s.io/component-base v0.0.0-20211002003808-8ef22d17745b h1:H+/3fRk52/qS0CiBg1irHq3rdrOtTyoZAR9yfKrgDII=
k8s.io/component-base v0.0.0-20211002003808-8ef22d17745b/go.mod h1:k8WbcY75pTS2zc0mVGbSkWIvjrqL51iRgENi98nxCog=
k8s.io/component-helpers v0.0.0-20211001004347-11b67914ce96 h1:5fnKBbxL2SG1vEH84wyKuN4KD1b5FrhfMCPj7miopFI=
k8s.io/component-helpers v0.0.0-20211001004347-11b67914ce96/go.mod h1:BE8rv6HCr8U9iH8YWO+p/lKTjLDQM6e3Am/K0LxZy28=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=

View File

@ -17,7 +17,6 @@ limitations under the License.
package cmd
import (
"flag"
"fmt"
"io"
"net/http"
@ -255,13 +254,11 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
return nil
},
}
// From this point and forward we get warnings on flags that contain "_" separators
// when adding them with hyphen instead of the original name.
cmds.SetGlobalNormalizationFunc(cliflag.WarnWordSepNormalizeFunc)
flags := cmds.PersistentFlags()
flags.SetNormalizeFunc(cliflag.WarnWordSepNormalizeFunc) // Warn for "_" flags
// Normalize all flags that are coming from other packages or pre-configurations
// a.k.a. change all "_" to "-". e.g. glog package
flags.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
addProfilingFlags(flags)
@ -270,12 +267,10 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
kubeConfigFlags.AddFlags(flags)
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
matchVersionKubeConfigFlags.AddFlags(cmds.PersistentFlags())
matchVersionKubeConfigFlags.AddFlags(flags)
// Updates hooks to add kubectl command headers: SIG CLI KEP 859.
addCmdHeaderHooks(cmds, kubeConfigFlags)
cmds.PersistentFlags().AddGoFlagSet(flag.CommandLine)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
// Sending in 'nil' for the getLanguageFn() results in using
@ -285,9 +280,6 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
// the language, instead of just loading from the LANG env. variable.
i18n.LoadTranslations("kubectl", nil)
// From this point and forward we get warnings on flags that contain "_" separators
cmds.SetGlobalNormalizationFunc(cliflag.WarnWordSepNormalizeFunc)
ioStreams := genericclioptions.IOStreams{In: in, Out: out, ErrOut: err}
// Proxy command is incompatible with CommandHeaderRoundTripper, so
@ -392,6 +384,9 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
cmds.AddCommand(apiresources.NewCmdAPIResources(f, ioStreams))
cmds.AddCommand(options.NewCmdOptions(ioStreams.Out))
// Stop warning about normalization of flags. That makes it possible to
// add the klog flags later.
cmds.SetGlobalNormalizationFunc(cliflag.WordSepNormalizeFunc)
return cmds
}

View File

@ -1,62 +0,0 @@
/*
Copyright 2014 The Kubernetes Authors.
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 logs
import (
"flag"
"log"
"time"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
)
var logFlushFreq = pflag.Duration("log-flush-frequency", 5*time.Second, "Maximum number of seconds between log flushes")
// TODO(thockin): This is temporary until we agree on log dirs and put those into each cmd.
func init() {
klog.InitFlags(flag.CommandLine)
flag.Set("logtostderr", "true")
}
// KlogWriter serves as a bridge between the standard log package and the glog package.
type KlogWriter struct{}
// Write implements the io.Writer interface.
func (writer KlogWriter) Write(data []byte) (n int, err error) {
klog.InfoDepth(1, string(data))
return len(data), nil
}
// InitLogs initializes logs the way we want for kubernetes.
func InitLogs() {
log.SetOutput(KlogWriter{})
log.SetFlags(0)
// The default glog flush interval is 5 seconds.
go wait.Until(klog.Flush, *logFlushFreq, wait.NeverStop)
}
// FlushLogs flushes logs immediately.
func FlushLogs() {
klog.Flush()
}
// NewLogger creates a new log.Logger which sends logs to klog.Info.
func NewLogger(prefix string) *log.Logger {
return log.New(KlogWriter{}, prefix, 0)
}