Class: Kubectl

Inherits:
Object
  • Object
show all
Defined in:
lib/kuberun/kubectl.rb

Constant Summary collapse

CAT_MULTILINE =
'EOFCONFIG'
KUBECTL_OPTIONS =
Kuberun::CLI::BASE_KUBECTL_OPTIONS.keys.map(&:to_s)

Instance Method Summary collapse

Instance Method Details

#auth_check(verb, resource:, resource_name: nil) ⇒ Object



14
15
16
# File 'lib/kuberun/kubectl.rb', line 14

def auth_check(verb, resource:, resource_name: nil)
  cmd.run(kubectl_base_command("auth can-i #{verb}", resource: resource, resource_name: resource_name))
end

#create(configuration:, options: nil) ⇒ Object



25
26
27
# File 'lib/kuberun/kubectl.rb', line 25

def create(configuration:, options: nil)
  cmd.run(kubectl_base_input_command('create', configuration: configuration, options: options))
end

#delete(resource:, resource_name:) ⇒ Object



69
70
71
# File 'lib/kuberun/kubectl.rb', line 69

def delete(resource:, resource_name:)
  cmd.run(kubectl_base_command('delete', resource: resource, resource_name: resource_name))
end

#exec(pod:, command:) ⇒ Object



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
58
59
60
61
62
63
64
65
66
67
# File 'lib/kuberun/kubectl.rb', line 29

def exec(pod:, command:)
  old_state = `stty -g`

  PTY.spawn("#{kubectl_base_command('exec', resource: pod)} #{command}") do |o, i, pid|
    t_in = Thread.new do
      until i.closed? do
        input = $stdin.getch
        i.write(input)
        i.flush
      end
    end

    t_out = Thread.new do
      begin
        until o.eof? do
          $stdout.print(o.readchar)
          $stdout.flush
        end
      rescue Errno::EIO, EOFError
        nil
      end
    end

    t_in.run

    Process::waitpid(pid) rescue nil
    # "rescue nil" is there in case process already ended.

    t_out.join
    t_in.kill
    sleep 0.1
  ensure
    t_out&.kill
    t_in&.kill
    $stdout.puts
    $stdout.flush
    system "stty #{ old_state }"
  end
end

#get(resource:, resource_name: nil, options: nil) ⇒ Object



18
19
20
21
22
23
# File 'lib/kuberun/kubectl.rb', line 18

def get(resource:, resource_name: nil, options: nil)
  options = "#{options} --output=json"
  parsed_json do
    cmd.run(kubectl_base_command('get', resource: resource, resource_name: resource_name, options: options)).out
  end
end

#load_options(options) ⇒ Object



10
11
12
# File 'lib/kuberun/kubectl.rb', line 10

def load_options(options)
  self.kubectl_options = parsed_options(options)
end