Skip to content

Commit

Permalink
logs: replace config methods with functions
Browse files Browse the repository at this point in the history
API types are only supposed to have methods related to serialization.

Kubernetes-commit: 4c6338ac0f66001ae0afa14f1b7a3b285accd97a
  • Loading branch information
pohly authored and k8s-publishing-bot committed May 31, 2022
1 parent fa36bad commit 41262c3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
28 changes: 14 additions & 14 deletions logs/api/v1/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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,
Expand Down Expand Up @@ -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)))
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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"
}
Expand Down
8 changes: 4 additions & 4 deletions logs/api/v1/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion logs/api/v1/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions logs/example/cmd/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions logs/json/register/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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".`
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 41262c3

Please sign in to comment.