| 193 | } |
| 194 | |
| 195 | func getContainerProblem(client kubectl.Client, pod *v1.Pod, containerStatus *v1.ContainerStatus, ignoreContainerRestarts bool) *containerProblem { |
| 196 | tailLines := int64(50) |
| 197 | hasProblem := false |
| 198 | containerProblem := &containerProblem{ |
| 199 | Name: containerStatus.Name, |
| 200 | Restarts: int(containerStatus.RestartCount), |
| 201 | Ready: true, |
| 202 | } |
| 203 | |
| 204 | // Check if restarted |
| 205 | if containerStatus.RestartCount > 0 && !ignoreContainerRestarts { |
| 206 | if containerStatus.LastTerminationState.Terminated != nil && (time.Since(containerStatus.LastTerminationState.Terminated.FinishedAt.Time) < IgnoreRestartsSince) { |
| 207 | hasProblem = true |
| 208 | |
| 209 | containerProblem.LastRestart = time.Since(containerStatus.LastTerminationState.Terminated.FinishedAt.Time).Round(time.Second) |
| 210 | containerProblem.LastExitCode = int(containerStatus.LastTerminationState.Terminated.ExitCode) |
| 211 | containerProblem.LastMessage = containerStatus.LastTerminationState.Terminated.Message |
| 212 | containerProblem.LastExitReason = containerStatus.LastTerminationState.Terminated.Reason |
| 213 | |
| 214 | if containerProblem.LastExitCode != 0 { |
| 215 | containerProblem.LastFaultyExecutionLog, _ = client.ReadLogs(context.TODO(), pod.Namespace, pod.Name, containerStatus.Name, containerProblem.Ready, &tailLines) |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // Check if ready |
| 221 | if !containerStatus.Ready { |
| 222 | containerProblem.Ready = false |
| 223 | |
| 224 | if containerStatus.State.Terminated != nil { |
| 225 | containerProblem.Terminated = true |
| 226 | containerProblem.TerminatedAt = time.Since(containerStatus.State.Terminated.FinishedAt.Time).Round(time.Second) |
| 227 | containerProblem.Reason = containerStatus.State.Terminated.Reason |
| 228 | containerProblem.Message = containerStatus.State.Terminated.Message |
| 229 | |
| 230 | containerProblem.LastExitCode = int(containerStatus.State.Terminated.ExitCode) |
| 231 | if containerProblem.LastExitCode != 0 { |
| 232 | hasProblem = true |
| 233 | containerProblem.LastFaultyExecutionLog, _ = client.ReadLogs(context.TODO(), pod.Namespace, pod.Name, containerStatus.Name, false, &tailLines) |
| 234 | } |
| 235 | } else if containerStatus.State.Waiting != nil { |
| 236 | hasProblem = true |
| 237 | containerProblem.Waiting = true |
| 238 | containerProblem.Reason = containerStatus.State.Waiting.Reason |
| 239 | containerProblem.Message = containerStatus.State.Waiting.Message |
| 240 | |
| 241 | // when containerStatus=Waiting && RestartCount>0, print LastFaultyExecutionLog |
| 242 | if containerStatus.RestartCount > 0 { |
| 243 | containerProblem.LastExitCode = int(containerStatus.LastTerminationState.Terminated.ExitCode) |
| 244 | if containerProblem.LastExitCode != 0 { |
| 245 | containerProblem.LastFaultyExecutionLog, _ = client.ReadLogs(context.TODO(), pod.Namespace, pod.Name, containerStatus.Name, false, &tailLines) |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if hasProblem { |
| 252 | return containerProblem |