summaryrefslogtreecommitdiff
path: root/app/controllers/projects/logs_controller.rb
blob: b9aa9bfc947b169236be448570a589a109033041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# frozen_string_literal: true

module Projects
  class LogsController < Projects::ApplicationController
    include ::Gitlab::Utils::StrongMemoize

    before_action :authorize_read_pod_logs!
    before_action :ensure_deployments, only: %i(k8s elasticsearch)

    feature_category :logging

    def index
      if environment || cluster
        render :index
      else
        render :empty_logs
      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::UsageCounters::PodLogs.increment(project.id)
      ::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

    # cluster is selected either via environment or directly by id
    def cluster_params
      params.permit(:environment_name, :cluster_id)
    end

    def k8s_params
      params.permit(:container_name, :pod_name)
    end

    def elasticsearch_params
      params.permit(:container_name, :pod_name, :search, :start_time, :end_time, :cursor)
    end

    def environment
      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
      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: {
        status: :error,
        message: _('Environment does not have deployments')
      }
    end

    def gitlab_managed_apps_logs?
      cluster_params.key?(:cluster_id)
    end
  end
end