Skip to content

Commit e784d0d

Browse files
authored
Merge pull request kubernetes#92177 from serathius/help
Update logging format flag help
2 parents 5ed7b1a + 5114dac commit e784d0d

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ limitations under the License.
1717
package logs
1818

1919
import (
20+
"flag"
2021
"fmt"
22+
"strings"
23+
2124
"github.com/go-logr/logr"
2225
"github.com/spf13/pflag"
26+
2327
"k8s.io/klog/v2"
2428
)
2529

@@ -28,6 +32,12 @@ const (
2832
defaultLogFormat = "text"
2933
)
3034

35+
// List of logs (k8s.io/klog + k8s.io/component-base/logs) flags supported by all logging formats
36+
var supportedLogsFlags = map[string]struct{}{
37+
"v": {},
38+
"vmodule": {},
39+
}
40+
3141
// Options has klog format parameters
3242
type Options struct {
3343
LogFormat string
@@ -50,7 +60,9 @@ func (o *Options) Validate() []error {
5060

5161
// AddFlags add logging-format flag
5262
func (o *Options) AddFlags(fs *pflag.FlagSet) {
53-
fs.StringVar(&o.LogFormat, logFormatFlagName, defaultLogFormat, "Set log format")
63+
unsupportedFlags := fmt.Sprintf("--%s", strings.Join(unsupportedLoggingFlags(), ", --"))
64+
formats := fmt.Sprintf(`"%s"`, strings.Join(logRegistry.List(), `", "`))
65+
fs.StringVar(&o.LogFormat, logFormatFlagName, defaultLogFormat, 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))
5466
}
5567

5668
// Apply set klog logger from LogFormat type
@@ -65,3 +77,26 @@ func (o *Options) Apply() {
6577
func (o *Options) Get() (logr.Logger, error) {
6678
return logRegistry.Get(o.LogFormat)
6779
}
80+
81+
func unsupportedLoggingFlags() []string {
82+
allFlags := []string{}
83+
84+
// k8s.io/klog flags
85+
fs := &flag.FlagSet{}
86+
klog.InitFlags(fs)
87+
fs.VisitAll(func(flag *flag.Flag) {
88+
if _, found := supportedLogsFlags[flag.Name]; !found {
89+
allFlags = append(allFlags, flag.Name)
90+
}
91+
})
92+
93+
// k8s.io/component-base/logs flags
94+
pfs := &pflag.FlagSet{}
95+
AddFlags(pfs)
96+
pfs.VisitAll(func(flag *pflag.Flag) {
97+
if _, found := supportedLogsFlags[flag.Name]; !found {
98+
allFlags = append(allFlags, flag.Name)
99+
}
100+
})
101+
return allFlags
102+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ func (lfr *LogFormatRegistry) Delete(name string) {
8080
delete(lfr.registry, name)
8181
}
8282

83+
// List names of registered log formats
84+
func (lfr *LogFormatRegistry) List() []string {
85+
lfr.mu.Lock()
86+
defer lfr.mu.Unlock()
87+
formats := make([]string, 0, len(lfr.registry))
88+
for f := range lfr.registry {
89+
formats = append(formats, f)
90+
}
91+
return formats
92+
}
93+
8394
func init() {
8495
// Text format is default klog format
8596
logRegistry.Register(defaultLogFormat, nil)

0 commit comments

Comments
 (0)