2020-03-11 11:09:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Projects
|
|
|
|
class LogsController < Projects::ApplicationController
|
2020-07-16 08:09:22 -04:00
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
2020-03-11 11:09:37 -04:00
|
|
|
before_action :authorize_read_pod_logs!
|
|
|
|
before_action :ensure_deployments, only: %i(k8s elasticsearch)
|
|
|
|
|
2020-10-08 14:08:32 -04:00
|
|
|
feature_category :logging
|
|
|
|
|
2020-03-11 11:09:37 -04:00
|
|
|
def index
|
2020-07-16 08:09:22 -04:00
|
|
|
if environment || cluster
|
2020-03-11 11:09:37 -04:00
|
|
|
render :index
|
2020-07-16 08:09:22 -04:00
|
|
|
else
|
|
|
|
render :empty_logs
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def k8s
|
|
|
|
render_logs(::PodLogs::KubernetesService, k8s_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def elasticsearch
|
|
|
|
render_logs(::PodLogs::ElasticsearchService, elasticsearch_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def render_logs(service, permitted_params)
|
|
|
|
::Gitlab::PollingInterval.set_header(response, interval: 3_000)
|
|
|
|
|
|
|
|
result = service.new(cluster, namespace, params: permitted_params).execute
|
|
|
|
|
|
|
|
if result.nil?
|
|
|
|
head :accepted
|
|
|
|
elsif result[:status] == :success
|
|
|
|
render json: result
|
|
|
|
else
|
|
|
|
render status: :bad_request, json: result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-16 08:09:22 -04:00
|
|
|
# cluster is selected either via environment or directly by id
|
|
|
|
def cluster_params
|
|
|
|
params.permit(:environment_name, :cluster_id)
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def k8s_params
|
|
|
|
params.permit(:container_name, :pod_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def elasticsearch_params
|
2020-03-23 20:09:24 -04:00
|
|
|
params.permit(:container_name, :pod_name, :search, :start_time, :end_time, :cursor)
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def environment
|
2020-07-16 08:09:22 -04:00
|
|
|
strong_memoize(:environment) do
|
|
|
|
if cluster_params.key?(:environment_name)
|
2021-04-22 05:09:45 -04:00
|
|
|
::Environments::EnvironmentsFinder.new(project, current_user, name: cluster_params[:environment_name]).execute.first
|
2020-07-16 08:09:22 -04:00
|
|
|
else
|
|
|
|
project.default_environment
|
|
|
|
end
|
|
|
|
end
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cluster
|
2020-07-16 08:09:22 -04:00
|
|
|
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
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def namespace
|
2020-07-16 08:09:22 -04:00
|
|
|
if gitlab_managed_apps_logs?
|
|
|
|
Gitlab::Kubernetes::Helm::NAMESPACE
|
|
|
|
else
|
|
|
|
environment.deployment_namespace
|
|
|
|
end
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_deployments
|
2020-07-16 08:09:22 -04:00
|
|
|
return if gitlab_managed_apps_logs?
|
2020-03-11 11:09:37 -04:00
|
|
|
return if cluster && namespace.present?
|
|
|
|
|
|
|
|
render status: :bad_request, json: {
|
|
|
|
status: :error,
|
|
|
|
message: _('Environment does not have deployments')
|
|
|
|
}
|
|
|
|
end
|
2020-07-16 08:09:22 -04:00
|
|
|
|
|
|
|
def gitlab_managed_apps_logs?
|
|
|
|
cluster_params.key?(:cluster_id)
|
|
|
|
end
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
end
|