2018-10-16 16:03:59 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Clusters
|
2019-03-28 09:17:42 -04:00
|
|
|
class KubernetesNamespace < ApplicationRecord
|
2018-11-02 11:46:15 -04:00
|
|
|
include Gitlab::Kubernetes
|
|
|
|
|
2018-10-16 16:03:59 -04: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'
|
2019-08-07 00:40:29 -04:00
|
|
|
belongs_to :environment, optional: true
|
2018-10-16 16:03:59 -04:00
|
|
|
has_one :platform_kubernetes, through: :cluster
|
|
|
|
|
|
|
|
validates :namespace, presence: true
|
|
|
|
validates :namespace, uniqueness: { scope: :cluster_id }
|
2019-08-07 00:40:29 -04:00
|
|
|
validates :environment_id, uniqueness: { scope: [:cluster_id, :project_id] }, allow_nil: true
|
2018-10-16 16:03:59 -04:00
|
|
|
|
2018-11-14 12:55:38 -05:00
|
|
|
validates :service_account_name, presence: true
|
|
|
|
|
2018-11-02 11:46:15 -04:00
|
|
|
delegate :ca_pem, to: :platform_kubernetes, allow_nil: true
|
|
|
|
delegate :api_url, to: :platform_kubernetes, allow_nil: true
|
2018-10-16 16:03:59 -04: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 05:27:21 -05:00
|
|
|
scope :has_service_account_token, -> { where.not(encrypted_service_account_token: nil) }
|
2019-09-23 11:06:32 -04:00
|
|
|
scope :with_environment_name, -> (name) { joins(:environment).where(environments: { name: name }) }
|
2018-11-13 05:27:21 -05:00
|
|
|
|
2018-10-16 16:03:59 -04:00
|
|
|
def token_name
|
|
|
|
"#{namespace}-token"
|
|
|
|
end
|
|
|
|
|
2018-11-02 11:46:15 -04:00
|
|
|
def predefined_variables
|
|
|
|
Gitlab::Ci::Variables::Collection.new.tap do |variables|
|
|
|
|
variables
|
2018-11-14 12:55:38 -05:00
|
|
|
.append(key: 'KUBE_SERVICE_ACCOUNT', value: service_account_name.to_s)
|
|
|
|
.append(key: 'KUBE_NAMESPACE', value: namespace.to_s)
|
2019-03-07 12:08:17 -05:00
|
|
|
.append(key: 'KUBE_TOKEN', value: service_account_token.to_s, public: false, masked: true)
|
2018-11-28 07:29:01 -05:00
|
|
|
.append(key: 'KUBECONFIG', value: kubeconfig, public: false, file: true)
|
2018-11-02 11:46:15 -04:00
|
|
|
end
|
2018-10-16 16:03:59 -04:00
|
|
|
end
|
|
|
|
|
2018-11-14 12:55:38 -05:00
|
|
|
private
|
|
|
|
|
2018-11-02 11:46:15 -04:00
|
|
|
def kubeconfig
|
|
|
|
to_kubeconfig(
|
|
|
|
url: api_url,
|
|
|
|
namespace: namespace,
|
|
|
|
token: service_account_token,
|
|
|
|
ca_pem: ca_pem)
|
|
|
|
end
|
2018-10-16 16:03:59 -04:00
|
|
|
end
|
|
|
|
end
|