Class: Floe::ContainerRunner::Kubernetes

Inherits:
Runner
  • Object
show all
Includes:
DockerMixin, HostPathVolumeHandler, PersistentVolumeHandler
Defined in:
lib/floe/container_runner/kubernetes.rb,
lib/floe/container_runner/kubernetes/host_path_volume_handler.rb,
lib/floe/container_runner/kubernetes/persistent_volume_handler.rb

Defined Under Namespace

Modules: HostPathVolumeHandler, PersistentVolumeHandler

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 HostPathVolumeHandler

HostPathVolumeHandler::DEFAULT_INIT_IMAGE, HostPathVolumeHandler::INIT_CONTAINER_NAME

Constants included from DockerMixin

DockerMixin::MAX_CONTAINER_NAME_SIZE

Constants inherited from Runner

Runner::OUTPUT_MARKER

Instance Method Summary collapse

Methods included from HostPathVolumeHandler

#cleanup_staged_volumes, #init_host_path_volume_options

Methods included from DockerMixin

#container_name, #image_name

Methods inherited from Runner

for_resource, register_scheme

Constructor Details

#initialize(options = {}) ⇒ Kubernetes



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
47
48
49
50
51
52
53
54
# File 'lib/floe/container_runner/kubernetes.rb', line 19

def initialize(options = {})
  require "active_support/core_ext/hash/keys" # deep_stringify_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"]
   = options["task_service_account"]

  init_host_path_volume_options(options)

  super
end

Instance Method Details

#cleanup(runner_context) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/floe/container_runner/kubernetes.rb', line 121

def cleanup(runner_context)
  pod, secret = runner_context.values_at("container_ref", "secrets_ref")

  delete_pod(pod)       if pod
  delete_secret(secret) if secret

  cleanup_staged_volumes(runner_context["staged_volumes"])
end

#inspectObject



178
179
180
181
182
# File 'lib/floe/container_runner/kubernetes.rb', line 178

def inspect
  vars = instance_variables_to_inspect.map { |ivar| "#{ivar}=#{instance_variable_get(ivar).inspect}" }.join(", ")
  prefix = Kernel.instance_method(:inspect).bind_call(self).split(' ', 2).first
  "#{prefix} #{vars}>"
end

#output(runner_context) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/floe/container_runner/kubernetes.rb', line 108

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
    log_options = {}
    log_options[:container] = runner_context["primary_container"] if runner_context["primary_container"]
    runner_context["output"] = kubeclient.get_pod_log(runner_context["container_ref"], namespace, **log_options).body
  end
end

#run_async!(resource, env, secrets, context, volumes: [], entrypoint: nil, command: nil) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/floe/container_runner/kubernetes.rb', line 56

def run_async!(resource, env, secrets, context, volumes: [], entrypoint: nil, command: nil)
  raise ArgumentError, "Invalid resource" unless resource&.start_with?("docker://")
  raise ArgumentError, "entrypoint must be a String" if entrypoint && !entrypoint.kind_of?(String)
  raise ArgumentError, "command must be an Array"    if command && !command.kind_of?(Array)

  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}

  persistent_volumes, host_volumes = volumes.partition { |v| v[:volume_name] }
  staged_volumes = stage_host_path_volumes(host_volumes, execution_id, context.logger) if host_volumes.any?
  runner_context["staged_volumes"] = staged_volumes if staged_volumes

  begin
    spec = pod_spec(name, image, env, execution_id, secret, staged_volumes || [], persistent_volumes || [], entrypoint, command)
    context.logger.debug("Running pod #{name} with image #{image}")
    kubeclient.create_pod(spec)
    # Always record the primary container name so get_pod_log targets the
    # right container even when init containers are present.
    runner_context["primary_container"] = spec.dig(:spec, :containers, 0, :name)
    runner_context
  rescue Kubeclient::HttpError => err
    cleanup(runner_context)
    {"Error" => "States.TaskFailed", "Cause" => err.to_s}
  end
end

#running?(runner_context) ⇒ Boolean



91
92
93
94
95
96
97
98
99
100
# File 'lib/floe/container_runner/kubernetes.rb', line 91

def running?(runner_context)
  return false if runner_context.key?("Error")
  return false unless pod_running?(runner_context)
  # If a pod is Pending and the containers are waiting with a failure
  # reason such as ImagePullBackOff or CrashLoopBackOff then the pod
  # will never be run.
  return false if container_failed?(runner_context)

  true
end

#status!(runner_context) ⇒ Object



85
86
87
88
89
# File 'lib/floe/container_runner/kubernetes.rb', line 85

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



102
103
104
105
106
# File 'lib/floe/container_runner/kubernetes.rb', line 102

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/floe/container_runner/kubernetes.rb', line 130

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 # If we break out before the timeout, kill the timeout thread
        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