2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Clusters
|
2019-05-18 00:54:41 +05:30
|
|
|
class KubernetesNamespace < ApplicationRecord
|
2018-12-13 13:39:08 +05:30
|
|
|
include Gitlab::Kubernetes
|
|
|
|
|
|
|
|
self.table_name = 'clusters_kubernetes_namespaces'
|
|
|
|
|
|
|
|
belongs_to :cluster_project, class_name: 'Clusters::Project'
|
|
|
|
belongs_to :cluster, class_name: 'Clusters::Cluster'
|
|
|
|
belongs_to :project, class_name: '::Project'
|
|
|
|
has_one :platform_kubernetes, through: :cluster
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
before_validation :set_defaults
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
validates :namespace, presence: true
|
|
|
|
validates :namespace, uniqueness: { scope: :cluster_id }
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
validates :service_account_name, presence: true
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
delegate :ca_pem, to: :platform_kubernetes, allow_nil: true
|
|
|
|
delegate :api_url, to: :platform_kubernetes, allow_nil: true
|
|
|
|
|
|
|
|
attr_encrypted :service_account_token,
|
|
|
|
mode: :per_attribute_iv,
|
|
|
|
key: Settings.attr_encrypted_db_key_base_truncated,
|
|
|
|
algorithm: 'aes-256-cbc'
|
|
|
|
|
|
|
|
scope :has_service_account_token, -> { where.not(encrypted_service_account_token: nil) }
|
|
|
|
|
|
|
|
def token_name
|
|
|
|
"#{namespace}-token"
|
|
|
|
end
|
|
|
|
|
|
|
|
def predefined_variables
|
|
|
|
Gitlab::Ci::Variables::Collection.new.tap do |variables|
|
|
|
|
variables
|
2019-02-15 15:39:39 +05:30
|
|
|
.append(key: 'KUBE_SERVICE_ACCOUNT', value: service_account_name.to_s)
|
|
|
|
.append(key: 'KUBE_NAMESPACE', value: namespace.to_s)
|
2019-05-18 00:54:41 +05:30
|
|
|
.append(key: 'KUBE_TOKEN', value: service_account_token.to_s, public: false, masked: true)
|
2019-02-15 15:39:39 +05:30
|
|
|
.append(key: 'KUBECONFIG', value: kubeconfig, public: false, file: true)
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def set_defaults
|
|
|
|
self.namespace ||= default_platform_kubernetes_namespace
|
|
|
|
self.namespace ||= default_project_namespace
|
|
|
|
self.service_account_name ||= default_service_account_name
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
private
|
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
def default_service_account_name
|
2019-02-15 15:39:39 +05:30
|
|
|
return unless namespace
|
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
"#{namespace}-service-account"
|
2018-12-23 12:14:25 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def default_platform_kubernetes_namespace
|
|
|
|
platform_kubernetes&.namespace.presence
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_project_namespace
|
|
|
|
Gitlab::NamespaceSanitizer.sanitize(project_slug) if project_slug
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def project_slug
|
2019-02-15 15:39:39 +05:30
|
|
|
return unless project
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
"#{project.path}-#{project.id}".downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def kubeconfig
|
|
|
|
to_kubeconfig(
|
|
|
|
url: api_url,
|
|
|
|
namespace: namespace,
|
|
|
|
token: service_account_token,
|
|
|
|
ca_pem: ca_pem)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|