Last active
April 8, 2020 06:27
-
-
Save jbbarth/20d26897cb2796031f8046a9247d1ebc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pod_status_from_spec(spec) | |
# translated from: https://github.com/kubernetes/kubernetes/blob/a9f6b93b62a98598913180c640ab044c85a6718b/pkg/printers/internalversion/printers.go#L695-L781 | |
# related issue: https://github.com/kubernetes/kubernetes/issues/22839 | |
metadata = spec.fetch("metadata") | |
pod_status = spec.fetch("status") | |
reason = pod_status.fetch("phase") | |
restarts = 0 | |
if pod_status["reason"] | |
# unsure about this, the go code is opaque | |
reason = pod_status["reason"] | |
end | |
initializing = false | |
pod_status.fetch("initContainerStatuses", []).each_with_index do |container, i| | |
restarts = container.fetch("restartCount") | |
state = container.fetch("state") | |
if state["terminated"] | |
next if state["terminated"]["exitCode"] == 0 | |
# initialization failed | |
if state["terinated"]["reason"].blank? | |
if state["terminated"]["signal"] != 0 | |
reason = "Init:Signal:#{state["terminated"]["signal"]}" | |
else | |
reason = "Init:ExitCode:#{state["terminated"]["exitCode"]}" | |
end | |
else | |
reason = "Init:#{state["terminated"]["reason"]}" | |
end | |
initializing = true | |
elsif state["waiting"] && state["waiting"]["reason"] && state["waiting"]["reason"] != "PodInitializing" | |
reason = "Init:#{state["waiting"]["reason"]}" | |
initializing = true | |
else | |
reason = "Init:#{i}/#{spec.fetch("initContainers").size}" | |
initializing = true | |
end | |
break | |
end | |
if !initializing | |
restarts = 0 | |
has_running = false | |
pod_status.fetch("containerStatuses", []).each_with_index do |container, i| | |
restarts += container["restartCount"].to_i | |
state = container.fetch("state") | |
if state["waiting"] && state["waiting"]["reason"].present? | |
reason = state["waiting"]["reason"] | |
elsif state["terminated"] | |
if state["terminated"]["reason"].present? | |
reason = state["terminated"]["reason"] | |
else | |
if state["terminated"]["signal"] != 0 | |
reason = "Signal:#{state["terminated"]["signal"]}" | |
else | |
reason = "ExitCode:#{state["terminated"]["exitCode"]}" | |
end | |
end | |
elsif container["ready"] && state["running"] | |
has_running = true | |
end | |
end | |
if reason == "Completed" && has_running | |
has_pod_ready_condition = pod_status.fetch("conditions", []).detect do |condition| | |
condition["type"] == "Ready" && condition["status"] == "True" | |
end | |
if has_pod_ready_condition | |
reason = "Running" | |
else | |
reason = "NotReady" | |
end | |
end | |
end | |
if metadata["deletionTimestamp"] | |
if pod_status["reason"] == "NodeLost" | |
reason = "Unknown" | |
else | |
reason = "Terminating" | |
end | |
end | |
return reason, restarts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment