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

Allow config.Load to load partial config #910

Merged
merged 2 commits into from
May 5, 2020
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
34 changes: 9 additions & 25 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func NewViper() *viper.Viper {
}

// Load loads a Config from Viper.
// After loading the config, need to check if it is valid by calling `ValidateConfig`.
func Load(
v *viper.Viper,
factories Factories,
Expand Down Expand Up @@ -349,15 +350,6 @@ func loadReceivers(v *viper.Viper, factories map[configmodels.Type]component.Rec
// Get the map of "receivers" sub-keys.
keyMap := v.GetStringMap(receiversKeyName)

// Currently there is no default receiver enabled. The configuration must specify at least one receiver to enable
// functionality.
if len(keyMap) == 0 {
return nil, &configError{
code: errMissingReceivers,
msg: "no receivers specified in config",
}
}

// Prepare resulting map
receivers := make(configmodels.Receivers)

Expand Down Expand Up @@ -408,14 +400,6 @@ func loadExporters(v *viper.Viper, factories map[configmodels.Type]component.Exp
// Get the map of "exporters" sub-keys.
keyMap := v.GetStringMap(exportersKeyName)

// There is no default exporter. The configuration must specify at least one exporter to enable functionality.
if len(keyMap) == 0 {
return nil, &configError{
code: errMissingExporters,
msg: "no exporters specified in config",
}
}

// Prepare resulting map
exporters := make(configmodels.Exporters)

Expand Down Expand Up @@ -602,6 +586,7 @@ func ValidateConfig(cfg *configmodels.Config, logger *zap.Logger) error {
if err := validateReceivers(cfg); err != nil {
return err
}

if err := validateExporters(cfg); err != nil {
return err
}
Expand All @@ -610,25 +595,24 @@ func ValidateConfig(cfg *configmodels.Config, logger *zap.Logger) error {
return err
}

if err := validatePipelines(cfg); err != nil {
return err
}

return nil
}

func validateService(cfg *configmodels.Config) error {
// Currently only to validate extensions.
return validateServiceExtensions(cfg, &cfg.Service)
if err := validatePipelines(cfg); err != nil {
return err
}

return validateServiceExtensions(cfg)
}

func validateServiceExtensions(cfg *configmodels.Config, service *configmodels.Service) error {
func validateServiceExtensions(cfg *configmodels.Config) error {
if len(cfg.Service.Extensions) == 0 {
return nil
}

// Validate extensions.
for _, ref := range service.Extensions {
for _, ref := range cfg.Service.Extensions {
// Check that the name referenced in the service extensions exists in the top-level extensions
if cfg.Extensions[ref] == nil {
return &configError{
Expand Down
31 changes: 27 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
)

func TestDecodeConfig(t *testing.T) {
factories, err := ExampleComponents()
assert.Nil(t, err)
assert.NoError(t, err)

// Load the config
config, err := LoadConfigFile(t, path.Join(".", "testdata", "valid-config.yaml"), factories)
Expand Down Expand Up @@ -192,7 +193,7 @@ func TestSimpleConfig(t *testing.T) {
for _, test := range testCases {
t.Logf("TEST[%s]", test.name)
factories, err := ExampleComponents()
assert.Nil(t, err)
assert.NoError(t, err)

// Load the config
config, err := LoadConfigFile(t, path.Join(".", "testdata", test.name+".yaml"), factories)
Expand Down Expand Up @@ -287,7 +288,7 @@ func TestSimpleConfig(t *testing.T) {

func TestDecodeConfig_MultiProto(t *testing.T) {
factories, err := ExampleComponents()
assert.Nil(t, err)
assert.NoError(t, err)

// Load the config
config, err := LoadConfigFile(t, path.Join(".", "testdata", "multiproto-config.yaml"), factories)
Expand Down Expand Up @@ -383,7 +384,7 @@ func TestDecodeConfig_Invalid(t *testing.T) {
}

factories, err := ExampleComponents()
assert.Nil(t, err)
assert.NoError(t, err)

for _, test := range testCases {
_, err := LoadConfigFile(t, path.Join(".", "testdata", test.name+".yaml"), factories)
Expand All @@ -407,3 +408,25 @@ func TestDecodeConfig_Invalid(t *testing.T) {
}
}
}

func TestLoadEmptyConfig(t *testing.T) {
factories, err := ExampleComponents()
assert.NoError(t, err)

// Open the file for reading.
file, err := os.Open(path.Join(".", "testdata", "empty-config.yaml"))
require.NoError(t, err)

defer func() {
require.NoError(t, file.Close())
}()

// Read yaml config from file
v := NewViper()
v.SetConfigType("yaml")
require.NoError(t, v.ReadConfig(file))

// Load the config from viper using the given factories.
_, err = Load(v, factories)
assert.NoError(t, err)
}