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

feat: Use loadfile that allows reading Config from local file or uri #558

Merged
merged 8 commits into from
Jul 17, 2023
8 changes: 7 additions & 1 deletion bootstrap/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,14 @@ func (cp *Processor) Process(

// Now load the private config from a local file if any of these conditions are true
if !useProvider || !cp.providerHasConfig || cp.overwriteConfig {
requestTimeout, err := time.ParseDuration(environment.GetRequestTimeout(cp.lc, cp.flags.RequestTimeout()))
if err != nil {
cp.lc.Debug("Failed to parse Service.RequestTimeout configuration value: %v, using default value", err)
requestTimeout = 15 * time.Second
}

filePath := GetConfigFileLocation(cp.lc, cp.flags)
configMap, err := cp.loadConfigYamlFromFile(filePath, file.DefaultTimeout, secretProvider)
configMap, err := cp.loadConfigYamlFromFile(filePath, requestTimeout, secretProvider)
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions bootstrap/environment/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
envKeyConfigDir = "EDGEX_CONFIG_DIR"
envKeyProfile = "EDGEX_PROFILE"
envKeyConfigFile = "EDGEX_CONFIG_FILE"
envKeyRequestTimeout = "EDGEX_SERVICE_REQUESTTIMEOUT"

noConfigProviderValue = "none"

Expand Down Expand Up @@ -456,3 +457,15 @@ func logEnvironmentOverride(lc logger.LoggingClient, name string, key string, va
}
lc.Infof("Variables override of '%s' by environment variable: %s=%s", name, key, valueStr)
}

// GetRequestTimeout gets the configuration request timeout value from a Variables variable value (if it exists)
// or uses passed in value.
func GetRequestTimeout(lc logger.LoggingClient, requestTimeout string) string {
envValue := os.Getenv(envKeyRequestTimeout)
if len(envValue) > 0 {
requestTimeout = envValue
logEnvironmentOverride(lc, "-rt/--requestTimeout", envKeyRequestTimeout, envValue)
}

return requestTimeout
}
29 changes: 29 additions & 0 deletions bootstrap/environment/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,32 @@ func TestOverrideConfigMapValues(t *testing.T) {
})
}
}

func TestGetRequestTimeout(t *testing.T) {
_, lc := initializeTest()

testCases := []struct {
TestName string
EnvTimeout string
PassedInTimeout string
ExpectedTimeout string
}{
{"With Env Var", envKeyRequestTimeout, "15", "14"},
{"With No Env Var", "", "15", "15"},
{"With No Env Var and no passed in", "", "", ""},
}

for _, test := range testCases {
t.Run(test.TestName, func(t *testing.T) {
os.Clearenv()

if len(test.EnvTimeout) > 0 {
err := os.Setenv(test.EnvTimeout, test.ExpectedTimeout)
require.NoError(t, err)
}

actual := GetRequestTimeout(lc, test.PassedInTimeout)
assert.Equal(t, test.ExpectedTimeout, actual)
})
}
}
11 changes: 11 additions & 0 deletions bootstrap/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
const (
DefaultConfigProvider = "consul.http://localhost:8500"
DefaultConfigFile = "configuration.yaml"
DefaultRequestTimeout = "15"
)

// Common is an interface that defines AP for the common command-line flags used by most EdgeX services
Expand All @@ -35,6 +36,7 @@ type Common interface {
Profile() string
ConfigDirectory() string
ConfigFileName() string
RequestTimeout() string
CommonConfig() string
Parse([]string)
Help()
Expand All @@ -52,6 +54,7 @@ type Default struct {
profile string
configDir string
configFileName string
requestTimeout string
}

// NewWithUsage returns a Default struct.
Expand Down Expand Up @@ -93,6 +96,8 @@ func (d *Default) Parse(arguments []string) {
d.FlagSet.BoolVar(&d.overwriteConfig, "o", false, "")
d.FlagSet.StringVar(&d.configFileName, "cf", DefaultConfigFile, "")
d.FlagSet.StringVar(&d.configFileName, "configFile", DefaultConfigFile, "")
d.FlagSet.StringVar(&d.requestTimeout, "rt", DefaultRequestTimeout, "")
d.FlagSet.StringVar(&d.requestTimeout, "requestTimeout", DefaultRequestTimeout, "")
d.FlagSet.StringVar(&d.profile, "profile", "", "")
d.FlagSet.StringVar(&d.profile, "p", "", ".")
d.FlagSet.StringVar(&d.configDir, "configDir", "", "")
Expand Down Expand Up @@ -151,6 +156,11 @@ func (d *Default) CommonConfig() string {
return d.commonConfig
}

// CommonConfig returns the location for the common configuration
func (d *Default) RequestTimeout() string {
return d.requestTimeout
}

// Help displays the usage help message and exit.
func (d *Default) Help() {
d.helpCallback()
Expand All @@ -169,6 +179,7 @@ func (d *Default) helpCallback() {
" *** Use with cation *** Use will clobber existing settings in provider,\n"+
" problematic if those settings were edited by hand intentionally\n"+
" -cf, --configFile <name> Indicates name of the local configuration file. Defaults to configuration.toml\n"+
" -rt, --requestTimeout <seconds> Set the uri file load request timeout\n"+
" -p, --profile <name> Indicate configuration profile other than default\n"+
" -cd, --configDir Specify local configuration directory\n"+
" -r, --registry Indicates service should use Registry.\n"+
Expand Down