2018-08-03 03:15:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
2018-09-06 06:03:38 -04:00
|
|
|
include EnumWithNil
|
2018-11-02 11:46:15 -04:00
|
|
|
include AfterCommitQueue
|
2017-11-21 07:09:55 -05:00
|
|
|
|
2018-10-16 16:03:59 -04:00
|
|
|
RESERVED_NAMESPACES = %w(gitlab-managed-apps).freeze
|
|
|
|
|
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
|
2018-10-24 18:13:33 -04:00
|
|
|
before_validation :enforce_ca_whitespace_trimming
|
2017-10-29 14:48:45 -04:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:03:59 -04:00
|
|
|
validates :namespace, exclusion: { in: RESERVED_NAMESPACES }
|
|
|
|
|
2018-11-06 18:30:36 -05:00
|
|
|
validate :no_namespace, unless: :allow_user_defined_namespace?
|
2018-11-06 03:55:10 -05:00
|
|
|
|
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!
|
2018-11-02 11:46:15 -04:00
|
|
|
after_update :update_kubernetes_namespace
|
2017-11-21 07:09:55 -05:00
|
|
|
|
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
|
2018-11-06 18:30:36 -05:00
|
|
|
delegate :allow_user_defined_namespace?, to: :cluster, allow_nil: true
|
2018-10-16 16:03:59 -04:00
|
|
|
delegate :kubernetes_namespace, to: :cluster
|
2017-10-29 14:48:45 -04:00
|
|
|
|
2017-11-27 09:55:25 -05:00
|
|
|
alias_method :active?, :enabled?
|
|
|
|
|
2018-09-06 06:03:38 -04:00
|
|
|
enum_with_nil authorization_type: {
|
|
|
|
unknown_authorization: nil,
|
|
|
|
rbac: 1,
|
|
|
|
abac: 2
|
|
|
|
}
|
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
def actual_namespace
|
|
|
|
if namespace.present?
|
|
|
|
namespace
|
|
|
|
else
|
|
|
|
default_namespace
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-02 11:46:15 -04:00
|
|
|
def predefined_variables(project:)
|
2018-03-14 06:15:18 -04:00
|
|
|
Gitlab::Ci::Variables::Collection.new.tap do |variables|
|
2018-11-02 11:46:15 -04:00
|
|
|
variables.append(key: 'KUBE_URL', value: api_url)
|
2018-03-14 06:15:18 -04:00
|
|
|
|
|
|
|
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
|
2018-11-02 11:46:15 -04:00
|
|
|
|
|
|
|
if kubernetes_namespace = cluster.kubernetes_namespaces.find_by(project: project)
|
|
|
|
variables.concat(kubernetes_namespace.predefined_variables)
|
|
|
|
else
|
|
|
|
# From 11.5, every Clusters::Project should have at least one
|
|
|
|
# Clusters::KubernetesNamespace, so once migration has been completed,
|
|
|
|
# this 'else' branch will be removed. For more information, please see
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/22433
|
|
|
|
config = YAML.dump(kubeconfig)
|
|
|
|
|
|
|
|
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)
|
|
|
|
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
|
2018-10-03 00:04:15 -04:00
|
|
|
@kubeclient ||= build_kube_client!
|
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
|
2018-10-16 16:03:59 -04:00
|
|
|
kubernetes_namespace&.namespace.presence || fallback_default_namespace
|
|
|
|
end
|
|
|
|
|
|
|
|
# DEPRECATED
|
|
|
|
#
|
|
|
|
# On 11.4 Clusters::KubernetesNamespace was introduced, this model will allow to
|
|
|
|
# have multiple namespaces per project. This method will be removed after migration
|
|
|
|
# has been completed.
|
|
|
|
def fallback_default_namespace
|
2017-11-21 08:03:07 -05:00
|
|
|
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
|
2018-10-16 16:03:59 -04:00
|
|
|
Gitlab::NamespaceSanitizer.sanitize(slug)
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
2017-11-21 07:09:55 -05:00
|
|
|
|
2018-10-23 06:52:34 -04:00
|
|
|
def build_kube_client!
|
2018-11-01 20:41:49 -04:00
|
|
|
raise "Incomplete settings" unless api_url
|
2018-11-08 05:52:58 -05:00
|
|
|
raise "No namespace" if cluster.project_type? && actual_namespace.empty? # can probably remove this line once we remove #actual_namespace
|
2017-11-21 07:09:55 -05:00
|
|
|
|
|
|
|
unless (username && password) || token
|
|
|
|
raise "Either username/password or token is required to access API"
|
|
|
|
end
|
|
|
|
|
2018-09-06 06:03:38 -04:00
|
|
|
Gitlab::Kubernetes::KubeClient.new(
|
|
|
|
api_url,
|
2017-11-21 07:09:55 -05:00
|
|
|
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
|
2018-09-06 06:03:38 -04:00
|
|
|
kubeclient = build_kube_client!
|
2017-11-21 08:03:07 -05:00
|
|
|
|
|
|
|
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 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
|
|
|
|
2018-10-24 18:13:33 -04:00
|
|
|
def enforce_ca_whitespace_trimming
|
|
|
|
self.ca_pem = self.ca_pem&.strip
|
|
|
|
self.token = self.token&.strip
|
|
|
|
end
|
|
|
|
|
2018-11-06 03:55:10 -05:00
|
|
|
def no_namespace
|
|
|
|
if namespace
|
|
|
|
errors.add(:namespace, 'only allowed for project cluster')
|
|
|
|
end
|
|
|
|
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
|
2018-11-02 11:46:15 -04:00
|
|
|
|
|
|
|
def update_kubernetes_namespace
|
|
|
|
return unless namespace_changed?
|
|
|
|
|
|
|
|
run_after_commit do
|
|
|
|
ClusterPlatformConfigureWorker.perform_async(cluster_id)
|
|
|
|
end
|
|
|
|
end
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|