Commit 4e1baa8b authored by ap4y's avatar ap4y

Fix reactive cache handling in NetworkPoliciesController#summary

Reactive cache flow goes through the background worker which causes
arguments to the PacketFlowQuery to be stringified. This causes
reactive cache worker to use a different cache key from the one used
by the web process, as a result background worker will not re-calc
cache. This patch converts PacketFlowQuery to use arguments that will
work consistently for both flows. This patch also updates controller
to send :accepted on initial caching request to improve frontend
polling logic.
parent fd63ac0c
......@@ -16,15 +16,19 @@ module Projects
result = adapter.query(
:packet_flow, environment.deployment_namespace,
interval: params[:interval] || "minute",
from: (Time.parse(params[:from]) rescue 1.hour.ago),
to: (Time.parse(params[:to]) rescue Time.now)
) || {}
params[:interval] || "minute",
(Time.parse(params[:from]) rescue 1.hour.ago).to_s,
(Time.parse(params[:to]) rescue Time.now).to_s
)
respond_to do |format|
format.json do
if result
status = result[:success] ? :ok : :bad_request
render status: status, json: result[:data]
else
render status: :accepted, json: {}
end
end
end
end
......
......@@ -7,7 +7,9 @@ module Gitlab
FORWARDED = "FORWARDED".freeze
DROPPED = "DROPPED".freeze
def query(namespace, interval: "hour", from: 1.day.ago, to: Time.now)
def query(namespace, interval = "hour", from = 1.day.ago.to_s, to = Time.now.to_s)
from = Time.parse(from)
to = Time.parse(to)
rate_interval = to_prometheus_interval(interval)
increase_interval = (to - from).to_i
......
......@@ -40,7 +40,7 @@ describe Projects::Security::NetworkPoliciesController do
Timecop.freeze do
expect(adapter).to(
receive(:query)
.with(:packet_flow, kubernetes_namespace, interval: "minute", from: 1.hour.ago, to: Time.now)
.with(:packet_flow, kubernetes_namespace, "minute", 1.hour.ago.to_s, Time.now.to_s)
.and_return({ success: true, data: { ops_rate: [[Time.at(0).to_i, 10]], ops_total: 10 } })
)
subject
......@@ -62,7 +62,7 @@ describe Projects::Security::NetworkPoliciesController do
it 'queries with requested arguments' do
expect(adapter).to(
receive(:query)
.with(:packet_flow, kubernetes_namespace, interval: "day", from: Time.at(0), to: Time.at(100))
.with(:packet_flow, kubernetes_namespace, "day", Time.at(0).to_s, Time.at(100).to_s)
.and_return({ success: true, data: {} })
)
subject
......@@ -81,7 +81,7 @@ describe Projects::Security::NetworkPoliciesController do
Timecop.freeze do
expect(adapter).to(
receive(:query)
.with(:packet_flow, kubernetes_namespace, interval: "minute", from: 1.hour.ago, to: Time.now)
.with(:packet_flow, kubernetes_namespace, "minute", 1.hour.ago.to_s, Time.now.to_s)
.and_return({ success: true, data: {} })
)
subject
......@@ -90,11 +90,11 @@ describe Projects::Security::NetworkPoliciesController do
end
context 'with nil results' do
it 'returns network policies summary' do
it 'responds with accepted' do
allow(adapter).to receive(:query).and_return(nil)
subject
expect(response).to have_gitlab_http_status(:bad_request)
expect(response).to have_gitlab_http_status(:accepted)
end
end
end
......
......@@ -70,7 +70,7 @@ describe Gitlab::Prometheus::Queries::PacketFlowQuery do
' or on(source,destination,verdict) ' \
"rate(hubble_flows_processed_total{source=\"query-12345678-production\"}[#{value}]))"
expect(client).to receive(:query_range).with(query, any_args)
subject.query(namespace, interval: interval)
subject.query(namespace, interval)
end
end
end
......@@ -83,7 +83,7 @@ describe Gitlab::Prometheus::Queries::PacketFlowQuery do
context 'ops_rate query' do
it 'sets query time range' do
expect(client).to receive(:query_range).with(anything, start_time: from, end_time: to)
subject.query(namespace, from: from, to: to)
subject.query(namespace, 'hour', from.to_s, to.to_s)
end
end
......@@ -94,7 +94,7 @@ describe Gitlab::Prometheus::Queries::PacketFlowQuery do
' or on(source,destination,verdict) ' \
'increase(hubble_flows_processed_total{source="query-12345678-production"}[100s]))'
expect(client).to receive(:query).with(query, time: to)
subject.query(namespace, from: from, to: to)
subject.query(namespace, 'hour', from.to_s, to.to_s)
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment