2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Gitlab
|
|
|
|
# Helper methods to do with Kubernetes network services & resources
|
|
|
|
module Kubernetes
|
2018-11-18 11:00:15 +05:30
|
|
|
def self.build_header_hash
|
|
|
|
Hash.new { |h, k| h[k] = [] }
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# This is the comand that is run to start a terminal session. Kubernetes
|
|
|
|
# expects `command=foo&command=bar, not `command[]=foo&command[]=bar`
|
|
|
|
EXEC_COMMAND = URI.encode_www_form(
|
|
|
|
['sh', '-c', 'bash || sh'].map { |value| ['command', value] }
|
|
|
|
)
|
|
|
|
|
|
|
|
# Filters an array of pods (as returned by the kubernetes API) by their labels
|
2017-09-10 17:25:29 +05:30
|
|
|
def filter_by_label(items, labels = {})
|
|
|
|
items.select do |item|
|
|
|
|
metadata = item.fetch("metadata", {})
|
|
|
|
item_labels = metadata.fetch("labels", nil)
|
|
|
|
next unless item_labels
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
labels.all? { |k, v| item_labels[k.to_s] == v }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
# Filters an array of pods (as returned by the kubernetes API) by their annotations
|
|
|
|
def filter_by_annotation(items, annotations = {})
|
|
|
|
items.select do |item|
|
|
|
|
metadata = item.fetch("metadata", {})
|
|
|
|
item_annotations = metadata.fetch("annotations", nil)
|
|
|
|
next unless item_annotations
|
|
|
|
|
|
|
|
annotations.all? { |k, v| item_annotations[k.to_s] == v }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Filters an array of pods (as returned by the kubernetes API) by their project and environment
|
|
|
|
def filter_by_project_environment(items, app, env)
|
2019-09-30 21:07:59 +05:30
|
|
|
filter_by_annotation(items, {
|
2019-07-07 11:18:12 +05:30
|
|
|
'app.gitlab.com/app' => app,
|
|
|
|
'app.gitlab.com/env' => env
|
|
|
|
})
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
def filter_by_legacy_label(items, app, env)
|
|
|
|
legacy_items = filter_by_label(items, { app: env })
|
|
|
|
|
|
|
|
non_legacy_items = filter_by_project_environment(legacy_items, app, env)
|
|
|
|
|
|
|
|
legacy_items - non_legacy_items
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Converts a pod (as returned by the kubernetes API) into a terminal
|
|
|
|
def terminals_for_pod(api_url, namespace, pod)
|
|
|
|
metadata = pod.fetch("metadata", {})
|
|
|
|
status = pod.fetch("status", {})
|
|
|
|
spec = pod.fetch("spec", {})
|
|
|
|
|
|
|
|
containers = spec["containers"]
|
|
|
|
pod_name = metadata["name"]
|
|
|
|
phase = status["phase"]
|
|
|
|
|
|
|
|
return unless containers.present? && pod_name.present? && phase == "Running"
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
created_at = begin
|
|
|
|
DateTime.parse(metadata["creationTimestamp"])
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
containers.map do |container|
|
|
|
|
{
|
2022-10-11 01:57:18 +05:30
|
|
|
selectors: { pod: pod_name, container: container["name"] },
|
|
|
|
url: container_exec_url(api_url, namespace, pod_name, container["name"]),
|
2017-08-17 22:00:37 +05:30
|
|
|
subprotocols: ['channel.k8s.io'],
|
2022-10-11 01:57:18 +05:30
|
|
|
headers: ::Gitlab::Kubernetes.build_header_hash,
|
|
|
|
created_at: created_at
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_terminal_auth(terminal, token:, max_session_time:, ca_pem: nil)
|
2018-11-18 11:00:15 +05:30
|
|
|
terminal[:headers] ||= ::Gitlab::Kubernetes.build_header_hash
|
2017-08-17 22:00:37 +05:30
|
|
|
terminal[:headers]['Authorization'] << "Bearer #{token}"
|
|
|
|
terminal[:max_session_time] = max_session_time
|
|
|
|
terminal[:ca_pem] = ca_pem if ca_pem.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def container_exec_url(api_url, namespace, pod_name, container_name)
|
|
|
|
url = URI.parse(api_url)
|
|
|
|
url.path = [
|
|
|
|
url.path.sub(%r{/+\z}, ''),
|
|
|
|
'api', 'v1',
|
|
|
|
'namespaces', ERB::Util.url_encode(namespace),
|
|
|
|
'pods', ERB::Util.url_encode(pod_name),
|
|
|
|
'exec'
|
|
|
|
].join('/')
|
|
|
|
|
|
|
|
url.query = {
|
|
|
|
container: container_name,
|
|
|
|
tty: true,
|
|
|
|
stdin: true,
|
|
|
|
stdout: true,
|
2017-09-10 17:25:29 +05:30
|
|
|
stderr: true
|
2017-08-17 22:00:37 +05:30
|
|
|
}.to_query + '&' + EXEC_COMMAND
|
|
|
|
|
|
|
|
case url.scheme
|
|
|
|
when 'http'
|
|
|
|
url.scheme = 'ws'
|
|
|
|
when 'https'
|
|
|
|
url.scheme = 'wss'
|
|
|
|
end
|
|
|
|
|
|
|
|
url.to_s
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
def to_kubeconfig(url:, namespace:, token:, ca_pem: nil)
|
2019-02-15 15:39:39 +05:30
|
|
|
return unless token.present?
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
config = {
|
|
|
|
apiVersion: 'v1',
|
|
|
|
clusters: [
|
|
|
|
name: 'gitlab-deploy',
|
|
|
|
cluster: {
|
|
|
|
server: url
|
|
|
|
}
|
|
|
|
],
|
|
|
|
contexts: [
|
|
|
|
name: 'gitlab-deploy',
|
|
|
|
context: {
|
|
|
|
cluster: 'gitlab-deploy',
|
|
|
|
namespace: namespace,
|
|
|
|
user: 'gitlab-deploy'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'current-context': 'gitlab-deploy',
|
|
|
|
kind: 'Config',
|
|
|
|
users: [
|
|
|
|
{
|
|
|
|
name: 'gitlab-deploy',
|
|
|
|
user: { token: token }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeconfig_embed_ca_pem(config, ca_pem) if ca_pem
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
YAML.dump(config.deep_stringify_keys)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def kubeconfig_embed_ca_pem(config, ca_pem)
|
|
|
|
cluster = config.dig(:clusters, 0, :cluster)
|
2018-03-17 18:26:18 +05:30
|
|
|
cluster[:'certificate-authority-data'] = Base64.strict_encode64(ca_pem)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|