Skip to content
Closed
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
88 changes: 88 additions & 0 deletions frontend/__tests__/module/k8s/pods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,92 @@ describe('podRestarts', () => {

expect(restartCount).toBe(3);
});

it('includes app container restarts when a native sidecar is running', () => {
pod.spec = {
initContainers: [{ name: 'istio-proxy', restartPolicy: 'Always' }],
containers: [{ name: 'httpd' }],
};
pod.status.initContainerStatuses = [
{
name: 'istio-proxy',
restartCount: 0,
started: true,
ready: true,
state: {
running: {
startedAt: '2026-09-14T12:00:00Z',
},
},
},
];
pod.status.containerStatuses = [
{
name: 'httpd',
restartCount: 5,
state: {
waiting: {
reason: 'CrashLoopBackOff',
},
},
},
];

expect(podRestarts(pod)).toBe(5);
});

it('includes native sidecar restarts after initialization', () => {
pod.spec = {
initContainers: [{ name: 'istio-proxy', restartPolicy: 'Always' }],
containers: [{ name: 'httpd' }],
};
pod.status.initContainerStatuses = [
{
name: 'istio-proxy',
restartCount: 2,
started: true,
ready: true,
state: {
running: {
startedAt: '2026-09-14T12:00:00Z',
},
},
},
];
pod.status.containerStatuses = [
{
name: 'httpd',
restartCount: 3,
},
];

expect(podRestarts(pod)).toBe(5);
});

it('counts only init restarts while a native sidecar has not started', () => {
pod.spec = {
initContainers: [{ name: 'istio-proxy', restartPolicy: 'Always' }],
containers: [{ name: 'httpd' }],
};
pod.status.initContainerStatuses = [
{
name: 'istio-proxy',
restartCount: 1,
started: false,
state: {
waiting: {
reason: 'PodInitializing',
},
},
},
];
pod.status.containerStatuses = [
{
name: 'httpd',
restartCount: 4,
},
];

expect(podRestarts(pod)).toBe(1);
});
});
30 changes: 24 additions & 6 deletions frontend/public/module/k8s/pods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,37 @@ export const getVolumeMountsByPermissions = (pod: PodKind) => {
return _.values(m);
};

const isRestartableInitContainer = (pod: PodKind, containerName: string): boolean =>
pod.spec?.initContainers?.find((c) => c.name === containerName)?.restartPolicy === 'Always';

const sumRestarts = (statuses: ContainerStatus[]): number =>
statuses.reduce((restartCount, status) => restartCount + (status.restartCount || 0), 0);

export const podRestarts = (pod: PodKind): number => {
if (!pod || !pod.status) {
return 0;
}
const { initContainerStatuses = [], containerStatuses = [] } = pod.status;
const isInitializing = initContainerStatuses.some(({ state }) => {
return !state.terminated || state.terminated.exitCode !== 0;
// Match kubectl printPod(): a native sidecar (init container with restartPolicy
// Always that has started) does not keep the pod in the initializing state.
const isInitializing = initContainerStatuses.some((status) => {
if (status.state?.terminated?.exitCode === 0) {
return false;
}
if (isRestartableInitContainer(pod, status.name) && status.started) {
return false;
}
return true;
});
const toCheck = isInitializing ? initContainerStatuses : containerStatuses;
return toCheck.reduce(
(restartCount, status: ContainerStatus) => restartCount + status.restartCount,
0,

if (isInitializing) {
return sumRestarts(initContainerStatuses);
}

const sidecarStatuses = initContainerStatuses.filter((status) =>
isRestartableInitContainer(pod, status.name),
);
return sumRestarts(containerStatuses) + sumRestarts(sidecarStatuses);
};

export const podReadiness = (pod: PodKind): { readyCount: number; totalContainers: number } => {
Expand Down