Class: KubernetesDeploy::Pod
- Inherits:
-
KubernetesResource
- Object
- KubernetesResource
- KubernetesDeploy::Pod
- 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
- #deploy_failed? ⇒ Boolean
- #deploy_succeeded? ⇒ Boolean
- #exists? ⇒ Boolean
- #failure_message ⇒ Object
-
#fetch_logs ⇒ Object
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”] }.
-
#initialize(namespace:, context:, definition:, logger:, parent: nil, deploy_started: nil) ⇒ Pod
constructor
A new instance of Pod.
- #sync(pod_data = nil) ⇒ Object
- #timeout_message ⇒ Object
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, #tpr?, #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
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
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
53 54 55 |
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 53 def exists? @found end |
#failure_message ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 69 def doomed_containers = @containers.select(&:doomed?) return unless doomed_containers.present? = 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" + .join("\n") + "\n" end |
#fetch_logs ⇒ Object
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"]
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 90 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_message ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kubernetes-deploy/kubernetes_resource/pod.rb', line 57 def return if unmanaged? return unless @phase == "Running" && !@ready pieces = ["Your pods are running, but the following containers seem to be failing their readiness probes:"] @containers.each do |c| next if c.init_container? || c.ready? yellow_name = ColorizedString.new(c.name).yellow pieces << "> #{yellow_name} must respond with a good status code at '#{c.probe_location}'" end pieces.join("\n") + "\n" end |