Skip to content

Commit 4433379

Browse files
fix non-existent valueFrom path value throw panic error issue
1 parent a9bdda0 commit 4433379

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
@@ -278,6 +278,9 @@ func (c *compiledGauge) Values(v interface{}) (result []eachValue, errs []error)
278278
onError(fmt.Errorf("[%d]: %w", i, err))
279279
continue
280280
}
281+
if value == nil {
282+
continue
283+
}
281284
addPathLabels(it, c.LabelFromPath(), value.Labels)
282285
result = append(result, *value)
283286
}
@@ -287,6 +290,9 @@ func (c *compiledGauge) Values(v interface{}) (result []eachValue, errs []error)
287290
onError(err)
288291
break
289292
}
293+
if value == nil {
294+
break
295+
}
290296
addPathLabels(v, c.LabelFromPath(), value.Labels)
291297
result = append(result, *value)
292298
}

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)