Skip to content
Snippets Groups Projects

Incorporates Kubernetes Namespace into Cluster's flow

Merged Thong Kuah requested to merge 51716-create-kube-namespace into master
Compare and Show latest version
23 files
+ 3056
122
Compare changes
  • Side-by-side
  • Inline
Files
23
@@ -6,10 +6,13 @@ class Kubernetes < ActiveRecord::Base
include Gitlab::Kubernetes
include ReactiveCaching
include EnumWithNil
include AfterCommitQueue
self.table_name = 'cluster_platforms_kubernetes'
self.reactive_cache_key = ->(kubernetes) { [kubernetes.class.model_name.singular, kubernetes.id] }
RESERVED_NAMESPACES = %w(gitlab-managed-apps).freeze
belongs_to :cluster, inverse_of: :platform_kubernetes, class_name: 'Clusters::Cluster'
attr_encrypted :password,
@@ -37,8 +40,10 @@ class Kubernetes < ActiveRecord::Base
validates :token, presence: true
validate :prevent_modification, on: :update
validate :prevent_reserved_namespaces
after_save :clear_reactive_cache!
after_update :update_kubernetes_namespace
alias_attribute :ca_pem, :ca_cert
@@ -47,6 +52,7 @@ class Kubernetes < ActiveRecord::Base
delegate :managed?, to: :cluster, allow_nil: true
delegate :cluster_project, to: :project, allow_nil: true
delegate :kubernetes_namespace, to: :cluster_project, allow_nil: true
alias_method :active?, :enabled?
@@ -57,12 +63,6 @@ class Kubernetes < ActiveRecord::Base
}
def actual_namespace
cluster_project&.namespace || fallback_actual_namespace
end
# DEPRECATED
# To remove after migration of data to cluster_projects
def fallback_actual_namespace
if namespace.present?
namespace
else
@@ -123,8 +123,12 @@ def kubeconfig
ca_pem: ca_pem)
end
# DEPRECATED
def default_namespace
cluster_project&.kubernetes_namespace&.namespace.presence || fallback_default_namespace
end
# DEPRECATED
def fallback_default_namespace
return unless project
slug = "#{project.path}-#{project.id}".downcase
@@ -196,6 +200,22 @@ def prevent_modification
true
end
def prevent_reserved_namespaces
return if namespace.blank?
if RESERVED_NAMESPACES.include?(namespace)
errors.add(:namespace, 'Cannot used a GitLab reserved namespace')
end
end
def update_kubernetes_namespace
return unless namespace_changed?
run_after_commit do
ClusterPlatformConfigureWorker.perform_async(cluster.id)
end
end
end
end
end
Loading