Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
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
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