Skip to content

Commit

Permalink
fix non-existent valueFrom path value throw panic error issue
Browse files Browse the repository at this point in the history
  • Loading branch information
chihshenghuang committed Aug 20, 2023
1 parent a9bdda0 commit 4433379
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/customresourcestate/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/customresourcestate/registry_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 4433379

Please sign in to comment.