Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pod statuses as a struct, add RunningButNotReady state #492

Merged
merged 1 commit into from
Jun 17, 2024
Merged
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
67 changes: 45 additions & 22 deletions clients/js/packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ export interface StarshipClientI {
podPorts: PodPorts
};

export interface PodStatus {
phase: string;
ready: boolean;
restartCount: number;
reason?: string;
}

export class StarshipClient implements StarshipClientI {
ctx: StarshipContext;
version: string;
Expand All @@ -89,7 +96,10 @@ export class StarshipClient implements StarshipClientI {
config: StarshipConfig;
podPorts: PodPorts = defaultPorts;

private podStatuses = new Map<string, string>(); // To keep track of pod statuses
private podStatuses = new Map<string, PodStatus>(); // To keep track of pod statuses

// Define a constant for the restart threshold
private readonly RESTART_THRESHOLD = 5;

constructor(ctx: StarshipContext) {
this.ctx = deepmerge(defaultStarshipContext, ctx);
Expand Down Expand Up @@ -351,7 +361,7 @@ export class StarshipClient implements StarshipClientI {
public areAllPodsRunning(): boolean {
let allRunning = true;
this.podStatuses.forEach((status) => {
if (status !== 'Running') {
if (status.phase !== 'Running' || !status.ready) {
allRunning = false;
}
});
Expand All @@ -366,28 +376,36 @@ export class StarshipClient implements StarshipClientI {
podName,
'--no-headers',
'-o',
'custom-columns=:status.phase,:status.containerStatuses[*].ready,:status.containerStatuses[*].state.waiting.reason',
'custom-columns=:status.phase,:status.containerStatuses[*].ready,:status.containerStatuses[*].restartCount,:status.containerStatuses[*].state.waiting.reason',
...this.getArgs(),
], false, true).trim();

const [status, readyList, reason] = result.split(/\s+/);
const [phase, readyList, restartCount, reason] = result.split(/\s+/);
const ready = readyList.split(',').every(state => state === 'true');
const restarts = parseInt(restartCount, 10);

this.podStatuses.set(podName, { phase, ready, restartCount: restarts, reason });

if (status === 'Running' && ready) {
// this.log(`[${chalk.blue(podName)}]: ${chalk.green('RUNNING')}`);
this.podStatuses.set(podName, status);
} else if (status === 'Running' && !ready) {
this.podStatuses.set(podName, 'RunningButNotReady');
} else if (status === 'Terminating') {
// this.log(`[${chalk.blue(podName)}]: ${chalk.gray('TERMINATING')}`);
} else if (reason && (reason.includes('ImagePullBackOff') || reason.includes('ErrImagePull'))) {
// this.log(`${chalk.blue(podName)} failed due to ${chalk.red(reason)}. Exiting...`);
if (restarts > this.RESTART_THRESHOLD) {
this.log(`${chalk.red('Error:')} Pod ${podName} has restarted more than ${this.RESTART_THRESHOLD} times.`);
this.exit(1);
} else {
this.podStatuses.set(podName, 'Pending');
// this.log(`[${chalk.blue(podName)}]: ${chalk.red(status)}`);
// setTimeout(() => this.checkPodStatus(podName), 2500); // check every 2.5 seconds
}

// if (status === 'Running' && ready) {
// // this.log(`[${chalk.blue(podName)}]: ${chalk.green('RUNNING')}`);
// this.podStatuses.set(podName, status);
// } else if (status === 'Running' && !ready) {
// this.podStatuses.set(podName, 'RunningButNotReady');
// } else if (status === 'Terminating') {
// // this.log(`[${chalk.blue(podName)}]: ${chalk.gray('TERMINATING')}`);
// } else if (reason && (reason.includes('ImagePullBackOff') || reason.includes('ErrImagePull'))) {
// // this.log(`${chalk.blue(podName)} failed due to ${chalk.red(reason)}. Exiting...`);
// this.exit(1);
// } else {
// this.podStatuses.set(podName, 'Pending');
// // this.log(`[${chalk.blue(podName)}]: ${chalk.red(status)}`);
// // setTimeout(() => this.checkPodStatus(podName), 2500); // check every 2.5 seconds
// }
}

public async waitForPods(): Promise<void> {
Expand All @@ -413,12 +431,17 @@ export class StarshipClient implements StarshipClientI {
private displayPodStatuses(): void {
console.clear();
this.podStatuses.forEach((status, podName) => {
let statusColor = chalk.red(status);
if (status === 'Running') {
statusColor = chalk.green(status);
} else if (status === 'Terminating') {
statusColor = chalk.gray(status);
let statusColor;
if (status.phase === 'Running' && status.ready) {
statusColor = chalk.green(status.phase);
} else if (status.phase === 'Running' && !status.ready) {
statusColor = chalk.yellow('RunningButNotReady');
} else if (status.phase === 'Terminating') {
statusColor = chalk.gray(status.phase);
} else {
statusColor = chalk.red(status.phase);
}

console.log(`[${chalk.blue(podName)}]: ${statusColor}`);
});
}
Expand Down