Skip to content

Commit

Permalink
set viper delimiter to :: to support . in config keys
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Dittamo <[email protected]>
  • Loading branch information
pvditt committed Jan 17, 2025
1 parent 729e71a commit c17c152
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
13 changes: 7 additions & 6 deletions flytestdlib/config/tests/accessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
k8sRand "k8s.io/apimachinery/pkg/util/rand"

"github.com/flyteorg/flyte/flytestdlib/config"
"github.com/flyteorg/flyte/flytestdlib/config/viper"
"github.com/flyteorg/flyte/flytestdlib/internal/utils"
)

Expand Down Expand Up @@ -132,7 +133,7 @@ func TestAccessor_InitializePflags(t *testing.T) {

set := pflag.NewFlagSet("test", pflag.ContinueOnError)
v.InitializePflags(set)
key := "MY_COMPONENT.STR2"
key := fmt.Sprintf("MY_COMPONENT%sSTR2", viper.KeyDelim)
assert.NoError(t, os.Setenv(key, "123"))
defer func() { assert.NoError(t, os.Unsetenv(key)) }()
assert.NoError(t, v.UpdateConfig(context.TODO()))
Expand Down Expand Up @@ -179,10 +180,10 @@ func TestAccessor_InitializePflags(t *testing.T) {

set := pflag.NewFlagSet("test", pflag.ExitOnError)
v.InitializePflags(set)
assert.NoError(t, set.Parse([]string{"--my-component.nested.int-val=3"}))
assert.NoError(t, set.Parse([]string{fmt.Sprintf("--my-component%snested%sint-val=3", viper.KeyDelim, viper.KeyDelim)}))
assert.True(t, set.Parsed())

flagValue, err := set.GetInt("my-component.nested.int-val")
flagValue, err := set.GetInt(fmt.Sprintf("my-component%snested%sint-val", viper.KeyDelim, viper.KeyDelim))
assert.NoError(t, err)
assert.Equal(t, 3, flagValue)

Expand Down Expand Up @@ -414,7 +415,7 @@ func TestAccessor_UpdateConfig(t *testing.T) {
SearchPaths: []string{filepath.Join("testdata", "config.yaml")},
RootSection: reg,
})
key := strings.ToUpper("my-component.str")
key := strings.ToUpper(fmt.Sprintf("my-component%sstr", viper.KeyDelim))
assert.NoError(t, os.Setenv(key, "Set From Env"))
defer func() { assert.NoError(t, os.Unsetenv(key)) }()
assert.NoError(t, v.UpdateConfig(context.TODO()))
Expand All @@ -428,7 +429,7 @@ func TestAccessor_UpdateConfig(t *testing.T) {
assert.NoError(t, err)

v := provider(config.Options{RootSection: reg})
key := strings.ToUpper("my-component.str3")
key := strings.ToUpper(fmt.Sprintf("my-component%sstr3", viper.KeyDelim))
assert.NoError(t, os.Setenv(key, "Set From Env"))
defer func() { assert.NoError(t, os.Unsetenv(key)) }()
assert.NoError(t, v.UpdateConfig(context.TODO()))
Expand Down Expand Up @@ -521,7 +522,7 @@ func TestAccessor_UpdateConfig(t *testing.T) {
SearchPaths: []string{filepath.Join("testdata", "config.yaml")},
RootSection: reg,
})
key := strings.ToUpper("my-component.str")
key := strings.ToUpper(fmt.Sprintf("my-component%sstr", viper.KeyDelim))
assert.NoError(t, os.Setenv(key, "Set From Env"))
defer func() { assert.NoError(t, os.Unsetenv(key)) }()
assert.NoError(t, v.UpdateConfig(context.TODO()))
Expand Down
2 changes: 1 addition & 1 deletion flytestdlib/config/viper/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (c CollectionProxy) MergeConfig(in io.Reader) error {
}

func (c CollectionProxy) MergeAllConfigs() (all Viper, err error) {
combinedConfig := viperLib.New()
combinedConfig := viperLib.NewWithOptions(viperLib.KeyDelimiter(KeyDelim))
if c.envVars != nil {
for _, envConfig := range c.envVars {
err = combinedConfig.BindEnv(envConfig...)
Expand Down
15 changes: 9 additions & 6 deletions flytestdlib/config/viper/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

const (
keyDelim = "."
KeyDelim = "::"
)

var (
Expand Down Expand Up @@ -81,7 +81,7 @@ func (v viperAccessor) addSectionsPFlags(flags *pflag.FlagSet) (err error) {

func (v viperAccessor) addSubsectionsPFlags(flags *pflag.FlagSet, rootKey string, root config.Section) error {
for key, section := range root.GetSections() {
prefix := rootKey + key + keyDelim
prefix := rootKey + key + KeyDelim
if asPFlagProvider, ok := section.GetConfig().(config.PFlagProvider); ok {
flags.AddFlagSet(asPFlagProvider.GetPFlagSet(prefix))
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func (v viperAccessor) bindViperConfigsEnvDepth(m map[string]interface{}, prefix
for key, val := range m {
subKey := prefix + key
if asMap, ok := val.(map[string]interface{}); ok {
errs.Append(v.bindViperConfigsEnvDepth(asMap, subKey+keyDelim))
errs.Append(v.bindViperConfigsEnvDepth(asMap, subKey+KeyDelim))
} else {
errs.Append(v.viper.BindEnv(subKey, strings.ToUpper(strings.Replace(subKey, "-", "_", -1))))
}
Expand Down Expand Up @@ -272,6 +272,9 @@ func (v viperAccessor) parseViperConfigRecursive(root config.Section, settings i
if asMap, casted := settings.(map[string]interface{}); casted {
myMap := map[string]interface{}{}
for childKey, childValue := range asMap {
if childKey == "default-annotations" {
logger.Debugf(context.Background(), "Found default-annotations in config. Skipping.")
}
if childSection, found := root.GetSections()[childKey]; found {
errs.Append(v.parseViperConfigRecursive(childSection, childValue))
} else {
Expand Down Expand Up @@ -396,7 +399,7 @@ func (v viperAccessor) sendUpdatedEvents(ctx context.Context, root config.Sectio
section.GetConfigUpdatedHandler()(ctx, section.GetConfig())
}

v.sendUpdatedEvents(ctx, section, forceSend, sectionKey+key+keyDelim)
v.sendUpdatedEvents(ctx, section, forceSend, sectionKey+key+KeyDelim)
}
}

Expand All @@ -413,15 +416,15 @@ func newAccessor(opts config.Options) *viperAccessor {
vipers := make([]Viper, 0, 1)
configFiles := files.FindConfigFiles(opts.SearchPaths)
for _, configFile := range configFiles {
v := viperLib.New()
v := viperLib.NewWithOptions(viperLib.KeyDelimiter(KeyDelim))
v.SetConfigFile(configFile)

vipers = append(vipers, v)
}

// Create a default viper even if we couldn't find any matching files
if len(configFiles) == 0 {
v := viperLib.New()
v := viperLib.NewWithOptions(viperLib.KeyDelimiter(KeyDelim))
vipers = append(vipers, v)
}

Expand Down

0 comments on commit c17c152

Please sign in to comment.