Skip to content

Commit

Permalink
wait-for-pods command
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed May 3, 2024
1 parent fa10a2f commit b00874f
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 10 deletions.
3 changes: 1 addition & 2 deletions clients/js/packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,5 @@
"inquirerer": "^1.9.0",
"js-yaml": "^4.1.0",
"minimist": "^1.2.8"
},
"gitHead": "f274802408d1984d5e2ba23bb260801b0c8db59a"
}
}
3 changes: 3 additions & 0 deletions clients/js/packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ async function main() {
case 'get-pods':
client.getPods();
break;
case 'wait-for-pods':
client.waitForPods();
break;
case 'port-pids':
client.printForwardPids();
break;
Expand Down
3 changes: 1 addition & 2 deletions clients/js/packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,5 @@
"devDependencies": {
"@types/js-yaml": "^4.0.9",
"@types/shelljs": "^0.8.15"
},
"gitHead": "f274802408d1984d5e2ba23bb260801b0c8db59a"
}
}
98 changes: 92 additions & 6 deletions clients/js/packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,19 @@ export class StarshipClient implements StarshipClientI {
config: StarshipConfig;
podPorts: PodPorts = defaultPorts;

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

constructor(ctx: StarshipContext) {
this.ctx = deepmerge(defaultStarshipContext, ctx);
// TODO add semver check against net
this.version = readAndParsePackageJson().version;
}

private exec(cmd: string[]): shell.ShellString {
private exec(cmd: string[], log: boolean = true, silent: boolean = false): shell.ShellString {
this.checkDependencies();
const str = cmd.join(' ');
this.log(str);
return shell.exec(str);
if (log) this.log(str);
return shell.exec(str, { silent });
}

private log(str: string): void {
Expand Down Expand Up @@ -306,9 +308,93 @@ export class StarshipClient implements StarshipClientI {
}

public getPods(): void {
this.exec([
"kubectl", "get pods --all-namespaces"
]);
this.exec([
"kubectl",
"get pods"
// "--all-namespaces"
]);
}

private getPodNames(): string[] {
const result = this.exec([
'kubectl',
'get',
'pods',
'--no-headers',
'-o',
'custom-columns=:metadata.name'
], false, true)

// Split the output by new lines and filter out any empty lines
const podNames = result.split('\n').filter(name => name.trim() !== '');

return podNames;
}

public areAllPodsRunning(): boolean {
let allRunning = true;
this.podStatuses.forEach((status) => {
if (status !== 'Running') {
allRunning = false;
}
});
return allRunning;
}

private checkPodStatus(podName: string): void {
const result = this.exec([
'kubectl',
'get',
'pods',
podName,
'--no-headers',
'-o',
'custom-columns=:status.phase,:status.containerStatuses[*].state.waiting.reason'
], false, true).trim();

const [status, reason] = result.split(' ');
this.podStatuses.set(podName, status);

if (status === 'Running') {
// this.log(`[${chalk.blue(podName)}]: ${chalk.green('RUNNING')}`);
} 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.log(`[${chalk.blue(podName)}]: ${chalk.red(status)}`);
// setTimeout(() => this.checkPodStatus(podName), 2500); // check every 2.5 seconds
}
}

public waitForPods(): void {
const podNames = this.getPodNames();

// Check the status of each pod retrieved
podNames.forEach(podName => {
this.checkPodStatus(podName);
});

this.displayPodStatuses();

if (!this.areAllPodsRunning()) {
setTimeout(() => this.waitForPods(), 2500)
}
}

private displayPodStatuses(): void {
this.exec(['clear'], false); // Clear the terminal for each update
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);
}

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

private forwardPort(chain: Chain, localPort: number, externalPort: number): void {
Expand Down
1 change: 1 addition & 0 deletions clients/js/packages/client/test-utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const createClient = () => {
client.checkDependencies();
ctx.commands.push(cmd.join(' '));
ctx.logs.push(cmd.join(' '));
return '';
};

// Overriding the log method
Expand Down

0 comments on commit b00874f

Please sign in to comment.