Commit e7a8a056 authored by Shinya Maeda's avatar Shinya Maeda

Improve ClustersController

parent 55ac72e5
...@@ -33,22 +33,32 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -33,22 +33,32 @@ class Projects::ClustersController < Projects::ApplicationController
# - If create manually, save in db (Prob, Project > Setting) # - If create manually, save in db (Prob, Project > Setting)
# - Dry up with Service # - Dry up with Service
# - Transaction # - Transaction
# - Sidekiq
def create def create
if params['creation_type'] == 'on_gke' if params['creation_type'] == 'on_gke'
# Create a cluster on GKE # Create a cluster on GKE
results = api_client.projects_zones_clusters_create( operation = api_client.projects_zones_clusters_create(
project_id: params['gcp_project_id'], params['gcp_project_id'], params['cluster_zone'], params['cluster_name'],
zone: params['cluster_zone'], cluster_size: params['cluster_size'], machine_type: params['machine_type']
cluster_name: params['cluster_name'], )
cluster_size: params['cluster_size'],
machine_type: params['machine_type'] # wait_operation_done
if operation&.operation_type == 'CREATE_CLUSTER'
api_client.wait_operation_done(operation.self_link)
else
raise "TODO: ERROR"
end
# Get cluster details (end point, etc)
gke_cluster = api_client.projects_zones_clusters_get(
params['gcp_project_id'], params['cluster_zone'], params['cluster_name']
) )
# Update service # Update service
kubernetes_service.attributes = service_params( kubernetes_service.attributes = service_params(
active: true, active: true,
api_url: results['end_point'], api_url: gke_cluster.endpoint,
ca_pem: results['ca_cert'], # TODO: Decode Base64 ca_pem: Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate),
namespace: params['project_namespace'], namespace: params['project_namespace'],
token: 'aaa' # TODO: username/password token: 'aaa' # TODO: username/password
) )
...@@ -93,13 +103,6 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -93,13 +103,6 @@ class Projects::ClustersController < Projects::ApplicationController
@authorize_url = api_client.authorize_url @authorize_url = api_client.authorize_url
render :edit render :edit
end end
# Get cluster information
api_client.projects_zones_clusters_get(
project_id: cluster.gcp_project_id,
zone: cluster.cluster_zone,
cluster_id: cluster.cluster_name
)
end end
def update def update
......
...@@ -4,6 +4,6 @@ Avaiable GCP project lists ...@@ -4,6 +4,6 @@ Avaiable GCP project lists
%br %br
Avaiable zones Avaiable zones
%br %br
= link_to "Create on Google Container Engine", namespace_project_clusters_path(@project.namespace, @project, creation_type: 'on_gke', cluster_name: 'gke-test-creation', gcp_project_id: 'gitlab-internal-153318', cluster_zone: 'us-central1-a', cluster_size: '1', project_namespace: 'aaa', machine_type: '???'), method: :post = link_to "Create on Google Container Engine", namespace_project_clusters_path(@project.namespace, @project, creation_type: 'on_gke', cluster_name: "gke-test-creation#{Random.rand(100)}", gcp_project_id: 'gitlab-internal-153318', cluster_zone: 'us-central1-a', cluster_size: '1', project_namespace: 'aaa', machine_type: '???'), method: :post
%br %br
= link_to "Use existing kubernets cluster", namespace_project_clusters_path(@project.namespace, @project, creation_type: 'manual', end_point: 'xxx.xxx.xxx.xxx', ca_cert: 'xxx...xxx', token: 'xxx', project_namespace: 'aaa'), method: :post = link_to "Use existing kubernets cluster", namespace_project_clusters_path(@project.namespace, @project, creation_type: 'manual', end_point: 'xxx.xxx.xxx.xxx', ca_cert: 'xxx...xxx', token: 'xxx', project_namespace: 'aaa'), method: :post
...@@ -13,17 +13,19 @@ module GoogleApi ...@@ -13,17 +13,19 @@ module GoogleApi
'https://www.googleapis.com/auth/cloud-platform' 'https://www.googleapis.com/auth/cloud-platform'
end end
def projects_zones_clusters_get(project_id:, zone:, cluster_id:) def projects_zones_clusters_get(project_id, zone, cluster_id)
service = Google::Apis::ContainerV1::ContainerService.new service = Google::Apis::ContainerV1::ContainerService.new
service.authorization = access_token service.authorization = access_token
response = service.get_zone_cluster(project_id, zone, cluster_id) cluster = service.get_zone_cluster(project_id, zone, cluster_id)
response.to_json puts "#{self.class.name} - #{__callee__}: cluster: #{cluster.inspect}"
cluster
end end
# Responce exmaple # Responce exmaple
# {"name":"operation-1506424047439-0293f57c","operationType":"CREATE_CLUSTER","selfLink":"https://container.googleapis.com/v1/projects/696404988091/zones/us-central1-a/operations/operation-1506424047439-0293f57c","startTime":"2017-09-26T11:07:27.439033158Z","status":"RUNNING","targetLink":"https://container.googleapis.com/v1/projects/696404988091/zones/us-central1-a/clusters/gke-test-creation","zone":"us-central1-a"} # {"name":"operation-1506424047439-0293f57c","operationType":"CREATE_CLUSTER","selfLink":"https://container.googleapis.com/v1/projects/696404988091/zones/us-central1-a/operations/operation-1506424047439-0293f57c","startTime":"2017-09-26T11:07:27.439033158Z","status":"RUNNING","targetLink":"https://container.googleapis.com/v1/projects/696404988091/zones/us-central1-a/clusters/gke-test-creation","zone":"us-central1-a"}
def projects_zones_clusters_create(project_id:, zone:, cluster_name:, cluster_size:, machine_type:) # TODO: machine_type : Defailt 3.75 GB
def projects_zones_clusters_create(project_id, zone, cluster_name, cluster_size:, machine_type:)
service = Google::Apis::ContainerV1::ContainerService.new service = Google::Apis::ContainerV1::ContainerService.new
service.authorization = access_token service.authorization = access_token
...@@ -36,14 +38,37 @@ module GoogleApi ...@@ -36,14 +38,37 @@ module GoogleApi
} }
) )
# TODO: machine_type : Defailt 3.75 GB begin
response = service.create_cluster(project_id, zone, request_body) operation = service.create_cluster(project_id, zone, request_body)
puts response.to_json rescue Google::Apis::ClientError, Google::Apis::AuthorizationError => e
response.to_json Rails.logger.error("#{self.class.name}: Could not create cluster #{cluster_name}: #{e}")
end
puts "#{self.class.name} - #{__callee__}: operation: #{operation.inspect}"
operation
end
def projects_zones_operations(project_id, zone, operation_id)
service = Google::Apis::ContainerV1::ContainerService.new
service.authorization = access_token
operation = service.get_zone_operation(project_id, zone, operation_id)
operation
end end
def get_status(project_id:, zone:, cluster_name:, cluster_size:, machine_type:) def wait_operation_done(self_link)
# Observe running = true
ret = self_link.match(/projects\/(.*)\/zones\/(.*)\/operations\/(.*)/)
project_id = ret[1]
zone = ret[2]
operation_id = ret[3]
while running
operation = projects_zones_operations(project_id, zone, operation_id)
if operation.status != 'RUNNING'
running = false
end
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment