diff --git a/pkg/kapis/monitoring/v1alpha3/helper.go b/pkg/kapis/monitoring/v1alpha3/helper.go index 03e17e999e5512bbda89b457abc62106e6e9de11..d6e50f4e7e4d0ee957269e62f68714a01677bd80 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 6a0169e9bc52b51c470cea7519c9cf7a80f16a2a..99ef67fd894ee474ba3eafde9ce518be9f461dd3 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{