summaryrefslogtreecommitdiff
path: root/app/presenters/clusters/cluster_presenter.rb
blob: 541a6363edd49b5d3c94e00d8d92a73a4565c211 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# frozen_string_literal: true

module Clusters
  class ClusterPresenter < Gitlab::View::Presenter::Delegated
    include ::Gitlab::Utils::StrongMemoize
    include ActionView::Helpers::SanitizeHelper
    include ActionView::Helpers::UrlHelper
    include IconsHelper

    presents :cluster

    # We do not want to show the group path for clusters belonging to the
    # clusterable, only for the ancestor clusters.
    def item_link(clusterable_presenter, *html_options)
      if cluster.group_type? && clusterable != clusterable_presenter.subject
        contracted_group_name(cluster.group) + ' / ' + link_to_cluster
      else
        link_to_cluster(*html_options)
      end
    end

    def provider_label
      if aws?
        s_('ClusterIntegration|Elastic Kubernetes Service')
      elsif gcp?
        s_('ClusterIntegration|Google Kubernetes Engine')
      end
    end

    def provider_management_url
      if aws?
        "https://console.aws.amazon.com/eks/home?region=#{provider.region}\#/clusters/#{name}"
      elsif gcp?
        "https://console.cloud.google.com/kubernetes/clusters/details/#{provider.zone}/#{name}"
      end
    end

    def can_read_cluster?
      can?(current_user, :read_cluster, cluster)
    end

    def cluster_type_description
      if cluster.project_type?
        s_("ClusterIntegration|Project cluster")
      elsif cluster.group_type?
        s_("ClusterIntegration|Group cluster")
      elsif cluster.instance_type?
        s_("ClusterIntegration|Instance cluster")
      end
    end

    def show_path
      if cluster.project_type?
        project_cluster_path(project, cluster)
      elsif cluster.group_type?
        group_cluster_path(group, cluster)
      elsif cluster.instance_type?
        admin_cluster_path(cluster)
      else
        raise NotImplementedError
      end
    end

    def gitlab_managed_apps_logs_path
      return unless logs_project && can_read_cluster?

      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?
      !cluster.provided_by_user?
    end

    def health_data(clusterable)
      {
        'clusters-path': clusterable.index_path,
        'dashboard-endpoint': clusterable.metrics_dashboard_path(cluster),
        'documentation-path': help_page_path('user/project/clusters/index', anchor: 'monitoring-your-kubernetes-cluster'),
        'add-dashboard-documentation-path': help_page_path('operations/metrics/dashboards/index.md', anchor: 'add-a-new-dashboard-to-your-project'),
        'empty-getting-started-svg-path': image_path('illustrations/monitoring/getting_started.svg'),
        'empty-loading-svg-path': image_path('illustrations/monitoring/loading.svg'),
        'empty-no-data-svg-path': image_path('illustrations/monitoring/no_data.svg'),
        'empty-no-data-small-svg-path': image_path('illustrations/chart-empty-state-small.svg'),
        'empty-unable-to-connect-svg-path': image_path('illustrations/monitoring/unable_to_connect.svg'),
        'settings-path': '',
        'project-path': '',
        'tags-path': ''
      }
    end

    private

    def image_path(path)
      ActionController::Base.helpers.image_path(path)
    end

    # currently log explorer is only available in the scope of the project
    # for group and instance level cluster selected project does not affects
    # fetching logs from gitlab managed apps namespace, therefore any project
    # available to user will be sufficient.
    def logs_project
      strong_memoize(:logs_project) do
        cluster.all_projects.first
      end
    end

    def clusterable
      if cluster.group_type?
        cluster.group
      elsif cluster.project_type?
        cluster.project
      end
    end

    def contracted_group_name(group)
      sanitize(group.full_name)
        .sub(%r{\/.*\/}, "/ #{contracted_icon} /")
        .html_safe
    end

    def contracted_icon
      sprite_icon('ellipsis_h', size: 12, css_class: 'vertical-align-middle')
    end

    def link_to_cluster(html_options: {})
      link_to_if(can_read_cluster?, cluster.name, show_path, html_options)
    end
  end
end

Clusters::ClusterPresenter.prepend_if_ee('EE::Clusters::ClusterPresenter')