Skip to content

Commit 2f762e4

Browse files
committed
logs: remove deprecated klog flags
This completes the deprecation of klog flags which are no longer supported. klog itself continues to support them, but Kubernetes components don't. This makes the command line interfaces simpler and reduces the attack surface because less functionality is exposed. For example, kube-controller-manager now has: Logs flags: --log-flush-frequency duration Maximum number of seconds between log flushes (default 5s) --log-json-info-buffer-size quantity [Alpha] In JSON format with split output streams, the info messages can be buffered for a while to increase performance. The default value of zero bytes disables buffering. The size can be specified as number of bytes (512), multiples of 1000 (1K), multiples of 1024 (2Ki), or powers of those (3M, 4G, 5Mi, 6Gi). Enable the LoggingAlphaOptions feature gate to use this. --log-json-split-stream [Alpha] In JSON format, write error messages to stderr and info messages to stdout. The default is to write a single stream to stdout. Enable the LoggingAlphaOptions feature gate to use this. --logging-format string Sets the log format. Permitted formats: "json" (gated by LoggingBetaOptions), "text". (default "text") -v, --v Level number for the log level verbosity --vmodule pattern=N,... comma-separated list of pattern=N settings for file-filtered logging (only works for text log format) Misc flags: --kubeconfig string Path to kubeconfig file with authorization and master location information. --master string The address of the Kubernetes API server (overrides any value in kubeconfig). Global flags: -h, --help help for kube-controller-manager --version version[=true] Print version information and quit For details see https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/2845-deprecate-klog-specific-flags-in-k8s-components
1 parent 41619ac commit 2f762e4

File tree

6 files changed

+58
-61
lines changed

6 files changed

+58
-61
lines changed

staging/src/k8s.io/component-base/cli/globalflag/globalflags_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestAddGlobalFlags(t *testing.T) {
6262
}{
6363
{
6464
// Happy case
65-
expectedFlag: []string{"add-dir-header", "alsologtostderr", "help", "log-backtrace-at", "log-dir", "log-file", "log-file-max-size", "log-flush-frequency", "logtostderr", "one-output", "skip-headers", "skip-log-headers", "stderrthreshold", "v", "vmodule"},
65+
expectedFlag: []string{"help", "log-flush-frequency", "v", "vmodule"},
6666
matchExpected: false,
6767
},
6868
{

staging/src/k8s.io/component-base/logs/api/v1/options.go

+3-20
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"flag"
2121
"fmt"
2222
"math"
23-
"sort"
2423
"strings"
2524
"time"
2625

@@ -32,6 +31,7 @@ import (
3231
"k8s.io/apimachinery/pkg/util/validation/field"
3332
cliflag "k8s.io/component-base/cli/flag"
3433
"k8s.io/component-base/featuregate"
34+
"k8s.io/component-base/logs/klogflags"
3535
)
3636

3737
const (
@@ -183,12 +183,8 @@ func apply(c *LoggingConfiguration, featureGate featuregate.FeatureGate) error {
183183

184184
// AddFlags adds command line flags for the configuration.
185185
func AddFlags(c *LoggingConfiguration, fs *pflag.FlagSet) {
186-
// The help text is generated assuming that flags will eventually use
187-
// hyphens, even if currently no normalization function is set for the
188-
// flag set yet.
189-
unsupportedFlags := strings.Join(unsupportedLoggingFlagNames(cliflag.WordSepNormalizeFunc), ", ")
190186
formats := logRegistry.list()
191-
fs.StringVar(&c.Format, "logging-format", c.Format, fmt.Sprintf("Sets the log format. Permitted formats: %s.\nNon-default formats don't honor these flags: %s.\nNon-default choices are currently alpha and subject to change without warning.", formats, unsupportedFlags))
187+
fs.StringVar(&c.Format, "logging-format", c.Format, fmt.Sprintf("Sets the log format. Permitted formats: %s.", formats))
192188
// No new log formats should be added after generation is of flag options
193189
logRegistry.freeze()
194190

@@ -236,14 +232,13 @@ var loggingFlags pflag.FlagSet
236232

237233
func init() {
238234
var fs flag.FlagSet
239-
klog.InitFlags(&fs)
235+
klogflags.Init(&fs)
240236
loggingFlags.AddGoFlagSet(&fs)
241237
}
242238

243239
// List of logs (k8s.io/klog + k8s.io/component-base/logs) flags supported by all logging formats
244240
var supportedLogsFlags = map[string]struct{}{
245241
"v": {},
246-
// TODO: support vmodule after 1.19 Alpha
247242
}
248243

249244
// unsupportedLoggingFlags lists unsupported logging flags. The normalize
@@ -268,15 +263,3 @@ func unsupportedLoggingFlags(normalizeFunc func(f *pflag.FlagSet, name string) p
268263
})
269264
return allFlags
270265
}
271-
272-
// unsupportedLoggingFlagNames lists unsupported logging flags by name, with
273-
// optional normalization and sorted.
274-
func unsupportedLoggingFlagNames(normalizeFunc func(f *pflag.FlagSet, name string) pflag.NormalizedName) []string {
275-
unsupportedFlags := unsupportedLoggingFlags(normalizeFunc)
276-
names := make([]string, 0, len(unsupportedFlags))
277-
for _, f := range unsupportedFlags {
278-
names = append(names, "--"+f.Name)
279-
}
280-
sort.Strings(names)
281-
return names
282-
}

staging/src/k8s.io/component-base/logs/api/v1/options_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ func TestFlags(t *testing.T) {
3939
fs.SetOutput(&output)
4040
fs.PrintDefaults()
4141
want := ` --log-flush-frequency duration Maximum number of seconds between log flushes (default 5s)
42-
--logging-format string Sets the log format. Permitted formats: "text".
43-
Non-default formats don't honor these flags: --add-dir-header, --alsologtostderr, --log-backtrace-at, --log-dir, --log-file, --log-file-max-size, --logtostderr, --one-output, --skip-headers, --skip-log-headers, --stderrthreshold, --vmodule.
44-
Non-default choices are currently alpha and subject to change without warning. (default "text")
42+
--logging-format string Sets the log format. Permitted formats: "text". (default "text")
4543
-v, --v Level number for the log level verbosity
4644
--vmodule pattern=N,... comma-separated list of pattern=N settings for file-filtered logging (only works for text log format)
4745
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package klogflags
18+
19+
import (
20+
"flag"
21+
22+
"k8s.io/klog/v2"
23+
)
24+
25+
// Init is a replacement for klog.InitFlags which only adds those flags
26+
// that are still supported for Kubernetes components (i.e. -v and -vmodule).
27+
// See
28+
// https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/2845-deprecate-klog-specific-flags-in-k8s-components.
29+
func Init(fs *flag.FlagSet) {
30+
var allFlags flag.FlagSet
31+
klog.InitFlags(&allFlags)
32+
if fs == nil {
33+
fs = flag.CommandLine
34+
}
35+
allFlags.VisitAll(func(f *flag.Flag) {
36+
switch f.Name {
37+
case "v", "vmodule":
38+
fs.Var(f.Value, f.Name, f.Usage)
39+
}
40+
})
41+
}

staging/src/k8s.io/component-base/logs/logs.go

+11-37
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,11 @@ import (
2727

2828
"github.com/spf13/pflag"
2929
logsapi "k8s.io/component-base/logs/api/v1"
30+
"k8s.io/component-base/logs/klogflags"
3031
"k8s.io/klog/v2"
3132
)
3233

33-
const deprecated = "will be removed in a future release, see https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/2845-deprecate-klog-specific-flags-in-k8s-components"
34-
35-
// TODO (https://github.com/kubernetes/kubernetes/issues/105310): once klog
36-
// flags are removed, stop warning about "Non-default formats don't honor these
37-
// flags" in config.go and instead add this remark here.
38-
//
39-
// const vmoduleUsage = " (only works for the default text log format)"
34+
const vmoduleUsage = " (only works for the default text log format)"
4035

4136
var (
4237
packageFlags = flag.NewFlagSet("logging", flag.ContinueOnError)
@@ -47,7 +42,7 @@ var (
4742
)
4843

4944
func init() {
50-
klog.InitFlags(packageFlags)
45+
klogflags.Init(packageFlags)
5146
packageFlags.DurationVar(&logFlushFreq, logsapi.LogFlushFreqFlagName, logsapi.LogFlushFreqDefault, "Maximum number of seconds between log flushes")
5247
}
5348

@@ -81,42 +76,29 @@ var NewOptions = logsapi.NewLoggingConfiguration
8176
//
8277
// May be called more than once.
8378
func AddFlags(fs *pflag.FlagSet, opts ...Option) {
84-
// Determine whether the flags are already present by looking up one
85-
// which always should exist.
86-
if fs.Lookup("logtostderr") != nil {
87-
return
88-
}
89-
9079
o := addFlagsOptions{}
9180
for _, opt := range opts {
9281
opt(&o)
9382
}
9483

95-
// Add flags with pflag deprecation remark for some klog flags.
84+
// Add all supported flags.
9685
packageFlags.VisitAll(func(f *flag.Flag) {
9786
pf := pflag.PFlagFromGoFlag(f)
9887
switch f.Name {
99-
case "v":
100-
// unchanged, potentially skip it
101-
if o.skipLoggingConfigurationFlags {
102-
return
103-
}
104-
case logsapi.LogFlushFreqFlagName:
88+
case "v", logsapi.LogFlushFreqFlagName:
10589
// unchanged, potentially skip it
10690
if o.skipLoggingConfigurationFlags {
10791
return
10892
}
10993
case "vmodule":
110-
// TODO: see above
111-
// pf.Usage += vmoduleUsage
11294
if o.skipLoggingConfigurationFlags {
11395
return
11496
}
115-
default:
116-
// deprecated, but not hidden
117-
pf.Deprecated = deprecated
97+
pf.Usage += vmoduleUsage
98+
}
99+
if fs.Lookup(pf.Name) == nil {
100+
fs.AddFlag(pf)
118101
}
119-
fs.AddFlag(pf)
120102
})
121103
}
122104

@@ -137,24 +119,16 @@ func AddGoFlags(fs *flag.FlagSet, opts ...Option) {
137119
packageFlags.VisitAll(func(f *flag.Flag) {
138120
usage := f.Usage
139121
switch f.Name {
140-
case "v":
141-
// unchanged
142-
if o.skipLoggingConfigurationFlags {
143-
return
144-
}
145-
case logsapi.LogFlushFreqFlagName:
122+
case "v", logsapi.LogFlushFreqFlagName:
146123
// unchanged
147124
if o.skipLoggingConfigurationFlags {
148125
return
149126
}
150127
case "vmodule":
151-
// TODO: see above
152-
// usage += vmoduleUsage
153128
if o.skipLoggingConfigurationFlags {
154129
return
155130
}
156-
default:
157-
usage += " (DEPRECATED: " + deprecated + ")"
131+
usage += vmoduleUsage
158132
}
159133
fs.Var(f.Value, f.Name, usage)
160134
})

vendor/modules.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,7 @@ k8s.io/component-base/logs
20292029
k8s.io/component-base/logs/api/v1
20302030
k8s.io/component-base/logs/json
20312031
k8s.io/component-base/logs/json/register
2032+
k8s.io/component-base/logs/klogflags
20322033
k8s.io/component-base/logs/logreduction
20332034
k8s.io/component-base/logs/testinit
20342035
k8s.io/component-base/metrics

0 commit comments

Comments
 (0)