2018-12-18 21:40:20 +00:00
# frozen_string_literal: true
module API
2020-10-15 00:08:42 +00:00
class ProjectClusters < :: API :: Base
2018-12-18 21:40:20 +00:00
include PaginationParams
2022-03-05 03:19:17 +00:00
before do
authenticate!
ensure_feature_enabled!
end
2018-12-18 21:40:20 +00:00
2020-10-30 15:08:59 +00:00
feature_category :kubernetes_management
2022-04-28 18:10:01 +00:00
urgency :low
2020-10-30 15:08:59 +00:00
2018-12-18 21:40:20 +00:00
params do
2022-11-03 15:11:31 +00:00
requires :id , types : [ String , Integer ] , desc : 'The ID or URL-encoded path of the project'
2018-12-18 21:40:20 +00:00
end
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2022-11-09 12:07:48 +00:00
desc 'List project clusters' do
detail 'This feature was introduced in GitLab 11.7. Returns a list of project clusters.'
2018-12-18 21:40:20 +00:00
success Entities :: Cluster
2022-11-09 12:07:48 +00:00
failure [
{ code : 403 , message : 'Forbidden' }
]
is_array true
tags %w[ clusters ]
2018-12-18 21:40:20 +00:00
end
params do
use :pagination
end
get ':id/clusters' do
authorize! :read_cluster , user_project
present paginate ( clusters_for_current_user ) , with : Entities :: Cluster
end
2022-11-09 12:07:48 +00:00
desc 'Get a single project cluster' do
detail 'This feature was introduced in GitLab 11.7. Gets a single project cluster.'
2018-12-18 21:40:20 +00:00
success Entities :: ClusterProject
2022-11-09 12:07:48 +00:00
failure [
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not found' }
]
tags %w[ clusters ]
2018-12-18 21:40:20 +00:00
end
params do
requires :cluster_id , type : Integer , desc : 'The cluster ID'
end
get ':id/clusters/:cluster_id' do
authorize! :read_cluster , cluster
present cluster , with : Entities :: ClusterProject
end
2022-11-09 12:07:48 +00:00
desc 'Add existing cluster to project' do
detail 'This feature was introduced in GitLab 11.7. Adds an existing Kubernetes cluster to the project.'
2018-12-18 21:40:20 +00:00
success Entities :: ClusterProject
2022-11-09 12:07:48 +00:00
failure [
{ code : 400 , message : 'Validation error' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not found' }
]
tags %w[ clusters ]
2018-12-18 21:40:20 +00:00
end
params do
requires :name , type : String , desc : 'Cluster name'
optional :enabled , type : Boolean , default : true , desc : 'Determines if cluster is active or not, defaults to true'
2019-04-01 16:29:05 +00:00
optional :domain , type : String , desc : 'Cluster base domain'
2020-07-08 12:09:33 +00:00
optional :environment_scope , default : '*' , type : String , desc : 'The associated environment to the cluster'
2020-09-29 18:09:52 +00:00
optional :namespace_per_environment , default : true , type : Boolean , desc : 'Deploy each environment to a separate Kubernetes namespace'
2020-04-14 15:09:44 +00:00
optional :management_project_id , type : Integer , desc : 'The ID of the management project'
2019-05-03 01:05:53 +00:00
optional :managed , type : Boolean , default : true , desc : 'Determines if GitLab will manage namespaces and service accounts for this cluster, defaults to true'
2018-12-18 21:40:20 +00:00
requires :platform_kubernetes_attributes , type : Hash , desc : %q( Platform Kubernetes data ) do
requires :api_url , type : String , allow_blank : false , desc : 'URL to access the Kubernetes API'
requires :token , type : String , desc : 'Token to authenticate against Kubernetes'
optional :ca_cert , type : String , desc : 'TLS certificate (needed if API is using a self-signed TLS certificate)'
optional :namespace , type : String , desc : 'Unique namespace related to Project'
2020-02-13 12:08:49 +00:00
optional :authorization_type , type : String , values : :: Clusters :: Platforms :: Kubernetes . authorization_types . keys , default : 'rbac' , desc : 'Cluster authorization type, defaults to RBAC'
2018-12-18 21:40:20 +00:00
end
end
post ':id/clusters/user' do
2019-07-10 05:33:09 +00:00
authorize! :add_cluster , user_project
2018-12-18 21:40:20 +00:00
user_cluster = :: Clusters :: CreateService
. new ( current_user , create_cluster_user_params )
. execute
if user_cluster . persisted?
present user_cluster , with : Entities :: ClusterProject
else
render_validation_error! ( user_cluster )
end
end
2022-11-09 12:07:48 +00:00
desc 'Edit project cluster' do
detail 'This feature was introduced in GitLab 11.7. Updates an existing project cluster.'
2018-12-18 21:40:20 +00:00
success Entities :: ClusterProject
2022-11-09 12:07:48 +00:00
failure [
{ code : 400 , message : 'Validation error' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not found' }
]
tags %w[ clusters ]
2018-12-18 21:40:20 +00:00
end
params do
requires :cluster_id , type : Integer , desc : 'The cluster ID'
optional :name , type : String , desc : 'Cluster name'
2019-04-01 16:29:05 +00:00
optional :domain , type : String , desc : 'Cluster base domain'
2020-07-08 12:09:33 +00:00
optional :environment_scope , type : String , desc : 'The associated environment to the cluster'
2020-09-29 18:09:52 +00:00
optional :namespace_per_environment , default : true , type : Boolean , desc : 'Deploy each environment to a separate Kubernetes namespace'
2019-10-24 12:06:03 +00:00
optional :management_project_id , type : Integer , desc : 'The ID of the management project'
2020-11-18 12:09:16 +00:00
optional :enabled , type : Boolean , desc : 'Determines if cluster is active or not'
optional :managed , type : Boolean , desc : 'Determines if GitLab will manage namespaces and service accounts for this cluster'
2018-12-18 21:40:20 +00:00
optional :platform_kubernetes_attributes , type : Hash , desc : %q( Platform Kubernetes data ) do
optional :api_url , type : String , desc : 'URL to access the Kubernetes API'
optional :token , type : String , desc : 'Token to authenticate against Kubernetes'
optional :ca_cert , type : String , desc : 'TLS certificate (needed if API is using a self-signed TLS certificate)'
optional :namespace , type : String , desc : 'Unique namespace related to Project'
end
end
put ':id/clusters/:cluster_id' do
authorize! :update_cluster , cluster
2020-02-13 12:08:49 +00:00
update_service = :: Clusters :: UpdateService . new ( current_user , update_cluster_params )
2018-12-18 21:40:20 +00:00
if update_service . execute ( cluster )
present cluster , with : Entities :: ClusterProject
else
render_validation_error! ( cluster )
end
end
2022-11-09 12:07:48 +00:00
desc 'Delete project cluster' do
detail 'This feature was introduced in GitLab 11.7. Deletes an existing project cluster. Does not remove existing resources within the connected Kubernetes cluster.'
2018-12-18 21:40:20 +00:00
success Entities :: ClusterProject
2022-11-09 12:07:48 +00:00
failure [
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not found' }
]
tags %w[ clusters ]
2018-12-18 21:40:20 +00:00
end
params do
requires :cluster_id , type : Integer , desc : 'The Cluster ID'
end
delete ':id/clusters/:cluster_id' do
authorize! :admin_cluster , cluster
destroy_conditionally! ( cluster )
end
end
helpers do
def clusters_for_current_user
@clusters_for_current_user || = ClustersFinder . new ( user_project , current_user , :all ) . execute
end
def cluster
@cluster || = clusters_for_current_user . find ( params [ :cluster_id ] )
end
def create_cluster_user_params
declared_params . merge ( {
provider_type : :user ,
platform_type : :kubernetes ,
clusterable : user_project
} )
end
def update_cluster_params
declared_params ( include_missing : false ) . without ( :cluster_id )
end
2022-03-05 03:19:17 +00:00
def ensure_feature_enabled!
2022-05-05 09:08:00 +00:00
namespace = user_project . namespace
not_found! unless namespace . certificate_based_clusters_enabled?
2022-03-05 03:19:17 +00:00
end
2018-12-18 21:40:20 +00:00
end
end
end