Skip to content

Commit

Permalink
fix: pod Containers tab may spin forever
Browse files Browse the repository at this point in the history
This can happen for pods that aren't ready yet, where there is a spec.containers but no status.containerStatuses.
This PR also fixes a similar issue with the Pod summary view.
  • Loading branch information
starpit committed Feb 9, 2023
1 parent 5abb502 commit 34378be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
15 changes: 11 additions & 4 deletions plugins/plugin-kubectl/src/lib/view/modes/Containers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@
*/

import type { ModeRegistration, Tab } from '@kui-shell/core'
import { KubeContainerStatus, Pod, isPod } from '@kui-shell/plugin-kubectl-core'
import { KubeResource, KubeContainerStatus, Pod, isPod } from '@kui-shell/plugin-kubectl-core'

async function content(_: Tab, resource: Pod) {
// this module is expensive to load, so we defer that expense
const { dump } = await import('js-yaml')

const { containers = [] } = resource.spec
const { containerStatuses = [] } = resource.status

// helps with unified just below
const statuses = resource.status.containerStatuses.reduce((M, _) => {
const statuses = containerStatuses.reduce((M, _) => {
const status = Object.assign({}, _)
delete status.name // no need to say this twice, once for spec, once for status
M[_.name] = status
return M
}, {} as Record<string, KubeContainerStatus>)

// unify spec and status
const unified = resource.spec.containers.reduce((M, _) => {
const unified = containers.reduce((M, _) => {
const combo = Object.assign(
{
args: _.args,
Expand All @@ -50,13 +53,17 @@ async function content(_: Tab, resource: Pod) {
}
}

function hasContainers(resource: KubeResource): resource is Pod {
return isPod(resource) && resource.spec.containers && resource.spec.containers.length > 0
}

/**
* The Summary mode applies to all KubeResources, and uses
* `renderContent` to render the view.
*
*/
const logsReg: ModeRegistration<Pod> = {
when: isPod,
when: hasContainers,
mode: {
mode: 'containers',
label: 'Containers',
Expand Down
22 changes: 13 additions & 9 deletions plugins/plugin-kubectl/src/lib/view/modes/Summary/impl/Pod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ import { age, none } from './Generic'
import toDescriptionList from './convert'

function ready(pod: Pod) {
const { containerStatuses } = pod.status
const { containerStatuses = [] } = pod.status

const numerator = !containerStatuses
? 0
: containerStatuses.reduce((count, status) => count + (status.ready ? 1 : 0), 0)
const numerator = containerStatuses.reduce((count, status) => count + (status.ready ? 1 : 0), 0)
const denominator = containerStatuses.length

return `${numerator}/${denominator}`
}

function restarts(pod: Pod) {
const { containerStatuses } = pod.status
const { containerStatuses = [] } = pod.status
return containerStatuses.reduce((count, status) => count + status.restartCount, 0)
}

Expand All @@ -59,14 +57,20 @@ function readinessGates(pod: Pod) {
export default function PodSummary(pod: Pod) {
const { spec, status } = pod

const model = {
const model: Record<string, number | boolean | string> = {
// Name: metadata.name,
Ready: ready(pod),
// Status: status.phase,
Restarts: restarts(pod),
Age: age(pod),
IP: status.podIP,
Node: spec.nodeName
Age: age(pod)
}

if (status.podIP) {
model.IP = status.podIP
}

if (spec.nodeName) {
model.Node = spec.nodeName
}

if (spec.readinessGates) {
Expand Down

0 comments on commit 34378be

Please sign in to comment.