From 0f0902035940927269a8eb9ad77d457197961a11 Mon Sep 17 00:00:00 2001 From: huanggze Date: Thu, 13 Aug 2020 17:33:36 +0800 Subject: [PATCH] fix: time skew in monitoring responses Signed-off-by: huanggze --- pkg/kapis/monitoring/v1alpha3/helper.go | 16 +++++++++------- pkg/kapis/monitoring/v1alpha3/helper_test.go | 14 ++------------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/pkg/kapis/monitoring/v1alpha3/helper.go b/pkg/kapis/monitoring/v1alpha3/helper.go index 03e17e99..d6e50f4e 100644 --- a/pkg/kapis/monitoring/v1alpha3/helper.go +++ b/pkg/kapis/monitoring/v1alpha3/helper.go @@ -37,7 +37,7 @@ const ( ComponentAPIServer = "apiserver" ComponentScheduler = "scheduler" - ErrNoHit = "'end' must be after the namespace creation time." + ErrNoHit = "'end' or 'time' must be after the namespace creation time." ErrParamConflict = "'time' and the combination of 'start' and 'end' are mutually exclusive." ErrInvalidStartEnd = "'start' must be before 'end'." ErrInvalidPage = "Invalid parameter 'page'." @@ -256,21 +256,23 @@ func (h handler) makeQueryOptions(r reqParams, lvl monitoring.Level) (q queryOpt cts := ns.CreationTimestamp.Time // Query should happen no earlier than namespace's creation time. - // For range query, check and mutate `start`. For instant query, check and mutate `time`. + // For range query, check and mutate `start`. For instant query, check `time`. // In range query, if `start` and `end` are both before namespace's creation time, it causes no hit. if !q.isRangeQuery() { if q.time.Before(cts) { - q.time = cts + return q, errors.New(ErrNoHit) } } else { - if q.start.Before(cts) { - q.start = cts - } if q.end.Before(cts) { return q, errors.New(ErrNoHit) } + if q.start.Before(cts) { + q.start = q.end + for q.start.Add(-q.step).After(cts) { + q.start = q.start.Add(-q.step) + } + } } - } // Parse sorting and paging params diff --git a/pkg/kapis/monitoring/v1alpha3/helper_test.go b/pkg/kapis/monitoring/v1alpha3/helper_test.go index 6a0169e9..99ef67fd 100644 --- a/pkg/kapis/monitoring/v1alpha3/helper_test.go +++ b/pkg/kapis/monitoring/v1alpha3/helper_test.go @@ -104,7 +104,7 @@ func TestParseRequestParams(t *testing.T) { }, }, expected: queryOptions{ - start: time.Unix(1585836666, 0), + start: time.Unix(1585836699, 0), end: time.Unix(1585839999, 0), step: time.Minute, identifier: model.IdentifierNamespace, @@ -131,17 +131,7 @@ func TestParseRequestParams(t *testing.T) { }, }, }, - expected: queryOptions{ - time: time.Unix(1585836666, 0), - identifier: model.IdentifierNamespace, - metricFilter: ".*", - namedMetrics: model.NamespaceMetrics, - option: monitoring.NamespaceOption{ - ResourceFilter: ".*", - NamespaceName: "default", - }, - }, - expectedErr: false, + expectedErr: true, }, { params: reqParams{ -- GitLab