Class: Floe::ContainerRunner::Kubernetes
Constant Summary
collapse
- TOKEN_FILE =
"/run/secrets/kubernetes.io/serviceaccount/token"
- CA_CERT_FILE =
"/run/secrets/kubernetes.io/serviceaccount/ca.crt"
- RUNNING_PHASES =
%w[Pending Running].freeze
- FAILURE_REASONS =
%w[CrashLoopBackOff ImagePullBackOff ErrImagePull].freeze
Constants included
from DockerMixin
DockerMixin::MAX_CONTAINER_NAME_SIZE
Constants inherited
from Runner
Runner::OUTPUT_MARKER
Instance Method Summary
collapse
#container_name, #image_name
Methods inherited from Runner
for_resource, register_scheme
Constructor Details
#initialize(options = {}) ⇒ Kubernetes
Returns a new instance of Kubernetes.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/floe/container_runner/kubernetes.rb', line 13
def initialize(options = {})
require "active_support/core_ext/hash/keys" require "awesome_spawn"
require "securerandom"
require "base64"
require "kubeclient"
require "yaml"
@kubeconfig_file = ENV.fetch("KUBECONFIG", nil) || options.fetch("kubeconfig", File.join(Dir.home, ".kube", "config"))
@kubeconfig_context = options["kubeconfig_context"]
@token = options["token"]
@token ||= File.read(options["token_file"]) if options.key?("token_file")
@token ||= File.read(TOKEN_FILE) if File.exist?(TOKEN_FILE)
@server = options["server"]
@server ||= URI::HTTPS.build(:host => ENV.fetch("KUBERNETES_SERVICE_HOST"), :port => ENV.fetch("KUBERNETES_SERVICE_PORT", 6443)) if ENV.key?("KUBERNETES_SERVICE_HOST")
@ca_file = options["ca_file"]
@ca_file ||= CA_CERT_FILE if File.exist?(CA_CERT_FILE)
@verify_ssl = options["verify_ssl"] == "false" ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
if server.nil? && token.nil? && !File.exist?(kubeconfig_file)
raise ArgumentError, "Missing connections options, provide a kubeconfig file or pass server and token via --docker-runner-options"
end
@namespace = options.fetch("namespace", "default")
@pull_policy = options["pull-policy"]
@task_service_account = options["task_service_account"]
super
end
|
Instance Method Details
#cleanup(runner_context) ⇒ Object
100
101
102
103
104
105
|
# File 'lib/floe/container_runner/kubernetes.rb', line 100
def cleanup(runner_context)
pod, secret = runner_context.values_at("container_ref", "secrets_ref")
delete_pod(pod) if pod
delete_secret(secret) if secret
end
|
#output(runner_context) ⇒ Object
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/floe/container_runner/kubernetes.rb', line 89
def output(runner_context)
if runner_context.key?("Error")
runner_context.slice("Error", "Cause")
elsif container_failed?(runner_context)
failed_state = failed_container_states(runner_context).first
{"Error" => failed_state["reason"], "Cause" => failed_state["message"]}
else
runner_context["output"] = kubeclient.get_pod_log(runner_context["container_ref"], namespace).body
end
end
|
#run_async!(resource, env, secrets, context) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/floe/container_runner/kubernetes.rb', line 48
def run_async!(resource, env, secrets, context)
raise ArgumentError, "Invalid resource" unless resource&.start_with?("docker://")
image = resource.sub("docker://", "")
name = container_name(image)
secret = create_secret!(secrets) if secrets && !secrets.empty?
execution_id = context.execution["Id"]
runner_context = {"container_ref" => name, "container_state" => {"phase" => "Pending"}, "secrets_ref" => secret}
begin
create_pod!(name, image, env, execution_id, secret)
runner_context
rescue Kubeclient::HttpError => err
cleanup(runner_context)
{"Error" => "States.TaskFailed", "Cause" => err.to_s}
end
end
|
#running?(runner_context) ⇒ Boolean
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/floe/container_runner/kubernetes.rb', line 72
def running?(runner_context)
return false if runner_context.key?("Error")
return false unless pod_running?(runner_context)
return false if container_failed?(runner_context)
true
end
|
#status!(runner_context) ⇒ Object
66
67
68
69
70
|
# File 'lib/floe/container_runner/kubernetes.rb', line 66
def status!(runner_context)
return if runner_context.key?("Error")
runner_context["container_state"] = pod_info(runner_context["container_ref"]).to_h.deep_stringify_keys["status"]
end
|
#success?(runner_context) ⇒ Boolean
83
84
85
86
87
|
# File 'lib/floe/container_runner/kubernetes.rb', line 83
def success?(runner_context)
return false if runner_context.key?("Error")
runner_context.dig("container_state", "phase") == "Succeeded"
end
|
#wait(timeout: nil, events: %i[create update delete])) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/floe/container_runner/kubernetes.rb', line 107
def wait(timeout: nil, events: %i[create update delete])
retry_connection = true
begin
watcher = kubeclient.watch_pods(:namespace => namespace)
retry_connection = true
if timeout.to_i > 0
timeout_thread = Thread.new do
sleep(timeout)
watcher.finish
end
end
watcher.each do |notice|
break if error_notice?(notice)
event = kube_notice_type_to_event(notice.type)
next unless events.include?(event)
runner_context = parse_notice(notice)
next if runner_context.nil?
if block_given?
yield [event, runner_context]
else
timeout_thread&.kill return [[event, runner_context]]
end
end
rescue Kubeclient::HttpError => err
raise unless err.error_code == 401 && retry_connection
@kubeclient = nil
retry_connection = false
retry
ensure
begin
watch&.finish
rescue
nil
end
timeout_thread&.join(0)
end
end
|