clusters_controller.rb 5.48 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
class Clusters::ClustersController < Clusters::BaseController
  include RoutableActions

6 7 8
  before_action :cluster, except: [:index, :new, :create_gcp, :create_user]
  before_action :generate_gcp_authorize_url, only: [:new]
  before_action :validate_gcp_token, only: [:new]
9 10
  before_action :gcp_cluster, only: [:new]
  before_action :user_cluster, only: [:new]
11
  before_action :authorize_create_cluster!, only: [:new]
12 13
  before_action :authorize_update_cluster!, only: [:update]
  before_action :authorize_admin_cluster!, only: [:destroy]
14
  before_action :update_applications_status, only: [:cluster_status]
15

16
  helper_method :token_in_session
17

Kamil Trzcinski's avatar
Kamil Trzcinski committed
18 19
  STATUS_POLLING_INTERVAL = 10_000

20
  def index
21 22
    clusters = ClusterAncestorsFinder.new(clusterable.subject, current_user).execute
    @clusters = Kaminari.paginate_array(clusters).page(params[:page]).per(20)
23 24
  end

25 26 27
  def new
  end

28 29
  # Overridding ActionController::Metal#status is NOT a good idea
  def cluster_status
30 31
    respond_to do |format|
      format.json do
32
        Gitlab::PollingInterval.set_header(response, interval: STATUS_POLLING_INTERVAL)
33

34
        render json: ClusterSerializer
35
          .new(current_user: @current_user)
36
          .represent_status(@cluster)
37 38
      end
    end
39 40
  end

41
  def show
42 43 44
  end

  def update
Shinya Maeda's avatar
Shinya Maeda committed
45
    Clusters::UpdateService
46
      .new(current_user, update_params)
47
      .execute(cluster)
48

Kamil Trzcinski's avatar
Kamil Trzcinski committed
49
    if cluster.valid?
50 51 52 53 54
      respond_to do |format|
        format.json do
          head :no_content
        end
        format.html do
55
          flash[:notice] = _('Kubernetes cluster was successfully updated.')
56
          redirect_to cluster.show_path
57 58
        end
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
59
    else
60 61 62 63
      respond_to do |format|
        format.json { head :bad_request }
        format.html { render :show }
      end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
64
    end
65 66
  end

67
  def destroy
68
    if cluster.destroy
69
      flash[:notice] = _('Kubernetes cluster integration was successfully removed.')
70
      redirect_to clusterable.index_path, status: :found
71
    else
72
      flash[:notice] = _('Kubernetes cluster integration was not removed.')
73
      render :show
74
    end
75 76
  end

77 78
  def create_gcp
    @gcp_cluster = ::Clusters::CreateService
79
      .new(current_user, create_gcp_cluster_params)
80
      .execute(access_token: token_in_session)
81
      .present(current_user: current_user)
82 83

    if @gcp_cluster.persisted?
84
      redirect_to @gcp_cluster.show_path
85 86 87 88 89 90 91 92 93 94 95
    else
      generate_gcp_authorize_url
      validate_gcp_token
      user_cluster

      render :new, locals: { active_tab: 'gcp' }
    end
  end

  def create_user
    @user_cluster = ::Clusters::CreateService
96
      .new(current_user, create_user_cluster_params)
97
      .execute(access_token: token_in_session)
98
      .present(current_user: current_user)
99 100

    if @user_cluster.persisted?
101
      redirect_to @user_cluster.show_path
102 103 104 105 106 107 108 109 110
    else
      generate_gcp_authorize_url
      validate_gcp_token
      gcp_cluster

      render :new, locals: { active_tab: 'user' }
    end
  end

111 112
  private

113
  def update_params
Kamil Trzcinski's avatar
Kamil Trzcinski committed
114 115 116
    if cluster.managed?
      params.require(:cluster).permit(
        :enabled,
117
        :environment_scope,
Kamil Trzcinski's avatar
Kamil Trzcinski committed
118 119 120 121 122 123 124 125
        platform_kubernetes_attributes: [
          :namespace
        ]
      )
    else
      params.require(:cluster).permit(
        :enabled,
        :name,
126
        :environment_scope,
Kamil Trzcinski's avatar
Kamil Trzcinski committed
127 128 129 130 131
        platform_kubernetes_attributes: [
          :api_url,
          :token,
          :ca_cert,
          :namespace
Kamil Trzcinski's avatar
Kamil Trzcinski committed
132
        ]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
133 134
      )
    end
135 136
  end

137 138 139 140 141 142 143 144 145
  def create_gcp_cluster_params
    params.require(:cluster).permit(
      :enabled,
      :name,
      :environment_scope,
      provider_gcp_attributes: [
        :gcp_project_id,
        :zone,
        :num_nodes,
146 147
        :machine_type,
        :legacy_abac
148 149
      ]).merge(
        provider_type: :gcp,
150
        platform_type: :kubernetes,
151
        clusterable: clusterable.subject
152 153 154 155 156 157 158 159 160 161 162 163
      )
  end

  def create_user_cluster_params
    params.require(:cluster).permit(
      :enabled,
      :name,
      :environment_scope,
      platform_kubernetes_attributes: [
        :namespace,
        :api_url,
        :token,
164 165
        :ca_cert,
        :authorization_type
166 167
      ]).merge(
        provider_type: :user,
168
        platform_type: :kubernetes,
169
        clusterable: clusterable.subject
170 171 172 173
      )
  end

  def generate_gcp_authorize_url
174
    state = generate_session_key_redirect(clusterable.new_path.to_s)
175 176 177 178 179 180 181 182 183

    @authorize_url = GoogleApi::CloudPlatform::Client.new(
      nil, callback_google_api_auth_url,
      state: state).authorize_url
  rescue GoogleApi::Auth::ConfigMissingError
    # no-op
  end

  def gcp_cluster
184 185 186
    cluster = Clusters::BuildService.new(clusterable.subject).execute
    cluster.build_provider_gcp
    @gcp_cluster = cluster.present(current_user: current_user)
187 188 189
  end

  def user_cluster
190 191 192
    cluster = Clusters::BuildService.new(clusterable.subject).execute
    cluster.build_platform_kubernetes
    @user_cluster = cluster.present(current_user: current_user)
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  end

  def validate_gcp_token
    @valid_gcp_token = GoogleApi::CloudPlatform::Client.new(token_in_session, nil)
      .validate_token(expires_at_in_session)
  end

  def token_in_session
    session[GoogleApi::CloudPlatform::Client.session_key_for_token]
  end

  def expires_at_in_session
    @expires_at_in_session ||=
      session[GoogleApi::CloudPlatform::Client.session_key_for_expires_at]
  end

  def generate_session_key_redirect(uri)
    GoogleApi::CloudPlatform::Client.new_session_key_for_redirect_uri do |key|
      session[key] = uri
    end
  end

215 216
  def update_applications_status
    @cluster.applications.each(&:schedule_status_update)
217
  end
218
end