Improve ClustersController
This commit is contained in:
parent
55ac72e56e
commit
e7a8a05659
|
@ -33,22 +33,32 @@ class Projects::ClustersController < Projects::ApplicationController
|
|||
# - If create manually, save in db (Prob, Project > Setting)
|
||||
# - Dry up with Service
|
||||
# - Transaction
|
||||
# - Sidekiq
|
||||
def create
|
||||
if params['creation_type'] == 'on_gke'
|
||||
# Create a cluster on GKE
|
||||
results = api_client.projects_zones_clusters_create(
|
||||
project_id: params['gcp_project_id'],
|
||||
zone: params['cluster_zone'],
|
||||
cluster_name: params['cluster_name'],
|
||||
cluster_size: params['cluster_size'],
|
||||
machine_type: params['machine_type']
|
||||
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']
|
||||
)
|
||||
|
||||
# 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
|
||||
kubernetes_service.attributes = service_params(
|
||||
active: true,
|
||||
api_url: results['end_point'],
|
||||
ca_pem: results['ca_cert'], # TODO: Decode Base64
|
||||
api_url: gke_cluster.endpoint,
|
||||
ca_pem: Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate),
|
||||
namespace: params['project_namespace'],
|
||||
token: 'aaa' # TODO: username/password
|
||||
)
|
||||
|
@ -93,13 +103,6 @@ class Projects::ClustersController < Projects::ApplicationController
|
|||
@authorize_url = api_client.authorize_url
|
||||
render :edit
|
||||
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
|
||||
|
||||
def update
|
||||
|
|
|
@ -4,6 +4,6 @@ Avaiable GCP project lists
|
|||
%br
|
||||
Avaiable zones
|
||||
%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
|
||||
= 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
|
|||
'https://www.googleapis.com/auth/cloud-platform'
|
||||
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.authorization = access_token
|
||||
|
||||
response = service.get_zone_cluster(project_id, zone, cluster_id)
|
||||
response.to_json
|
||||
cluster = service.get_zone_cluster(project_id, zone, cluster_id)
|
||||
puts "#{self.class.name} - #{__callee__}: cluster: #{cluster.inspect}"
|
||||
cluster
|
||||
end
|
||||
|
||||
# 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"}
|
||||
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.authorization = access_token
|
||||
|
||||
|
@ -36,14 +38,37 @@ module GoogleApi
|
|||
}
|
||||
)
|
||||
|
||||
# TODO: machine_type : Defailt 3.75 GB
|
||||
response = service.create_cluster(project_id, zone, request_body)
|
||||
puts response.to_json
|
||||
response.to_json
|
||||
begin
|
||||
operation = service.create_cluster(project_id, zone, request_body)
|
||||
rescue Google::Apis::ClientError, Google::Apis::AuthorizationError => e
|
||||
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 get_status(project_id:, zone:, cluster_name:, cluster_size:, machine_type:)
|
||||
# Observe
|
||||
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
|
||||
|
||||
def wait_operation_done(self_link)
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue