Class: Kubert::Pods

Inherits:
Object
  • Object
show all
Defined in:
lib/kubert/pods.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_name = Kubert.configuration[:project_name]) ⇒ Pods

Returns a new instance of Pods.



20
21
22
23
# File 'lib/kubert/pods.rb', line 20

def initialize(project_name= Kubert.configuration[:project_name])
  @project_name = project_name
  @pods = []
end

Instance Attribute Details

#podsObject (readonly)

Returns the value of attribute pods.



19
20
21
# File 'lib/kubert/pods.rb', line 19

def pods
  @pods
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



19
20
21
# File 'lib/kubert/pods.rb', line 19

def project_name
  @project_name
end

Class Method Details

.consoleObject



7
8
9
# File 'lib/kubert/pods.rb', line 7

def self.console
  new.console
end

.execute(command) ⇒ Object



11
12
13
# File 'lib/kubert/pods.rb', line 11

def self.execute(command)
  new.execute(command)
end

.list(pod_type, status) ⇒ Object



3
4
5
# File 'lib/kubert/pods.rb', line 3

def self.list(pod_type, status)
  new.all(pod_type).status(status).names
end

.logs(pod_type, status) ⇒ Object



15
16
17
# File 'lib/kubert/pods.rb', line 15

def self.logs(pod_type, status)
  new.all(pod_type).status(status).logs
end

Instance Method Details

#all(pod_type) ⇒ Object



25
26
27
28
# File 'lib/kubert/pods.rb', line 25

def all(pod_type)
  @pods = Kubert.client.get_pods(label_selector: "app=#{project_name}-#{pod_type}")
  self
end

#execute(command, pod_type = Kubert.task_pod) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/kubert/pods.rb', line 39

def execute(command, pod_type=Kubert.task_pod)
  pod = all(pod_type).status(:running).pods.sample
  exec_command = "kubectl exec -n #{pod.metadata.namespace} #{pod.metadata.name} -it #{Kubert.command_prefix} #{command.join(' ')}"
  puts "Executing command: \n#{exec_command}"
  Open3.popen3("bash") do
    exec exec_command
  end
  puts "THIS WILL NEVER EXECUTE BECAUSE OF EXEC ABOVE"
end

#logsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kubert/pods.rb', line 49

def logs
  fibers = names.map.with_index do |pod_name, i|
    puts "logging #{pod_name}:"
    watcher = Kubert.client.watch_pod_log(pod_name, pods.first..namespace)
    pod_name = names[i]
    log_enum = watcher.to_enum
    Fiber.new do
      loop do
        Fiber.yield(puts "#{pod_name} |> #{log_enum.next}")
      end
    end
  end
  while fibers.all?(&:alive?) do
    fibers.shuffle.each(&:resume)
  end
end

#namesObject



35
36
37
# File 'lib/kubert/pods.rb', line 35

def names
  pods.map(&:metadata).map(&:name)
end

#status(pod_status) ⇒ Object



30
31
32
33
# File 'lib/kubert/pods.rb', line 30

def status(pod_status)
  @pods = pods.select {|pod| pod.status.phase.downcase == pod_status.to_s }
  self
end