forked from opendatahub-io/odh-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass-through.ts
98 lines (89 loc) · 3.01 KB
/
pass-through.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {
K8sResourceCommon,
K8sStatus,
KubeFastifyInstance,
OauthFastifyRequest,
} from '../../../types';
import { DEV_MODE } from '../../../utils/constants';
import { proxyCall, ProxyError, ProxyErrorType } from '../../../utils/httpUtils';
export type PassThroughData = {
method: string;
requestData?: string;
url: string;
};
export const isK8sStatus = (data: unknown): data is K8sStatus =>
(data as K8sStatus).kind === 'Status';
const passThroughCatch = (fastify: KubeFastifyInstance) => (error: Error) => {
let errorMessage = 'Unknown error';
if (error instanceof ProxyError) {
errorMessage = error.message || errorMessage;
switch (error.proxyErrorType) {
case ProxyErrorType.CALL_FAILURE:
fastify.log.error(`Kube parsing response error: ${errorMessage}`);
throw { code: 500, response: error };
case ProxyErrorType.HTTP_FAILURE:
fastify.log.error(`Kube request error: ${errorMessage}`);
throw { code: 500, response: error };
default:
// unhandled type, fall-through
}
} else if (!(error instanceof Error)) {
errorMessage = JSON.stringify(error);
}
fastify.log.error(`Unhandled error during Kube call: ${errorMessage}`);
throw error;
};
export const passThroughText = (
fastify: KubeFastifyInstance,
request: OauthFastifyRequest,
data: PassThroughData,
): Promise<string> => {
return (
proxyCall(fastify, request, data)
// TODO: is there bad text states that we want to error out on?
.then(([rawData]) => rawData) // noop intentionally until above inquiry is figured out
.catch(passThroughCatch(fastify))
);
};
export const passThroughResource = <T extends K8sResourceCommon>(
fastify: KubeFastifyInstance,
request: OauthFastifyRequest,
data: PassThroughData,
): Promise<T | K8sStatus> => {
return proxyCall(fastify, request, data)
.then(([rawData]) => {
let parsedData: T | K8sStatus;
try {
parsedData = JSON.parse(rawData);
} catch (e) {
if (rawData.trim() === '404 page not found') {
// API on k8s doesn't exist, generate a status.
parsedData = {
kind: 'Status',
apiVersion: 'v1',
status: 'Failure',
message: rawData,
reason: 'NotFound',
code: 404,
};
} else {
// Likely not JSON, print the error and return the content to the client
fastify.log.error(e, `Parsing response error: ${data}`);
throw { code: 500, response: data };
}
}
if (isK8sStatus(parsedData)) {
if (parsedData.status !== 'Success') {
fastify.log.warn(
`Unsuccessful status Object, ${
DEV_MODE ? JSON.stringify(parsedData, null, 2) : JSON.stringify(parsedData)
}`,
);
throw { code: parsedData.code, response: parsedData };
}
}
fastify.log.info('Successful request, returning data to caller.');
return parsedData;
})
.catch(passThroughCatch(fastify));
};