Skip to content

Commit

Permalink
Allow config.Load to load partial config
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed May 3, 2020
1 parent 5d1aacc commit 3a5044c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 41 deletions.
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
8 changes: 4 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

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 +192,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 +287,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 +383,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 Down
17 changes: 5 additions & 12 deletions config/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,27 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/zap"

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

// LoadConfigFile loads a config from file.
// LoadConfigFile loads a config from file. Fails if file is not accessible.
func LoadConfigFile(t *testing.T, fileName string, factories Factories) (*configmodels.Config, error) {
// Open the file for reading.
file, err := os.Open(filepath.Clean(fileName))
if err != nil {
t.Error(err)
return nil, err
}
require.NoError(t, err)

defer func() {
if errClose := file.Close(); errClose != nil {
t.Error(errClose)
}
require.NoError(t, file.Close())
}()

// Read yaml config from file
v := NewViper()
v.SetConfigType("yaml")
err = v.ReadConfig(file)
if err != nil {
t.Errorf("unable to read yaml, %v", err)
return nil, err
}
require.NoError(t, err)

// Load the config from viper using the given factories.
cfg, err := Load(v, factories)
Expand Down

0 comments on commit 3a5044c

Please sign in to comment.