Add last restart time to 'RESTARTS' column to 'kubectl get pods' output

This commit adds the last time since a container has restarted in a pod
to the 'RESTARTS' column to the 'kubectl get pods' output

Signed-off-by: Jordan Jacobelli <jordanjacobelli04@gmail.com>
This commit is contained in:
Jordan Jacobelli
2021-03-11 18:27:32 +01:00
parent 6404eda8de
commit ec4182d003

View File

@@ -90,7 +90,7 @@ func AddHandlers(h printers.PrintHandler) {
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
{Name: "Ready", Type: "string", Description: "The aggregate readiness state of this pod for accepting traffic."},
{Name: "Status", Type: "string", Description: "The aggregate status of the containers in this pod."},
{Name: "Restarts", Type: "integer", Description: "The number of times the containers in this pod have been restarted."},
{Name: "Restarts", Type: "string", Description: "The number of times the containers in this pod have been restarted and when the last container in this pod has restarted."},
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
{Name: "IP", Type: "string", Priority: 1, Description: apiv1.PodStatus{}.SwaggerDoc()["podIP"]},
{Name: "Node", Type: "string", Priority: 1, Description: apiv1.PodSpec{}.SwaggerDoc()["nodeName"]},
@@ -741,6 +741,7 @@ func printPod(pod *api.Pod, options printers.GenerateOptions) ([]metav1.TableRow
restarts := 0
totalContainers := len(pod.Spec.Containers)
readyContainers := 0
lastRestartDate := metav1.NewTime(time.Time{})
reason := string(pod.Status.Phase)
if pod.Status.Reason != "" {
@@ -762,6 +763,12 @@ func printPod(pod *api.Pod, options printers.GenerateOptions) ([]metav1.TableRow
for i := range pod.Status.InitContainerStatuses {
container := pod.Status.InitContainerStatuses[i]
restarts += int(container.RestartCount)
if container.LastTerminationState.Terminated != nil {
terminatedDate := container.LastTerminationState.Terminated.FinishedAt
if lastRestartDate.Before(&terminatedDate) {
lastRestartDate = terminatedDate
}
}
switch {
case container.State.Terminated != nil && container.State.Terminated.ExitCode == 0:
continue
@@ -793,6 +800,12 @@ func printPod(pod *api.Pod, options printers.GenerateOptions) ([]metav1.TableRow
container := pod.Status.ContainerStatuses[i]
restarts += int(container.RestartCount)
if container.LastTerminationState.Terminated != nil {
terminatedDate := container.LastTerminationState.Terminated.FinishedAt
if lastRestartDate.Before(&terminatedDate) {
lastRestartDate = terminatedDate
}
}
if container.State.Waiting != nil && container.State.Waiting.Reason != "" {
reason = container.State.Waiting.Reason
} else if container.State.Terminated != nil && container.State.Terminated.Reason != "" {
@@ -825,7 +838,12 @@ func printPod(pod *api.Pod, options printers.GenerateOptions) ([]metav1.TableRow
reason = "Terminating"
}
row.Cells = append(row.Cells, pod.Name, fmt.Sprintf("%d/%d", readyContainers, totalContainers), reason, int64(restarts), translateTimestampSince(pod.CreationTimestamp))
restartsStr := strconv.Itoa(restarts)
if !lastRestartDate.IsZero() {
restartsStr = fmt.Sprintf("%d (%s ago)", restarts, translateTimestampSince(lastRestartDate))
}
row.Cells = append(row.Cells, pod.Name, fmt.Sprintf("%d/%d", readyContainers, totalContainers), reason, restartsStr, translateTimestampSince(pod.CreationTimestamp))
if options.Wide {
nodeName := pod.Spec.NodeName
nominatedNodeName := pod.Status.NominatedNodeName