@@ -57,9 +57,9 @@ func NewOptions() *Options {
57
57
func (o * Options ) Validate () []error {
58
58
errs := []error {}
59
59
if o .LogFormat != defaultLogFormat {
60
- allFlags := unsupportedLoggingFlags ()
60
+ allFlags := unsupportedLoggingFlags (hyphensToUnderscores )
61
61
for _ , fname := range allFlags {
62
- if flagIsSet (fname ) {
62
+ if flagIsSet (fname , hyphensToUnderscores ) {
63
63
errs = append (errs , fmt .Errorf ("non-default logging format doesn't honor flag: %s" , fname ))
64
64
}
65
65
}
@@ -70,11 +70,23 @@ func (o *Options) Validate() []error {
70
70
return errs
71
71
}
72
72
73
- func flagIsSet (name string ) bool {
73
+ // hyphensToUnderscores replaces hyphens with underscores
74
+ // we should always use underscores instead of hyphens when validate flags
75
+ func hyphensToUnderscores (s string ) string {
76
+ return strings .Replace (s , "-" , "_" , - 1 )
77
+ }
78
+
79
+ func flagIsSet (name string , normalizeFunc func (name string ) string ) bool {
74
80
f := flag .Lookup (name )
75
81
if f != nil {
76
82
return f .DefValue != f .Value .String ()
77
83
}
84
+ if normalizeFunc != nil {
85
+ f = flag .Lookup (normalizeFunc (name ))
86
+ if f != nil {
87
+ return f .DefValue != f .Value .String ()
88
+ }
89
+ }
78
90
pf := pflag .Lookup (name )
79
91
if pf != nil {
80
92
return pf .DefValue != pf .Value .String ()
@@ -84,7 +96,12 @@ func flagIsSet(name string) bool {
84
96
85
97
// AddFlags add logging-format flag
86
98
func (o * Options ) AddFlags (fs * pflag.FlagSet ) {
87
- unsupportedFlags := fmt .Sprintf ("--%s" , strings .Join (unsupportedLoggingFlags (), ", --" ))
99
+ normalizeFunc := func (name string ) string {
100
+ f := fs .GetNormalizeFunc ()
101
+ return string (f (fs , name ))
102
+ }
103
+
104
+ unsupportedFlags := fmt .Sprintf ("--%s" , strings .Join (unsupportedLoggingFlags (normalizeFunc ), ", --" ))
88
105
formats := fmt .Sprintf (`"%s"` , strings .Join (logRegistry .List (), `", "` ))
89
106
fs .StringVar (& o .LogFormat , logFormatFlagName , defaultLogFormat , fmt .Sprintf ("Sets the log format. Permitted formats: %s.\n Non-default formats don't honor these flags: %s.\n Non-default choices are currently alpha and subject to change without warning." , formats , unsupportedFlags ))
90
107
@@ -109,15 +126,19 @@ func (o *Options) Get() (logr.Logger, error) {
109
126
return logRegistry .Get (o .LogFormat )
110
127
}
111
128
112
- func unsupportedLoggingFlags () []string {
129
+ func unsupportedLoggingFlags (normalizeFunc func ( name string ) string ) []string {
113
130
allFlags := []string {}
114
131
115
132
// k8s.io/klog flags
116
133
fs := & flag.FlagSet {}
117
134
klog .InitFlags (fs )
118
135
fs .VisitAll (func (flag * flag.Flag ) {
119
136
if _ , found := supportedLogsFlags [flag .Name ]; ! found {
120
- allFlags = append (allFlags , strings .Replace (flag .Name , "_" , "-" , - 1 ))
137
+ name := flag .Name
138
+ if normalizeFunc != nil {
139
+ name = normalizeFunc (name )
140
+ }
141
+ allFlags = append (allFlags , name )
121
142
}
122
143
})
123
144
0 commit comments