Module: KBE

Defined in:
lib/kbe.rb,
lib/kbe/cli.rb,
lib/kbe/version.rb,
lib/kbe/cli/help_command.rb,
lib/kbe/cli/list_command.rb,
lib/kbe/cli/root_command.rb,
lib/kbe/cli/enter_command.rb,
lib/kbe/cli/delete_command.rb,
lib/kbe/cli/version_command.rb

Defined Under Namespace

Modules: CLI Classes: Error

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.get_pods(selector: nil, name: nil, only_running: true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kbe.rb', line 43

def self.get_pods(selector:nil, name:nil, only_running: true)
  parts = []
  parts << "kubectl get pods --no-headers -o custom-columns=':metadata.name'"
  if selector
    parts << "--field-selector=status.phase=Running" if only_running
    parts << "--selector=#{selector}"
  else
    parts << name
  end

  cmd = parts.join " "
  # dev/null supresses the "pod not found" by name
  `#{cmd} 2>/dev/null`.split("\n")
end

.kubectl(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/kbe.rb', line 5

def self.kubectl(*args)
  cmd = []
  cmd << "kubectl"
  cmd << args

  cmd_string = cmd.join " "
  STDERR.puts cmd_string.colorize(:light_black)
  exec cmd_string
end

.pod_by(name_or_selector, only_running: true) ⇒ Object



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
# File 'lib/kbe.rb', line 15

def self.pod_by(name_or_selector, only_running: true)
  pods = if name_or_selector.match? "="
    get_pods selector: name_or_selector, only_running: only_running
  else
    get_pods name: name_or_selector, only_running: only_running
  end

  if pods.empty?
    ["app", "job-name"].each do |selector_prefix|
      magic_selector = "#{selector_prefix}=#{name_or_selector}"

      pods = get_pods selector: magic_selector, only_running: only_running
      break if pods.any?
    end
  end

  if pods.size == 1
    return pods.first
  elsif pods.size > 1
    STDERR.puts "too many pods found:"
    STDERR.p pods
  else
    STDERR.puts "no pods found."
  end

  exit 1
end