diff options
author | Shinya Maeda <shinya@gitlab.com> | 2017-10-03 23:44:06 +0900 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2017-10-03 23:44:06 +0900 |
commit | 6b7889f750c56962c0674467c3fbfd7976b9b44f (patch) | |
tree | 1491f42c332465194630417afc138010e23fc163 /app/services/ci | |
parent | fd6776214abb2603ce2fb5dfb078229f0a0195e5 (diff) | |
download | gitlab-ce-6b7889f750c56962c0674467c3fbfd7976b9b44f.tar.gz |
Implement Policy. Use show instead of edit. Chnage db column. fix comments. dry up workers
Diffstat (limited to 'app/services/ci')
-rw-r--r-- | app/services/ci/create_cluster_service.rb | 8 | ||||
-rw-r--r-- | app/services/ci/create_gke_cluster_service.rb | 37 | ||||
-rw-r--r-- | app/services/ci/fetch_gcp_operation_service.rb | 17 | ||||
-rw-r--r-- | app/services/ci/finalize_cluster_creation_service.rb | 33 |
4 files changed, 91 insertions, 4 deletions
diff --git a/app/services/ci/create_cluster_service.rb b/app/services/ci/create_cluster_service.rb index 48d7c9aef23..2fb6df9d669 100644 --- a/app/services/ci/create_cluster_service.rb +++ b/app/services/ci/create_cluster_service.rb @@ -1,14 +1,14 @@ module Ci class CreateClusterService < BaseService def execute(access_token) - if params['machine_type'].blank? - params['machine_type'] = GoogleApi::CloudPlatform::Client::DEFAULT_MACHINE_TYPE - end + params['gcp_machine_type'] ||= GoogleApi::CloudPlatform::Client::DEFAULT_MACHINE_TYPE project.create_cluster( params.merge(user: current_user, status: Gcp::Cluster.statuses[:scheduled], - gcp_token: access_token)) + gcp_token: access_token)).tap do |cluster| + ClusterCreationWorker.perform_async(cluster.id) if cluster.persisted? + end end end end diff --git a/app/services/ci/create_gke_cluster_service.rb b/app/services/ci/create_gke_cluster_service.rb new file mode 100644 index 00000000000..2407de5fc75 --- /dev/null +++ b/app/services/ci/create_gke_cluster_service.rb @@ -0,0 +1,37 @@ +module Ci + class CreateGkeClusterService + def execute(cluster) + api_client = + GoogleApi::CloudPlatform::Client.new(cluster.gcp_token, nil) + + begin + operation = api_client.projects_zones_clusters_create( + cluster.gcp_project_id, + cluster.gcp_cluster_zone, + cluster.gcp_cluster_name, + cluster.gcp_cluster_size, + machine_type: cluster.gcp_machine_type + ) + rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e + return cluster.errored!("Failed to request to CloudPlatform; #{e.message}") + end + + unless operation.status == 'RUNNING' || operation.status == 'PENDING' + return cluster.errored!("Operation status is unexpected; #{operation.status_message}") + end + + operation_id = api_client.parse_operation_id(operation.self_link) + + unless operation_id + return cluster.errored!('Can not find operation_id from self_link') + end + + if cluster.creating!(operation_id) + WaitForClusterCreationWorker.perform_in( + WaitForClusterCreationWorker::INITIAL_INTERVAL, cluster.id) + else + return cluster.errored!("Failed to update cluster record; #{cluster.errors}") + end + end + end +end diff --git a/app/services/ci/fetch_gcp_operation_service.rb b/app/services/ci/fetch_gcp_operation_service.rb new file mode 100644 index 00000000000..24892d1ab47 --- /dev/null +++ b/app/services/ci/fetch_gcp_operation_service.rb @@ -0,0 +1,17 @@ +module Ci + class FetchGcpOperationService + def execute(cluster) + api_client = + GoogleApi::CloudPlatform::Client.new(cluster.gcp_token, nil) + + operation = api_client.projects_zones_operations( + cluster.gcp_project_id, + cluster.gcp_cluster_zone, + cluster.gcp_operation_id) + + yield(operation) if block_given? + rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e + return cluster.errored!("Failed to request to CloudPlatform; #{e.message}") + end + end +end diff --git a/app/services/ci/finalize_cluster_creation_service.rb b/app/services/ci/finalize_cluster_creation_service.rb new file mode 100644 index 00000000000..03cd761deb7 --- /dev/null +++ b/app/services/ci/finalize_cluster_creation_service.rb @@ -0,0 +1,33 @@ +module Ci + class FinalizeClusterCreationService + def execute(cluster) + api_client = + GoogleApi::CloudPlatform::Client.new(cluster.gcp_token, nil) + + begin + gke_cluster = api_client.projects_zones_clusters_get( + cluster.gcp_project_id, + cluster.gcp_cluster_zone, + cluster.gcp_cluster_name) + rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e + return cluster.errored!("Failed to request to CloudPlatform; #{e.message}") + end + + endpoint = gke_cluster.endpoint + api_url = 'https://' + endpoint + ca_cert = Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate) + username = gke_cluster.master_auth.username + password = gke_cluster.master_auth.password + + kubernetes_token = Ci::FetchKubernetesTokenService.new( + api_url, ca_cert, username, password).execute + + unless kubernetes_token + return cluster.errored!('Failed to get a default token of kubernetes') + end + + Ci::IntegrateClusterService.new.execute( + cluster, endpoint, ca_cert, kubernetes_token, username, password) + end + end +end |