未验证 提交 0f090203 编写于 作者: H huanggze

fix: time skew in monitoring responses

Signed-off-by: Nhuanggze <loganhuang@yunify.com>
上级 95cd23b7
...@@ -37,7 +37,7 @@ const ( ...@@ -37,7 +37,7 @@ const (
ComponentAPIServer = "apiserver" ComponentAPIServer = "apiserver"
ComponentScheduler = "scheduler" 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." ErrParamConflict = "'time' and the combination of 'start' and 'end' are mutually exclusive."
ErrInvalidStartEnd = "'start' must be before 'end'." ErrInvalidStartEnd = "'start' must be before 'end'."
ErrInvalidPage = "Invalid parameter 'page'." ErrInvalidPage = "Invalid parameter 'page'."
...@@ -256,21 +256,23 @@ func (h handler) makeQueryOptions(r reqParams, lvl monitoring.Level) (q queryOpt ...@@ -256,21 +256,23 @@ func (h handler) makeQueryOptions(r reqParams, lvl monitoring.Level) (q queryOpt
cts := ns.CreationTimestamp.Time cts := ns.CreationTimestamp.Time
// Query should happen no earlier than namespace's creation 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. // In range query, if `start` and `end` are both before namespace's creation time, it causes no hit.
if !q.isRangeQuery() { if !q.isRangeQuery() {
if q.time.Before(cts) { if q.time.Before(cts) {
q.time = cts return q, errors.New(ErrNoHit)
} }
} else { } else {
if q.start.Before(cts) {
q.start = cts
}
if q.end.Before(cts) { if q.end.Before(cts) {
return q, errors.New(ErrNoHit) 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 // Parse sorting and paging params
......
...@@ -104,7 +104,7 @@ func TestParseRequestParams(t *testing.T) { ...@@ -104,7 +104,7 @@ func TestParseRequestParams(t *testing.T) {
}, },
}, },
expected: queryOptions{ expected: queryOptions{
start: time.Unix(1585836666, 0), start: time.Unix(1585836699, 0),
end: time.Unix(1585839999, 0), end: time.Unix(1585839999, 0),
step: time.Minute, step: time.Minute,
identifier: model.IdentifierNamespace, identifier: model.IdentifierNamespace,
...@@ -131,17 +131,7 @@ func TestParseRequestParams(t *testing.T) { ...@@ -131,17 +131,7 @@ func TestParseRequestParams(t *testing.T) {
}, },
}, },
}, },
expected: queryOptions{ expectedErr: true,
time: time.Unix(1585836666, 0),
identifier: model.IdentifierNamespace,
metricFilter: ".*",
namedMetrics: model.NamespaceMetrics,
option: monitoring.NamespaceOption{
ResourceFilter: ".*",
NamespaceName: "default",
},
},
expectedErr: false,
}, },
{ {
params: reqParams{ params: reqParams{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册