Skip to content

Commit 60d4c4e

Browse files
authored
Merge pull request #2140 from chihshenghuang/fix-guage-value-func-panic
fix: Don't crash on non-existent valueFrom path values
2 parents 5709703 + 4433379 commit 60d4c4e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pkg/customresourcestate/registry_factory.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ func (c *compiledGauge) Values(v interface{}) (result []eachValue, errs []error)
282282
onError(fmt.Errorf("[%d]: %w", i, err))
283283
continue
284284
}
285+
if value == nil {
286+
continue
287+
}
285288
addPathLabels(it, c.LabelFromPath(), value.Labels)
286289
result = append(result, *value)
287290
}
@@ -291,6 +294,9 @@ func (c *compiledGauge) Values(v interface{}) (result []eachValue, errs []error)
291294
onError(err)
292295
break
293296
}
297+
if value == nil {
298+
break
299+
}
294300
addPathLabels(v, c.LabelFromPath(), value.Labels)
295301
result = append(result, *value)
296302
}

pkg/customresourcestate/registry_factory_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,23 @@ func Test_values(t *testing.T) {
210210
}, wantResult: nil, wantErrors: []error{
211211
errors.New("[foo]: got nil while resolving path"),
212212
}},
213+
{name: "exist path but valueFrom path is non-existent single", each: &compiledGauge{
214+
compiledCommon: compiledCommon{
215+
path: mustCompilePath(t, "spec", "replicas"),
216+
},
217+
ValueFrom: mustCompilePath(t, "non-existent"),
218+
}, wantResult: nil, wantErrors: nil,
219+
},
220+
{name: "exist path but valueFrom path non-existent array", each: &compiledGauge{
221+
compiledCommon: compiledCommon{
222+
path: mustCompilePath(t, "status", "condition_values"),
223+
labelFromPath: map[string]valuePath{
224+
"name": mustCompilePath(t, "name"),
225+
},
226+
},
227+
ValueFrom: mustCompilePath(t, "non-existent"),
228+
}, wantResult: nil, wantErrors: nil,
229+
},
213230
{name: "array", each: &compiledGauge{
214231
compiledCommon: compiledCommon{
215232
path: mustCompilePath(t, "status", "condition_values"),

0 commit comments

Comments
 (0)