2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module Clusters
|
|
|
|
module Gcp
|
|
|
|
class ProvisionService
|
2019-12-21 20:55:43 +05:30
|
|
|
CLOUD_RUN_ADDONS = %i[http_load_balancing istio_config cloud_run_config].freeze
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
attr_reader :provider
|
|
|
|
|
|
|
|
def execute(provider)
|
|
|
|
@provider = provider
|
|
|
|
|
|
|
|
get_operation_id do |operation_id|
|
|
|
|
if provider.make_creating(operation_id)
|
|
|
|
WaitForClusterCreationWorker.perform_in(
|
|
|
|
Clusters::Gcp::VerifyProvisionStatusService::INITIAL_INTERVAL,
|
|
|
|
provider.cluster_id)
|
|
|
|
else
|
|
|
|
provider.make_errored!("Failed to update provider record; #{provider.errors}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_operation_id
|
2019-12-21 20:55:43 +05:30
|
|
|
enable_addons = provider.cloud_run? ? CLOUD_RUN_ADDONS : []
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
operation = provider.api_client.projects_zones_clusters_create(
|
|
|
|
provider.gcp_project_id,
|
|
|
|
provider.zone,
|
|
|
|
provider.cluster.name,
|
|
|
|
provider.num_nodes,
|
2018-12-05 23:21:45 +05:30
|
|
|
machine_type: provider.machine_type,
|
2019-12-21 20:55:43 +05:30
|
|
|
legacy_abac: provider.legacy_abac,
|
|
|
|
enable_addons: enable_addons
|
2018-12-05 23:21:45 +05:30
|
|
|
)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
unless operation.status == 'PENDING' || operation.status == 'RUNNING'
|
|
|
|
return provider.make_errored!("Operation status is unexpected; #{operation.status_message}")
|
|
|
|
end
|
|
|
|
|
|
|
|
operation_id = provider.api_client.parse_operation_id(operation.self_link)
|
|
|
|
|
|
|
|
unless operation_id
|
|
|
|
return provider.make_errored!('Can not find operation_id from self_link')
|
|
|
|
end
|
|
|
|
|
|
|
|
yield(operation_id)
|
|
|
|
|
|
|
|
rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e
|
|
|
|
provider.make_errored!("Failed to request to CloudPlatform; #{e.message}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|