Module: KubeclientExec::Execute

Included in:
KubeclientExec
Defined in:
lib/kubeclient_exec/execute/execute.rb,
lib/kubeclient_exec/execute/executor.rb

Defined Under Namespace

Classes: Executor

Constant Summary collapse

DEFAULT_EXEC_OPTIONS =
{
  container: nil,
  stdin: true,
  stdout: true,
  stderror: true,
  tty: true,
  suppress_errors: false,
  tls: {
    cert_chain_file: nil,
    cert: nil,
    private_key_file: nil,
    private_key: nil,
    verify_peer: true
  }
}

Instance Method Summary collapse

Instance Method Details

#exec_pod(command, name, namespace, options: {}, &block) ⇒ Object



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
55
56
57
# File 'lib/kubeclient_exec/execute/execute.rb', line 23

def exec_pod(command, name, namespace, options: {}, &block)
  ns_prefix = build_namespace_prefix(namespace)
  client = rest_client["#{ns_prefix}pods/#{name}/exec"]
  url = URI.parse(client.url)

  # Reverse merge with the default options
  options.merge!(Execute::DEFAULT_EXEC_OPTIONS) { |_, option, _| option }

  kubeclient_options = { headers: @headers, tls: options[:tls] }

  url.query = (options.filter { |k| ![:suppress_errors, :tls].include?(k) }.compact.map { |k, v| "#{k}=#{v}"} << command.split(' ').map { |c| "command=#{c}"}).join('&')

  if url.to_s.start_with?('https')
    url = "wss" + url.to_s[5..-1]
  end

  last_output = { last_stdout: nil, last_stderr: nil }

  EM.run do
    executor = if block_given?
        Executor.new(command, url, kubeclient_options, options) do |executor|
          block.call(executor)
        end
    else
        Executor.new(command, url, kubeclient_options, options.merge!(mode: :adhoc))
    end

    EM.add_shutdown_hook do
      last_output[:last_stdout] = executor.last_stdout
      last_output[:last_stderr] = executor.last_stderr
    end
  end

  last_output
end