Class: Dapp::Deployment::KubeBase

Inherits:
Object
  • Object
show all
Defined in:
lib/dapp/deployment/kube_base.rb

Direct Known Subclasses

KubeApp, KubeDeployment

Instance Method Summary collapse

Instance Method Details

#delete_pod!(name) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/dapp/deployment/kube_base.rb', line 13

def delete_pod!(name)
  deployment.kubernetes.delete_pod!(name)
  loop do
    break unless deployment.kubernetes.pod?(name)
    sleep 1
  end
end

#merge_kube_controller_spec(spec1, spec2) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dapp/deployment/kube_base.rb', line 74

def merge_kube_controller_spec(spec1, spec2)
  spec1.kube_in_depth_merge(spec2).tap do |spec|
    spec['spec']['template']['spec']['containers'] = begin
      containers1 = spec1['spec']['template']['spec']['containers']
      containers2 = spec2['spec']['template']['spec']['containers']
      containers2.map do |container2|
        if (container1 = containers1.find { |c| c['name'] == container2['name'] }).nil?
          container2
        else
          container1.kube_in_depth_merge(container2)
        end
      end
    end
  end
end

#pod_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/dapp/deployment/kube_base.rb', line 4

def pod_exist?(name)
  deployment.kubernetes.pod?(name, labelSelector: labelSelector)
end

#pod_succeeded?(name) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/dapp/deployment/kube_base.rb', line 8

def pod_succeeded?(name)
  return false unless pod_exist?(name)
  deployment.kubernetes.pod_status(name)['status']['phase'] == 'Succeeded'
end

#run_job!(spec, name) ⇒ Object



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
# File 'lib/dapp/deployment/kube_base.rb', line 21

def run_job!(spec, name)
  current_spec = deployment.kubernetes.create_pod!(spec)

  deployment.dapp.log_process(:pending) do
    loop do
      current_spec = deployment.kubernetes.pod_status(name)
      break if current_spec['status']['phase'] != 'Pending'
      unless current_spec['status']['containerStatuses'].nil?
        current_spec['status']['containerStatuses'].first['state'].each do |_, desc|
          if ['ErrImagePull', 'ImagePullBackOff'].include? desc['reason']
            raise Error::Deployment,
                  code: :bootstrap_image_not_found,
                  data: { reason: desc['reason'], message: desc['message'] }
          end
        end
      end
      sleep 1
    end
  end

  deployment.dapp.log_process(:running) do
    log_thread = Thread.new do
      begin
        deployment.kubernetes.pod_log(name, follow: true) { |chunk| puts chunk }
      rescue Kubernetes::Error::Timeout
        deployment.dapp.log_warning('Pod log: read timeout reached!')
      end
    end

    loop do
      current_spec = deployment.kubernetes.pod_status(name)
      break if current_spec['status']['phase'] != 'Running'
      sleep 1
    end

    sleep 1 # last chance to get a message
    log_thread.kill if log_thread.alive?

    phase = current_spec['status']['phase']
    if phase == 'Failed'
      current_spec['status']['containerStatuses'].first['state'].each do |status, desc|
        next if status == 'running'
        raise Error::Deployment,
              code: :bootstrap_command_failed,
              data: { reason: desc['reason'], message: desc['message'], exit_code: desc['exitCode'] }
      end
    end
    raise Error::Deployment,
          code: :bootstrap_failed,
          data: { phase: phase, message: current_spec['status']['message'] } unless phase == 'Succeeded'
  end
end