Remove default JSON logging format registration from component-base/logs package

This commit is contained in:
Mengjiao Liu 2021-06-16 10:10:55 +08:00
parent f363322d4a
commit af825b4357
4 changed files with 101 additions and 14 deletions

View File

@ -150,7 +150,7 @@ func TestValidateKubeletConfiguration(t *testing.T) {
"GracefulNodeShutdown": true,
},
Logging: componentbaseconfig.LoggingConfiguration{
Format: "json",
Format: "text",
},
}
if allErrors := ValidateKubeletConfiguration(successCase3); allErrors != nil {

View File

@ -22,10 +22,9 @@ import (
"strings"
"github.com/spf13/pflag"
"k8s.io/klog/v2"
"k8s.io/component-base/config"
json "k8s.io/component-base/logs/json"
"k8s.io/klog/v2"
)
// Supported klog formats
@ -40,7 +39,6 @@ var LogRegistry = NewLogFormatRegistry()
func init() {
// Text format is default klog format
LogRegistry.Register(DefaultLogFormat, nil)
LogRegistry.Register(JSONLogFormat, json.JSONLogger)
}
// List of logs (k8s.io/klog + k8s.io/component-base/logs) flags supported by all logging formats

View File

@ -0,0 +1,98 @@
/*
Copyright 2021 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 register
import (
"bytes"
"testing"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/component-base/config"
"k8s.io/component-base/logs"
)
func TestJSONFlag(t *testing.T) {
o := logs.NewOptions()
fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError)
output := bytes.Buffer{}
o.AddFlags(fs)
fs.SetOutput(&output)
fs.PrintDefaults()
want := ` --experimental-logging-sanitization [Experimental] When enabled prevents logging of fields tagged as sensitive (passwords, keys, tokens).
Runtime log sanitization may introduce significant computation overhead and therefore should not be enabled in production.
--logging-format string Sets the log format. Permitted formats: "json", "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, --log-flush-frequency.
Non-default choices are currently alpha and subject to change without warning. (default "text")
`
if !assert.Equal(t, want, output.String()) {
t.Errorf("Wrong list of flags. expect %q, got %q", want, output.String())
}
}
func TestJSONFormatRegister(t *testing.T) {
testcases := []struct {
name string
args []string
want *logs.Options
errs field.ErrorList
}{
{
name: "JSON log format",
args: []string{"--logging-format=json"},
want: &logs.Options{
Config: config.LoggingConfiguration{
Format: logs.JSONLogFormat,
},
},
},
{
name: "Unsupported log format",
args: []string{"--logging-format=test"},
want: &logs.Options{
Config: config.LoggingConfiguration{
Format: "test",
},
},
errs: field.ErrorList{&field.Error{
Type: "FieldValueInvalid",
Field: "format",
BadValue: "test",
Detail: "Unsupported log format",
}},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
o := logs.NewOptions()
fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError)
o.AddFlags(fs)
fs.Parse(tc.args)
if !assert.Equal(t, tc.want, o) {
t.Errorf("Wrong Validate() result for %q. expect %v, got %v", tc.name, tc.want, o)
}
errs := o.Validate()
if !assert.ElementsMatch(t, tc.errs, errs) {
t.Errorf("Wrong Validate() result for %q.\n expect:\t%+v\n got:\t%+v", tc.name, tc.errs, errs)
}
})
}
}

View File

@ -36,7 +36,7 @@ func TestFlags(t *testing.T) {
fs.PrintDefaults()
want := ` --experimental-logging-sanitization [Experimental] When enabled prevents logging of fields tagged as sensitive (passwords, keys, tokens).
Runtime log sanitization may introduce significant computation overhead and therefore should not be enabled in production.
--logging-format string Sets the log format. Permitted formats: "json", "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, --log-flush-frequency.
Non-default choices are currently alpha and subject to change without warning. (default "text")
`
@ -61,15 +61,6 @@ func TestOptions(t *testing.T) {
args: []string{"--logging-format=text"},
want: NewOptions(),
},
{
name: "JSON log format",
args: []string{"--logging-format=json"},
want: &Options{
Config: config.LoggingConfiguration{
Format: JSONLogFormat,
},
},
},
{
name: "log sanitization",
args: []string{"--experimental-logging-sanitization"},