Class: Clusters::Platforms::Kubernetes

Inherits:
ApplicationRecord show all
Includes:
AfterCommitQueue, EnumWithNil, Gitlab::Kubernetes, ReactiveCaching
Defined in:
app/models/clusters/platforms/kubernetes.rb

Constant Summary collapse

RESERVED_NAMESPACES =
%w(gitlab-managed-apps).freeze

Constants included from ReactiveCaching

ReactiveCaching::ExceededReactiveCacheLimit, ReactiveCaching::InvalidateReactiveCache, ReactiveCaching::WORK_TYPE

Constants included from Gitlab::Kubernetes

Gitlab::Kubernetes::EXEC_COMMAND

Instance Method Summary collapse

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods included from Gitlab::Kubernetes

#add_terminal_auth, build_header_hash, #container_exec_url, #filter_by_annotation, #filter_by_label, #filter_by_legacy_label, #filter_by_project_environment, #terminals_for_pod, #to_kubeconfig

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Instance Method Details

#calculate_reactive_cache_for(environment) ⇒ Object


92
93
94
95
96
97
98
99
# File 'app/models/clusters/platforms/kubernetes.rb', line 92

def calculate_reactive_cache_for(environment)
  return unless enabled?

  pods = read_pods(environment.deployment_namespace)

  # extract_relevant_pod_data avoids uploading all the pod info into ReactiveCaching
  { pods: extract_relevant_pod_data(pods) }
end

#kubeclientObject


107
108
109
# File 'app/models/clusters/platforms/kubernetes.rb', line 107

def kubeclient
  @kubeclient ||= build_kube_client!
end

#predefined_variables(project:, environment_name:, kubernetes_namespace: nil) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/clusters/platforms/kubernetes.rb', line 66

def predefined_variables(project:, environment_name:, kubernetes_namespace: nil)
  Gitlab::Ci::Variables::Collection.new.tap do |variables|
    variables.append(key: 'KUBE_URL', value: api_url)

    if ca_pem.present?
      variables
        .append(key: 'KUBE_CA_PEM', value: ca_pem)
        .append(key: 'KUBE_CA_PEM_FILE', value: ca_pem, file: true)
    end

    if !cluster.managed? || cluster.management_project == project
      namespace = kubernetes_namespace || default_namespace(project, environment_name: environment_name)

      variables
        .append(key: 'KUBE_TOKEN', value: token, public: false, masked: true)
        .append(key: 'KUBE_NAMESPACE', value: namespace)
        .append(key: 'KUBECONFIG', value: kubeconfig(namespace), public: false, file: true)

    elsif persisted_namespace = find_persisted_namespace(project, environment_name: environment_name)
      variables.concat(persisted_namespace.predefined_variables)
    end

    variables.concat(cluster.predefined_variables)
  end
end

#terminals(environment, data) ⇒ Object


101
102
103
104
105
# File 'app/models/clusters/platforms/kubernetes.rb', line 101

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