Skip to content

Commit

Permalink
feat: Add env override capability for common configuration (#4449)
Browse files Browse the repository at this point in the history
* feat: Add env override capability for common configuration

closes #4448

Signed-off-by: Leonard Goodell <[email protected]>
  • Loading branch information
Lenny Goodell authored Mar 16, 2023
1 parent fb374ba commit 98f26a0
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 17 deletions.
1 change: 1 addition & 0 deletions cmd/core-command/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
clientkey = ""

[Service]
Host = "localhost"
Port = 59882
StartupMsg = "This is the Core Command Microservice"

Expand Down
2 changes: 1 addition & 1 deletion cmd/core-common-config-bootstrapper/res/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ all-services:
SecuritySecretsStored: false
SecurityConsulTokensRequested: false
SecurityConsulTokenDuration: false
Tags: # Contains the service level tags to be attached to all the service's metrics
# Tags: # Contains the service level tags to be attached to all the service's metrics
# Gateway: "my-iot-gateway" # Tag must be added here or via Consul Env Override can only change existing value, not added new ones.

Service:
Expand Down
1 change: 1 addition & 0 deletions cmd/core-data/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ PersistData = true
# Gateway="my-iot-gateway" # Tag must be added here or via Consul Env Override can only change existing value, not added new ones.

[Service]
Host = "localhost"
Port = 59880
StartupMsg = "This is the Core Data Microservice"

Expand Down
1 change: 1 addition & 0 deletions cmd/core-metadata/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ LogLevel = "INFO"
Validation = false

[Service]
Host = "localhost"
Port = 59881
StartupMsg = "This is the EdgeX Core Metadata Microservice"

Expand Down
1 change: 1 addition & 0 deletions cmd/security-proxy-auth/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
LogLevel = "INFO"

[Service]
Host = "localhost"
Port = 59842
StartupMsg = "This is the proxy authentication microservice"
1 change: 1 addition & 0 deletions cmd/security-spiffe-token-provider/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
LogLevel = "DEBUG"

[Service]
Host = "localhost"
Port = 59841
StartupMsg = "This is the SPIFFE token provider microservice"

Expand Down
1 change: 1 addition & 0 deletions cmd/support-notifications/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ResendInterval = "5s"
password = ""

[Service]
Host = "localhost"
Port = 59860
StartupMsg = "This is the Support Notifications Microservice"

Expand Down
1 change: 1 addition & 0 deletions cmd/support-scheduler/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ScheduleIntervalTime = 500
LogLevel = "INFO"

[Service]
Host = "localhost"
Port = 59861
StartupMsg = "This is the Support Scheduler Microservice"

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.20

require (
github.com/eclipse/paho.mqtt.golang v1.4.2
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.46
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.50
github.com/edgexfoundry/go-mod-configuration/v3 v3.0.0-dev.7
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.27
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.16
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.30
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.18
github.com/edgexfoundry/go-mod-secrets/v3 v3.0.0-dev.9
github.com/fxamacker/cbor/v2 v2.4.0
github.com/gomodule/redigo v1.8.9
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=
github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA=
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.46 h1:A15UzeUgFfQM+R27LvXeoUJAbVFgY41NEhMcDF8XOQU=
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.46/go.mod h1:nweayvPW3G+nBZwwdRB6lWq3u0e1rWGvReEWEjDQjFE=
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.50 h1:iP/2tKszlM6Ud1El3kY9wdB7lnsvuIHWquYNLDXwvQw=
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.50/go.mod h1:oz5+G3SImWqKvRIcKrh/ErXae9nNapmDNnlXsdykpGg=
github.com/edgexfoundry/go-mod-configuration/v3 v3.0.0-dev.7 h1:pOH2GLDg1KB4EmAzo6IEvl4NEVFAw3ywxPUMa5Wi1Vw=
github.com/edgexfoundry/go-mod-configuration/v3 v3.0.0-dev.7/go.mod h1:ZZbOu7K0/P8B1VKhZygVujLQyhvWuPe0E2vC/k2yscw=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.27 h1:lKM/NwRBL/pddoSnlXbFIlpgu+XozWbqz608EeB7S94=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.27/go.mod h1:QBzXOAoyLzBm5k9CshgH9CR9nWMNXxZjknBFMnGJN/A=
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.16 h1:JdZiqZ6CAboZ3+GuWZLyXc42y8HfsH1CmsIKNPY+0PY=
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.16/go.mod h1:3q+Ys51HwpR9/kOv6sT7+EwAANgOfFWi509sEP5iPEo=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.30 h1:diarfNbfUeSYD7Hq8pOqHzxMtIbaPO6W8nWq34W8X6g=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.30/go.mod h1:SYoD+tmUP/zwWuuIySmsQSjPx6MHP+2w9FsLgm0IR7A=
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.18 h1:8empqTOIVhKzebC3gnP9C0LGQvwXtHLJF8x8AqlTGLM=
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.18/go.mod h1:Sf7lvKtICWRy0Tmvw+bV6L1wdXKRaTSmKmJKw4Caxe4=
github.com/edgexfoundry/go-mod-registry/v3 v3.0.0-dev.5 h1:FUUfsQUKHJqSwOXEj0j/qAn+uoWzM8nV7IOapGf7D80=
github.com/edgexfoundry/go-mod-registry/v3 v3.0.0-dev.5/go.mod h1:MroNeX/i6ymRu8cyuRB4U5zdBusyUpFhUphko6pInTQ=
github.com/edgexfoundry/go-mod-secrets/v3 v3.0.0-dev.9 h1:12UHZG/Nl+dfFOaNnC87ANP/FQ3rOXe0Z6NVlYdmYgU=
Expand Down
42 changes: 35 additions & 7 deletions internal/core/common_config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
"github.com/edgexfoundry/go-mod-core-contracts/v3/models"
"github.com/pelletier/go-toml"
"gopkg.in/yaml.v3"
)

Expand All @@ -44,7 +45,7 @@ const (
func Main(ctx context.Context, cancel context.CancelFunc) {
startupTimer := startup.NewStartUpTimer(common.CoreCommonConfigServiceKey)

// All common command-line flags have been moved to DefaultCommonFlags. Service specific flags can be add here,
// All common command-line flags have been moved to DefaultCommonFlags. Service specific flags can be added here,
// by inserting service specific flag prior to call to commonFlags.Parse().
// Example:
// flags.FlagSet.StringVar(&myvar, "m", "", "Specify a ....")
Expand Down Expand Up @@ -117,17 +118,18 @@ func Main(ctx context.Context, cancel context.CancelFunc) {
os.Exit(1)
}

lc.Infof("configuration exists: %v", hasConfig)

// load the yaml file and push it using the config client
if !hasConfig || f.OverwriteConfig() {
lc.Info("Pushing common configuration. It doesn't exists or overwrite flag is set")

yamlFile := config.GetConfigLocation(lc, f)
lc.Infof("parsing %s for configuration", yamlFile)
err = loadYaml(lc, yamlFile, configClient)
err = pushConfiguration(lc, yamlFile, configClient)
if err != nil {
lc.Error(err.Error())
os.Exit(1)
}
} else {
lc.Info("Skipped pushing common configuration. It already exists and overwrite flag not set")
}

lc.Info("Core Common Config exiting")
Expand Down Expand Up @@ -157,13 +159,13 @@ func translateInterruptToCancel(ctx context.Context, wg *sync.WaitGroup, cancel
}()
}

func loadYaml(lc logger.LoggingClient, yamlFile string, configClient configuration.Client) error {
func pushConfiguration(lc logger.LoggingClient, yamlFile string, configClient configuration.Client) error {
// push not done flag to configClient
err := configClient.PutConfigurationValue(commonConfigDone, []byte("false"))
if err != nil {
return fmt.Errorf("failed to push %s on startup: %s", commonConfigDone, err.Error())
}
lc.Infof("reading %s", yamlFile)
lc.Infof("Using common configuration from %s", yamlFile)
contents, err := os.ReadFile(yamlFile)
if err != nil {
return fmt.Errorf("failed to read common configuration file %s: %s", yamlFile, err.Error())
Expand All @@ -179,6 +181,11 @@ func loadYaml(lc logger.LoggingClient, yamlFile string, configClient configurati

kv = buildKeyValues(data, kv, "")

kv, err = applyEnvOverrides(kv, lc)
if err != nil {
return fmt.Errorf("failed to apply env overrides to common configuration: %s", err.Error())
}

keys := make([]string, 0, len(kv))

for k := range kv {
Expand All @@ -203,6 +210,9 @@ func loadYaml(lc logger.LoggingClient, yamlFile string, configClient configurati
if err != nil {
return fmt.Errorf("failed to push %s on completion: %s", commonConfigDone, err.Error())
}

lc.Info("Common configuration has been pushed to into Configuration Provider with overrides applied")

return nil
}

Expand All @@ -229,3 +239,21 @@ func buildKeyValues(data map[string]interface{}, kv map[string]interface{}, orig

return kv
}

func applyEnvOverrides(keyValues map[string]any, lc logger.LoggingClient) (map[string]any, error) {
env := environment.NewVariables(lc)
tomlTree, err := toml.TreeFromMap(keyValues)
if err != nil {
return nil, err
}

overrideCount, err := env.OverrideTomlValues(tomlTree)
if err != nil {
return nil, err
}

keyValues = tomlTree.ToMap()
lc.Infof("Common configuration loaded from file with %d overrides applied", overrideCount)

return keyValues, nil
}

0 comments on commit 98f26a0

Please sign in to comment.