Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[configtls] Add NewDefault* funcs #9658

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/configtls-add-newdefault-funcs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: configtls

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Adds `NewDefault*` functions for all the config structs.

# One or more tracking issues or pull requests related to the change
issues: [9658]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
19 changes: 19 additions & 0 deletions config/configtls/configtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ type Config struct {
ReloadInterval time.Duration `mapstructure:"reload_interval"`
}

// NewDefaultConfig creates a new TLSSetting with any default values set.
func NewDefaultConfig() Config {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need for this from users perspective? Or we think it is enough only the Client/Server? Happy to keep it if we have few needs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont have a specific user cases, only following the pattern defined in https://github.com/open-telemetry/opentelemetry-collector/blob/main/CONTRIBUTING.md#default-configuration. The pattern implies to me that we should have a NewDefault for all public config APIs. It is probably overkill, but it does allow us to add new default values to Config in the future if we want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather have the NewDefaultConfig function.

One thing is that we can use it in the NewDefaultClientConfig and NewDefaultServerConfig, although admittedly it doesn't need to be exported for this.

The other thing is that users can create their own configs that embed the configtls.Config struct and then they can use the configtls.NewDefaultConfig to create their instances of their configs.

I hope you don't ask me for potential examples of other configs that embed configtls.Config, because I don't have any such examples. 😄

return Config{}
}

// ClientConfig contains TLS configurations that are specific to client
// connections in addition to the common configurations. This should be used by
// components configuring TLS client connections.
Expand All @@ -96,6 +101,13 @@ type ClientConfig struct {
ServerName string `mapstructure:"server_name_override"`
}

// NewDefaultClientConfig creates a new TLSClientSetting with any default values set.
func NewDefaultClientConfig() ClientConfig {
return ClientConfig{
Config: NewDefaultConfig(),
}
}

// ServerConfig contains TLS configurations that are specific to server
// connections in addition to the common configurations. This should be used by
// components configuring TLS server connections.
Expand All @@ -115,6 +127,13 @@ type ServerConfig struct {
ReloadClientCAFile bool `mapstructure:"client_ca_file_reload"`
}

// NewDefaultServerConfig creates a new TLSServerSetting with any default values set.
func NewDefaultServerConfig() ServerConfig {
return ServerConfig{
Config: NewDefaultConfig(),
}
}

// certReloader is a wrapper object for certificate reloading
// Its GetCertificate method will either return the current certificate or reload from disk
// if the last reload happened more than ReloadInterval ago
Expand Down
22 changes: 22 additions & 0 deletions config/configtls/configtls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ import (
"go.opentelemetry.io/collector/config/configopaque"
)

func TestNewDefaultConfig(t *testing.T) {
expectedConfig := Config{}
config := NewDefaultConfig()
require.Equal(t, expectedConfig, config)
}

func TestNewDefaultClientConfig(t *testing.T) {
expectedConfig := ClientConfig{
Config: NewDefaultConfig(),
}
config := NewDefaultClientConfig()
require.Equal(t, expectedConfig, config)
}

func TestNewDefaultServerConfig(t *testing.T) {
expectedConfig := ServerConfig{
Config: NewDefaultConfig(),
}
config := NewDefaultServerConfig()
require.Equal(t, expectedConfig, config)
}

func TestOptionsToConfig(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading