Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webhook: fix panic on unedpected parser expression #1182

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions kubernetes/api/v1alpha1/servicelevelobjective_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,20 @@ func (in *ServiceLevelObjective) validate() (admission.Warnings, error) {

switch parsedTotal.Type() {
case parser.ValueTypeVector:
v := parsedTotal.(*parser.VectorSelector)
v, ok := parsedTotal.(*parser.VectorSelector)
if !ok {
return warnings, fmt.Errorf("latency total metric must be a vector selector, but got %T", parsedTotal)
}
if !strings.HasSuffix(v.Name, "_count") {
warnings = append(warnings, "latency total metric should usually be a histogram count")
}
}
switch parsedSuccess.Type() {
case parser.ValueTypeVector:
v := parsedSuccess.(*parser.VectorSelector)
v, ok := parsedSuccess.(*parser.VectorSelector)
if !ok {
return warnings, fmt.Errorf("latency success metric must be a vector selector, but got %T", parsedSuccess)
}
var bucketFound bool
for _, matcher := range v.LabelMatchers {
if matcher.Name == labels.BucketLabel {
Expand Down
7 changes: 7 additions & 0 deletions kubernetes/api/v1alpha1/servicelevelobjective_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,13 @@ func TestServiceLevelObjective_Validate(t *testing.T) {
latency.Spec.ServiceLevelIndicator.Latency.Success.Metric = `foo{le="foo"}`
_, err = latency.ValidateCreate()
require.EqualError(t, err, `latency success metric must contain a le label matcher with a float value: strconv.ParseFloat: parsing "foo": invalid syntax`)

latency.Spec.ServiceLevelIndicator.Latency.Success.Metric = `foo{le="1.0"} or vector(0)`
_, err = latency.ValidateCreate()
require.EqualError(t, err, `latency success metric must be a vector selector, but got *parser.BinaryExpr`)
latency.Spec.ServiceLevelIndicator.Latency.Total.Metric = `foo{le="1.0"} or vector(0)`
_, err = latency.ValidateCreate()
require.EqualError(t, err, `latency total metric must be a vector selector, but got *parser.BinaryExpr`)
})
})

Expand Down
Loading