2018-09-26 03:45:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-01 00:39:01 +00:00
|
|
|
class Clusters::ClustersController < Clusters::BaseController
|
|
|
|
include RoutableActions
|
2020-07-07 06:09:06 +00:00
|
|
|
include Metrics::Dashboard::PrometheusApiProxy
|
2020-07-13 15:09:08 +00:00
|
|
|
include MetricsDashboard
|
2018-11-01 00:39:01 +00:00
|
|
|
|
2019-11-26 09:08:36 +00:00
|
|
|
before_action :cluster, only: [:cluster_status, :show, :update, :destroy, :clear_cache]
|
2018-06-12 05:57:04 +00:00
|
|
|
before_action :generate_gcp_authorize_url, only: [:new]
|
|
|
|
before_action :validate_gcp_token, only: [:new]
|
2018-06-06 03:04:40 +00:00
|
|
|
before_action :gcp_cluster, only: [:new]
|
|
|
|
before_action :user_cluster, only: [:new]
|
2019-12-12 00:07:43 +00:00
|
|
|
before_action :authorize_create_cluster!, only: [:new, :authorize_aws_role]
|
2017-10-03 14:44:06 +00:00
|
|
|
before_action :authorize_update_cluster!, only: [:update]
|
2019-11-26 09:08:36 +00:00
|
|
|
before_action :authorize_admin_cluster!, only: [:destroy, :clear_cache]
|
2018-11-01 00:39:01 +00:00
|
|
|
before_action :update_applications_status, only: [:cluster_status]
|
2018-10-15 03:37:51 +00:00
|
|
|
|
2018-05-31 09:28:48 +00:00
|
|
|
helper_method :token_in_session
|
2017-09-25 07:10:25 +00:00
|
|
|
|
2017-12-03 20:34:00 +00:00
|
|
|
STATUS_POLLING_INTERVAL = 10_000
|
|
|
|
|
2017-10-05 15:04:35 +00:00
|
|
|
def index
|
2020-04-30 21:09:47 +00:00
|
|
|
@clusters = cluster_list
|
2018-12-11 05:13:29 +00:00
|
|
|
|
2020-04-30 21:09:47 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
2020-05-27 15:08:11 +00:00
|
|
|
Gitlab::PollingInterval.set_header(response, interval: STATUS_POLLING_INTERVAL)
|
2020-04-30 21:09:47 +00:00
|
|
|
serializer = ClusterSerializer.new(current_user: current_user)
|
2018-12-11 05:13:29 +00:00
|
|
|
|
2020-04-30 21:09:47 +00:00
|
|
|
render json: {
|
|
|
|
clusters: serializer.with_pagination(request, response).represent_list(@clusters),
|
|
|
|
has_ancestor_clusters: @has_ancestor_clusters
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2017-10-05 15:04:35 +00:00
|
|
|
end
|
|
|
|
|
2017-12-03 14:00:33 +00:00
|
|
|
def new
|
2019-11-08 06:06:24 +00:00
|
|
|
if params[:provider] == 'aws'
|
2021-09-03 12:09:03 +00:00
|
|
|
@aws_role = Aws::Role.create_or_find_by!(user: current_user)
|
2019-12-12 00:07:43 +00:00
|
|
|
@instance_types = load_instance_types.to_json
|
2019-09-09 20:27:51 +00:00
|
|
|
|
2019-11-08 06:06:24 +00:00
|
|
|
elsif params[:provider] == 'gcp'
|
|
|
|
redirect_to @authorize_url if @authorize_url && !@valid_gcp_token
|
|
|
|
end
|
2017-12-03 14:00:33 +00:00
|
|
|
end
|
|
|
|
|
2018-11-01 00:39:01 +00:00
|
|
|
# Overridding ActionController::Metal#status is NOT a good idea
|
|
|
|
def cluster_status
|
2017-09-28 09:11:17 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2017-12-04 13:21:23 +00:00
|
|
|
Gitlab::PollingInterval.set_header(response, interval: STATUS_POLLING_INTERVAL)
|
2017-10-02 12:58:50 +00:00
|
|
|
|
2017-10-03 21:21:54 +00:00
|
|
|
render json: ClusterSerializer
|
2018-10-19 01:42:30 +00:00
|
|
|
.new(current_user: @current_user)
|
2017-10-03 21:21:54 +00:00
|
|
|
.represent_status(@cluster)
|
2017-09-27 12:53:50 +00:00
|
|
|
end
|
|
|
|
end
|
2017-09-25 07:10:25 +00:00
|
|
|
end
|
|
|
|
|
2017-10-03 14:44:06 +00:00
|
|
|
def show
|
2021-04-07 00:09:26 +00:00
|
|
|
if params[:tab] == 'integrations'
|
2021-04-15 12:09:05 +00:00
|
|
|
@prometheus_integration = Clusters::IntegrationPresenter.new(@cluster.find_or_build_integration_prometheus)
|
2021-05-14 03:10:11 +00:00
|
|
|
@elastic_stack_integration = Clusters::IntegrationPresenter.new(@cluster.find_or_build_integration_elastic_stack)
|
2021-04-07 00:09:26 +00:00
|
|
|
end
|
2017-09-25 07:10:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2017-10-23 08:36:35 +00:00
|
|
|
Clusters::UpdateService
|
2018-10-10 23:15:04 +00:00
|
|
|
.new(current_user, update_params)
|
2017-09-30 15:54:22 +00:00
|
|
|
.execute(cluster)
|
2017-09-28 15:08:11 +00:00
|
|
|
|
2017-10-04 19:13:01 +00:00
|
|
|
if cluster.valid?
|
2017-11-24 13:43:00 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
format.html do
|
2018-01-29 19:49:00 +00:00
|
|
|
flash[:notice] = _('Kubernetes cluster was successfully updated.')
|
2018-10-30 10:33:43 +00:00
|
|
|
redirect_to cluster.show_path
|
2017-11-24 13:43:00 +00:00
|
|
|
end
|
|
|
|
end
|
2017-10-04 19:13:01 +00:00
|
|
|
else
|
2017-11-24 13:43:00 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.json { head :bad_request }
|
|
|
|
format.html { render :show }
|
|
|
|
end
|
2017-10-04 19:13:01 +00:00
|
|
|
end
|
2017-09-25 07:10:25 +00:00
|
|
|
end
|
|
|
|
|
2017-09-27 13:17:41 +00:00
|
|
|
def destroy
|
2019-11-08 12:06:32 +00:00
|
|
|
response = Clusters::DestroyService
|
|
|
|
.new(current_user, destroy_params)
|
|
|
|
.execute(cluster)
|
|
|
|
|
|
|
|
flash[:notice] = response[:message]
|
|
|
|
redirect_to clusterable.index_path, status: :found
|
2017-09-27 13:17:41 +00:00
|
|
|
end
|
|
|
|
|
2018-06-12 05:57:04 +00:00
|
|
|
def create_gcp
|
|
|
|
@gcp_cluster = ::Clusters::CreateService
|
2018-10-10 23:15:04 +00:00
|
|
|
.new(current_user, create_gcp_cluster_params)
|
2018-10-19 01:42:30 +00:00
|
|
|
.execute(access_token: token_in_session)
|
2018-10-30 10:33:43 +00:00
|
|
|
.present(current_user: current_user)
|
2018-06-12 05:57:04 +00:00
|
|
|
|
|
|
|
if @gcp_cluster.persisted?
|
2018-10-30 10:33:43 +00:00
|
|
|
redirect_to @gcp_cluster.show_path
|
2018-06-12 05:57:04 +00:00
|
|
|
else
|
|
|
|
generate_gcp_authorize_url
|
|
|
|
validate_gcp_token
|
|
|
|
user_cluster
|
2019-11-27 00:06:23 +00:00
|
|
|
params[:provider] = 'gcp'
|
2018-06-12 05:57:04 +00:00
|
|
|
|
2019-09-09 20:27:51 +00:00
|
|
|
render :new, locals: { active_tab: 'create' }
|
2018-06-12 05:57:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-13 06:06:38 +00:00
|
|
|
def create_aws
|
|
|
|
@aws_cluster = ::Clusters::CreateService
|
|
|
|
.new(current_user, create_aws_cluster_params)
|
|
|
|
.execute
|
|
|
|
.present(current_user: current_user)
|
|
|
|
|
|
|
|
if @aws_cluster.persisted?
|
|
|
|
head :created, location: @aws_cluster.show_path
|
|
|
|
else
|
|
|
|
render status: :unprocessable_entity, json: @aws_cluster.errors
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-12 05:57:04 +00:00
|
|
|
def create_user
|
|
|
|
@user_cluster = ::Clusters::CreateService
|
2018-10-10 23:15:04 +00:00
|
|
|
.new(current_user, create_user_cluster_params)
|
2018-10-19 01:42:30 +00:00
|
|
|
.execute(access_token: token_in_session)
|
2018-10-30 10:33:43 +00:00
|
|
|
.present(current_user: current_user)
|
2018-06-12 05:57:04 +00:00
|
|
|
|
|
|
|
if @user_cluster.persisted?
|
2018-10-30 10:33:43 +00:00
|
|
|
redirect_to @user_cluster.show_path
|
2018-06-12 05:57:04 +00:00
|
|
|
else
|
|
|
|
generate_gcp_authorize_url
|
|
|
|
validate_gcp_token
|
|
|
|
gcp_cluster
|
|
|
|
|
2019-09-09 20:27:51 +00:00
|
|
|
render :new, locals: { active_tab: 'add' }
|
2018-06-12 05:57:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-08 06:06:24 +00:00
|
|
|
def authorize_aws_role
|
2019-12-12 00:07:43 +00:00
|
|
|
response = Clusters::Aws::AuthorizeRoleService.new(
|
|
|
|
current_user,
|
|
|
|
params: aws_role_params
|
2019-11-13 06:06:38 +00:00
|
|
|
).execute
|
|
|
|
|
|
|
|
render json: response.body, status: response.status
|
|
|
|
end
|
|
|
|
|
2019-11-26 09:08:36 +00:00
|
|
|
def clear_cache
|
|
|
|
cluster.delete_cached_resources!
|
|
|
|
|
|
|
|
redirect_to cluster.show_path, notice: _('Cluster cache cleared.')
|
|
|
|
end
|
|
|
|
|
2017-09-25 07:10:25 +00:00
|
|
|
private
|
|
|
|
|
2020-04-30 21:09:47 +00:00
|
|
|
def cluster_list
|
|
|
|
finder = ClusterAncestorsFinder.new(clusterable.subject, current_user)
|
|
|
|
clusters = finder.execute
|
|
|
|
|
|
|
|
@has_ancestor_clusters = finder.has_ancestor_clusters?
|
|
|
|
|
|
|
|
# Note: We are paginating through an array here but this should OK as:
|
|
|
|
#
|
|
|
|
# In CE, we can have a maximum group nesting depth of 21, so including
|
|
|
|
# project cluster, we can have max 22 clusters for a group hierarchy.
|
|
|
|
# In EE (Premium) we can have any number, as multiple clusters are
|
|
|
|
# supported, but the number of clusters are fairly low currently.
|
|
|
|
#
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab-foss/issues/55260 also.
|
|
|
|
Kaminari.paginate_array(clusters).page(params[:page]).per(20)
|
|
|
|
end
|
|
|
|
|
2019-11-08 12:06:32 +00:00
|
|
|
def destroy_params
|
2019-11-19 03:06:07 +00:00
|
|
|
params.permit(:cleanup)
|
2019-11-08 12:06:32 +00:00
|
|
|
end
|
|
|
|
|
2020-09-29 18:09:52 +00:00
|
|
|
def base_permitted_cluster_params
|
|
|
|
[
|
|
|
|
:enabled,
|
|
|
|
:environment_scope,
|
|
|
|
:managed,
|
|
|
|
:namespace_per_environment
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2017-10-05 15:04:35 +00:00
|
|
|
def update_params
|
2019-03-29 18:23:18 +00:00
|
|
|
if cluster.provided_by_user?
|
2017-12-03 14:10:18 +00:00
|
|
|
params.require(:cluster).permit(
|
2020-09-29 18:09:52 +00:00
|
|
|
*base_permitted_cluster_params,
|
2019-03-29 18:23:18 +00:00
|
|
|
:name,
|
2019-01-31 14:58:58 +00:00
|
|
|
:base_domain,
|
2019-10-24 12:06:03 +00:00
|
|
|
:management_project_id,
|
2017-12-03 14:10:18 +00:00
|
|
|
platform_kubernetes_attributes: [
|
2019-03-29 18:23:18 +00:00
|
|
|
:api_url,
|
|
|
|
:token,
|
|
|
|
:ca_cert,
|
2017-12-03 14:10:18 +00:00
|
|
|
:namespace
|
|
|
|
]
|
|
|
|
)
|
|
|
|
else
|
|
|
|
params.require(:cluster).permit(
|
2020-09-29 18:09:52 +00:00
|
|
|
*base_permitted_cluster_params,
|
2019-01-31 14:58:58 +00:00
|
|
|
:base_domain,
|
2019-10-24 12:06:03 +00:00
|
|
|
:management_project_id,
|
2017-12-03 14:10:18 +00:00
|
|
|
platform_kubernetes_attributes: [
|
|
|
|
:namespace
|
2017-12-03 14:33:10 +00:00
|
|
|
]
|
2017-12-03 14:10:18 +00:00
|
|
|
)
|
|
|
|
end
|
2017-10-05 15:04:35 +00:00
|
|
|
end
|
|
|
|
|
2018-06-12 05:57:04 +00:00
|
|
|
def create_gcp_cluster_params
|
|
|
|
params.require(:cluster).permit(
|
2020-09-29 18:09:52 +00:00
|
|
|
*base_permitted_cluster_params,
|
2018-06-12 05:57:04 +00:00
|
|
|
:name,
|
|
|
|
provider_gcp_attributes: [
|
|
|
|
:gcp_project_id,
|
|
|
|
:zone,
|
|
|
|
:num_nodes,
|
2018-09-07 13:32:31 +00:00
|
|
|
:machine_type,
|
2019-09-23 00:06:29 +00:00
|
|
|
:cloud_run,
|
2018-09-07 13:32:31 +00:00
|
|
|
:legacy_abac
|
2018-06-12 05:57:04 +00:00
|
|
|
]).merge(
|
|
|
|
provider_type: :gcp,
|
2018-10-19 01:42:30 +00:00
|
|
|
platform_type: :kubernetes,
|
2018-10-30 10:33:43 +00:00
|
|
|
clusterable: clusterable.subject
|
2019-11-13 06:06:38 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_aws_cluster_params
|
|
|
|
params.require(:cluster).permit(
|
2020-09-29 18:09:52 +00:00
|
|
|
*base_permitted_cluster_params,
|
2019-11-13 06:06:38 +00:00
|
|
|
:name,
|
|
|
|
provider_aws_attributes: [
|
2020-08-26 00:10:31 +00:00
|
|
|
:kubernetes_version,
|
2019-11-13 06:06:38 +00:00
|
|
|
:key_name,
|
|
|
|
:role_arn,
|
|
|
|
:region,
|
|
|
|
:vpc_id,
|
|
|
|
:instance_type,
|
|
|
|
:num_nodes,
|
|
|
|
:security_group_id,
|
|
|
|
subnet_ids: []
|
|
|
|
]).merge(
|
|
|
|
provider_type: :aws,
|
|
|
|
platform_type: :kubernetes,
|
|
|
|
clusterable: clusterable.subject
|
2018-06-12 05:57:04 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_user_cluster_params
|
|
|
|
params.require(:cluster).permit(
|
2020-09-29 18:09:52 +00:00
|
|
|
*base_permitted_cluster_params,
|
2018-06-12 05:57:04 +00:00
|
|
|
:name,
|
|
|
|
platform_kubernetes_attributes: [
|
|
|
|
:namespace,
|
|
|
|
:api_url,
|
|
|
|
:token,
|
2018-09-06 10:03:38 +00:00
|
|
|
:ca_cert,
|
|
|
|
:authorization_type
|
2018-06-12 05:57:04 +00:00
|
|
|
]).merge(
|
|
|
|
provider_type: :user,
|
2018-10-19 01:42:30 +00:00
|
|
|
platform_type: :kubernetes,
|
2018-10-30 10:33:43 +00:00
|
|
|
clusterable: clusterable.subject
|
2018-06-12 05:57:04 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-12-12 00:07:43 +00:00
|
|
|
def aws_role_params
|
2020-10-23 00:08:30 +00:00
|
|
|
params.require(:cluster).permit(:role_arn, :region)
|
2019-11-08 06:06:24 +00:00
|
|
|
end
|
|
|
|
|
2018-06-12 05:57:04 +00:00
|
|
|
def generate_gcp_authorize_url
|
2019-11-27 00:06:23 +00:00
|
|
|
state = generate_session_key_redirect(clusterable.new_path(provider: :gcp).to_s)
|
2018-06-12 05:57:04 +00:00
|
|
|
|
|
|
|
@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
|
2018-12-10 12:33:36 +00:00
|
|
|
cluster = Clusters::BuildService.new(clusterable.subject).execute
|
|
|
|
cluster.build_provider_gcp
|
|
|
|
@gcp_cluster = cluster.present(current_user: current_user)
|
2018-06-12 05:57:04 +00:00
|
|
|
end
|
|
|
|
|
2020-07-07 06:09:06 +00:00
|
|
|
def proxyable
|
|
|
|
cluster.cluster
|
|
|
|
end
|
|
|
|
|
|
|
|
# During first iteration of dashboard variables implementation
|
|
|
|
# cluster health case was omitted. Existing service for now is tied to
|
|
|
|
# environment, which is not always present for cluster health dashboard.
|
|
|
|
# It is planned to break coupling to environment https://gitlab.com/gitlab-org/gitlab/-/issues/213833.
|
|
|
|
# It is also planned to move cluster health to metrics dashboard section https://gitlab.com/gitlab-org/gitlab/-/issues/220214
|
|
|
|
# but for now I've used dummy class to stub variable substitution service, as there are no variables
|
|
|
|
# in cluster health dashboard
|
|
|
|
def proxy_variable_substitution_service
|
|
|
|
@empty_service ||= Class.new(BaseService) do
|
|
|
|
def initialize(proxyable, params)
|
2021-04-19 21:09:27 +00:00
|
|
|
@proxyable = proxyable
|
|
|
|
@params = params
|
2020-07-07 06:09:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
success(params: @params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-12 05:57:04 +00:00
|
|
|
def user_cluster
|
2018-12-10 12:33:36 +00:00
|
|
|
cluster = Clusters::BuildService.new(clusterable.subject).execute
|
|
|
|
cluster.build_platform_kubernetes
|
|
|
|
@user_cluster = cluster.present(current_user: current_user)
|
2018-06-12 05:57:04 +00:00
|
|
|
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
|
|
|
|
|
2019-12-12 00:07:43 +00:00
|
|
|
##
|
|
|
|
# Unfortunately the EC2 API doesn't provide a list of
|
|
|
|
# possible instance types. There is a workaround, using
|
|
|
|
# the Pricing API, but instead of requiring the
|
|
|
|
# user to grant extra permissions for this we use the
|
|
|
|
# values that validate the CloudFormation template.
|
|
|
|
def load_instance_types
|
|
|
|
stack_template = File.read(Rails.root.join('vendor', 'aws', 'cloudformation', 'eks_cluster.yaml'))
|
|
|
|
instance_types = YAML.safe_load(stack_template).dig('Parameters', 'NodeInstanceType', 'AllowedValues')
|
|
|
|
|
|
|
|
instance_types.map { |type| Hash(name: type, value: type) }
|
|
|
|
end
|
|
|
|
|
2018-02-22 22:08:12 +00:00
|
|
|
def update_applications_status
|
|
|
|
@cluster.applications.each(&:schedule_status_update)
|
2018-02-20 02:49:35 +00:00
|
|
|
end
|
2017-09-25 07:10:25 +00:00
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
Clusters::ClustersController.prepend_mod_with('Clusters::ClustersController')
|