From e434cfdce0460ca50b9e4eb39b13d3f67b6c892e Mon Sep 17 00:00:00 2001 From: Cloud Tsai Date: Tue, 20 Aug 2024 17:47:25 +0800 Subject: [PATCH] feat: Add new env to support -o flag Since the flag is hard to use for the container environment, create a new env EDGEX_OVERWRITE_CONFIG to provide the same feature support as -o/--overwrite flag. Close #747 Signed-off-by: Cloud Tsai --- bootstrap/config/config.go | 16 +++++++++------- bootstrap/environment/variables.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/bootstrap/config/config.go b/bootstrap/config/config.go index b7afedc1..f37c64ac 100644 --- a/bootstrap/config/config.go +++ b/bootstrap/config/config.go @@ -75,7 +75,6 @@ type Processor struct { wg *sync.WaitGroup configUpdated UpdatedStream dic *di.Container - overwriteConfig bool providerHasConfig bool commonConfigClient configuration.Client appConfigClient configuration.Client @@ -125,7 +124,6 @@ func (cp *Processor) Process( serviceConfig interfaces.Configuration, secretProvider interfaces.SecretProviderExt) error { - cp.overwriteConfig = cp.flags.OverwriteConfig() configProviderUrl := cp.flags.ConfigProviderUrl() remoteHosts := environment.GetRemoteServiceHosts(cp.lc, cp.flags.RemoteServiceHosts()) @@ -190,7 +188,7 @@ func (cp *Processor) Process( return fmt.Errorf("failed check for Configuration Provider has private configiuration: %s", err.Error()) } - if cp.providerHasConfig && !cp.overwriteConfig { + if cp.providerHasConfig && !cp.overwriteConfig() { privateServiceConfig, err = copyConfigurationStruct(serviceConfig) if err != nil { return err @@ -236,7 +234,7 @@ 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 { + if !useProvider || !cp.providerHasConfig || cp.overwriteConfig() { filePath := GetConfigFileLocation(cp.lc, cp.flags) configMap, err := cp.loadConfigYamlFromFile(filePath) if err != nil { @@ -255,7 +253,7 @@ func (cp *Processor) Process( } if useProvider { - if err := privateConfigClient.PutConfigurationMap(configMap, cp.overwriteConfig); err != nil { + if err := privateConfigClient.PutConfigurationMap(configMap, cp.overwriteConfig()); err != nil { return fmt.Errorf("could not push private configuration into Configuration Provider: %s", err.Error()) } @@ -323,6 +321,10 @@ func (cp *Processor) Process( return err } +func (cp *Processor) overwriteConfig() bool { + return environment.OverwriteConfig() || cp.flags.OverwriteConfig() +} + func getLocalIP() string { // Because of how UDP works, the connection is not established - no handshake is performed, no data is sent. // The purpose of this is to get the local IP address that a UDP connection would use if it were sending data to @@ -582,7 +584,7 @@ func (cp *Processor) LoadCustomConfigSection(updatableConfig interfaces.Updatabl err.Error()) } - if exists && !cp.flags.OverwriteConfig() { + if exists && !cp.overwriteConfig() { rawConfig, err := configClient.GetConfiguration(updatableConfig) if err != nil { return fmt.Errorf( @@ -626,7 +628,7 @@ func (cp *Processor) LoadCustomConfigSection(updatableConfig interfaces.Updatabl } var overwriteMessage = "" - if exists && cp.flags.OverwriteConfig() { + if exists && cp.overwriteConfig() { overwriteMessage = "(overwritten)" } cp.lc.Infof("Custom Config loaded from file and pushed to Configuration Provider %s", overwriteMessage) diff --git a/bootstrap/environment/variables.go b/bootstrap/environment/variables.go index 298c7ed3..5d8d5b95 100644 --- a/bootstrap/environment/variables.go +++ b/bootstrap/environment/variables.go @@ -47,6 +47,7 @@ const ( envKeyConfigFile = "EDGEX_CONFIG_FILE" envKeyFileURITimeout = "EDGEX_FILE_URI_TIMEOUT" envKeyRemoteServiceHosts = "EDGEX_REMOTE_SERVICE_HOSTS" + envOverwriteConfig = "EDGEX_OVERWRITE_CONFIG" noConfigProviderValue = "none" @@ -491,3 +492,18 @@ func GetRemoteServiceHosts(lc logger.LoggingClient, remoteHosts []string) []stri return strings.Split(envValue, ",") } + +// OverwriteConfig returns whether the local configuration should be pushed (overwrite) into the Configuration provider +func OverwriteConfig() bool { + envValue := os.Getenv(envOverwriteConfig) + if len(envValue) == 0 { + return false + } + + boolValue, err := strconv.ParseBool(envValue) + if err != nil { + return false + } + + return boolValue +}