Skip to content

Commit

Permalink
feature: add waitForPodsTerminating on stop command, update related…
Browse files Browse the repository at this point in the history
… cli and test (#586)

* add waitForPodsTerminating on stop command, update related cli and test

* run lint fixes
  • Loading branch information
Anmol1696 authored Nov 8, 2024
1 parent 7bc28b0 commit ee89dd6
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 33 deletions.
5 changes: 4 additions & 1 deletion clients/js/packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ async function main() {
client.stopPortForward();
break;
case 'stop':
client.stop();
client.stop().catch((err: any) => {
console.error('An error occurred during stop:', err);
process.exit(1);
});
break;
case 'undeploy':
client.deleteHelm();
Expand Down
3 changes: 1 addition & 2 deletions clients/js/packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export const params: string[] = [
];

export const loadConfig = (argv: any): Config => {
console.log('argv: ', argv);
const context: StarshipContext = {
...defaultStarshipContext
} as StarshipContext;
Expand All @@ -52,7 +51,7 @@ export const loadConfig = (argv: any): Config => {
// Override context with command-line arguments dynamically based on StarshipContext keys
params.forEach((key) => {
if (argv[key] !== undefined) {
console.log('key: ', key, ' argv[key]: ', argv[key]);
console.log('argv:', key, ':', argv[key]);
// @ts-expect-error - dynamic assignment
context[key] = argv[key];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ Call: stop()
Log: Trying to stop all port-forward, if any....
ps -ef | grep -i 'kubectl port-forward' | grep -v 'grep' | awk '{print $2}'
sleep 2
helm delete osmojs"
kubectl get pods --no-headers -o custom-columns=:metadata.name
helm delete osmojs
kubectl get pods --no-headers -o custom-columns=:metadata.name
Log: All pods have been sucessfully terminated!"
`;

exports[`StarshipClient setup 2`] = `
Expand All @@ -59,5 +62,7 @@ kubectl port-forward service/registry 8081:8080 > /dev/null 2>&1 &
kubectl port-forward service/registry 9091:9090 > /dev/null 2>&1 &
ps -ef | grep -i 'kubectl port-forward' | grep -v 'grep' | awk '{print $2}'
sleep 2
helm delete osmojs"
kubectl get pods --no-headers -o custom-columns=:metadata.name
helm delete osmojs
kubectl get pods --no-headers -o custom-columns=:metadata.name"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ Call: stop()
Log: Trying to stop all port-forward, if any....
ps -ef | grep -i 'kubectl port-forward' | grep -v 'grep' | awk '{print $2}'
sleep 2
helm delete osmojs"
kubectl get pods --no-headers -o custom-columns=:metadata.name
helm delete osmojs
kubectl get pods --no-headers -o custom-columns=:metadata.name
Log: All pods have been sucessfully terminated!"
`;

exports[`StarshipClient setup 2`] = `
Expand All @@ -56,5 +59,7 @@ kubectl port-forward service/registry 8081:8080 > /dev/null 2>&1 &
kubectl port-forward service/registry 9091:9090 > /dev/null 2>&1 &
ps -ef | grep -i 'kubectl port-forward' | grep -v 'grep' | awk '{print $2}'
sleep 2
helm delete osmojs"
kubectl get pods --no-headers -o custom-columns=:metadata.name
helm delete osmojs
kubectl get pods --no-headers -o custom-columns=:metadata.name"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ Call: stop()
Log: Trying to stop all port-forward, if any....
ps -ef | grep -i 'kubectl port-forward' | grep -v 'grep' | awk '{print $2}'
sleep 2
helm delete osmojs"
kubectl get pods --no-headers -o custom-columns=:metadata.name
helm delete osmojs
kubectl get pods --no-headers -o custom-columns=:metadata.name
Log: All pods have been sucessfully terminated!"
`;

exports[`StarshipClient setup 2`] = `
Expand All @@ -57,5 +60,7 @@ kubectl port-forward service/registry 8081:8080 > /dev/null 2>&1 &
kubectl port-forward service/registry 9091:9090 > /dev/null 2>&1 &
ps -ef | grep -i 'kubectl port-forward' | grep -v 'grep' | awk '{print $2}'
sleep 2
helm delete osmojs"
kubectl get pods --no-headers -o custom-columns=:metadata.name
helm delete osmojs
kubectl get pods --no-headers -o custom-columns=:metadata.name"
`;
92 changes: 68 additions & 24 deletions clients/js/packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,11 @@ export class StarshipClient implements StarshipClientI {
]);
}

public stop(): void {
public async stop(): Promise<void> {
this.stopPortForward();
this.setPodStatues(); // set pod statues before deleting the helm
this.deleteHelm();
await this.waitForPodsTermination();
}

public async start(): Promise<void> {
Expand Down Expand Up @@ -495,7 +497,7 @@ export class StarshipClient implements StarshipClientI {
return allRunning;
}

private checkPodStatus(podName: string): void {
private checkPodStatus(podName: string, exitEarly: boolean = true): void {
const result = this.exec(
[
'kubectl',
Expand Down Expand Up @@ -537,7 +539,7 @@ export class StarshipClient implements StarshipClientI {
.reduce((acc, count) => acc + parseInt(count, 10), 0);

// check for repeated image pull errors
this.checkImagePullFailures(podName);
this.checkImagePullFailures(podName, exitEarly);

this.podStatuses.set(podName, {
phase,
Expand All @@ -550,10 +552,18 @@ export class StarshipClient implements StarshipClientI {
this.log(
`${chalk.red('Error:')} Pod ${podName} has restarted more than ${this.ctx.restartThreshold} times.`
);
this.exit(1);
if (exitEarly) this.exit(1);
}
}

private setPodStatues(): void {
const podNames = this.getPodNames();

podNames.forEach((podName) => {
this.checkPodStatus(podName, false); // set exitEarly to false, only set the this.PodStatuses
});
}

public async waitForPods(): Promise<void> {
const podNames = this.getPodNames();

Expand Down Expand Up @@ -581,6 +591,34 @@ export class StarshipClient implements StarshipClientI {
}
}

public async waitForPodsTermination(): Promise<void> {
const podNames = this.getPodNames();

// Remove pods that are no longer active from the podStatuses map
this.podStatuses.forEach((_value, podName) => {
if (!podNames.includes(podName)) {
this.podStatuses.delete(podName);
}
});

if (this.podStatuses.size === 0) {
this.log(chalk.green('All pods have been sucessfully terminated!'));
// once the pods are in done state, wait for 1 more seconds
await new Promise((resolve) => setTimeout(resolve, 1000));
return;
}

// Check the status of each pod to terminating
podNames.forEach((podName) => {
const podStatus = this.podStatuses.get(podName);
podStatus.phase = 'Terminating';
});
this.displayPodStatuses();

await new Promise((resolve) => setTimeout(resolve, 2500));
await this.waitForPodsTermination(); // Recursive call
}

private displayPodStatuses(): void {
console.clear();
this.podStatuses.forEach((status, podName) => {
Expand All @@ -590,7 +628,7 @@ export class StarshipClient implements StarshipClientI {
} else if (status.phase === 'Running' && !status.ready) {
statusColor = chalk.yellow('RunningButNotReady');
} else if (status.phase === 'Terminating') {
statusColor = chalk.gray(status.phase);
statusColor = chalk.red(status.phase);
} else {
statusColor = chalk.red(status.phase);
}
Expand All @@ -601,7 +639,10 @@ export class StarshipClient implements StarshipClientI {
});
}

public checkImagePullFailures(podName: string): void {
public checkImagePullFailures(
podName: string,
exitEarly: boolean = true
): void {
// Fetch events from kubectl describe for the given pod
const eventLines = this.getPodEventsFromDescribe(podName);
const errorPattern = /Failed to pull image/;
Expand Down Expand Up @@ -631,7 +672,7 @@ export class StarshipClient implements StarshipClientI {
`
)}`
);
this.exit(1);
if (exitEarly) this.exit(1);
}
});
}
Expand Down Expand Up @@ -840,21 +881,24 @@ export class StarshipClient implements StarshipClientI {
}

private getForwardPids(): string[] {
const result = this.exec([
'ps',
'-ef',
'|',
'grep',
'-i',
"'kubectl port-forward'",
'|',
'grep',
'-v',
"'grep'",
'|',
'awk',
"'{print $2}'"
]);
const result = this.exec(
[
'ps',
'-ef',
'|',
'grep',
'-i',
"'kubectl port-forward'",
'|',
'grep',
'-v',
"'grep'",
'|',
'awk',
"'{print $2}'"
],
{ log: false, silent: true }
);
const pids = (result || '')
.split('\n')
.map((pid) => pid.trim())
Expand All @@ -866,9 +910,9 @@ export class StarshipClient implements StarshipClientI {
this.log(chalk.green('Trying to stop all port-forward, if any....'));
const pids = this.getForwardPids();
pids.forEach((pid) => {
this.exec(['kill', '-15', pid]);
this.exec(['kill', '-15', pid], { log: false, silent: true });
});
this.exec(['sleep', '2']);
this.exec(['sleep', '2'], { log: false, silent: true });
}

public printForwardPids(): void {
Expand Down

0 comments on commit ee89dd6

Please sign in to comment.