src/Formats/PodV1.Format.ps1xml

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
    <ViewDefinitions>
        <View>
            <!-- Resembles the default output of kubectl get pod -->
            <Name>Default</Name>
            <ViewSelectedBy>
                <TypeName>KubeClient.Models.PodV1</TypeName>
            </ViewSelectedBy>
            <TableControl>
                <TableHeaders>
                    <TableColumnHeader>
                        <Label>Name</Label>
                    </TableColumnHeader>
                    <TableColumnHeader>
                        <Label>Ready</Label>
                    </TableColumnHeader>
                    <TableColumnHeader>
                        <Label>Status</Label>
                    </TableColumnHeader>
                    <TableColumnHeader>
                        <Label>Restarts</Label>
                    </TableColumnHeader>
                    <TableColumnHeader>
                        <Label>Age</Label>
                    </TableColumnHeader>
                </TableHeaders>
                <TableRowEntries>
                    <TableRowEntry>
                        <TableColumnItems>
                            <TableColumnItem>
                                <PropertyName>Name</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <ScriptBlock>"$(($_.Status.ContainerStatuses | Where-Object {$_.Ready} | Measure-Object).Count)/$($_.Status.ContainerStatuses.Count)"</ScriptBlock>
                            </TableColumnItem>
                            <!-- Cannot define this as Type extension because Status is a real property -->
                            <TableColumnItem>
                                <ScriptBlock>
                                    $pod = $_
                                    $reason = $pod.Status.Phase
                                    if ($pod.Status.Reason) {
                                        $reason = $pod.Status.Reason
                                    }
                                    $initializing = $false
                                    $i = 0
                                    foreach ($container in $pod.Status.InitContainerStatuses) {
                                        if ($container.State.Terminated -and $container.State.Terminated.ExitCode -eq 0) {
                                            $i++
                                            continue
                                        }
                                        if ($container.State.Terminated) {
                                            # initialization is failed
                                            if ($container.State.Terminated.Reason) {
                                                if ($container.State.Terminated.Signal -ne 0) {
                                                    $reason = "Init:Signal:" + $container.State.Terminated.Signal
                                                } else {
                                                    $reason = "Init:ExitCode:" + $container.State.Terminated.ExitCode
                                                }
                                            } else {
                                                $reason = "Init:" + $container.State.Terminated.Reason
                                            }
                                            $initializing = $true
                                        } elseif ($container.State.Waiting -and $container.State.Waiting.Reason -and $container.State.Waiting.Reason -ne "PodInitializing") {
                                            $reason = "Init:" + $container.State.Waiting.Reason
                                            $initializing = $true
                                        } else {
                                            $reason = "Init:$i/$($pod.Spec.InitContainers.Count)"
                                            $initializing = $true
                                        }
                                        break
                                    }
                                    if (-not $initializing) {
                                        $restarts = 0
                                        $hasRunning = $false
                                        for ($i = $pod.Status.ContainerStatuses.Count - 1; $i -ge 0; $i--) {
                                            $container = $pod.Status.ContainerStatuses[$i]
                                            $restarts += $container.RestartCount
                                            if ($container.State.Waiting -and $container.State.Waiting.Reason) {
                                                Write-Information "here"
                                                $reason = $container.State.Waiting.Reason
                                            } elseif ($container.State.Terminated -and $container.State.Terminated.Reason) {
                                                $reason = $container.State.Terminated.Reason
                                            } elseif ($container.State.Terminated -and (-not $container.State.Terminated.Reason)) {
                                                if ($container.State.Terminated.Signal -ne 0) {
                                                    $reason = "Signal:" + $container.State.Terminated.Signal
                                                } else {
                                                    $reason = "ExitCode:" + $container.State.Terminated.ExitCode
                                                }
                                            } elseif ($container.Ready -and $container.State.Running) {
                                                $hasRunning = $true
                                                $readyContainers++
                                            }
                                        }

                                        # change pod status back to "Running" if there is at least one container still reporting as "Running" status
                                        if ($reason -eq "Completed" -and $hasRunning) {
                                            $reason = "Running"
                                        }
                                    }
                                    if ($pod.DeletionTimestamp -and $pod.Status.Reason -eq "NodeLost") {
                                        $reason = "Unknown"
                                    } elseif ($pod.DeletionTimestamp) {
                                        $reason = "Terminating"
                                    }
                                    $reason
                                </ScriptBlock>
                            </TableColumnItem>
                            <TableColumnItem>
                                <PropertyName>Restarts</PropertyName>
                            </TableColumnItem>
                            <TableColumnItem>
                                <PropertyName>Age</PropertyName>
                                <FormatString>%d\D\ hh\h\ mm\m\ ss\s</FormatString>
                                <Alignment>Right</Alignment>
                            </TableColumnItem>
                        </TableColumnItems>
                    </TableRowEntry>
                </TableRowEntries>
            </TableControl>
        </View>
    </ViewDefinitions>
</Configuration>