diff --git a/logs/api/v1/options.go b/logs/api/v1/options.go index 84cbdf97..dd4e7f8a 100644 --- a/logs/api/v1/options.go +++ b/logs/api/v1/options.go @@ -50,7 +50,7 @@ const ( // NewLoggingConfiguration returns a struct holding the default logging configuration. func NewLoggingConfiguration() *LoggingConfiguration { c := LoggingConfiguration{} - c.SetRecommendedLoggingConfiguration() + SetRecommendedLoggingConfiguration(&c) return &c } @@ -61,26 +61,26 @@ func NewLoggingConfiguration() *LoggingConfiguration { // // The optional FeatureGate controls logging features. If nil, the default for // these features is used. -func (c *LoggingConfiguration) ValidateAndApply(featureGate featuregate.FeatureGate) error { - return c.ValidateAndApplyAsField(featureGate, nil) +func ValidateAndApply(c *LoggingConfiguration, featureGate featuregate.FeatureGate) error { + return ValidateAndApplyAsField(c, featureGate, nil) } // ValidateAndApplyAsField is a variant of ValidateAndApply that should be used // when the LoggingConfiguration is embedded in some larger configuration // structure. -func (c *LoggingConfiguration) ValidateAndApplyAsField(featureGate featuregate.FeatureGate, fldPath *field.Path) error { - errs := c.Validate(featureGate, fldPath) +func ValidateAndApplyAsField(c *LoggingConfiguration, featureGate featuregate.FeatureGate, fldPath *field.Path) error { + errs := Validate(c, featureGate, fldPath) if len(errs) > 0 { return errs.ToAggregate() } - return c.apply(featureGate) + return apply(c, featureGate) } // Validate can be used to check for invalid settings without applying them. // Most binaries should validate and apply the logging configuration as soon // as possible via ValidateAndApply. The field path is optional: nil // can be passed when the struct is not embedded in some larger struct. -func (c *LoggingConfiguration) Validate(featureGate featuregate.FeatureGate, fldPath *field.Path) field.ErrorList { +func Validate(c *LoggingConfiguration, featureGate featuregate.FeatureGate, fldPath *field.Path) field.ErrorList { errs := field.ErrorList{} if c.Format != DefaultLogFormat { // WordSepNormalizeFunc is just a guess. Commands should use it, @@ -127,17 +127,17 @@ func (c *LoggingConfiguration) Validate(featureGate featuregate.FeatureGate, fld } } - errs = append(errs, c.validateFormatOptions(featureGate, fldPath.Child("options"))...) + errs = append(errs, validateFormatOptions(c, featureGate, fldPath.Child("options"))...) return errs } -func (c *LoggingConfiguration) validateFormatOptions(featureGate featuregate.FeatureGate, fldPath *field.Path) field.ErrorList { +func validateFormatOptions(c *LoggingConfiguration, featureGate featuregate.FeatureGate, fldPath *field.Path) field.ErrorList { errs := field.ErrorList{} - errs = append(errs, c.validateJSONOptions(featureGate, fldPath.Child("json"))...) + errs = append(errs, validateJSONOptions(c, featureGate, fldPath.Child("json"))...) return errs } -func (c *LoggingConfiguration) validateJSONOptions(featureGate featuregate.FeatureGate, fldPath *field.Path) field.ErrorList { +func validateJSONOptions(c *LoggingConfiguration, featureGate featuregate.FeatureGate, fldPath *field.Path) field.ErrorList { errs := field.ErrorList{} if gate := LoggingAlphaOptions; c.Options.JSON.SplitStream && !featureEnabled(featureGate, gate) { errs = append(errs, field.Forbidden(fldPath.Child("splitStream"), fmt.Sprintf("Feature %s is disabled", gate))) @@ -156,7 +156,7 @@ func featureEnabled(featureGate featuregate.FeatureGate, feature featuregate.Fea return enabled } -func (c *LoggingConfiguration) apply(featureGate featuregate.FeatureGate) error { +func apply(c *LoggingConfiguration, featureGate featuregate.FeatureGate) error { contextualLoggingEnabled := contextualLoggingDefault if featureGate != nil { contextualLoggingEnabled = featureGate.Enabled(ContextualLogging) @@ -182,7 +182,7 @@ func (c *LoggingConfiguration) apply(featureGate featuregate.FeatureGate) error } // AddFlags adds command line flags for the configuration. -func (c *LoggingConfiguration) AddFlags(fs *pflag.FlagSet) { +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. @@ -210,7 +210,7 @@ func (c *LoggingConfiguration) AddFlags(fs *pflag.FlagSet) { // Consumers who embed LoggingConfiguration in their own configuration structs // may set custom defaults and then should call this function to add the // global defaults. -func (c *LoggingConfiguration) SetRecommendedLoggingConfiguration() { +func SetRecommendedLoggingConfiguration(c *LoggingConfiguration) { if c.Format == "" { c.Format = "text" } diff --git a/logs/api/v1/options_test.go b/logs/api/v1/options_test.go index 40f334df..0d2083dc 100644 --- a/logs/api/v1/options_test.go +++ b/logs/api/v1/options_test.go @@ -35,7 +35,7 @@ func TestFlags(t *testing.T) { c := NewLoggingConfiguration() fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError) output := bytes.Buffer{} - c.AddFlags(fs) + AddFlags(c, fs) fs.SetOutput(&output) fs.PrintDefaults() want := ` --log-flush-frequency duration Maximum number of seconds between log flushes (default 5s) @@ -88,12 +88,12 @@ func TestOptions(t *testing.T) { t.Run(tc.name, func(t *testing.T) { c := NewLoggingConfiguration() fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError) - c.AddFlags(fs) + AddFlags(c, fs) fs.Parse(tc.args) if !assert.Equal(t, tc.want, c) { t.Errorf("Wrong Validate() result for %q. expect %v, got %v", tc.name, tc.want, c) } - errs := c.ValidateAndApply(nil /* We don't care about feature gates here. */) + errs := ValidateAndApply(c, nil /* We don't care about feature gates here. */) defer klog.StopFlushDaemon() 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) @@ -120,7 +120,7 @@ func testContextualLogging(t *testing.T, enabled bool) { AddFeatureGates(featureGate) err = featureGate.SetFromMap(map[string]bool{string(ContextualLogging): enabled}) require.NoError(t, err) - err = c.ValidateAndApply(featureGate) + err = ValidateAndApply(c, featureGate) require.NoError(t, err) defer klog.StopFlushDaemon() defer klog.EnableContextualLogging(true) diff --git a/logs/api/v1/validate_test.go b/logs/api/v1/validate_test.go index 99c7f657..10a8d790 100644 --- a/logs/api/v1/validate_test.go +++ b/logs/api/v1/validate_test.go @@ -151,7 +151,7 @@ func TestValidation(t *testing.T) { if featureGate == nil { featureGate = defaultFeatureGate } - err := test.config.Validate(featureGate, test.path) + err := Validate(&test.config, featureGate, test.path) if len(err) == 0 { if test.expectErrors != "" { t.Fatalf("did not get expected error(s): %s", test.expectErrors) diff --git a/logs/example/cmd/logger.go b/logs/example/cmd/logger.go index 16d9f3b9..5b8ed2e4 100644 --- a/logs/example/cmd/logger.go +++ b/logs/example/cmd/logger.go @@ -53,7 +53,7 @@ func NewLoggerCommand() *cobra.Command { cmd := &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { logs.InitLogs() - if err := c.ValidateAndApply(featureGate); err != nil { + if err := logsapi.ValidateAndApply(c, featureGate); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } @@ -67,7 +67,7 @@ func NewLoggerCommand() *cobra.Command { } logsapi.AddFeatureGates(featureGate) featureGate.AddFlag(cmd.Flags()) - c.AddFlags(cmd.Flags()) + logsapi.AddFlags(c, cmd.Flags()) return cmd } diff --git a/logs/json/register/register_test.go b/logs/json/register/register_test.go index 0b5398ae..6ec74341 100644 --- a/logs/json/register/register_test.go +++ b/logs/json/register/register_test.go @@ -34,7 +34,7 @@ func TestJSONFlag(t *testing.T) { c := logsapi.NewLoggingConfiguration() fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError) output := bytes.Buffer{} - c.AddFlags(fs) + logsapi.AddFlags(c, fs) fs.SetOutput(&output) fs.PrintDefaults() wantSubstring := `Permitted formats: "json" (gated by LoggingBetaOptions), "text".` @@ -142,7 +142,7 @@ func TestJSONFormatRegister(t *testing.T) { t.Run(tc.name, func(t *testing.T) { c := logsapi.NewLoggingConfiguration() fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError) - c.AddFlags(fs) + logsapi.AddFlags(c, fs) fs.Parse(tc.args) if !assert.Equal(t, tc.want, c) { t.Errorf("Wrong Validate() result for %q. expect %v, got %v", tc.name, tc.want, c) @@ -155,7 +155,7 @@ func TestJSONFormatRegister(t *testing.T) { err := mutable.SetFromMap(map[string]bool{string(logsapi.ContextualLogging): tc.contextualLogging}) require.NoError(t, err) featureGate = mutable - errs := c.ValidateAndApply(featureGate) + errs := logsapi.ValidateAndApply(c, featureGate) defer klog.ClearLogger() 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)