Skip to content

Commit

Permalink
Revert "logs: remove deprecated klog flags"
Browse files Browse the repository at this point in the history
This reverts commit 2f762e4.
  • Loading branch information
soltysh committed Dec 20, 2022
1 parent 4d57b3e commit 36716ee
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 58 deletions.
Expand Up @@ -62,7 +62,7 @@ func TestAddGlobalFlags(t *testing.T) {
}{
{
// Happy case
expectedFlag: []string{"help", "log-flush-frequency", "v", "vmodule"},
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"},
matchExpected: false,
},
{
Expand Down
23 changes: 20 additions & 3 deletions staging/src/k8s.io/component-base/logs/api/v1/options.go
Expand Up @@ -20,6 +20,7 @@ import (
"flag"
"fmt"
"math"
"sort"
"strings"
"time"

Expand All @@ -31,7 +32,6 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/featuregate"
"k8s.io/component-base/logs/klogflags"
)

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

// AddFlags adds command line flags for the configuration.
func AddFlags(c *LoggingConfiguration, fs *pflag.FlagSet) {
// The help text is generated assuming that flags will eventually use
// hyphens, even if currently no normalization function is set for the
// flag set yet.
unsupportedFlags := strings.Join(unsupportedLoggingFlagNames(cliflag.WordSepNormalizeFunc), ", ")
formats := logRegistry.list()
fs.StringVar(&c.Format, "logging-format", c.Format, fmt.Sprintf("Sets the log format. Permitted formats: %s.", formats))
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))
// No new log formats should be added after generation is of flag options
logRegistry.freeze()

Expand Down Expand Up @@ -232,13 +236,14 @@ var loggingFlags pflag.FlagSet

func init() {
var fs flag.FlagSet
klogflags.Init(&fs)
klog.InitFlags(&fs)
loggingFlags.AddGoFlagSet(&fs)
}

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

// unsupportedLoggingFlags lists unsupported logging flags. The normalize
Expand All @@ -263,3 +268,15 @@ func unsupportedLoggingFlags(normalizeFunc func(f *pflag.FlagSet, name string) p
})
return allFlags
}

// unsupportedLoggingFlagNames lists unsupported logging flags by name, with
// optional normalization and sorted.
func unsupportedLoggingFlagNames(normalizeFunc func(f *pflag.FlagSet, name string) pflag.NormalizedName) []string {
unsupportedFlags := unsupportedLoggingFlags(normalizeFunc)
names := make([]string, 0, len(unsupportedFlags))
for _, f := range unsupportedFlags {
names = append(names, "--"+f.Name)
}
sort.Strings(names)
return names
}
Expand Up @@ -39,7 +39,9 @@ func TestFlags(t *testing.T) {
fs.SetOutput(&output)
fs.PrintDefaults()
want := ` --log-flush-frequency duration Maximum number of seconds between log flushes (default 5s)
--logging-format string Sets the log format. Permitted formats: "text". (default "text")
--logging-format string Sets the log format. Permitted formats: "text".
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.
Non-default choices are currently alpha and subject to change without warning. (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)
`
Expand Down
41 changes: 0 additions & 41 deletions staging/src/k8s.io/component-base/logs/klogflags/klogflags.go

This file was deleted.

48 changes: 37 additions & 11 deletions staging/src/k8s.io/component-base/logs/logs.go
Expand Up @@ -27,11 +27,16 @@ import (

"github.com/spf13/pflag"
logsapi "k8s.io/component-base/logs/api/v1"
"k8s.io/component-base/logs/klogflags"
"k8s.io/klog/v2"
)

const vmoduleUsage = " (only works for the default text log format)"
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"

// TODO (https://github.com/kubernetes/kubernetes/issues/105310): once klog
// flags are removed, stop warning about "Non-default formats don't honor these
// flags" in config.go and instead add this remark here.
//
// const vmoduleUsage = " (only works for the default text log format)"

var (
packageFlags = flag.NewFlagSet("logging", flag.ContinueOnError)
Expand All @@ -42,7 +47,7 @@ var (
)

func init() {
klogflags.Init(packageFlags)
klog.InitFlags(packageFlags)
packageFlags.DurationVar(&logFlushFreq, logsapi.LogFlushFreqFlagName, logsapi.LogFlushFreqDefault, "Maximum number of seconds between log flushes")
}

Expand Down Expand Up @@ -76,29 +81,42 @@ var NewOptions = logsapi.NewLoggingConfiguration
//
// May be called more than once.
func AddFlags(fs *pflag.FlagSet, opts ...Option) {
// Determine whether the flags are already present by looking up one
// which always should exist.
if fs.Lookup("logtostderr") != nil {
return
}

o := addFlagsOptions{}
for _, opt := range opts {
opt(&o)
}

// Add all supported flags.
// Add flags with pflag deprecation remark for some klog flags.
packageFlags.VisitAll(func(f *flag.Flag) {
pf := pflag.PFlagFromGoFlag(f)
switch f.Name {
case "v", logsapi.LogFlushFreqFlagName:
case "v":
// unchanged, potentially skip it
if o.skipLoggingConfigurationFlags {
return
}
case logsapi.LogFlushFreqFlagName:
// unchanged, potentially skip it
if o.skipLoggingConfigurationFlags {
return
}
case "vmodule":
// TODO: see above
// pf.Usage += vmoduleUsage
if o.skipLoggingConfigurationFlags {
return
}
pf.Usage += vmoduleUsage
}
if fs.Lookup(pf.Name) == nil {
fs.AddFlag(pf)
default:
// deprecated, but not hidden
pf.Deprecated = deprecated
}
fs.AddFlag(pf)
})
}

Expand All @@ -119,16 +137,24 @@ func AddGoFlags(fs *flag.FlagSet, opts ...Option) {
packageFlags.VisitAll(func(f *flag.Flag) {
usage := f.Usage
switch f.Name {
case "v", logsapi.LogFlushFreqFlagName:
case "v":
// unchanged
if o.skipLoggingConfigurationFlags {
return
}
case logsapi.LogFlushFreqFlagName:
// unchanged
if o.skipLoggingConfigurationFlags {
return
}
case "vmodule":
// TODO: see above
// usage += vmoduleUsage
if o.skipLoggingConfigurationFlags {
return
}
usage += vmoduleUsage
default:
usage += " (DEPRECATED: " + deprecated + ")"
}
fs.Var(f.Value, f.Name, usage)
})
Expand Down
1 change: 0 additions & 1 deletion vendor/modules.txt
Expand Up @@ -2223,7 +2223,6 @@ k8s.io/component-base/logs
k8s.io/component-base/logs/api/v1
k8s.io/component-base/logs/json
k8s.io/component-base/logs/json/register
k8s.io/component-base/logs/klogflags
k8s.io/component-base/logs/logreduction
k8s.io/component-base/logs/testinit
k8s.io/component-base/metrics
Expand Down

0 comments on commit 36716ee

Please sign in to comment.