clusters_controller.rb 2.87 KB
Newer Older
1
class Projects::ClustersController < Projects::ApplicationController
2 3
  include ClustersHelper
  before_action :cluster, except: [:index, :new]
4
  before_action :authorize_read_cluster!
5 6
  before_action :gcp_cluster, only: [:new]
  before_action :user_cluster, only: [:new]
7
  before_action :authorize_create_cluster!, only: [:new]
8 9
  before_action :authorize_update_cluster!, only: [:update]
  before_action :authorize_admin_cluster!, only: [:destroy]
10
  before_action :update_applications_status, only: [:status]
11
  helper_method :gcp_authorize_url
12
  helper_method :token_in_session
13
  helper_method :valid_gcp_token
14

Kamil Trzcinski's avatar
Kamil Trzcinski committed
15 16
  STATUS_POLLING_INTERVAL = 10_000

17
  def index
18 19
    clusters = ClustersFinder.new(project, current_user, :all).execute
    @clusters = clusters.page(params[:page]).per(20)
20 21
  end

22 23 24
  def new
  end

25
  def status
26 27
    respond_to do |format|
      format.json do
28
        Gitlab::PollingInterval.set_header(response, interval: STATUS_POLLING_INTERVAL)
29

30 31 32
        render json: ClusterSerializer
          .new(project: @project, current_user: @current_user)
          .represent_status(@cluster)
33 34
      end
    end
35 36
  end

37
  def show
38 39 40
  end

  def update
Shinya Maeda's avatar
Shinya Maeda committed
41
    Clusters::UpdateService
42
      .new(project, current_user, update_params)
43
      .execute(cluster)
44

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

63
  def destroy
64
    if cluster.destroy
65
      flash[:notice] = _('Kubernetes cluster integration was successfully removed.')
66 67
      redirect_to project_clusters_path(project), status: 302
    else
68
      flash[:notice] = _('Kubernetes cluster integration was not removed.')
69
      render :show
70
    end
71 72
  end

73 74 75
  private

  def cluster
76
    @cluster ||= project.clusters.find(params[:id])
77
                                 .present(current_user: current_user)
78 79
  end

80
  def update_params
Kamil Trzcinski's avatar
Kamil Trzcinski committed
81 82 83
    if cluster.managed?
      params.require(:cluster).permit(
        :enabled,
84
        :environment_scope,
Kamil Trzcinski's avatar
Kamil Trzcinski committed
85 86 87 88 89 90 91 92
        platform_kubernetes_attributes: [
          :namespace
        ]
      )
    else
      params.require(:cluster).permit(
        :enabled,
        :name,
93
        :environment_scope,
Kamil Trzcinski's avatar
Kamil Trzcinski committed
94 95 96 97 98
        platform_kubernetes_attributes: [
          :api_url,
          :token,
          :ca_cert,
          :namespace
Kamil Trzcinski's avatar
Kamil Trzcinski committed
99
        ]
Kamil Trzcinski's avatar
Kamil Trzcinski committed
100 101
      )
    end
102 103
  end

104
  def authorize_update_cluster!
105
    access_denied! unless can?(current_user, :update_cluster, cluster)
106 107 108
  end

  def authorize_admin_cluster!
109
    access_denied! unless can?(current_user, :admin_cluster, cluster)
110
  end
111

112 113
  def update_applications_status
    @cluster.applications.each(&:schedule_status_update)
114
  end
115
end