Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ yarn-error.log*
.env.development
.env.production
.env.test

.CLAUDE.md
# Runtime data
pids
*.pid
Expand Down
93 changes: 20 additions & 73 deletions src/common/interfaces/client.interface.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
import { PodStatus } from "./podstatus.interface";



export interface DiagnoseRequest {
podName: string;
namespace: string;
logs: string[];
events?: string[];
phase?: string;
containerState?: PodStatus; // refine if needed
}

export interface CredentialsFile {
authenticated: boolean;
token: string;
user_id: string;
first_name?: string;
}

export interface HelmReleaseInfo {
releaseName: string;
confidence: number;
}
// Re-export from helm-release-resolver for consistency
export type { HelmReleaseInfo, HelmReleaseEvidence } from '../../core/helm-release-resolver';

export interface StackComponent {
podName: string;
Expand All @@ -30,60 +10,27 @@ export interface StackComponent {
logs: string[];
}

export interface StackAnalysisPayload {
primaryPod: string;
helmRelease: string;
namespace: string;
timestamp: string;
components: StackComponent[];
}


export interface StackDiagnosisResult {
export interface StackAnalysisResponse {
stackOverview: {
summary: string;
components: Array<{
name: string;
role: string;
status: 'healthy' | 'degraded' | 'down';
restartCount: number;
issues: string[];
}>;
totalPods: number;
failingPods: number;
healthyPods: number;
namespace: string;
helmRelease: string;
};
rootCauseAnalysis: {
primaryCause: {
component: string;
issue: string;
evidence: string[];
startTime: string | null;
containerState: string;
};
failureCascade: Array<{
step: number;
component: string;
effect: string;
evidence: string;
timestamp: string;
}>;
};
recommendations: {
immediateFix: {
description: string;
commands: string[];
actions: string[];
};
preventRecurrence: {
description: string;
helmValues: Record<string, any>;
configChanges: string[];
};
improvements: string[];
};
verification: {
commands: Array<{
description: string;
command: string;
expectedOutput: string;
}>;
healthyLogPatterns: string[];
primaryCause: string;
confidence: number;
evidence: string[];
};
debuggingCommands: Array<{
description: string;
command: string;
}>;
confidence: number;
alternativeCauses: string[];
recommendations: string[];
analysis: string;
}
1 change: 1 addition & 0 deletions src/common/interfaces/containerStatus.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface ContainerStatusSummary {
type: 'init' | 'main';
state: string;
reason?: string;
message?: string;
}
124 changes: 19 additions & 105 deletions src/core/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { promisify } from "util";
import { gzip } from "zlib";
import axios from 'axios';
import { printErrorAndExit } from "../utils/utils";
import { DiagnoseRequest, HelmReleaseInfo } from "../common/interfaces/client.interface";
import { StackAnalysisPayload, StackAnalysisResponse } from "../common/interfaces/client.interface";
import { DEFAULT_API_URL } from "./config";
import { TokenStorage } from "./token-storage";

Expand All @@ -12,80 +10,27 @@ interface RefreshTokenResponseDto {
expiresIn: number;
}

const gzipAsync = promisify(gzip);

export async function runFurtherDiagnosis(payload: DiagnoseRequest): Promise<any> {
try {
const compressed = await gzipAsync(Buffer.from(JSON.stringify(payload)));

const tokenStorage = new TokenStorage();
const token = await tokenStorage.getValidAccessToken();
if (!token) {
console.warn('No valid authentication token available. Please ensure cluster is registered.');
}

const response = await axios.post(`${DEFAULT_API_URL}/diagnose`, compressed, {
headers: {
'Content-Type': 'application/json',
'Content-Encoding': 'gzip',
Authorization: `Bearer ${token}`,
},
});

return response.data;
} catch (error: any) {
printErrorAndExit(error.response?.data.message ?? 'External request failed');
}
}

export async function parsePodManifest(manifest: any): Promise<HelmReleaseInfo> {
try {
const tokenStorage = new TokenStorage();
const token = await tokenStorage.getValidAccessToken();
if (!token) {
console.warn('No valid authentication token available. Please ensure cluster is registered.');
}

const response = await fetch(`${DEFAULT_API_URL}/diagnose/parse-pod-manifest`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(manifest),
});

return await response.json() as HelmReleaseInfo;
} catch (error) {
printErrorAndExit('Error parsing pod manifest');
throw error; // This will never execute but satisfies TypeScript
}
}

export async function runStackAnalysis(compressedPayload: Buffer): Promise<string> {
export async function runStackAnalysis(payload: StackAnalysisPayload): Promise<StackAnalysisResponse | null> {
try {
const tokenStorage = new TokenStorage();
const token = await tokenStorage.getValidAccessToken();
if (!token) {
console.warn('No valid authentication token available. Please ensure cluster is registered.');
}

const response = await axios.post(
`${DEFAULT_API_URL}/diagnose/analyze-stack`,
compressedPayload,
{
headers: {
'Content-Type': 'application/json',
'Content-Encoding': 'gzip',
Authorization: `Bearer ${token}`,
return await tokenStorage.makeAuthenticatedRequest(async (token) => {
const response = await axios.post(
`${DEFAULT_API_URL}/daemon/analyze-stack`,
payload,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
},
},
);
);

return response.data.analysis || response.data; // depends on backend response shape
return response.data as StackAnalysisResponse;
});
} catch (error: any) {
printErrorAndExit(error.response?.data.message ?? 'Failed to analyze stack');
throw error; // This will never execute but satisfies TypeScript
console.error(`❌ Failed to analyze stack: ${error.response?.data?.message || error.message}`);
return null;
}
}

Expand Down Expand Up @@ -115,7 +60,6 @@ export async function getUserClusterTokens(
orgId: string
): Promise<any> {
try {

const response = await axios.get(
`${DEFAULT_API_URL}/auth/cluster/tokens?clusterId=${clusterId}&orgId=${orgId}`,
{
Expand All @@ -128,13 +72,13 @@ export async function getUserClusterTokens(
return response.data;
} catch (error: any) {
console.error(`📡 getUserClusterTokens error:`, error.response?.data || error.message);
}
}
}

export async function getDaemonInfo(): Promise<any> {
try {
const tokenStorage = new TokenStorage();

return await tokenStorage.makeAuthenticatedRequest(async (token) => {
const response = await axios.get(
`${DEFAULT_API_URL}/daemon/me`,
Expand All @@ -147,38 +91,8 @@ export async function getDaemonInfo(): Promise<any> {
);
return response.data;
});
} catch (error: any) {
console.error(error.response?.data.message ?? 'Failed to get daemon info');
return null;
}
}

export async function reportPodFailure(failureData: {
podName: string;
namespace: string;
logs: string[];
events?: string[];
phase?: string;
containerState?: any;
}): Promise<any> {
try {
const tokenStorage = new TokenStorage();

return await tokenStorage.makeAuthenticatedRequest(async (token) => {
const response = await axios.post(
`${DEFAULT_API_URL}/daemon/diagnose-pod`,
failureData,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
}
);
return response.data;
});
} catch (error: any) {
console.error(`❌ Failed to report pod failure: ${error.response?.data?.message || error.message}`);
console.error(error.response?.data.message ?? 'Failed to get daemon info');
return null;
}
}
Loading