Skip to content

Commit

Permalink
format starship js client
Browse files Browse the repository at this point in the history
  • Loading branch information
Anmol1696 committed Oct 4, 2024
1 parent 38e1f85 commit cf703bf
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
2 changes: 1 addition & 1 deletion clients/js/packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const params: string[] = [
'repoUrl',
'chart',
'namespace',
'timeout',
'timeout'
];

export const loadConfig = (argv: any): Config => {
Expand Down
93 changes: 92 additions & 1 deletion clients/js/packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,35 @@ export class StarshipClient implements StarshipClientI {
{ log: false, silent: true }
).trim();

const [phase, readyList, restartCountList, reason] = result.split(/\s+/);
// Ensure the output contains valid fields to split
const parts = result.split(/\s+/);
if (parts.length < 3) {
this.log(
chalk.red(`Unexpected pod status output for ${podName}: ${result}`)
);
return;
}

const [phase, readyList, restartCountList, reason] = parts;

// Validate readyList and restartCountList before applying split
if (!readyList || !restartCountList) {
this.log(
chalk.red(
`Invalid ready or restart count for pod ${podName}: ${result}`
)
);
return;
}

const ready = readyList.split(',').every((state) => state === 'true');
const restarts = restartCountList
.split(',')
.reduce((acc, count) => acc + parseInt(count, 10), 0);

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

this.podStatuses.set(podName, {
phase,
ready,
Expand Down Expand Up @@ -572,6 +595,74 @@ export class StarshipClient implements StarshipClientI {
});
}

public checkImagePullFailures(podName: string): void {
// Fetch events from kubectl describe for the given pod
const eventLines = this.getPodEventsFromDescribe(podName);
const errorPattern = /Failed to pull image/;
const imageErrors: { [image: string]: number } = {};

// Parse through event lines to identify image pull failures
eventLines.forEach((line) => {
const message = line || '';
if (errorPattern.test(message)) {
const imageMatch = message.match(/image "(.*?)"/);
if (imageMatch && imageMatch[1]) {
const imageName = imageMatch[1];
imageErrors[imageName] = (imageErrors[imageName] || 0) + 1;
}
}
});

// Log errors for images that have failed more than twice
Object.entries(imageErrors).forEach(([imageName, errorCount]) => {
if (errorCount >= 3) {
this.log(
`${chalk.red(
`
Error: Image '${imageName}' failed to pull ${errorCount} times for pod ${podName}.
Please check the image name and ensure it is correct.
Run "starship stop" to stop the deployment which would be in stuck state.
`
)}`
);
this.exit(1);
}
});
}

private getPodEventsFromDescribe(podName: string): string[] {
// Execute the 'kubectl describe pod' command
const result = this.exec(
['kubectl', 'describe', 'pod', podName, ...this.getArgs()],
{ log: false, silent: true }
);

// Check if the command was successful
if (result.code !== 0) {
this.log(
chalk.red(`Failed to describe pod ${podName}: ${result.stderr}`)
);
return [];
}

const describeOutput = result.stdout;

// Extract the 'Events' section from the describe output
const eventsSection = describeOutput.split('Events:')[1];
if (!eventsSection) {
this.log(chalk.yellow(`No events found for pod ${podName}`));
return [];
}

// Split the events section into individual lines
const eventLines = eventsSection
.split('\n')
.filter((line) => line.trim() !== '');
this.log(`event lints: ${eventLines.join('\n')}`);

return eventLines;
}

private forwardPort(
chain: Chain,
localPort: number,
Expand Down

0 comments on commit cf703bf

Please sign in to comment.