Class: KubernetesDeploy::Pod

Inherits:
KubernetesResource show all
Defined in:
lib/kubernetes-deploy/kubernetes_resource/pod.rb

Constant Summary collapse

TIMEOUT =
15.minutes
SUSPICIOUS_CONTAINER_STATES =
%w(ImagePullBackOff RunContainerError).freeze

Instance Attribute Summary

Attributes inherited from KubernetesResource

#context, #deploy_started, #file, #name, #namespace, #type

Instance Method Summary collapse

Methods inherited from KubernetesResource

#deploy_finished?, #deploy_timed_out?, for_type, #id, #log_status, #run_kubectl, #status, #status_data, timeout, #timeout, #tpr?

Constructor Details

#initialize(name, namespace, context, file, parent: nil) ⇒ Pod

Returns a new instance of Pod.



6
7
8
9
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 6

def initialize(name, namespace, context, file, parent: nil)
  @name, @namespace, @context, @file, @parent = name, namespace, context, file, parent
  @bare = !@parent
end

Instance Method Details

#deploy_failed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 57

def deploy_failed?
  @phase == "Failed"
end

#deploy_succeeded?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 49

def deploy_succeeded?
  if @bare
    @phase == "Succeeded"
  else
    @phase == "Running" && @ready
  end
end

#exists?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 61

def exists?
  @bare ? @found : true
end

#group_nameObject



65
66
67
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 65

def group_name
  @bare ? "Bare pods" : @parent
end

#interpret_json_data(pod_data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 25

def interpret_json_data(pod_data)
  @phase = (pod_data["metadata"]["deletionTimestamp"] ? "Terminating" : pod_data["status"]["phase"])
  @containers = pod_data["spec"]["containers"].map { |c| c["name"] }

  if @deploy_started && pod_data["status"]["containerStatuses"]
    pod_data["status"]["containerStatuses"].each do |status|
      waiting_state = status["state"]["waiting"] if status["state"]
      reason = waiting_state["reason"] if waiting_state
      next unless SUSPICIOUS_CONTAINER_STATES.include?(reason)
      KubernetesDeploy.logger.warn("#{id} has container in state #{reason} (#{waiting_state["message"]})")
    end
  end

  if @phase == "Failed"
    @status = "#{@phase} (Reason: #{pod_data["status"]["reason"]})"
  elsif @phase == "Terminating"
    @status = @phase
  else
    ready_condition = pod_data["status"].fetch("conditions", []).find { |condition| condition["type"] == "Ready" }
    @ready = ready_condition.present? && (ready_condition["status"] == "True")
    @status = "#{@phase} (Ready: #{@ready})"
  end
end

#syncObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 11

def sync
  out, st = run_kubectl("get", type, @name, "-a", "--output=json")
  if @found = st.success?
    pod_data = JSON.parse(out)
    interpret_json_data(pod_data)
  else # reset
    @status = @phase = nil
    @ready = false
    @containers = []
  end
  display_logs if @bare && deploy_finished?
  log_status
end