Class: Dapp::Kube::Kubernetes::Manager::Container

Inherits:
Base
  • Object
show all
Defined in:
lib/dapp/kube/kubernetes/manager/container.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#dapp, #name

Instance Method Summary collapse

Constructor Details

#initialize(dapp, name, pod_manager) ⇒ Container

Returns a new instance of Container.



7
8
9
10
11
12
13
14
# File 'lib/dapp/kube/kubernetes/manager/container.rb', line 7

def initialize(dapp, name, pod_manager)
  super(dapp, name)

  @pod_manager = pod_manager
  @processed_containers_ids = []
  @processed_log_till_time = nil
  @processed_log_timestamps = Set.new
end

Instance Attribute Details

#pod_managerObject (readonly)

Returns the value of attribute pod_manager.



5
6
7
# File 'lib/dapp/kube/kubernetes/manager/container.rb', line 5

def pod_manager
  @pod_manager
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dapp/kube/kubernetes/manager/container.rb', line 89

def done?
  pod = Kubernetes::Client::Resource::Pod.new(dapp.kubernetes.pod(pod_manager.name))
  container_state, container_state_data = pod.container_state(name)
  if container_state == 'terminated'
    failed = (container_state_data['exitCode'].to_i != 0)
    return true if pod.restart_policy == 'Never'
    return true if not failed and pod.restart_policy == 'OnFailure'
  end

  return false
end

#watch_till_terminated!Object



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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dapp/kube/kubernetes/manager/container.rb', line 16

def watch_till_terminated!
  pod = Kubernetes::Client::Resource::Pod.new(dapp.kubernetes.pod(pod_manager.name))
  _, container_state_data = pod.container_state(name)
  return if @processed_containers_ids.include? container_state_data['containerID']

  pod_manager.wait_till_launched!

  pod = Kubernetes::Client::Resource::Pod.new(dapp.kubernetes.pod(pod_manager.name))
  container_state, container_state_data = pod.container_state(name)

  dapp.log_process("Watch pod's '#{pod_manager.name}' container '#{name}' log") do
    loop do
      pod = Kubernetes::Client::Resource::Pod.new(dapp.kubernetes.pod(pod_manager.name))
      container_state, container_state_data = pod.container_state(name)

      if container_state == "waiting"
        if container_state_data["reason"] == "RunContainerError"
          raise Kubernetes::Error::Default, code: :container_stuck, data: {
            state_reason: container_state_data["reason"],
            state_message: container_state_data["message"],
            state: container_state,
            pod_name: pod_manager.name,
            container_name: name
          }
        else
          sleep 0.1
          next
        end
      end

      chunk_lines_by_time = {}
      begin
        chunk_lines_by_time = dapp.kubernetes.pod_log(pod_manager.name, container: name, timestamps: true, sinceTime: @processed_log_till_time)
          .lines
          .map(&:strip)
          .map do |line|
            timestamp, _, data = line.partition(' ')
            [timestamp, data]
          end
          .reject {|timestamp, _| @processed_log_timestamps.include? timestamp}
      rescue Kubernetes::Client::Error::Pod::ContainerCreating, Kubernetes::Client::Error::Pod::PodInitializing
        sleep 0.1
        next
      rescue Kubernetes::Client::Error::Base => err
        dapp.log_warning("#{dapp.log_time}Error while fetching pod's #{pod_manager.name} logs: #{err.message}", stream: dapp.service_stream)
        break
      end

      chunk_lines_by_time.each do |timestamp, data|
        dapp.log("[#{timestamp}] #{data}")
        @processed_log_timestamps.add timestamp
      end

      if container_state == 'terminated'
        failed = (container_state_data['exitCode'].to_i != 0)

        dapp.log_warning("".tap do |msg|
          msg << "Pod's '#{pod_manager.name}' container '#{name}' has been terminated unsuccessfuly: "
          msg << container_state_data.to_s
        end) if failed

        @processed_containers_ids << container_state_data['containerID']

        break
      end

      @processed_log_till_time = chunk_lines_by_time.last.first if chunk_lines_by_time.any?

      sleep 0.1 if chunk_lines_by_time.empty?
    end
  end # log_process
end