2018-10-16 20:03:59 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Clusters
|
|
|
|
class KubernetesNamespace < ActiveRecord::Base
|
2018-11-02 15:46:15 +00:00
|
|
|
include Gitlab::Kubernetes
|
|
|
|
|
2018-10-16 20:03:59 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
validates :namespace, presence: true
|
|
|
|
validates :namespace, uniqueness: { scope: :cluster_id }
|
|
|
|
|
2018-11-02 15:46:15 +00:00
|
|
|
delegate :ca_pem, to: :platform_kubernetes, allow_nil: true
|
|
|
|
delegate :api_url, to: :platform_kubernetes, allow_nil: true
|
2018-10-16 20:03:59 +00:00
|
|
|
|
|
|
|
attr_encrypted :service_account_token,
|
|
|
|
mode: :per_attribute_iv,
|
|
|
|
key: Settings.attr_encrypted_db_key_base_truncated,
|
|
|
|
algorithm: 'aes-256-cbc'
|
|
|
|
|
2018-11-13 10:27:21 +00:00
|
|
|
scope :has_service_account_token, -> { where.not(encrypted_service_account_token: nil) }
|
|
|
|
|
2018-10-16 20:03:59 +00:00
|
|
|
def token_name
|
|
|
|
"#{namespace}-token"
|
|
|
|
end
|
|
|
|
|
2018-11-02 15:46:15 +00:00
|
|
|
def configure_predefined_credentials
|
|
|
|
self.namespace = kubernetes_or_project_namespace
|
|
|
|
self.service_account_name = default_service_account_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def predefined_variables
|
|
|
|
config = YAML.dump(kubeconfig)
|
2018-10-16 20:03:59 +00:00
|
|
|
|
2018-11-02 15:46:15 +00:00
|
|
|
Gitlab::Ci::Variables::Collection.new.tap do |variables|
|
|
|
|
variables
|
|
|
|
.append(key: 'KUBE_SERVICE_ACCOUNT', value: service_account_name)
|
|
|
|
.append(key: 'KUBE_NAMESPACE', value: namespace)
|
|
|
|
.append(key: 'KUBE_TOKEN', value: service_account_token, public: false)
|
|
|
|
.append(key: 'KUBECONFIG', value: config, public: false, file: true)
|
|
|
|
end
|
2018-10-16 20:03:59 +00:00
|
|
|
end
|
|
|
|
|
2018-11-02 15:46:15 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def kubernetes_or_project_namespace
|
2018-10-16 20:03:59 +00:00
|
|
|
platform_kubernetes&.namespace.presence || project_namespace
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_service_account_name
|
|
|
|
"#{namespace}-service-account"
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_namespace
|
|
|
|
Gitlab::NamespaceSanitizer.sanitize(project_slug)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_slug
|
|
|
|
"#{project.path}-#{project.id}".downcase
|
|
|
|
end
|
2018-11-02 15:46:15 +00:00
|
|
|
|
|
|
|
def kubeconfig
|
|
|
|
to_kubeconfig(
|
|
|
|
url: api_url,
|
|
|
|
namespace: namespace,
|
|
|
|
token: service_account_token,
|
|
|
|
ca_pem: ca_pem)
|
|
|
|
end
|
2018-10-16 20:03:59 +00:00
|
|
|
end
|
|
|
|
end
|