Class: KubernetesDeploy::Pod

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

Constant Summary collapse

TIMEOUT =
10.minutes
SUSPICIOUS_CONTAINER_STATES =
%w(ImagePullBackOff RunContainerError ErrImagePull CrashLoopBackOff).freeze

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

Instance Method Summary collapse

Methods inherited from KubernetesResource

build, #debug_message, #deploy_finished?, #deploy_method, #deploy_timed_out?, #failure_message, #fetch_events, #file_path, #id, #kubectl, #pretty_status, #status, timeout, #timeout, #timeout_message, #tpr?

Constructor Details

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

Returns a new instance of Pod.



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

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| c["name"] }
  unless @containers.present?
    logger.summary.add_paragraph("Rendered template content:\n#{definition.to_yaml}")
    raise FatalDeploymentError, "Template is missing required field spec.containers"
  end
  super(namespace: namespace, context: context, definition: definition, logger: logger)
end

Instance Method Details

#deploy_failed?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 46

def deploy_failed?
  @phase == "Failed"
end

#deploy_succeeded?Boolean

Returns:

  • (Boolean)


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

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

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  @found
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"]



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 59

def fetch_logs
  return {} unless exists? && @containers.present?
  @containers.each_with_object({}) do |container_name, 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
# 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?
  end

  if pod_data.present?
    @found = true
    interpret_pod_status_data(pod_data["status"], pod_data["metadata"]) # sets @phase, @status and @ready
    if @deploy_started
      log_suspicious_states(pod_data["status"].fetch("containerStatuses", []))
    end
  else # reset
    @found = false
    @phase = @status = nil
    @ready = false
  end
  display_logs if unmanaged? && deploy_succeeded?
end