From c493044e4adc1b3f45b046c61c8b334b914a0db8 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Fri, 17 Feb 2023 00:07:02 +0530 Subject: [PATCH 1/2] Don't crash on non-existent path values Don't crash on non-existent path values in CRS. Signed-off-by: Pranshu Srivastava --- pkg/customresourcestate/registry_factory.go | 4 ++++ pkg/customresourcestate/registry_factory_test.go | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index 91bbde4735..ca825f26d6 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -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 } diff --git a/pkg/customresourcestate/registry_factory_test.go b/pkg/customresourcestate/registry_factory_test.go index bce47c1c4e..c1d6f1621c 100644 --- a/pkg/customresourcestate/registry_factory_test.go +++ b/pkg/customresourcestate/registry_factory_test.go @@ -18,6 +18,7 @@ package customresourcestate import ( "encoding/json" + "errors" "reflect" "testing" @@ -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"), From b54e43d555a984145cfbdf186cfe9a8b344c0510 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Fri, 17 Feb 2023 18:12:22 +0530 Subject: [PATCH 2/2] fixup! Don't crash on non-existent path values --- pkg/customresourcestate/registry_factory.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index ca825f26d6..9b4674d9de 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -434,7 +434,7 @@ func (c compiledGauge) value(it interface{}) (*eachValue, error) { 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. + // Don't error if there was not a type-casting issue (`toFloat64`). return nil, nil } value, err := toFloat64(got, c.NilIsZero)