2017-10-13 13:21:23 -04:00
|
|
|
module Clusters
|
|
|
|
module Platforms
|
|
|
|
class Kubernetes < ActiveRecord::Base
|
2017-11-21 07:09:55 -05:00
|
|
|
include Gitlab::Kubernetes
|
|
|
|
include ReactiveCaching
|
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
self.table_name = 'cluster_platforms_kubernetes'
|
2017-11-27 09:55:25 -05:00
|
|
|
self.reactive_cache_key = ->(kubernetes) { [kubernetes.class.model_name.singular, kubernetes.id] }
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
belongs_to :cluster, inverse_of: :platform_kubernetes, class_name: 'Clusters::Cluster'
|
2017-10-13 13:21:23 -04:00
|
|
|
|
|
|
|
attr_encrypted :password,
|
|
|
|
mode: :per_attribute_iv,
|
2018-06-02 05:32:30 -04:00
|
|
|
key: Settings.attr_encrypted_db_key_base_truncated,
|
2017-10-13 13:21:23 -04:00
|
|
|
algorithm: 'aes-256-cbc'
|
|
|
|
|
|
|
|
attr_encrypted :token,
|
|
|
|
mode: :per_attribute_iv,
|
2018-06-02 05:32:30 -04:00
|
|
|
key: Settings.attr_encrypted_db_key_base_truncated,
|
2017-10-13 13:21:23 -04:00
|
|
|
algorithm: 'aes-256-cbc'
|
|
|
|
|
2017-10-29 14:48:45 -04:00
|
|
|
before_validation :enforce_namespace_to_lower_case
|
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
validates :namespace,
|
|
|
|
allow_blank: true,
|
|
|
|
length: 1..63,
|
|
|
|
format: {
|
|
|
|
with: Gitlab::Regex.kubernetes_namespace_regex,
|
|
|
|
message: Gitlab::Regex.kubernetes_namespace_regex_message
|
|
|
|
}
|
|
|
|
|
2017-11-01 07:57:05 -04:00
|
|
|
# We expect to be `active?` only when enabled and cluster is created (the api_url is assigned)
|
2017-11-02 10:19:11 -04:00
|
|
|
validates :api_url, url: true, presence: true
|
|
|
|
validates :token, presence: true
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-12-05 07:00:02 -05:00
|
|
|
validate :prevent_modification, on: :update
|
|
|
|
|
2017-11-21 07:09:55 -05:00
|
|
|
after_save :clear_reactive_cache!
|
|
|
|
|
2017-10-29 14:48:45 -04:00
|
|
|
alias_attribute :ca_pem, :ca_cert
|
|
|
|
|
|
|
|
delegate :project, to: :cluster, allow_nil: true
|
|
|
|
delegate :enabled?, to: :cluster, allow_nil: true
|
2017-12-05 07:00:02 -05:00
|
|
|
delegate :managed?, to: :cluster, allow_nil: true
|
2017-10-29 14:48:45 -04:00
|
|
|
|
2017-11-27 09:55:25 -05:00
|
|
|
alias_method :active?, :enabled?
|
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
def actual_namespace
|
|
|
|
if namespace.present?
|
|
|
|
namespace
|
|
|
|
else
|
|
|
|
default_namespace
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-21 07:09:55 -05:00
|
|
|
def predefined_variables
|
|
|
|
config = YAML.dump(kubeconfig)
|
|
|
|
|
2018-03-14 06:15:18 -04:00
|
|
|
Gitlab::Ci::Variables::Collection.new.tap do |variables|
|
|
|
|
variables
|
|
|
|
.append(key: 'KUBE_URL', value: api_url)
|
|
|
|
.append(key: 'KUBE_TOKEN', value: token, public: false)
|
|
|
|
.append(key: 'KUBE_NAMESPACE', value: actual_namespace)
|
|
|
|
.append(key: 'KUBECONFIG', value: config, public: false, file: true)
|
|
|
|
|
|
|
|
if ca_pem.present?
|
|
|
|
variables
|
|
|
|
.append(key: 'KUBE_CA_PEM', value: ca_pem)
|
|
|
|
.append(key: 'KUBE_CA_PEM_FILE', value: ca_pem, file: true)
|
|
|
|
end
|
2017-11-21 07:09:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Constructs a list of terminals from the reactive cache
|
|
|
|
#
|
|
|
|
# Returns nil if the cache is empty, in which case you should try again a
|
|
|
|
# short time later
|
|
|
|
def terminals(environment)
|
|
|
|
with_reactive_cache do |data|
|
|
|
|
pods = filter_by_label(data[:pods], app: environment.slug)
|
|
|
|
terminals = pods.flat_map { |pod| terminals_for_pod(api_url, actual_namespace, pod) }
|
|
|
|
terminals.each { |terminal| add_terminal_auth(terminal, terminal_auth) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Caches resources in the namespace so other calls don't need to block on
|
|
|
|
# network access
|
|
|
|
def calculate_reactive_cache
|
2017-11-21 09:29:33 -05:00
|
|
|
return unless enabled? && project && !project.pending_delete?
|
2017-11-21 07:09:55 -05:00
|
|
|
|
|
|
|
# We may want to cache extra things in the future
|
|
|
|
{ pods: read_pods }
|
|
|
|
end
|
|
|
|
|
2017-11-02 10:10:46 -04:00
|
|
|
def kubeclient
|
2017-11-21 09:29:33 -05:00
|
|
|
@kubeclient ||= build_kubeclient!
|
2017-11-06 09:50:55 -05:00
|
|
|
end
|
|
|
|
|
2017-11-01 07:57:05 -04:00
|
|
|
private
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-11-21 08:03:07 -05:00
|
|
|
def kubeconfig
|
|
|
|
to_kubeconfig(
|
|
|
|
url: api_url,
|
|
|
|
namespace: actual_namespace,
|
|
|
|
token: token,
|
|
|
|
ca_pem: ca_pem)
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
|
|
|
|
2017-11-21 08:03:07 -05:00
|
|
|
def default_namespace
|
|
|
|
return unless project
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-11-21 08:03:07 -05:00
|
|
|
slug = "#{project.path}-#{project.id}".downcase
|
|
|
|
slug.gsub(/[^-a-z0-9]/, '-').gsub(/^-+/, '')
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
2017-11-21 07:09:55 -05:00
|
|
|
|
|
|
|
def build_kubeclient!(api_path: 'api', api_version: 'v1')
|
|
|
|
raise "Incomplete settings" unless api_url && actual_namespace
|
|
|
|
|
|
|
|
unless (username && password) || token
|
|
|
|
raise "Either username/password or token is required to access API"
|
|
|
|
end
|
|
|
|
|
|
|
|
::Kubeclient::Client.new(
|
|
|
|
join_api_url(api_path),
|
|
|
|
api_version,
|
|
|
|
auth_options: kubeclient_auth_options,
|
|
|
|
ssl_options: kubeclient_ssl_options,
|
|
|
|
http_proxy_uri: ENV['http_proxy']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-11-21 08:03:07 -05:00
|
|
|
# Returns a hash of all pods in the namespace
|
|
|
|
def read_pods
|
|
|
|
kubeclient = build_kubeclient!
|
|
|
|
|
|
|
|
kubeclient.get_pods(namespace: actual_namespace).as_json
|
2018-03-15 03:14:13 -04:00
|
|
|
rescue Kubeclient::HttpError => err
|
2017-11-21 08:03:07 -05:00
|
|
|
raise err unless err.error_code == 404
|
2017-11-27 11:26:14 -05:00
|
|
|
|
2017-11-21 08:03:07 -05:00
|
|
|
[]
|
|
|
|
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
|
|
|
|
|
2017-11-21 07:09:55 -05:00
|
|
|
def kubeclient_auth_options
|
2017-11-21 08:03:07 -05:00
|
|
|
{ bearer_token: token }
|
2017-11-21 07:09:55 -05:00
|
|
|
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 terminal_auth
|
|
|
|
{
|
|
|
|
token: token,
|
|
|
|
ca_pem: ca_pem,
|
2018-02-02 13:39:55 -05:00
|
|
|
max_session_time: Gitlab::CurrentSettings.terminal_max_session_time
|
2017-11-21 07:09:55 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def enforce_namespace_to_lower_case
|
|
|
|
self.namespace = self.namespace&.downcase
|
|
|
|
end
|
2017-12-05 07:00:02 -05:00
|
|
|
|
|
|
|
def prevent_modification
|
|
|
|
return unless managed?
|
|
|
|
|
|
|
|
if api_url_changed? || token_changed? || ca_pem_changed?
|
2018-01-29 14:49:00 -05:00
|
|
|
errors.add(:base, _('Cannot modify managed Kubernetes cluster'))
|
2017-12-05 07:00:02 -05:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|