Commit e78938d5 authored by Adrien Kohlbecker's avatar Adrien Kohlbecker Committed by Martin Wortschack

Pass logs api URL to frontend

parent 05f5d6e7
...@@ -11,8 +11,6 @@ export default { ...@@ -11,8 +11,6 @@ export default {
groupEpicsPath: groupEpicsPath:
'/api/:version/groups/:id/epics?include_ancestor_groups=:includeAncestorGroups&include_descendant_groups=:includeDescendantGroups', '/api/:version/groups/:id/epics?include_ancestor_groups=:includeAncestorGroups&include_descendant_groups=:includeDescendantGroups',
epicIssuePath: '/api/:version/groups/:id/epics/:epic_iid/issues/:issue_id', epicIssuePath: '/api/:version/groups/:id/epics/:epic_iid/issues/:issue_id',
k8sPodLogsPath: ':project_path/-/logs/k8s.json',
elasticsearchPodLogsPath: ':project_path/-/logs/elasticsearch.json',
groupPackagesPath: '/api/:version/groups/:id/packages', groupPackagesPath: '/api/:version/groups/:id/packages',
projectPackagesPath: '/api/:version/projects/:id/packages', projectPackagesPath: '/api/:version/projects/:id/packages',
projectPackagePath: '/api/:version/projects/:id/packages/:package_id', projectPackagePath: '/api/:version/projects/:id/packages/:package_id',
...@@ -103,17 +101,9 @@ export default { ...@@ -103,17 +101,9 @@ export default {
* @returns {Promise} Axios promise for the result of a GET request of logs * @returns {Promise} Axios promise for the result of a GET request of logs
*/ */
getPodLogs({ environment, podName, containerName, search, start, end }) { getPodLogs({ environment, podName, containerName, search, start, end }) {
let baseUrl; const url = this.buildUrl(environment.logs_api_path);
if (environment.enable_advanced_logs_querying) {
baseUrl = this.elasticsearchPodLogsPath;
} else {
baseUrl = this.k8sPodLogsPath;
}
const url = this.buildUrl(baseUrl.replace(':project_path', environment.project_path));
const params = { const params = {};
environment_name: environment.name,
};
if (podName) { if (podName) {
params.pod_name = podName; params.pod_name = podName;
......
...@@ -32,10 +32,6 @@ module EE ...@@ -32,10 +32,6 @@ module EE
::Gitlab::Kubernetes::RolloutStatus.from_deployments(*deployments, pods: pods, legacy_deployments: legacy_deployments) ::Gitlab::Kubernetes::RolloutStatus.from_deployments(*deployments, pods: pods, legacy_deployments: legacy_deployments)
end end
def elastic_stack_available?
!!cluster.application_elastic_stack&.installed?
end
private private
def read_deployments(namespace) def read_deployments(namespace)
......
...@@ -77,6 +77,10 @@ module EE ...@@ -77,6 +77,10 @@ module EE
result || ::Gitlab::Kubernetes::RolloutStatus.loading result || ::Gitlab::Kubernetes::RolloutStatus.loading
end end
def elastic_stack_available?
!!deployment_platform&.cluster&.application_elastic_stack&.installed?
end
private private
def rollout_status_available? def rollout_status_available?
......
...@@ -16,8 +16,16 @@ module EE ...@@ -16,8 +16,16 @@ module EE
project_logs_path(environment.project, environment_name: environment.name) project_logs_path(environment.project, environment_name: environment.name)
end end
expose :logs_api_path, if: -> (*) { can_read_pod_logs? } do |environment|
if environment.elastic_stack_available?
elasticsearch_project_logs_path(environment.project, environment_name: environment.name, format: :json)
else
k8s_project_logs_path(environment.project, environment_name: environment.name, format: :json)
end
end
expose :enable_advanced_logs_querying, if: -> (*) { can_read_pod_logs? } do |environment| expose :enable_advanced_logs_querying, if: -> (*) { can_read_pod_logs? } do |environment|
environment.deployment_platform&.elastic_stack_available? environment.elastic_stack_available?
end end
end end
......
...@@ -166,14 +166,14 @@ describe('Api', () => { ...@@ -166,14 +166,14 @@ describe('Api', () => {
describe('getPodLogs', () => { describe('getPodLogs', () => {
const projectPath = '/root/test-project'; const projectPath = '/root/test-project';
const environmentName = 'production';
const podName = 'pod'; const podName = 'pod';
const containerName = 'container'; const containerName = 'container';
const search = 'foo +bar'; const search = 'foo +bar';
const expectedUrl = '/gitlab/dummy_api_path.json';
const environment = { const environment = {
name: environmentName,
enable_advanced_logs_querying: false, enable_advanced_logs_querying: false,
project_path: projectPath, project_path: projectPath,
logs_api_path: '/dummy_api_path.json',
}; };
const getRequest = () => mock.history.get[0]; const getRequest = () => mock.history.get[0];
...@@ -187,13 +187,10 @@ describe('Api', () => { ...@@ -187,13 +187,10 @@ describe('Api', () => {
}); });
it('calls `axios.get` with pod_name and container_name', done => { it('calls `axios.get` with pod_name and container_name', done => {
const expectedUrl = `${dummyUrlRoot}${projectPath}/-/logs/k8s.json`;
Api.getPodLogs({ environment, podName, containerName }) Api.getPodLogs({ environment, podName, containerName })
.then(() => { .then(() => {
expect(getRequest().url).toBe(expectedUrl); expect(getRequest().url).toBe(expectedUrl);
expect(getRequest().params).toEqual({ expect(getRequest().params).toEqual({
environment_name: environmentName,
pod_name: podName, pod_name: podName,
container_name: containerName, container_name: containerName,
}); });
...@@ -203,27 +200,20 @@ describe('Api', () => { ...@@ -203,27 +200,20 @@ describe('Api', () => {
}); });
it('calls `axios.get` without pod_name and container_name', done => { it('calls `axios.get` without pod_name and container_name', done => {
const expectedUrl = `${dummyUrlRoot}${projectPath}/-/logs/k8s.json`;
Api.getPodLogs({ environment }) Api.getPodLogs({ environment })
.then(() => { .then(() => {
expect(getRequest().url).toBe(expectedUrl); expect(getRequest().url).toBe(expectedUrl);
expect(getRequest().params).toEqual({ expect(getRequest().params).toEqual({});
environment_name: environmentName,
});
}) })
.then(done) .then(done)
.catch(done.fail); .catch(done.fail);
}); });
it('calls `axios.get` with pod_name', done => { it('calls `axios.get` with pod_name', done => {
const expectedUrl = `${dummyUrlRoot}${projectPath}/-/logs/k8s.json`;
Api.getPodLogs({ environment, podName }) Api.getPodLogs({ environment, podName })
.then(() => { .then(() => {
expect(getRequest().url).toBe(expectedUrl); expect(getRequest().url).toBe(expectedUrl);
expect(getRequest().params).toEqual({ expect(getRequest().params).toEqual({
environment_name: environmentName,
pod_name: podName, pod_name: podName,
}); });
}) })
...@@ -232,13 +222,10 @@ describe('Api', () => { ...@@ -232,13 +222,10 @@ describe('Api', () => {
}); });
it('calls `axios.get` with pod_name and search', done => { it('calls `axios.get` with pod_name and search', done => {
const expectedUrl = `${dummyUrlRoot}${projectPath}/-/logs/k8s.json`;
Api.getPodLogs({ environment, podName, search }) Api.getPodLogs({ environment, podName, search })
.then(() => { .then(() => {
expect(getRequest().url).toBe(expectedUrl); expect(getRequest().url).toBe(expectedUrl);
expect(getRequest().params).toEqual({ expect(getRequest().params).toEqual({
environment_name: environmentName,
pod_name: podName, pod_name: podName,
search, search,
}); });
......
...@@ -6,9 +6,9 @@ export const mockDocumentationPath = '/documentation.md'; ...@@ -6,9 +6,9 @@ export const mockDocumentationPath = '/documentation.md';
const makeMockEnvironment = (id, name, advancedQuerying) => ({ const makeMockEnvironment = (id, name, advancedQuerying) => ({
id, id,
logs_path: `${mockProjectPath}/environments/${id}/logs`,
project_path: mockProjectPath, project_path: mockProjectPath,
name, name,
logs_api_path: '/dummy_logs_path.json',
enable_advanced_logs_querying: advancedQuerying, enable_advanced_logs_querying: advancedQuerying,
}); });
......
...@@ -154,12 +154,11 @@ describe('Logs Store actions', () => { ...@@ -154,12 +154,11 @@ describe('Logs Store actions', () => {
state.environments.current = mockEnvName; state.environments.current = mockEnvName;
state.pods.current = mockPodName; state.pods.current = mockPodName;
const endpoint = `/${mockProjectPath}/-/logs/elasticsearch.json`; const endpoint = '/dummy_logs_path.json';
mock mock
.onGet(endpoint, { .onGet(endpoint, {
params: { params: {
environment_name: mockEnvName,
pod_name: mockPodName, pod_name: mockPodName,
...mockDefaultRange, ...mockDefaultRange,
}, },
...@@ -194,12 +193,11 @@ describe('Logs Store actions', () => { ...@@ -194,12 +193,11 @@ describe('Logs Store actions', () => {
state.pods.current = mockPodName; state.pods.current = mockPodName;
state.timeRange.current = mockFixedRange; state.timeRange.current = mockFixedRange;
const endpoint = `/${mockProjectPath}/-/logs/elasticsearch.json`; const endpoint = '/dummy_logs_path.json';
mock mock
.onGet(endpoint, { .onGet(endpoint, {
params: { params: {
environment_name: mockEnvName,
pod_name: mockPodName, pod_name: mockPodName,
start: mockFixedRange.start, start: mockFixedRange.start,
end: mockFixedRange.end, end: mockFixedRange.end,
...@@ -233,12 +231,11 @@ describe('Logs Store actions', () => { ...@@ -233,12 +231,11 @@ describe('Logs Store actions', () => {
state.search = mockSearch; state.search = mockSearch;
state.timeRange.current = 'INVALID_TIME_RANGE'; state.timeRange.current = 'INVALID_TIME_RANGE';
const endpoint = `/${mockProjectPath}/-/logs/elasticsearch.json`; const endpoint = '/dummy_logs_path.json';
mock mock
.onGet(endpoint, { .onGet(endpoint, {
params: { params: {
environment_name: mockEnvName,
pod_name: mockPodName, pod_name: mockPodName,
search: mockSearch, search: mockSearch,
}, },
...@@ -275,11 +272,9 @@ describe('Logs Store actions', () => { ...@@ -275,11 +272,9 @@ describe('Logs Store actions', () => {
state.environments.options = mockEnvironments; state.environments.options = mockEnvironments;
state.environments.current = mockEnvName; state.environments.current = mockEnvName;
const endpoint = `/${mockProjectPath}/-/logs/elasticsearch.json`; const endpoint = '/dummy_logs_path.json';
mock mock.onGet(endpoint, { params: { ...mockDefaultRange } }).reply(200, {
.onGet(endpoint, { params: { environment_name: mockEnvName, ...mockDefaultRange } })
.reply(200, {
pod_name: mockPodName, pod_name: mockPodName,
pods: mockPods, pods: mockPods,
logs: mockLogsResult, logs: mockLogsResult,
......
...@@ -4,6 +4,7 @@ require 'spec_helper' ...@@ -4,6 +4,7 @@ require 'spec_helper'
describe EnvironmentEntity do describe EnvironmentEntity do
include KubernetesHelpers include KubernetesHelpers
include Gitlab::Routing.url_helpers
let(:user) { create(:user) } let(:user) { create(:user) }
let(:environment) { create(:environment) } let(:environment) { create(:environment) }
...@@ -51,14 +52,32 @@ describe EnvironmentEntity do ...@@ -51,14 +52,32 @@ describe EnvironmentEntity do
stub_licensed_features(pod_logs: true) stub_licensed_features(pod_logs: true)
end end
it 'exposes logs_path' do it 'exposes logs keys' do
expect(subject).to include(:logs_path) expect(subject).to include(:logs_path)
expect(subject).to include(:logs_api_path)
expect(subject).to include(:enable_advanced_logs_querying)
end
it 'uses k8s api when ES is not available' do
expect(subject[:logs_api_path]).to eq(k8s_project_logs_path(environment.project, environment_name: environment.name, format: :json))
end
it 'uses ES api when ES is available' do
allow(environment).to receive(:elastic_stack_available?).and_return(true)
expect(subject[:logs_api_path]).to eq(elasticsearch_project_logs_path(environment.project, environment_name: environment.name, format: :json))
end end
end end
context 'when pod_logs are not available' do context 'when pod_logs are not available' do
it 'does not expose logs_path' do before do
stub_licensed_features(pod_logs: false)
end
it 'does not expose logs keys' do
expect(subject).not_to include(:logs_path) expect(subject).not_to include(:logs_path)
expect(subject).not_to include(:logs_api_path)
expect(subject).not_to include(:enable_advanced_logs_querying)
end end
end 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