Class: Vidar::K8s::Container
- Inherits:
-
Object
- Object
- Vidar::K8s::Container
- Defined in:
- lib/vidar/k8s/container.rb
Overview
Represents a single Kubernetes container and its current state.
Constant Summary collapse
- JOB_KIND =
"Job".freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#pod_name ⇒ Object
readonly
Returns the value of attribute pod_name.
-
#reason ⇒ Object
readonly
Returns the value of attribute reason.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
-
#deployed? ⇒ Boolean
True if the container is considered successfully deployed.
-
#initialize(data) ⇒ Container
constructor
A new instance of Container.
-
#istio? ⇒ Boolean
True if this is an istio-proxy sidecar container.
-
#job? ⇒ Boolean
True if this container belongs to a Job.
-
#name ⇒ String
Container name, falling back to pod name.
- #print ⇒ Object
-
#ready? ⇒ Boolean
True if container is ready.
-
#ready_and_running? ⇒ Boolean
True if the container is ready and running.
-
#running? ⇒ Boolean
True if container is running.
- #running_started_at ⇒ Object
-
#sidecar?(sidecar_names = ["istio-proxy"]) ⇒ Boolean
True if this container is a known sidecar.
-
#success? ⇒ Boolean
True if the container completed successfully.
-
#terminated? ⇒ Boolean
True if container has terminated (any reason).
-
#terminated_completed? ⇒ Boolean
True if container terminated successfully.
-
#terminated_error? ⇒ Boolean
True if container terminated with an error exit code.
- #terminated_finished_at ⇒ Object
-
#text_statuses ⇒ Array<String>
Two-element array with status label and detail.
- #to_text ⇒ Object
-
#unknown? ⇒ Boolean
True if container state is unknown.
-
#unschedulable? ⇒ Boolean
True if container reason is Unschedulable.
-
#waiting? ⇒ Boolean
True if container state is “waiting”.
Constructor Details
#initialize(data) ⇒ Container
Returns a new instance of Container.
11 12 13 14 15 16 17 18 19 |
# File 'lib/vidar/k8s/container.rb', line 11 def initialize(data) @data = data @state = data["state"] || {} @namespace = data["namespace"] @kind = data["kind"] @pod_name = data["pod_name"] @reason = data["reason"] @message = data["message"] end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/vidar/k8s/container.rb', line 7 def data @data end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
7 8 9 |
# File 'lib/vidar/k8s/container.rb', line 7 def kind @kind end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
7 8 9 |
# File 'lib/vidar/k8s/container.rb', line 7 def @message end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
7 8 9 |
# File 'lib/vidar/k8s/container.rb', line 7 def namespace @namespace end |
#pod_name ⇒ Object (readonly)
Returns the value of attribute pod_name.
7 8 9 |
# File 'lib/vidar/k8s/container.rb', line 7 def pod_name @pod_name end |
#reason ⇒ Object (readonly)
Returns the value of attribute reason.
7 8 9 |
# File 'lib/vidar/k8s/container.rb', line 7 def reason @reason end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
7 8 9 |
# File 'lib/vidar/k8s/container.rb', line 7 def state @state end |
Instance Method Details
#deployed? ⇒ Boolean
Returns true if the container is considered successfully deployed.
27 28 29 30 31 |
# File 'lib/vidar/k8s/container.rb', line 27 def deployed? return terminated? if job? ready? && running? end |
#istio? ⇒ Boolean
Returns true if this is an istio-proxy sidecar container.
141 142 143 |
# File 'lib/vidar/k8s/container.rb', line 141 def istio? name == "istio-proxy" end |
#job? ⇒ Boolean
Returns true if this container belongs to a Job.
130 131 132 |
# File 'lib/vidar/k8s/container.rb', line 130 def job? kind == JOB_KIND end |
#name ⇒ String
Returns container name, falling back to pod name.
22 23 24 |
# File 'lib/vidar/k8s/container.rb', line 22 def name data["name"] || pod_name end |
#print ⇒ Object
45 46 47 |
# File 'lib/vidar/k8s/container.rb', line 45 def print puts to_text end |
#ready? ⇒ Boolean
Returns true if container is ready.
86 87 88 |
# File 'lib/vidar/k8s/container.rb', line 86 def ready? data["ready"] end |
#ready_and_running? ⇒ Boolean
Returns true if the container is ready and running.
41 42 43 |
# File 'lib/vidar/k8s/container.rb', line 41 def ready_and_running? ready? && running? end |
#running? ⇒ Boolean
Returns true if container is running.
91 92 93 |
# File 'lib/vidar/k8s/container.rb', line 91 def running? !running_started_at.nil? end |
#running_started_at ⇒ Object
95 96 97 |
# File 'lib/vidar/k8s/container.rb', line 95 def running_started_at state.dig("running", "startedAt") end |
#sidecar?(sidecar_names = ["istio-proxy"]) ⇒ Boolean
Returns true if this container is a known sidecar.
136 137 138 |
# File 'lib/vidar/k8s/container.rb', line 136 def sidecar?(sidecar_names = ["istio-proxy"]) sidecar_names.include?(name.to_s) end |
#success? ⇒ Boolean
Returns true if the container completed successfully.
34 35 36 37 38 |
# File 'lib/vidar/k8s/container.rb', line 34 def success? return terminated_completed? if job? ready_and_running? end |
#terminated? ⇒ Boolean
Returns true if container has terminated (any reason).
100 101 102 |
# File 'lib/vidar/k8s/container.rb', line 100 def terminated? !state["terminated"].nil? end |
#terminated_completed? ⇒ Boolean
Returns true if container terminated successfully.
105 106 107 |
# File 'lib/vidar/k8s/container.rb', line 105 def terminated_completed? state.dig("terminated", "reason") == "Completed" || state.dig("terminated", "exitCode") == 0 end |
#terminated_error? ⇒ Boolean
Returns true if container terminated with an error exit code.
114 115 116 117 |
# File 'lib/vidar/k8s/container.rb', line 114 def terminated_error? exit_code = state.dig("terminated", "exitCode") state.dig("terminated", "reason") == "Error" || (!exit_code.nil? && exit_code != 0) end |
#terminated_finished_at ⇒ Object
109 110 111 |
# File 'lib/vidar/k8s/container.rb', line 109 def terminated_finished_at state.dig("terminated", "finishedAt") end |
#text_statuses ⇒ Array<String>
Returns two-element array with status label and detail.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/vidar/k8s/container.rb', line 58 def text_statuses if unschedulable? [ColorizedString["Unschedulable"].light_red, ColorizedString[].light_red] elsif running? if job? [ColorizedString["Running"].light_yellow, "Started at: #{running_started_at}"] elsif ready? [ColorizedString["Ready & Running"].light_green, "Started at: #{running_started_at}"] else [ColorizedString["Not ready"].light_red, "Started at: #{running_started_at}"] end elsif terminated_completed? [ColorizedString["Terminated/Completed"].light_green, terminated_finished_at ? "Finished at: #{terminated_finished_at}" : ""] elsif terminated_error? [ColorizedString["Terminated/Error"].light_red, ""] elsif waiting? [ColorizedString["Waiting"].light_yellow, ""] else [ColorizedString["Unknown"].light_yellow, state.empty? ? "" : state.inspect] end end |
#to_text ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/vidar/k8s/container.rb', line 49 def to_text parts = [] parts << namespace.to_s.ljust(20, " ") parts << name.to_s.ljust(35, " ") parts += text_statuses.map { |s| s.ljust(45, " ") } "| #{parts.join(" | ")} |" end |
#unknown? ⇒ Boolean
Returns true if container state is unknown.
120 121 122 |
# File 'lib/vidar/k8s/container.rb', line 120 def unknown? !unschedulable? && !running? && !terminated? && !waiting? end |
#unschedulable? ⇒ Boolean
Returns true if container reason is Unschedulable.
125 126 127 |
# File 'lib/vidar/k8s/container.rb', line 125 def unschedulable? reason == "Unschedulable" end |
#waiting? ⇒ Boolean
Returns true if container state is “waiting”.
81 82 83 |
# File 'lib/vidar/k8s/container.rb', line 81 def waiting? state["waiting"] end |