Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions source/code/plugin/in_kube_podinventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,38 @@ def parse_and_emit_records(podInventory, serviceList)
record["ContainerStatusReason"] = containerStatus[containerStatus.keys[0]]["reason"]
end
end

# Record the last state of the container. This may have information on why a container was killed.
begin
if !container["lastState"].nil? && container["lastState"].keys.length == 1
lastStateName = container["lastState"].keys[0]
lastStateObject = container["lastState"][lastStateName]
if !lastStateObject.is_a?(Hash)
raise "expected a hash object. This could signify a bug or a kubernetes API change"
end

if lastStateObject.key?("reason") && lastStateObject.key?("startedAt") && lastStateObject.key?("finishedAt")
newRecord = Hash.new
newRecord["lastState"] = lastStateName # get the name of the last state (ex: terminated)
newRecord["reason"] = lastStateObject["reason"] # (ex: OOMKilled)
newRecord["startedAt"] = lastStateObject["startedAt"] # (ex: 2019-07-02T14:58:51Z)
newRecord["finishedAt"] = lastStateObject["finishedAt"] # (ex: 2019-07-02T14:58:52Z)

# only write to the output field if everything previously ran without error
record["ContainerLastStatus"] = newRecord
else
record["ContainerLastStatus"] = Hash.new
end
else
record["ContainerLastStatus"] = Hash.new
end
rescue => errorStr
$log.warn "Failed in parse_and_emit_record pod inventory while processing ContainerLastStatus: #{errorStr}"
$log.debug_backtrace(errorStr.backtrace)
ApplicationInsightsUtility.sendExceptionTelemetry(errorStr)
record["ContainerLastStatus"] = Hash.new
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, you are assuming all these properties exist all the time. Is that true ? Can you please add check for existence ?

end

podRestartCount += containerRestartCount
records.push(record.dup)

Expand Down