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 @@ ...@@ -2,15 +2,16 @@
module Projects module Projects
class LogsController < Projects::ApplicationController class LogsController < Projects::ApplicationController
include ::Gitlab::Utils::StrongMemoize
before_action :authorize_read_pod_logs! before_action :authorize_read_pod_logs!
before_action :environment
before_action :ensure_deployments, only: %i(k8s elasticsearch) before_action :ensure_deployments, only: %i(k8s elasticsearch)
def index def index
if environment.nil? if environment || cluster
render :empty_logs
else
render :index render :index
else
render :empty_logs
end end
end end
...@@ -39,8 +40,9 @@ module Projects ...@@ -39,8 +40,9 @@ module Projects
end end
end end
def index_params # cluster is selected either via environment or directly by id
params.permit(:environment_name) def cluster_params
params.permit(:environment_name, :cluster_id)
end end
def k8s_params def k8s_params
...@@ -52,22 +54,36 @@ module Projects ...@@ -52,22 +54,36 @@ module Projects
end end
def environment def environment
@environment ||= if index_params.key?(:environment_name) strong_memoize(:environment) do
EnvironmentsFinder.new(project, current_user, name: index_params[:environment_name]).find.first if cluster_params.key?(:environment_name)
else EnvironmentsFinder.new(project, current_user, name: cluster_params[:environment_name]).find.first
project.default_environment else
end project.default_environment
end
end
end end
def cluster 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 end
def namespace def namespace
environment.deployment_namespace if gitlab_managed_apps_logs?
Gitlab::Kubernetes::Helm::NAMESPACE
else
environment.deployment_namespace
end
end end
def ensure_deployments def ensure_deployments
return if gitlab_managed_apps_logs?
return if cluster && namespace.present? return if cluster && namespace.present?
render status: :bad_request, json: { render status: :bad_request, json: {
...@@ -75,5 +91,9 @@ module Projects ...@@ -75,5 +91,9 @@ module Projects
message: _('Environment does not have deployments') message: _('Environment does not have deployments')
} }
end end
def gitlab_managed_apps_logs?
cluster_params.key?(:cluster_id)
end
end end
end end
...@@ -64,7 +64,11 @@ module Clusters ...@@ -64,7 +64,11 @@ module Clusters
def gitlab_managed_apps_logs_path def gitlab_managed_apps_logs_path
return unless logs_project && can_read_cluster? 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 end
def read_only_kubernetes_platform_fields? 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 ...@@ -104,6 +104,34 @@ RSpec.describe Projects::LogsController do
expect(response.headers['Poll-Interval']).to eq('3000') expect(response.headers['Poll-Interval']).to eq('3000')
end 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 context 'when service is processing' do
let(:service_result) { nil } let(:service_result) { nil }
......
...@@ -301,7 +301,17 @@ RSpec.describe Clusters::ClusterPresenter do ...@@ -301,7 +301,17 @@ RSpec.describe Clusters::ClusterPresenter do
end end
it 'returns path to 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
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
end end
...@@ -318,7 +328,7 @@ RSpec.describe Clusters::ClusterPresenter do ...@@ -318,7 +328,7 @@ RSpec.describe Clusters::ClusterPresenter do
let!(:project) { create(:project, namespace: group) } let!(:project) { create(:project, namespace: group) }
it 'returns path to 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 end
...@@ -342,7 +352,7 @@ RSpec.describe Clusters::ClusterPresenter do ...@@ -342,7 +352,7 @@ RSpec.describe Clusters::ClusterPresenter do
context 'user can read logs' do context 'user can read logs' do
it 'returns path to 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 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