diff --git a/pkg/kubelet/apis/config/validation/validation_test.go b/pkg/kubelet/apis/config/validation/validation_test.go index d9a33e71d57..7b65714e7b6 100644 --- a/pkg/kubelet/apis/config/validation/validation_test.go +++ b/pkg/kubelet/apis/config/validation/validation_test.go @@ -150,7 +150,7 @@ func TestValidateKubeletConfiguration(t *testing.T) { "GracefulNodeShutdown": true, }, Logging: componentbaseconfig.LoggingConfiguration{ - Format: "json", + Format: "text", }, } if allErrors := ValidateKubeletConfiguration(successCase3); allErrors != nil { diff --git a/staging/src/k8s.io/component-base/logs/config.go b/staging/src/k8s.io/component-base/logs/config.go index 55d28a6cdbd..8a029fa7cc5 100644 --- a/staging/src/k8s.io/component-base/logs/config.go +++ b/staging/src/k8s.io/component-base/logs/config.go @@ -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 diff --git a/staging/src/k8s.io/component-base/logs/json/register/register_test.go b/staging/src/k8s.io/component-base/logs/json/register/register_test.go new file mode 100644 index 00000000000..1ce3a71ad9f --- /dev/null +++ b/staging/src/k8s.io/component-base/logs/json/register/register_test.go @@ -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) + + } + }) + } +} diff --git a/staging/src/k8s.io/component-base/logs/options_test.go b/staging/src/k8s.io/component-base/logs/options_test.go index 09b44bcef20..7be7cdb22cb 100644 --- a/staging/src/k8s.io/component-base/logs/options_test.go +++ b/staging/src/k8s.io/component-base/logs/options_test.go @@ -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"},