2017-09-30 15:54:22 +00:00
|
|
|
class WaitForClusterCreationWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
include DedicatedSidekiqQueue
|
|
|
|
|
|
|
|
INITIAL_INTERVAL = 2.minutes
|
|
|
|
EAGER_INTERVAL = 10.seconds
|
|
|
|
TIMEOUT = 20.minutes
|
|
|
|
|
|
|
|
def perform(cluster_id)
|
2017-10-02 12:58:50 +00:00
|
|
|
cluster = Gcp::Cluster.find_by_id(cluster_id)
|
2017-09-30 15:54:22 +00:00
|
|
|
|
|
|
|
unless cluster
|
|
|
|
return Rails.logger.error "Cluster object is not found; #{cluster_id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
api_client =
|
|
|
|
GoogleApi::CloudPlatform::Client.new(cluster.gcp_token, nil)
|
|
|
|
|
|
|
|
operation = api_client.projects_zones_operations(
|
2017-10-01 08:48:21 +00:00
|
|
|
cluster.gcp_project_id,
|
|
|
|
cluster.cluster_zone,
|
|
|
|
cluster.gcp_operation_id)
|
2017-09-30 15:54:22 +00:00
|
|
|
|
|
|
|
if operation.is_a?(StandardError)
|
2017-10-01 11:30:32 +00:00
|
|
|
return cluster.errored!("Failed to request to CloudPlatform; #{operation.message}")
|
2017-09-30 15:54:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
case operation.status
|
|
|
|
when 'RUNNING'
|
|
|
|
if Time.now < operation.start_time.to_time + TIMEOUT
|
|
|
|
WaitForClusterCreationWorker.perform_in(EAGER_INTERVAL, cluster.id)
|
|
|
|
else
|
2017-10-01 11:30:32 +00:00
|
|
|
return cluster.errored!("Cluster creation time exceeds timeout; #{TIMEOUT}")
|
2017-09-30 15:54:22 +00:00
|
|
|
end
|
2017-10-01 08:48:21 +00:00
|
|
|
when 'DONE'
|
|
|
|
integrate(cluster, api_client)
|
2017-09-30 15:54:22 +00:00
|
|
|
else
|
2017-10-01 11:30:32 +00:00
|
|
|
return cluster.errored!("Unexpected operation status; #{operation.status} #{operation.status_message}")
|
2017-09-30 15:54:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-01 08:48:21 +00:00
|
|
|
def integrate(cluster, api_client)
|
2017-09-30 15:54:22 +00:00
|
|
|
gke_cluster = api_client.projects_zones_clusters_get(
|
2017-10-01 08:48:21 +00:00
|
|
|
cluster.gcp_project_id,
|
|
|
|
cluster.cluster_zone,
|
|
|
|
cluster.cluster_name)
|
2017-09-30 15:54:22 +00:00
|
|
|
|
|
|
|
if gke_cluster.is_a?(StandardError)
|
2017-10-01 11:30:32 +00:00
|
|
|
return cluster.errored!("Failed to request to CloudPlatform; #{gke_cluster.message}")
|
2017-09-30 15:54:22 +00:00
|
|
|
end
|
|
|
|
|
2017-10-01 08:48:21 +00:00
|
|
|
begin
|
|
|
|
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
|
|
|
|
rescue Exception => e
|
2017-10-02 12:58:50 +00:00
|
|
|
return cluster.errored!("Can not extract the expected data; #{e}")
|
2017-09-30 15:54:22 +00:00
|
|
|
end
|
|
|
|
|
2017-10-01 08:48:21 +00:00
|
|
|
kubernetes_token = Ci::FetchKubernetesTokenService.new(
|
|
|
|
api_url, ca_cert, username, password).execute
|
|
|
|
|
2017-09-30 15:54:22 +00:00
|
|
|
unless kubernetes_token
|
2017-10-01 11:30:32 +00:00
|
|
|
return cluster.errored!('Failed to get a default token of kubernetes')
|
2017-09-30 15:54:22 +00:00
|
|
|
end
|
|
|
|
|
2017-10-01 08:48:21 +00:00
|
|
|
Ci::IntegrateClusterService.new.execute(
|
|
|
|
cluster, endpoint, ca_cert, kubernetes_token, username, password)
|
2017-09-30 15:54:22 +00:00
|
|
|
end
|
|
|
|
end
|