Skip to content

Commit

Permalink
Don't crash on non-existent path values
Browse files Browse the repository at this point in the history
Don't crash on non-existent path values in CRS.

Signed-off-by: Pranshu Srivastava <rexagod@gmail.com>
  • Loading branch information
rexagod committed Feb 16, 2023
1 parent da10923 commit c493044
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ func (c compiledGauge) value(it interface{}) (*eachValue, error) {
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
}
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{
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

0 comments on commit c493044

Please sign in to comment.