diff options
Diffstat (limited to 'app/services/ci')
-rw-r--r-- | app/services/ci/create_cluster_service.rb | 29 | ||||
-rw-r--r-- | app/services/ci/integrate_cluster_service.rb | 36 | ||||
-rw-r--r-- | app/services/ci/update_cluster_service.rb | 26 |
3 files changed, 67 insertions, 24 deletions
diff --git a/app/services/ci/create_cluster_service.rb b/app/services/ci/create_cluster_service.rb index edae245ec38..86820706831 100644 --- a/app/services/ci/create_cluster_service.rb +++ b/app/services/ci/create_cluster_service.rb @@ -1,29 +1,10 @@ module Ci class CreateClusterService < BaseService - UnexpectedOperationError = Class.new(StandardError) - - def create_cluster_on_gke(api_client) - # Create a cluster on GKE - operation = api_client.projects_zones_clusters_create( - params['gcp_project_id'], params['cluster_zone'], params['cluster_name'], - cluster_size: params['cluster_size'], machine_type: params['machine_type'] - ) - - if operation&.status != ('RUNNING' || 'PENDING') - raise UnexpectedOperationError.new(operation&.status_message) - end - - api_client.parse_self_link(operation.self_link).tap do |project_id, zone, operation_id| - project.clusters.create(user: current_user, - gcp_project_id: params['gcp_project_id'], - cluster_zone: params['cluster_zone'], - cluster_name: params['cluster_name'], - project_namespace: params['project_namespace'], - gcp_operation_id: operation_id).tap do |cluster| - # Start status polling. When the operation finish, create KubernetesService. - cluster.creation_status(api_client.access_token) - end - end + def execute(access_token) + project.clusters.create( + params.merge(user: current_user, + status: Ci::Cluster.statuses[:scheduled], + gcp_token: access_token)) end end end diff --git a/app/services/ci/integrate_cluster_service.rb b/app/services/ci/integrate_cluster_service.rb new file mode 100644 index 00000000000..d5b1ccd345d --- /dev/null +++ b/app/services/ci/integrate_cluster_service.rb @@ -0,0 +1,36 @@ +module Ci + class IntegrateClusterService + def execute(cluster, endpoint, ca_cert, token, username, password) + kubernetes_service ||= cluster.project.find_or_initialize_service('kubernetes') + + Ci::Cluster.transaction do + # Update service + kubernetes_service.attributes = { + active: true, + api_url: endpoint, + ca_pem: ca_cert, + namespace: cluster.project_namespace, + token: token + } + + kubernetes_service.save! + + # Save info in cluster record + cluster.update!( + enabled: true, + service: kubernetes_service, + username: username, + password: password, + kubernetes_token: token, + ca_cert: ca_cert, + endpoint: endpoint, + gcp_token: nil, + status: Ci::Cluster.statuses[:created] + ) + end + + rescue ActiveRecord::RecordInvalid => e + cluster.error!("Failed to integrate cluster into kubernetes_service: #{e.message}") + end + end +end diff --git a/app/services/ci/update_cluster_service.rb b/app/services/ci/update_cluster_service.rb new file mode 100644 index 00000000000..0b458e27b90 --- /dev/null +++ b/app/services/ci/update_cluster_service.rb @@ -0,0 +1,26 @@ +module Ci + class UpdateClusterService < BaseService + def execute(cluster) + Ci::Cluster.transaction do + if params['enabled'] == 'true' + + cluster.service.attributes = { + active: true, + api_url: cluster.endpoint, + ca_pem: cluster.ca_cert, + namespace: cluster.project_namespace, + token: cluster.kubernetes_token + } + + cluster.service.save! + else + cluster.service.update(active: false) + end + + cluster.update!(enabled: params['enabled']) + end + rescue ActiveRecord::RecordInvalid => e + cluster.errors.add(:base, e.message) + end + end +end |