Commit ac8eca43 authored by Mikolaj Wawrzyniak's avatar Mikolaj Wawrzyniak

Expose gitlab managed apps logs in logs explorer

We should allow users to brows logs of gitlab managed apps
inside logs explorer. To do that we need to use correct namespace and
allow users to select right cluster.
parent 957212b5
......@@ -2,15 +2,16 @@
module Projects
class LogsController < Projects::ApplicationController
include ::Gitlab::Utils::StrongMemoize
before_action :authorize_read_pod_logs!
before_action :environment
before_action :ensure_deployments, only: %i(k8s elasticsearch)
def index
if environment.nil?
render :empty_logs
else
if environment || cluster
render :index
else
render :empty_logs
end
end
......@@ -39,8 +40,9 @@ module Projects
end
end
def index_params
params.permit(:environment_name)
# cluster is selected either via environment or directly by id
def cluster_params
params.permit(:environment_name, :cluster_id)
end
def k8s_params
......@@ -52,22 +54,36 @@ module Projects
end
def environment
@environment ||= if index_params.key?(:environment_name)
EnvironmentsFinder.new(project, current_user, name: index_params[:environment_name]).find.first
strong_memoize(:environment) do
if cluster_params.key?(:environment_name)
EnvironmentsFinder.new(project, current_user, name: cluster_params[:environment_name]).find.first
else
project.default_environment
end
end
end
def cluster
environment.deployment_platform&.cluster
strong_memoize(:cluster) do
if gitlab_managed_apps_logs?
clusters = ClusterAncestorsFinder.new(project, current_user).execute
clusters.find { |cluster| cluster.id == cluster_params[:cluster_id].to_i }
else
environment&.deployment_platform&.cluster
end
end
end
def namespace
if gitlab_managed_apps_logs?
Gitlab::Kubernetes::Helm::NAMESPACE
else
environment.deployment_namespace
end
end
def ensure_deployments
return if gitlab_managed_apps_logs?
return if cluster && namespace.present?
render status: :bad_request, json: {
......@@ -75,5 +91,9 @@ module Projects
message: _('Environment does not have deployments')
}
end
def gitlab_managed_apps_logs?
cluster_params.key?(:cluster_id)
end
end
end
......@@ -64,7 +64,11 @@ module Clusters
def gitlab_managed_apps_logs_path
return unless logs_project && can_read_cluster?
project_logs_path(logs_project, cluster_id: cluster.id)
if cluster.application_elastic_stack&.available?
elasticsearch_project_logs_path(logs_project, cluster_id: cluster.id, format: :json)
else
k8s_project_logs_path(logs_project, cluster_id: cluster.id, format: :json)
end
end
def read_only_kubernetes_platform_fields?
......
---
title: Expose gitlab managed apps logs inside log explorer
merge_request: 36336
author:
type: added
......@@ -104,6 +104,34 @@ RSpec.describe Projects::LogsController do
expect(response.headers['Poll-Interval']).to eq('3000')
end
context 'with gitlab managed apps logs' do
it 'uses cluster finder services to select cluster', :aggregate_failures do
cluster_list = [cluster]
service_params = { params: ActionController::Parameters.new(pod_name: pod_name).permit! }
request_params = {
namespace_id: project.namespace,
project_id: project,
cluster_id: cluster.id,
pod_name: pod_name,
format: :json
}
expect_next_instance_of(ClusterAncestorsFinder, project, user) do |finder|
expect(finder).to receive(:execute).and_return(cluster_list)
expect(cluster_list).to receive(:find).and_call_original
end
expect_next_instance_of(service, cluster, Gitlab::Kubernetes::Helm::NAMESPACE, service_params) do |instance|
expect(instance).to receive(:execute).and_return(service_result)
end
get endpoint, params: request_params
expect(response).to have_gitlab_http_status(:success)
expect(json_response).to eq(service_result_json)
end
end
context 'when service is processing' do
let(:service_result) { nil }
......
......@@ -301,7 +301,17 @@ RSpec.describe Clusters::ClusterPresenter do
end
it 'returns path to logs' do
expect(presenter.gitlab_managed_apps_logs_path).to eq project_logs_path(project, cluster_id: cluster.id)
expect(presenter.gitlab_managed_apps_logs_path).to eq k8s_project_logs_path(project, cluster_id: cluster.id, format: :json)
end
context 'cluster has elastic stack application installed' do
before do
create(:clusters_applications_elastic_stack, :installed, cluster: cluster)
end
it 'returns path to logs' do
expect(presenter.gitlab_managed_apps_logs_path).to eq elasticsearch_project_logs_path(project, cluster_id: cluster.id, format: :json)
end
end
end
......@@ -318,7 +328,7 @@ RSpec.describe Clusters::ClusterPresenter do
let!(:project) { create(:project, namespace: group) }
it 'returns path to logs' do
expect(presenter.gitlab_managed_apps_logs_path).to eq project_logs_path(project, cluster_id: cluster.id)
expect(presenter.gitlab_managed_apps_logs_path).to eq k8s_project_logs_path(project, cluster_id: cluster.id, format: :json)
end
end
......@@ -342,7 +352,7 @@ RSpec.describe Clusters::ClusterPresenter do
context 'user can read logs' do
it 'returns path to logs' do
expect(presenter.gitlab_managed_apps_logs_path).to eq project_logs_path(project, cluster_id: cluster.id)
expect(presenter.gitlab_managed_apps_logs_path).to eq k8s_project_logs_path(project, cluster_id: cluster.id, format: :json)
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