Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hush-hush committed Nov 7, 2024
1 parent 6da2c74 commit 41d9e5d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
14 changes: 8 additions & 6 deletions pkg/config/nodetreemodel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ func (c *ntmConfig) set(key string, value interface{}, tree InnerNode, source mo
if tree == nil {
return false, fmt.Errorf("cannot assign to nil Node")
}
parts := strings.Split(strings.ToLower(key), ",")
parts := splitKey(key)
return tree.SetAt(parts, value, source)
}

func (c *ntmConfig) setDefault(key string, value interface{}) {
parts := strings.Split(strings.ToLower(key), ",")
parts := splitKey(key)
// TODO: Ensure that for default tree, setting nil to a node will not override
// an existing value
_, _ = c.defaults.SetAt(parts, value, model.SourceDefault)
Expand Down Expand Up @@ -267,6 +267,7 @@ func (c *ntmConfig) isReady() bool {
func (c *ntmConfig) buildEnvVars() {
root := newInnerNode(nil)
envWarnings := []string{}

for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
if len(pair) != 2 {
Expand All @@ -286,13 +287,14 @@ func (c *ntmConfig) buildEnvVars() {
}

func (c *ntmConfig) insertNodeFromString(curr InnerNode, key string, envval string) error {
parts := strings.Split(key, ".")
var actualValue interface{} = envval
// TODO: When the nodetreemodel config is further along, we should get the default[key] node
// and use its type to convert the envval into something appropriate.
if xform, found := c.envTransform[key]; found {
actualValue = xform(envval)
}

parts := splitKey(key)
_, err := curr.SetAt(parts, actualValue, model.SourceEnvVar)
return err
}
Expand Down Expand Up @@ -352,7 +354,7 @@ func (c *ntmConfig) leafAtPath(key string) LeafNode {
return missingLeaf
}

pathParts := strings.Split(strings.ToLower(key), ".")
pathParts := splitKey(key)
var curr Node = c.root
for _, part := range pathParts {
next, err := curr.GetChild(part)
Expand All @@ -372,7 +374,7 @@ func (c *ntmConfig) GetNode(key string) (Node, error) {
if !c.isReady() {
return nil, log.Errorf("attempt to read key before config is constructed: %s", key)
}
pathParts := strings.Split(key, ".")
pathParts := splitKey(key)
var curr Node = c.root
for _, part := range pathParts {
next, err := curr.GetChild(part)
Expand Down Expand Up @@ -810,8 +812,8 @@ func (c *ntmConfig) GetEnvVars() []string {

// BindEnvAndSetDefault binds an environment variable and sets a default for the given key
func (c *ntmConfig) BindEnvAndSetDefault(key string, val interface{}, envvars ...string) {
c.SetDefault(key, val)
c.BindEnv(key, envvars...) //nolint:errcheck
c.SetDefault(key, val)
}

// Warnings just returns nil
Expand Down
7 changes: 3 additions & 4 deletions pkg/config/nodetreemodel/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ func TestBuildDefaultMakesTooManyNodes(t *testing.T) {

// Test that default, file, and env layers can build, get merged, and retrieve settings
func TestBuildDefaultFileAndEnv(t *testing.T) {
t.Skip("fix merge to enable this test")

configData := `network_path:
collector:
workers: 6
Expand All @@ -42,7 +40,7 @@ secret_backend_command: ./my_secret_fetcher.sh
os.Setenv("DD_SECRET_BACKEND_TIMEOUT", "60")
os.Setenv("DD_NETWORK_PATH_COLLECTOR_INPUT_CHAN_SIZE", "23456")

cfg := NewConfig("test", "DD", nil)
cfg := NewConfig("test", "DD", strings.NewReplacer(".", "_"))
cfg.BindEnvAndSetDefault("network_path.collector.input_chan_size", 100000)
cfg.BindEnvAndSetDefault("network_path.collector.processing_chan_size", 100000)
cfg.BindEnvAndSetDefault("network_path.collector.workers", 4)
Expand All @@ -64,7 +62,7 @@ secret_backend_command: ./my_secret_fetcher.sh
{
description: "nested setting from env var works",
setting: "network_path.collector.input_chan_size",
expectValue: 23456,
expectValue: "23456",
expectSource: model.SourceEnvVar,
},
{
Expand Down Expand Up @@ -101,6 +99,7 @@ secret_backend_command: ./my_secret_fetcher.sh

for i, tc := range testCases {
t.Run(fmt.Sprintf("case %d: setting %s", i, tc.setting), func(t *testing.T) {

val := cfg.Get(tc.setting)
require.Equal(t, tc.expectValue, val)
src := cfg.GetSource(tc.setting)
Expand Down
36 changes: 36 additions & 0 deletions pkg/config/nodetreemodel/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package nodetreemodel

import (
"fmt"
"strings"
)

func printDepth(depth int) {
for i := 0; i < depth; i++ {
fmt.Printf("\t")
}
}

func printTree(root InnerNode, depth int) {
for _, k := range root.ChildrenKeys() {
printDepth(depth)

c, _ := root.GetChild(k)
if leaf, ok := c.(LeafNode); ok {
v, _ := leaf.GetAny()
fmt.Printf("%s: %v\n", k, v)
} else {
fmt.Printf("%s:\n", k)
printTree(c.(InnerNode), depth+1)
}
}
}

func splitKey(key string) []string {
return strings.Split(strings.ToLower(key), ".")
}

0 comments on commit 41d9e5d

Please sign in to comment.