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