2017-09-25 03:10:25 -04:00
|
|
|
class Projects::ClustersController < Projects::ApplicationController
|
2018-06-12 01:57:04 -04:00
|
|
|
before_action :cluster, except: [:index, :new, :create_gcp, :create_user]
|
2017-10-03 10:44:06 -04:00
|
|
|
before_action :authorize_read_cluster!
|
2018-06-12 01:57:04 -04:00
|
|
|
before_action :generate_gcp_authorize_url, only: [:new]
|
|
|
|
before_action :validate_gcp_token, only: [:new]
|
2018-06-05 23:04:40 -04:00
|
|
|
before_action :gcp_cluster, only: [:new]
|
|
|
|
before_action :user_cluster, only: [:new]
|
2017-11-02 06:30:07 -04:00
|
|
|
before_action :authorize_create_cluster!, only: [:new]
|
2017-10-03 10:44:06 -04:00
|
|
|
before_action :authorize_update_cluster!, only: [:update]
|
|
|
|
before_action :authorize_admin_cluster!, only: [:destroy]
|
2018-02-22 17:08:12 -05:00
|
|
|
before_action :update_applications_status, only: [:status]
|
2018-05-31 05:28:48 -04:00
|
|
|
helper_method :token_in_session
|
2017-09-25 03:10:25 -04:00
|
|
|
|
2017-12-03 15:34:00 -05:00
|
|
|
STATUS_POLLING_INTERVAL = 10_000
|
|
|
|
|
2017-10-05 11:04:35 -04:00
|
|
|
def index
|
2017-11-30 16:23:10 -05:00
|
|
|
clusters = ClustersFinder.new(project, current_user, :all).execute
|
|
|
|
@clusters = clusters.page(params[:page]).per(20)
|
2017-10-05 11:04:35 -04:00
|
|
|
end
|
|
|
|
|
2017-12-03 09:00:33 -05:00
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
2017-09-30 11:54:22 -04:00
|
|
|
def status
|
2017-09-28 05:11:17 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2017-12-04 08:21:23 -05:00
|
|
|
Gitlab::PollingInterval.set_header(response, interval: STATUS_POLLING_INTERVAL)
|
2017-10-02 08:58:50 -04:00
|
|
|
|
2017-10-03 17:21:54 -04:00
|
|
|
render json: ClusterSerializer
|
|
|
|
.new(project: @project, current_user: @current_user)
|
|
|
|
.represent_status(@cluster)
|
2017-09-27 08:53:50 -04:00
|
|
|
end
|
|
|
|
end
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
|
2017-10-03 10:44:06 -04:00
|
|
|
def show
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2017-10-23 04:36:35 -04:00
|
|
|
Clusters::UpdateService
|
2017-10-05 11:04:35 -04:00
|
|
|
.new(project, current_user, update_params)
|
2017-09-30 11:54:22 -04:00
|
|
|
.execute(cluster)
|
2017-09-28 11:08:11 -04:00
|
|
|
|
2017-10-04 15:13:01 -04:00
|
|
|
if cluster.valid?
|
2017-11-24 08:43:00 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
format.html do
|
2018-01-29 14:49:00 -05:00
|
|
|
flash[:notice] = _('Kubernetes cluster was successfully updated.')
|
2017-12-04 11:02:18 -05:00
|
|
|
redirect_to project_cluster_path(project, cluster)
|
2017-11-24 08:43:00 -05:00
|
|
|
end
|
|
|
|
end
|
2017-10-04 15:13:01 -04:00
|
|
|
else
|
2017-11-24 08:43:00 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.json { head :bad_request }
|
|
|
|
format.html { render :show }
|
|
|
|
end
|
2017-10-04 15:13:01 -04:00
|
|
|
end
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
|
2017-09-27 09:17:41 -04:00
|
|
|
def destroy
|
2017-09-28 11:08:11 -04:00
|
|
|
if cluster.destroy
|
2018-01-29 14:49:00 -05:00
|
|
|
flash[:notice] = _('Kubernetes cluster integration was successfully removed.')
|
2018-07-02 06:43:06 -04:00
|
|
|
redirect_to project_clusters_path(project), status: :found
|
2017-09-28 11:08:11 -04:00
|
|
|
else
|
2018-01-29 14:49:00 -05:00
|
|
|
flash[:notice] = _('Kubernetes cluster integration was not removed.')
|
2017-10-03 10:44:06 -04:00
|
|
|
render :show
|
2017-09-28 11:08:11 -04:00
|
|
|
end
|
2017-09-27 09:17:41 -04:00
|
|
|
end
|
|
|
|
|
2018-06-12 01:57:04 -04:00
|
|
|
def create_gcp
|
|
|
|
@gcp_cluster = ::Clusters::CreateService
|
|
|
|
.new(project, current_user, create_gcp_cluster_params)
|
|
|
|
.execute(token_in_session)
|
|
|
|
|
|
|
|
if @gcp_cluster.persisted?
|
|
|
|
redirect_to project_cluster_path(project, @gcp_cluster)
|
|
|
|
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
|
|
|
|
.new(project, current_user, create_user_cluster_params)
|
|
|
|
.execute(token_in_session)
|
|
|
|
|
|
|
|
if @user_cluster.persisted?
|
|
|
|
redirect_to project_cluster_path(project, @user_cluster)
|
|
|
|
else
|
|
|
|
generate_gcp_authorize_url
|
|
|
|
validate_gcp_token
|
|
|
|
gcp_cluster
|
|
|
|
|
|
|
|
render :new, locals: { active_tab: 'user' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-25 03:10:25 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def cluster
|
2017-12-05 04:02:15 -05:00
|
|
|
@cluster ||= project.clusters.find(params[:id])
|
2017-12-04 08:27:31 -05:00
|
|
|
.present(current_user: current_user)
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
|
2017-10-05 11:04:35 -04:00
|
|
|
def update_params
|
2017-12-03 09:10:18 -05:00
|
|
|
if cluster.managed?
|
|
|
|
params.require(:cluster).permit(
|
|
|
|
:enabled,
|
2017-12-16 11:28:59 -05:00
|
|
|
:environment_scope,
|
2017-12-03 09:10:18 -05:00
|
|
|
platform_kubernetes_attributes: [
|
|
|
|
:namespace
|
|
|
|
]
|
|
|
|
)
|
|
|
|
else
|
|
|
|
params.require(:cluster).permit(
|
|
|
|
:enabled,
|
|
|
|
:name,
|
2017-12-16 11:28:59 -05:00
|
|
|
:environment_scope,
|
2017-12-03 09:10:18 -05:00
|
|
|
platform_kubernetes_attributes: [
|
|
|
|
:api_url,
|
|
|
|
:token,
|
|
|
|
:ca_cert,
|
|
|
|
:namespace
|
2017-12-03 09:33:10 -05:00
|
|
|
]
|
2017-12-03 09:10:18 -05:00
|
|
|
)
|
|
|
|
end
|
2017-10-05 11:04:35 -04:00
|
|
|
end
|
|
|
|
|
2018-06-12 01:57:04 -04:00
|
|
|
def create_gcp_cluster_params
|
|
|
|
params.require(:cluster).permit(
|
|
|
|
:enabled,
|
|
|
|
:name,
|
|
|
|
:environment_scope,
|
|
|
|
provider_gcp_attributes: [
|
|
|
|
:gcp_project_id,
|
|
|
|
:zone,
|
|
|
|
:num_nodes,
|
2018-09-07 09:32:31 -04:00
|
|
|
:machine_type,
|
|
|
|
:legacy_abac
|
2018-06-12 01:57:04 -04:00
|
|
|
]).merge(
|
|
|
|
provider_type: :gcp,
|
|
|
|
platform_type: :kubernetes
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_user_cluster_params
|
|
|
|
params.require(:cluster).permit(
|
|
|
|
:enabled,
|
|
|
|
:name,
|
|
|
|
:environment_scope,
|
|
|
|
platform_kubernetes_attributes: [
|
|
|
|
:namespace,
|
|
|
|
:api_url,
|
|
|
|
:token,
|
2018-09-06 06:03:38 -04:00
|
|
|
:ca_cert,
|
|
|
|
:authorization_type
|
2018-06-12 01:57:04 -04:00
|
|
|
]).merge(
|
|
|
|
provider_type: :user,
|
|
|
|
platform_type: :kubernetes
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_gcp_authorize_url
|
|
|
|
state = generate_session_key_redirect(new_project_cluster_path(@project).to_s)
|
|
|
|
|
|
|
|
@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
|
|
|
|
@gcp_cluster = ::Clusters::Cluster.new.tap do |cluster|
|
|
|
|
cluster.build_provider_gcp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_cluster
|
|
|
|
@user_cluster = ::Clusters::Cluster.new.tap do |cluster|
|
|
|
|
cluster.build_platform_kubernetes
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2017-10-03 10:44:06 -04:00
|
|
|
def authorize_update_cluster!
|
2017-10-03 17:21:54 -04:00
|
|
|
access_denied! unless can?(current_user, :update_cluster, cluster)
|
2017-10-03 10:44:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_admin_cluster!
|
2017-10-03 17:21:54 -04:00
|
|
|
access_denied! unless can?(current_user, :admin_cluster, cluster)
|
2017-10-03 10:44:06 -04:00
|
|
|
end
|
2018-02-19 21:49:35 -05:00
|
|
|
|
2018-02-22 17:08:12 -05:00
|
|
|
def update_applications_status
|
|
|
|
@cluster.applications.each(&:schedule_status_update)
|
2018-02-19 21:49:35 -05:00
|
|
|
end
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|