gitlab-org--gitlab-foss/app/controllers/projects/clusters_controller.rb

167 lines
4.6 KiB
Ruby
Raw Normal View History

class Projects::ClustersController < Projects::ApplicationController
# before_action :authenticate_google_api
before_action :cluster
# before_action :authorize_admin_clusters! # TODO: Authentication
def index
if cluster
redirect_to action: 'edit'
else
redirect_to action: 'new'
end
end
##
# TODO:
# - Show form for "Create on Google Container Engine"
# - Show form for "Use existing kubernets cluster"
# - If user has not authroized yet, Show "Sign in with Google" button
# - If user has already authroized, Skip "Sign in with Google" button
# - user.is_authenticated_for_gcp?
# - user.authenticate_for_gcp!
# - Create this module which can be used from view
def new
2017-09-25 17:11:26 +00:00
unless session[GoogleApi::CloudPlatform::Client.token_in_session]
@authorize_url = api_client.authorize_url
end
end
##
# TODO:
# - If create on GKE, Use Google::Apis::ContainerV1::ContainerService
# - If create manually, save in db (Prob, Project > Setting)
# - Dry up with Service
2017-09-26 08:46:09 +00:00
# - Transaction
def create
2017-09-25 17:11:26 +00:00
if params['creation_type'] == 'on_gke'
2017-09-26 08:46:09 +00:00
# Create a cluster on GKE
2017-09-25 17:11:26 +00:00
results = api_client.projects_zones_clusters_create(
2017-09-26 11:34:49 +00:00
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']
2017-09-25 17:11:26 +00:00
)
2017-09-26 08:46:09 +00:00
# Update service
kubernetes_service.attributes = service_params(
active: true,
api_url: results['end_point'],
ca_pem: results['ca_cert'], # TODO: Decode Base64
namespace: params['project_namespace'],
token: 'aaa' # TODO: username/password
)
2017-09-25 17:11:26 +00:00
2017-09-26 08:46:09 +00:00
kubernetes_service.save!
# Save info
2017-09-25 17:11:26 +00:00
project.clusters.create(
creation_type: params['creation_type'],
gcp_project_id: params['gcp_project_id'],
cluster_zone: params['cluster_zone'],
cluster_name: params['cluster_name'],
2017-09-26 08:46:09 +00:00
service: kubernetes_service
2017-09-25 17:11:26 +00:00
)
elsif params['creation_type'] == 'manual'
# TODO: Transaction
project.kubernetes_service.save(
end_point: params['end_point'],
ca_cert: params['ca_cert'],
token: params['token'],
username: params['username'],
password: params['password'],
project_namespace: params['project_namespace']
)
project.clusters.create(
creation_type: params['creation_type'],
kubernetes_service: project.kubernetes_service
)
end
redirect_to action: 'index'
end
# TODO: Show results/status. Edits Swtich for enable/disable.
# If created with GKE, non-editable form. enable/disable switch.
# If created manually, editable form. enable/disable switch.
# GKE params are on-off swtich
# Manul params are on-off swtich, Endpoint, CACert, k8s Token, Proj namespace.
def edit
2017-09-25 17:11:26 +00:00
unless session[GoogleApi::CloudPlatform::Client.token_in_session]
@authorize_url = api_client.authorize_url
2017-09-26 11:34:49 +00:00
render :edit
end
2017-09-26 11:34:49 +00:00
# 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
cluster.update(schedule_params)
render :edit
end
# In presenter
# TODO: Generate a link to the cluster on GKE
def gcp_projects
# api_client.blah
# TODO: Return all avaiable GCP Projects.
# TODO: Return json
# TODO: Dry with concern
end
def gke_zones
# api_client.blah
# TODO: Return all avaiable zones on GKE.
# TODO: Return json
# TODO: Dry with concern
end
private
# def authenticate_google_api
# if cluster&.on_gke? && session[access_token_key].blank?
# redirect_to api_client.authorize_url(callback_import_url)
# end
# end
def cluster
# Each project has only one cluster, for now. In the future iteraiton, we'll support multiple clusters
2017-09-26 08:46:09 +00:00
@cluster ||= project.clusters.last
end
2017-09-25 17:11:26 +00:00
# def cluster_params
# params.require(:cluster).permit(:aaa)
# end
def api_client
@api_client ||=
GoogleApi::CloudPlatform::Client.new(
2017-09-25 17:11:26 +00:00
session[GoogleApi::CloudPlatform::Client.token_in_session],
callback_google_api_authorizations_url,
state: namespace_project_clusters_url.to_s
)
end
2017-09-26 08:46:09 +00:00
def kubernetes_service
@kubernetes_service ||= project.find_or_initialize_service('kubernetes')
end
def service_params(active:, api_url:, ca_pem:, namespace:, token:)
{
active: active,
api_url: api_url,
ca_pem: ca_pem,
namespace: namespace,
token: token
}
end
end