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

Don't crash on non-existent path values #1998

Merged
merged 2 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,11 @@ func (c compiledGauge) value(it interface{}) (*eachValue, error) {
Value: 0,
}, nil
}
// Don't error if there was not a type-casting issue (`toFloat64`), but rather a failed lookup.
// no it means no iterables were passed down, meaning that the path resolution never happened
Copy link
Member

Choose a reason for hiding this comment

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

should this be moved to the beginning of the function so we can return the error early?

Copy link
Member Author

@rexagod rexagod Feb 17, 2023

Choose a reason for hiding this comment

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

That'll fail cases that expect a zero in case of nil.

// If `valueFrom` was not resolved, respect `NilIsZero` and return.
if got == nil {
if c.NilIsZero {
return &eachValue{
Labels: labels,
Value: 0,
}, nil
}
// no it means no iterables were passed down, meaning that the path resolution never happened
if it == nil {
return nil, fmt.Errorf("got nil while resolving path")
}
// Don't error if there was not a type-casting issue (`toFloat64`), but rather a failed lookup.
return nil, nil
}

if it == nil {
return nil, fmt.Errorf("got nil while resolving path")
}
// Don't error if there was not a type-casting issue (`toFloat64`).
return nil, nil
}
value, err := toFloat64(got, c.NilIsZero)
Expand Down
12 changes: 12 additions & 0 deletions pkg/customresourcestate/registry_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package customresourcestate

import (
"encoding/json"
"errors"
"reflect"
"testing"

Expand Down Expand Up @@ -194,6 +195,17 @@ func Test_values(t *testing.T) {
}, wantResult: []eachValue{
newEachValue(t, 1.6563744e+09),
}},
{name: "non-existent path", each: &compiledGauge{
rexagod marked this conversation as resolved.
Show resolved Hide resolved
compiledCommon: compiledCommon{
path: mustCompilePath(t, "foo"),
labelFromPath: map[string]valuePath{
"name": mustCompilePath(t, "name"),
},
},
ValueFrom: mustCompilePath(t, "creationTimestamp"),
}, wantResult: nil, wantErrors: []error{
errors.New("[foo]: got nil while resolving path"),
}},
{name: "array", each: &compiledGauge{
compiledCommon: compiledCommon{
path: mustCompilePath(t, "status", "condition_values"),
Expand Down