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

Use new UnmarshalKey in a few locations. Fix edge cases. #29225

Merged
merged 16 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/config/structure/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ func UnmarshalKey(cfg model.Reader, key string, target interface{}, opts ...Unma
for _, o := range opts {
o(fs)
}
source, err := newNode(reflect.ValueOf(cfg.Get(key)))
rawval := cfg.Get(key)
// Don't create a reflect.Value out of nil, just return immediately
if rawval == nil {
Copy link
Contributor Author

@dustmop dustmop Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setupAutoDiscovery in cmd/agent/common/autodiscovery.go expects a nil config setting to return no error. Adding this fix gets the new-e2e-aml test to pass.

return nil
}
source, err := newNode(reflect.ValueOf(rawval))
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/config/structure/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ feature:
assert.Equal(t, diffcase.ENaBLEd, true)
}

func TestUnmarshalKeyMissing(t *testing.T) {
confYaml := `
feature:
enabled: "true"
`
mockConfig := mock.NewFromYAML(t, confYaml)
mockConfig.SetKnown("feature")

// If the data from the config is missing, UnmarshalKey is a no-op, does
// nothing, and returns no error
var endpoints = []Endpoint{}
err := UnmarshalKey(mockConfig, "config_providers", &endpoints)
assert.NoError(t, err)
}

func TestMapGetChildNotFound(t *testing.T) {
m := map[string]string{"a": "apple", "b": "banana"}
n, err := newNode(reflect.ValueOf(m))
Expand Down
Loading