Class: Dapp::Kube::Kubernetes::Manager::Pod

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

Instance Attribute Summary

Attributes inherited from Base

#dapp, #name

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Dapp::Kube::Kubernetes::Manager::Base

Instance Method Details

#check_readiness_condition_if_available!(pod) ⇒ Object



12
13
14
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
# File 'lib/dapp/kube/kubernetes/manager/pod.rb', line 12

def check_readiness_condition_if_available!(pod)
  return if [nil, "True"].include? pod.ready_condition_status

  if pod.ready_condition['reason'] == 'ContainersNotReady'
    [*Array(pod.status["initContainerStatuses"]), *Array(pod.status["containerStatuses"])].each do |container_status|
      next if container_status['ready']

      waiting_reason = container_status.fetch('state', {}).fetch('waiting', {}).fetch('reason', nil)
      case waiting_reason
      when 'ImagePullBackOff', 'ErrImagePull'
        raise Kubernetes::Error::Default,
          code: :bad_image,
          data: {pod_name: pod.name,
                 reason: waiting_reason,
                 message: container_status['state']['waiting']['message']}
      when 'CrashLoopBackOff'
        raise Kubernetes::Error::Default,
          code: :container_crash,
          data: {pod_name: pod.name,
                 reason: waiting_reason,
                 message: container_status['state']['waiting']['message']}
      end
    end
  else
    dapp.with_log_indent do
      dapp.log_warning("#{dapp.log_time}Unknown pod readiness condition reason '#{pod.ready_condition['reason']}': #{pod.ready_condition}", stream: dapp.service_stream)
    end
  end
end

#containersObject



5
6
7
8
9
10
# File 'lib/dapp/kube/kubernetes/manager/pod.rb', line 5

def containers
  pod = Kubernetes::Client::Resource::Pod.new(dapp.kubernetes.pod(name))
  @containers ||= pod.containers_names.map do |container_name|
    Container.new(dapp, container_name, self)
  end
end

#wait_till_launched!Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dapp/kube/kubernetes/manager/pod.rb', line 42

def wait_till_launched!
  loop do
    pod = Kubernetes::Client::Resource::Pod.new(dapp.kubernetes.pod(name))

    break if pod.phase != "Pending"

    check_readiness_condition_if_available!(pod)

    sleep 0.1
  end
end

#watch_till_done!Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dapp/kube/kubernetes/manager/pod.rb', line 54

def watch_till_done!
  process_queue = containers.map {|c| [c, nil]}
  loop do
    container, last_processed_at = process_queue.shift
    break unless container

    sleep 1 if last_processed_at and (Time.now - last_processed_at < 1)

    container.watch_till_terminated!
    process_queue.push([container, Time.now]) unless container.done?
  end
end