From 08a490a4a966b2c48122454c5a3eed1291f78311 Mon Sep 17 00:00:00 2001 From: Mateusz Hawrus Date: Mon, 7 Aug 2023 16:29:00 +0200 Subject: [PATCH] fix slo validation --- manifest/v1alpha/slo.go | 10 +++++++++- manifest/v1alpha/validator.go | 13 ++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/manifest/v1alpha/slo.go b/manifest/v1alpha/slo.go index 7e65a92f..39091653 100644 --- a/manifest/v1alpha/slo.go +++ b/manifest/v1alpha/slo.go @@ -1,6 +1,8 @@ package v1alpha import ( + "github.com/pkg/errors" + "github.com/nobl9/nobl9-go/manifest" ) @@ -145,7 +147,7 @@ type AnomalyConfigAlertMethod struct { } // postParse implements postParser interface. -// nolint: unparam, unused +// nolint: unused, gocognit func (s SLO) postParse() (SLO, error) { // to keep BC with the ThousandEyes initial implementation (that did not support passing TestType), // we default `res.Spec.Indicator.RawMetrics.ThousandEyes.TestType` to a value that, until now, was implicitly assumed @@ -177,6 +179,12 @@ func (s SLO) postParse() (SLO, error) { // we have to make sure that old contract (with indicator defined directly on the SLO's spec) is also supported if s.Spec.Indicator.RawMetric != nil { for i := range s.Spec.Thresholds { + if s.Spec.Thresholds[i].RawMetric != nil { + return SLO{}, errors.New( + "raw metric definition must be provided on either" + + " indicator level ('SLO.spec.indicator.rawMetric') or" + + " objective level ('SLO.spec.objectives[*].rawMetric')") + } s.Spec.Thresholds[i].RawMetric = &RawMetricSpec{ MetricQuery: s.Spec.Indicator.RawMetric, } diff --git a/manifest/v1alpha/validator.go b/manifest/v1alpha/validator.go index ed28e7f5..70b2f612 100644 --- a/manifest/v1alpha/validator.go +++ b/manifest/v1alpha/validator.go @@ -698,7 +698,18 @@ func isBadOverTotalEnabledForDataSource(spec SLOSpec) bool { } func hasOnlyOneRawMetricDefinitionTypeOrNone(spec SLOSpec) bool { - return spec.ObjectivesRawMetricsCount() == 0 || !spec.containsIndicatorRawMetric() + indicatorHasRawMetric := spec.containsIndicatorRawMetric() + if indicatorHasRawMetric { + for _, threshold := range spec.Thresholds { + if !threshold.HasRawMetricQuery() { + continue + } + if !reflect.DeepEqual(threshold.RawMetric.MetricQuery, spec.Indicator.RawMetric) { + return false + } + } + } + return true } func areRawMetricsSetForAllThresholdsOrNone(spec SLOSpec) bool {