From 44333795fe3548c60cdaa0e33c4c525c98075c35 Mon Sep 17 00:00:00 2001 From: chihshenghuang Date: Wed, 9 Aug 2023 16:07:01 -0700 Subject: [PATCH] fix non-existent valueFrom path value throw panic error issue --- pkg/customresourcestate/registry_factory.go | 6 ++++++ .../registry_factory_test.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/pkg/customresourcestate/registry_factory.go b/pkg/customresourcestate/registry_factory.go index 15ebdcbe72..968bb1f80c 100644 --- a/pkg/customresourcestate/registry_factory.go +++ b/pkg/customresourcestate/registry_factory.go @@ -278,6 +278,9 @@ func (c *compiledGauge) Values(v interface{}) (result []eachValue, errs []error) onError(fmt.Errorf("[%d]: %w", i, err)) continue } + if value == nil { + continue + } addPathLabels(it, c.LabelFromPath(), value.Labels) result = append(result, *value) } @@ -287,6 +290,9 @@ func (c *compiledGauge) Values(v interface{}) (result []eachValue, errs []error) onError(err) break } + if value == nil { + break + } addPathLabels(v, c.LabelFromPath(), value.Labels) result = append(result, *value) } diff --git a/pkg/customresourcestate/registry_factory_test.go b/pkg/customresourcestate/registry_factory_test.go index d68f22b617..3d53c696fd 100644 --- a/pkg/customresourcestate/registry_factory_test.go +++ b/pkg/customresourcestate/registry_factory_test.go @@ -210,6 +210,23 @@ func Test_values(t *testing.T) { }, wantResult: nil, wantErrors: []error{ errors.New("[foo]: got nil while resolving path"), }}, + {name: "exist path but valueFrom path is non-existent single", each: &compiledGauge{ + compiledCommon: compiledCommon{ + path: mustCompilePath(t, "spec", "replicas"), + }, + ValueFrom: mustCompilePath(t, "non-existent"), + }, wantResult: nil, wantErrors: nil, + }, + {name: "exist path but valueFrom path non-existent array", each: &compiledGauge{ + compiledCommon: compiledCommon{ + path: mustCompilePath(t, "status", "condition_values"), + labelFromPath: map[string]valuePath{ + "name": mustCompilePath(t, "name"), + }, + }, + ValueFrom: mustCompilePath(t, "non-existent"), + }, wantResult: nil, wantErrors: nil, + }, {name: "array", each: &compiledGauge{ compiledCommon: compiledCommon{ path: mustCompilePath(t, "status", "condition_values"),