Commit 44cebfe7 authored by Ryan Cobb's avatar Ryan Cobb

Add validation and specs around k8s pod logs

This adds validations and specs around k8s elasticstack pod logs.
parent bda626cd
......@@ -15,6 +15,7 @@ class PodLogsService < ::BaseService
:check_deployment_platform,
:check_pod_names,
:check_pod_name,
:check_times,
:pod_logs,
:filter_return_keys
......@@ -72,6 +73,17 @@ class PodLogsService < ::BaseService
success(result)
end
def check_times(result)
if params['start'] && params['end']
Time.iso8601(params['start'])
Time.iso8601(params['end'])
end
success(result)
rescue ArgumentError
error(_('Invalid start or end time format'))
end
def pod_logs(result)
response = environment.deployment_platform.read_pod_logs(
environment.id,
......
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"kubernetes.pod.name": {
"query": "production-6866bc8974-m4sk4"
}
}
},
{
"match_phrase": {
"kubernetes.namespace": {
"query": "autodevops-deploy-9-production"
}
}
}
],
"filter": [
{
"range": {
"@timestamp": {
"gte": "2019-12-13T14:35:34.034Z",
"lt": "2019-12-13T14:35:34.034Z"
}
}
}
]
}
},
"sort": [
{
"@timestamp": {
"order": "desc"
}
},
{
"offset": {
"order": "desc"
}
}
],
"_source": [
"@timestamp",
"message"
],
"size": 500
}
......@@ -18,10 +18,13 @@ describe Gitlab::Elasticsearch::Logs do
let(:pod_name) { "production-6866bc8974-m4sk4" }
let(:container_name) { "auto-deploy-app" }
let(:search) { "foo +bar "}
let(:start_time) { "2019-12-13T14:35:34.034Z" }
let(:end_time) { "2019-12-13T14:35:34.034Z" }
let(:body) { JSON.parse(fixture_file('lib/elasticsearch/query.json', dir: 'ee')) }
let(:body_with_container) { JSON.parse(fixture_file('lib/elasticsearch/query_with_container.json', dir: 'ee')) }
let(:body_with_search) { JSON.parse(fixture_file('lib/elasticsearch/query_with_search.json', dir: 'ee')) }
let(:body_with_times) { JSON.parse(fixture_file('lib/elasticsearch/query_with_times.json', dir: 'ee')) }
RSpec::Matchers.define :a_hash_equal_to_json do |expected|
match do |actual|
......@@ -50,5 +53,12 @@ describe Gitlab::Elasticsearch::Logs do
result = subject.pod_logs(namespace, pod_name, nil, search)
expect(result).to eq([es_message_4, es_message_3, es_message_2, es_message_1])
end
it 'can further filter the logs by start_time and end_time' do
expect(client).to receive(:search).with(body: a_hash_equal_to_json(body_with_times)).and_return(es_response)
result = subject.pod_logs(namespace, pod_name, nil, nil, start_time, end_time)
expect(result).to eq([es_message_4, es_message_3, es_message_2, es_message_1])
end
end
end
......@@ -268,7 +268,9 @@ describe Clusters::Platforms::Kubernetes do
'pod_name' => pod_name,
'namespace' => namespace,
'container' => container,
'search' => nil
'search' => nil,
'start_time' => nil,
'end_time' => nil
}
]
end
......
......@@ -197,6 +197,26 @@ describe PodLogsService do
it_behaves_like 'success'
end
context 'when start and end time is specified' do
let(:pod_name) { 'some-pod' }
let(:container_name) { nil }
let(:start_time) { '2019-12-13T14:35:34.034Z' }
let(:end_time) { '2019-12-13T14:35:34.034Z' }
include_context 'return success'
it_behaves_like 'success'
end
context 'when start and end time are invalid' do
let(:pod_name) { 'some-pod' }
let(:container_name) { nil }
let(:start_time) { '1' }
let(:end_time) { '2' }
it_behaves_like 'error', 'Invalid start or end time format'
end
context 'when error is returned' do
include_context 'return error', 'Kubernetes API returned status code: 400'
......
......@@ -10177,6 +10177,9 @@ msgstr ""
msgid "Invalid server response"
msgstr ""
msgid "Invalid start or end time format"
msgstr ""
msgid "Invalid two-factor code."
msgstr ""
......
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