debian-mirror-gitlab/app/controllers/clusters/clusters_controller.rb

228 lines
6.3 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
class Clusters::ClustersController < Clusters::BaseController
include RoutableActions
2020-07-28 23:09:34 +05:30
include Metrics::Dashboard::PrometheusApiProxy
include MetricsDashboard
2018-12-13 13:39:08 +05:30
2020-01-01 13:55:28 +05:30
before_action :cluster, only: [:cluster_status, :show, :update, :destroy, :clear_cache]
2022-07-16 23:28:13 +05:30
before_action :user_cluster, only: [:connect]
2022-04-04 11:22:00 +05:30
before_action :authorize_read_cluster!, only: [:show, :index]
2022-07-23 23:45:48 +05:30
before_action :authorize_create_cluster!, only: [:connect]
2018-12-13 13:39:08 +05:30
before_action :authorize_update_cluster!, only: [:update]
2022-06-21 17:19:12 +05:30
before_action :ensure_feature_enabled!, except: [:index, :new_cluster_docs]
2018-12-13 13:39:08 +05:30
helper_method :token_in_session
STATUS_POLLING_INTERVAL = 10_000
def index
2020-05-24 23:13:21 +05:30
@clusters = cluster_list
2019-02-15 15:39:39 +05:30
2020-05-24 23:13:21 +05:30
respond_to do |format|
format.html
format.json do
2020-06-23 00:09:42 +05:30
Gitlab::PollingInterval.set_header(response, interval: STATUS_POLLING_INTERVAL)
2020-05-24 23:13:21 +05:30
serializer = ClusterSerializer.new(current_user: current_user)
2019-02-15 15:39:39 +05:30
2020-05-24 23:13:21 +05:30
render json: {
clusters: serializer.with_pagination(request, response).represent_list(@clusters),
has_ancestor_clusters: @has_ancestor_clusters
}
end
end
2018-12-13 13:39:08 +05:30
end
# Overridding ActionController::Metal#status is NOT a good idea
def cluster_status
respond_to do |format|
format.json do
Gitlab::PollingInterval.set_header(response, interval: STATUS_POLLING_INTERVAL)
render json: ClusterSerializer
.new(current_user: @current_user)
.represent_status(@cluster)
end
end
end
def show
2021-04-29 21:17:54 +05:30
if params[:tab] == 'integrations'
@prometheus_integration = Clusters::IntegrationPresenter.new(@cluster.find_or_build_integration_prometheus)
end
2018-12-13 13:39:08 +05:30
end
def update
Clusters::UpdateService
.new(current_user, update_params)
.execute(cluster)
if cluster.valid?
respond_to do |format|
format.json do
head :no_content
end
format.html do
flash[:notice] = _('Kubernetes cluster was successfully updated.')
redirect_to cluster.show_path
end
end
else
respond_to do |format|
format.json { head :bad_request }
format.html { render :show }
end
end
end
def destroy
2019-12-26 22:10:19 +05:30
response = Clusters::DestroyService
.new(current_user, destroy_params)
.execute(cluster)
flash[:notice] = response[:message]
redirect_to clusterable.index_path, status: :found
2018-12-13 13:39:08 +05:30
end
def create_user
@user_cluster = ::Clusters::CreateService
.new(current_user, create_user_cluster_params)
.execute(access_token: token_in_session)
.present(current_user: current_user)
if @user_cluster.persisted?
redirect_to @user_cluster.show_path
else
2022-05-07 20:08:51 +05:30
render :connect
2018-12-13 13:39:08 +05:30
end
end
2020-01-01 13:55:28 +05:30
def clear_cache
cluster.delete_cached_resources!
2019-12-26 22:10:19 +05:30
2020-01-01 13:55:28 +05:30
redirect_to cluster.show_path, notice: _('Cluster cache cleared.')
2019-12-26 22:10:19 +05:30
end
2018-12-13 13:39:08 +05:30
private
2022-05-07 20:08:51 +05:30
def ensure_feature_enabled!
2022-07-16 23:28:13 +05:30
render_404 unless clusterable.certificate_based_clusters_enabled?
2022-05-07 20:08:51 +05:30
end
2020-05-24 23:13:21 +05:30
def cluster_list
2022-07-16 23:28:13 +05:30
return [] unless clusterable.certificate_based_clusters_enabled?
2022-05-07 20:08:51 +05:30
2022-06-21 17:19:12 +05:30
finder = ClusterAncestorsFinder.new(clusterable.__subject__, current_user)
2020-05-24 23:13:21 +05:30
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-12-26 22:10:19 +05:30
def destroy_params
2020-01-01 13:55:28 +05:30
params.permit(:cleanup)
2019-12-26 22:10:19 +05:30
end
2021-01-03 14:25:43 +05:30
def base_permitted_cluster_params
[
:enabled,
:environment_scope,
:managed,
:namespace_per_environment
]
end
2018-12-13 13:39:08 +05:30
def update_params
2019-07-07 11:18:12 +05:30
if cluster.provided_by_user?
2018-12-13 13:39:08 +05:30
params.require(:cluster).permit(
2021-01-03 14:25:43 +05:30
*base_permitted_cluster_params,
2019-07-07 11:18:12 +05:30
:name,
2019-03-02 22:35:43 +05:30
:base_domain,
2019-12-26 22:10:19 +05:30
:management_project_id,
2018-12-13 13:39:08 +05:30
platform_kubernetes_attributes: [
2019-07-07 11:18:12 +05:30
:api_url,
:token,
:ca_cert,
2018-12-13 13:39:08 +05:30
:namespace
]
)
else
params.require(:cluster).permit(
2021-01-03 14:25:43 +05:30
*base_permitted_cluster_params,
2019-03-02 22:35:43 +05:30
:base_domain,
2019-12-26 22:10:19 +05:30
:management_project_id,
2018-12-13 13:39:08 +05:30
platform_kubernetes_attributes: [
:namespace
]
)
end
end
def create_user_cluster_params
params.require(:cluster).permit(
2021-01-03 14:25:43 +05:30
*base_permitted_cluster_params,
2018-12-13 13:39:08 +05:30
:name,
platform_kubernetes_attributes: [
:namespace,
:api_url,
:token,
:ca_cert,
:authorization_type
]).merge(
provider_type: :user,
platform_type: :kubernetes,
2022-06-21 17:19:12 +05:30
clusterable: clusterable.__subject__
2018-12-13 13:39:08 +05:30
)
end
2020-07-28 23:09:34 +05:30
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-29 21:17:54 +05:30
@proxyable = proxyable
@params = params
2020-07-28 23:09:34 +05:30
end
def execute
success(params: @params)
end
end
end
2018-12-13 13:39:08 +05:30
def user_cluster
2022-06-21 17:19:12 +05:30
cluster = Clusters::BuildService.new(clusterable.__subject__).execute
2019-02-15 15:39:39 +05:30
cluster.build_platform_kubernetes
@user_cluster = cluster.present(current_user: current_user)
2018-12-13 13:39:08 +05:30
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
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Clusters::ClustersController.prepend_mod_with('Clusters::ClustersController')