diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2017-11-01 13:56:27 +0100 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2017-11-01 13:58:08 +0100 |
commit | 1427bdcadf5f4026d141a5c4e93db8b1b00fe40a (patch) | |
tree | 2a7fe415bce79a87bccc154a68b30ea7d45add48 /app/services | |
parent | ccf09824f6d3ef41db4be3b40aa99b6dfd0dc9ac (diff) | |
download | gitlab-ce-1427bdcadf5f4026d141a5c4e93db8b1b00fe40a.tar.gz |
Revert back FetchKubernetesTokenService
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/ci/fetch_kubernetes_token_service.rb | 72 | ||||
-rw-r--r-- | app/services/clusters/gcp/finalize_creation_service.rb | 35 |
2 files changed, 85 insertions, 22 deletions
diff --git a/app/services/ci/fetch_kubernetes_token_service.rb b/app/services/ci/fetch_kubernetes_token_service.rb new file mode 100644 index 00000000000..44da87cb00c --- /dev/null +++ b/app/services/ci/fetch_kubernetes_token_service.rb @@ -0,0 +1,72 @@ +## +# TODO: +# Almost components in this class were copied from app/models/project_services/kubernetes_service.rb +# We should dry up those classes not to repeat the same code. +# Maybe we should have a special facility (e.g. lib/kubernetes_api) to maintain all Kubernetes API caller. +module Ci + class FetchKubernetesTokenService + attr_reader :api_url, :ca_pem, :username, :password + + def initialize(api_url, ca_pem, username, password) + @api_url = api_url + @ca_pem = ca_pem + @username = username + @password = password + end + + def execute + read_secrets.each do |secret| + name = secret.dig('metadata', 'name') + if /default-token/ =~ name + token_base64 = secret.dig('data', 'token') + return Base64.decode64(token_base64) if token_base64 + end + end + + nil + end + + private + + def read_secrets + kubeclient = build_kubeclient! + + kubeclient.get_secrets.as_json + rescue KubeException => err + raise err unless err.error_code == 404 + [] + end + + def build_kubeclient!(api_path: 'api', api_version: 'v1') + raise "Incomplete settings" unless api_url && username && password + + ::Kubeclient::Client.new( + join_api_url(api_path), + api_version, + auth_options: { username: username, password: password }, + ssl_options: kubeclient_ssl_options, + http_proxy_uri: ENV['http_proxy'] + ) + end + + def join_api_url(api_path) + url = URI.parse(api_url) + prefix = url.path.sub(%r{/+\z}, '') + + url.path = [prefix, api_path].join("/") + + url.to_s + end + + def kubeclient_ssl_options + 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 + end + end +end diff --git a/app/services/clusters/gcp/finalize_creation_service.rb b/app/services/clusters/gcp/finalize_creation_service.rb index d379870924a..53b13518771 100644 --- a/app/services/clusters/gcp/finalize_creation_service.rb +++ b/app/services/clusters/gcp/finalize_creation_service.rb @@ -9,10 +9,7 @@ module Clusters configure_provider configure_kubernetes - ActiveRecord::Base.transaction do - kubernetes.save! - provider.make_created! - end + provider.make_created! rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e provider.make_errored!("Failed to request to CloudPlatform; #{e.message}") rescue KubeException => e @@ -28,23 +25,21 @@ module Clusters end def configure_kubernetes - kubernetes.api_url = 'https://' + gke_cluster.endpoint - kubernetes.ca_cert = Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate) - kubernetes.username = gke_cluster.master_auth.username - kubernetes.password = gke_cluster.master_auth.password - kubernetes.token = request_kuberenetes_token + 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, + token: request_kuberenetes_token) end def request_kuberenetes_token - kubernetes.read_secrets.each do |secret| - name = secret.dig('metadata', 'name') - if /default-token/ =~ name - token_base64 = secret.dig('data', 'token') - return Base64.decode64(token_base64) if token_base64 - end - end - - nil + Ci::FetchKubernetesTokenService.new( + 'https://' + gke_cluster.endpoint, + Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate), + gke_cluster.master_auth.username, + gke_cluster.master_auth.password) end def gke_cluster @@ -57,10 +52,6 @@ module Clusters def cluster @cluster ||= provider.cluster end - - def kubernetes - @kubernetes ||= cluster.platform_kubernetes - end end end end |