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 FinalizeCreationService
|
|
|
|
attr_reader :provider
|
|
|
|
|
|
|
|
def execute(provider)
|
|
|
|
@provider = provider
|
|
|
|
|
|
|
|
configure_provider
|
2018-12-05 23:21:45 +05:30
|
|
|
create_gitlab_service_account!
|
2018-03-17 18:26:18 +05:30
|
|
|
configure_kubernetes
|
|
|
|
|
|
|
|
cluster.save!
|
|
|
|
rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e
|
|
|
|
provider.make_errored!("Failed to request to CloudPlatform; #{e.message}")
|
2018-12-05 23:21:45 +05:30
|
|
|
rescue Kubeclient::HttpError => e
|
|
|
|
provider.make_errored!("Failed to run Kubeclient: #{e.message}")
|
2018-03-17 18:26:18 +05:30
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
2018-10-15 14:42:47 +05:30
|
|
|
provider.make_errored!("Failed to configure Google Kubernetes Engine Cluster: #{e.message}")
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def create_gitlab_service_account!
|
|
|
|
Clusters::Gcp::Kubernetes::CreateServiceAccountService.new(kube_client, rbac: create_rbac_cluster?).execute
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def configure_provider
|
|
|
|
provider.endpoint = gke_cluster.endpoint
|
|
|
|
provider.status_event = :make_created
|
|
|
|
end
|
|
|
|
|
|
|
|
def configure_kubernetes
|
|
|
|
cluster.platform_type = :kubernetes
|
|
|
|
cluster.build_platform_kubernetes(
|
|
|
|
api_url: 'https://' + gke_cluster.endpoint,
|
|
|
|
ca_cert: Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate),
|
|
|
|
username: gke_cluster.master_auth.username,
|
|
|
|
password: gke_cluster.master_auth.password,
|
2018-12-05 23:21:45 +05:30
|
|
|
authorization_type: authorization_type,
|
2018-03-17 18:26:18 +05:30
|
|
|
token: request_kubernetes_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_kubernetes_token
|
2018-12-05 23:21:45 +05:30
|
|
|
Clusters::Gcp::Kubernetes::FetchKubernetesTokenService.new(kube_client).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorization_type
|
|
|
|
create_rbac_cluster? ? 'rbac' : 'abac'
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_rbac_cluster?
|
|
|
|
!provider.legacy_abac?
|
|
|
|
end
|
|
|
|
|
|
|
|
def kube_client
|
|
|
|
@kube_client ||= build_kube_client!(
|
2018-03-17 18:26:18 +05:30
|
|
|
'https://' + gke_cluster.endpoint,
|
|
|
|
Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate),
|
|
|
|
gke_cluster.master_auth.username,
|
2018-12-05 23:21:45 +05:30
|
|
|
gke_cluster.master_auth.password,
|
|
|
|
api_groups: ['api', 'apis/rbac.authorization.k8s.io']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_kube_client!(api_url, ca_pem, username, password, api_groups: ['api'], api_version: 'v1')
|
|
|
|
raise "Incomplete settings" unless api_url && username && password
|
|
|
|
|
|
|
|
Gitlab::Kubernetes::KubeClient.new(
|
|
|
|
api_url,
|
|
|
|
api_groups,
|
|
|
|
api_version,
|
|
|
|
auth_options: { username: username, password: password },
|
|
|
|
ssl_options: kubeclient_ssl_options(ca_pem),
|
|
|
|
http_proxy_uri: ENV['http_proxy']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def kubeclient_ssl_options(ca_pem)
|
|
|
|
opts = { verify_ssl: OpenSSL::SSL::VERIFY_PEER }
|
|
|
|
|
|
|
|
if ca_pem.present?
|
|
|
|
opts[:cert_store] = OpenSSL::X509::Store.new
|
|
|
|
opts[:cert_store].add_cert(OpenSSL::X509::Certificate.new(ca_pem))
|
|
|
|
end
|
|
|
|
|
|
|
|
opts
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def gke_cluster
|
|
|
|
@gke_cluster ||= provider.api_client.projects_zones_clusters_get(
|
|
|
|
provider.gcp_project_id,
|
|
|
|
provider.zone,
|
|
|
|
cluster.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cluster
|
|
|
|
@cluster ||= provider.cluster
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|