summaryrefslogtreecommitdiff
path: root/app/models/clusters/platforms/kubernetes.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/clusters/platforms/kubernetes.rb')
-rw-r--r--app/models/clusters/platforms/kubernetes.rb72
1 files changed, 19 insertions, 53 deletions
diff --git a/app/models/clusters/platforms/kubernetes.rb b/app/models/clusters/platforms/kubernetes.rb
index 8e06156c73d..9296c28776b 100644
--- a/app/models/clusters/platforms/kubernetes.rb
+++ b/app/models/clusters/platforms/kubernetes.rb
@@ -4,7 +4,6 @@ module Clusters
module Platforms
class Kubernetes < ApplicationRecord
include Gitlab::Kubernetes
- include ReactiveCaching
include EnumWithNil
include AfterCommitQueue
@@ -46,9 +45,6 @@ module Clusters
validate :prevent_modification, on: :update
- after_save :clear_reactive_cache!
- after_update :update_kubernetes_namespace
-
alias_attribute :ca_pem, :ca_cert
delegate :enabled?, to: :cluster, allow_nil: true
@@ -80,48 +76,33 @@ module Clusters
.append(key: 'KUBE_CA_PEM_FILE', value: ca_pem, file: true)
end
- if kubernetes_namespace = cluster.kubernetes_namespaces.has_service_account_token.find_by(project: project)
- variables.concat(kubernetes_namespace.predefined_variables)
- elsif cluster.project_type? || !cluster.managed?
- # As of 11.11 a user can create a cluster that they manage themselves,
- # which replicates the existing project-level cluster behaviour.
- # Once we have marked all project-level clusters that make use of this
- # behaviour as "unmanaged", we can remove the `cluster.project_type?`
- # check here.
- project_namespace = cluster.kubernetes_namespace_for(project)
+ if !cluster.managed?
+ project_namespace = namespace.presence || "#{project.path}-#{project.id}".downcase
variables
.append(key: 'KUBE_URL', value: api_url)
.append(key: 'KUBE_TOKEN', value: token, public: false, masked: true)
.append(key: 'KUBE_NAMESPACE', value: project_namespace)
.append(key: 'KUBECONFIG', value: kubeconfig(project_namespace), public: false, file: true)
+
+ elsif kubernetes_namespace = cluster.kubernetes_namespaces.has_service_account_token.find_by(project: project)
+ variables.concat(kubernetes_namespace.predefined_variables)
end
variables.concat(cluster.predefined_variables)
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|
- project = environment.project
-
- pods = filter_by_project_environment(data[:pods], project.full_path_slug, environment.slug)
- terminals = pods.flat_map { |pod| terminals_for_pod(api_url, cluster.kubernetes_namespace_for(project), pod) }.compact
- 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
+ def calculate_reactive_cache_for(environment)
return unless enabled?
- # We may want to cache extra things in the future
- { pods: read_pods }
+ { pods: read_pods(environment.deployment_namespace) }
+ end
+
+ def terminals(environment, data)
+ pods = filter_by_project_environment(data[:pods], environment.project.full_path_slug, environment.slug)
+ terminals = pods.flat_map { |pod| terminals_for_pod(api_url, environment.deployment_namespace, pod) }.compact
+ terminals.each { |terminal| add_terminal_auth(terminal, terminal_auth) }
end
def kubeclient
@@ -138,6 +119,12 @@ module Clusters
ca_pem: ca_pem)
end
+ def read_pods(namespace)
+ kubeclient.get_pods(namespace: namespace).as_json
+ rescue Kubeclient::ResourceNotFoundError
+ []
+ end
+
def build_kube_client!
raise "Incomplete settings" unless api_url
@@ -153,19 +140,6 @@ module Clusters
)
end
- # Returns a hash of all pods in the namespace
- def read_pods
- # TODO: The project lookup here should be moved (to environment?),
- # which will enable reading pods from the correct namespace for group
- # and instance clusters.
- # This will be done in https://gitlab.com/gitlab-org/gitlab-ce/issues/61156
- return [] unless cluster.project_type?
-
- kubeclient.get_pods(namespace: cluster.kubernetes_namespace_for(cluster.first_project)).as_json
- rescue Kubeclient::ResourceNotFoundError
- []
- end
-
def kubeclient_ssl_options
opts = { verify_ssl: OpenSSL::SSL::VERIFY_PEER }
@@ -214,14 +188,6 @@ module Clusters
true
end
-
- def update_kubernetes_namespace
- return unless saved_change_to_namespace?
-
- run_after_commit do
- ClusterConfigureWorker.perform_async(cluster_id)
- end
- end
end
end
end