Class: KubernetesDeploy::Pod

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

Defined Under Namespace

Classes: Container

Constant Summary collapse

TIMEOUT =
10.minutes

Constants inherited from KubernetesResource

KubernetesResource::DEBUG_RESOURCE_NOT_FOUND_MESSAGE, KubernetesResource::LOG_LINE_COUNT, KubernetesResource::STANDARD_TIMEOUT_MESSAGE, KubernetesResource::UNUSUAL_FAILURE_MESSAGE

Instance Attribute Summary

Attributes inherited from KubernetesResource

#context, #deploy_started, #name, #namespace, #type, #validation_error_msg

Instance Method Summary collapse

Methods inherited from KubernetesResource

build, #debug_message, #deploy_finished?, #deploy_method, #deploy_timed_out?, #fetch_events, #file_path, #id, #kubectl, #pretty_status, #report_status_to_statsd, #status, timeout, #timeout, #validate_definition, #validation_failed?

Constructor Details

#initialize(namespace:, context:, definition:, logger:, parent: nil, deploy_started: nil) ⇒ Pod

Returns a new instance of Pod.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 6

def initialize(namespace:, context:, definition:, logger:, parent: nil, deploy_started: nil)
  @parent = parent
  @deploy_started = deploy_started
  @containers = definition.fetch("spec", {}).fetch("containers", []).map { |c| Container.new(c) }
  unless @containers.present?
    logger.summary.add_paragraph("Rendered template content:\n#{definition.to_yaml}")
    raise FatalDeploymentError, "Template is missing required field spec.containers"
  end
  @containers += definition["spec"].fetch("initContainers", []).map { |c| Container.new(c, init_container: true) }
  super(namespace: namespace, context: context, definition: definition, logger: logger)
end

Instance Method Details

#deploy_failed?Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 48

def deploy_failed?
  return true if @phase == "Failed"
  @containers.any?(&:doomed?)
end

#deploy_succeeded?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 40

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

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  @found
end

#failure_messageObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 64

def failure_message
  doomed_containers = @containers.select(&:doomed?)
  return unless doomed_containers.present?
  container_messages = doomed_containers.map do |c|
    red_name = ColorizedString.new(c.name).red
    "> #{red_name}: #{c.doom_reason}"
  end

  intro = if unmanaged?
    "The following containers encountered errors:"
  else
    "The following containers are in a state that is unlikely to be recoverable:"
  end
  intro + "\n" + container_messages.join("\n") + "\n"
end

#fetch_logsObject

Returns a hash in the following format:

"app" => ["array of log lines", "received from app container"],
"nginx" => ["array of log lines", "received from nginx container"]



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 85

def fetch_logs
  return {} unless exists? && @containers.present?
  @containers.each_with_object({}) do |container, container_logs|
    cmd = [
      "logs",
      @name,
      "--container=#{container.name}",
      "--since-time=#{@deploy_started.to_datetime.rfc3339}",
    ]
    cmd << "--tail=#{LOG_LINE_COUNT}" unless unmanaged?
    out, _err, _st = kubectl.run(*cmd)
    container_logs[container.name] = out.split("\n")
  end
end

#sync(pod_data = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 18

def sync(pod_data = nil)
  if pod_data.blank?
    raw_json, _err, st = kubectl.run("get", type, @name, "-a", "--output=json")
    pod_data = JSON.parse(raw_json) if st.success?
    raise_predates_deploy_error if pod_data.present? && unmanaged? && !@deploy_started
  end

  if pod_data.present?
    @found = true
    @phase = @status = pod_data["status"]["phase"]
    @status += " (Reason: #{pod_data['status']['reason']})" if pod_data['status']['reason'].present?
    @ready = ready?(pod_data["status"])
    update_container_statuses(pod_data["status"])
  else # reset
    @found = @ready = false
    @status = @phase = 'Unknown'
    @containers.each(&:reset_status)
  end

  display_logs if unmanaged? && deploy_succeeded?
end

#timeout_messageObject



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

def timeout_message
  return STANDARD_TIMEOUT_MESSAGE unless readiness_probe_failure?
  probe_failure_msgs = @containers.map(&:readiness_fail_reason).compact
  header = "The following containers have not passed their readiness probes on at least one pod:\n"
  header + probe_failure_msgs.join("\n") + "\n"
end