Skip to content

Commit

Permalink
Merge pull request #1183 from squat/fix_webhook_panic
Browse files Browse the repository at this point in the history
webhook: fix panic on unexpected parser expression
  • Loading branch information
metalmatze authored May 23, 2024
2 parents 5eb637d + 2f40e9c commit 5da66df
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions kubernetes/api/v1alpha1/servicelevelobjective_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,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

0 comments on commit 5da66df

Please sign in to comment.