Skip to content

Commit

Permalink
first pass on merging eslint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
rsun19 committed Jun 18, 2024
1 parent c9ec3f9 commit a2e33eb
Show file tree
Hide file tree
Showing 30 changed files with 431 additions and 294 deletions.
28 changes: 12 additions & 16 deletions backend/src/plugins/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import fp from 'fastify-plugin';
import { FastifyInstance } from 'fastify';
import * as jsYaml from 'js-yaml';
import * as k8s from '@kubernetes/client-node';
import { errorHandler } from '../utils';
import { DEV_MODE } from '../utils/constants';
import { cleanupGPU, initializeWatchedResources } from '../utils/resourceUtils';
import { User } from '@kubernetes/client-node/dist/config_types';

const CONSOLE_CONFIG_YAML_FIELD = 'console-config.yaml';

Expand All @@ -30,7 +30,10 @@ export default fp(async (fastify: FastifyInstance) => {

let currentToken;
try {
currentToken = await getCurrentToken(currentUser);
if (currentUser === null) {
throw new Error('Current user is null');
}
currentToken = await getCurrentToken();
} catch (e) {
currentToken = '';
fastify.log.error(e, 'Failed to retrieve current token');
Expand All @@ -46,25 +49,20 @@ export default fp(async (fastify: FastifyInstance) => {
);
clusterID = (clusterVersion.body as { spec: { clusterID: string } }).spec.clusterID;
} catch (e) {
fastify.log.error(
e,
`Failed to retrieve cluster id: ${e.response?.body?.message || e.message}.`,
);
fastify.log.error(e, `Failed to retrieve cluster id: ${errorHandler(e)}.`);
}
let clusterBranding = 'okd';
try {
const consoleConfig = await coreV1Api
.readNamespacedConfigMap('console-config', 'openshift-console')
.then((result) => result.body);
if (consoleConfig?.data?.[CONSOLE_CONFIG_YAML_FIELD]) {
if (consoleConfig.data?.[CONSOLE_CONFIG_YAML_FIELD]) {
const consoleConfigData = jsYaml.load(consoleConfig.data[CONSOLE_CONFIG_YAML_FIELD]);
clusterBranding = consoleConfigData.customization?.branding || 'okd';
fastify.log.info(`Cluster Branding: ${clusterBranding}`);
}
} catch (e) {
fastify.log.error(
`Failed to retrieve console cluster info: ${e.response?.body?.message || e.message}`,
);
fastify.log.error(`Failed to retrieve console cluster info: ${errorHandler(e)}`);
}

fastify.decorate('kube', {
Expand Down Expand Up @@ -94,8 +92,8 @@ export default fp(async (fastify: FastifyInstance) => {
);
});

const getCurrentNamespace = async () => {
return new Promise((resolve, reject) => {
const getCurrentNamespace = async () =>
new Promise((resolve, reject) => {
if (currentContext === 'inClusterContext') {
fs.readFile(
'/var/run/secrets/kubernetes.io/serviceaccount/namespace',
Expand All @@ -113,10 +111,9 @@ const getCurrentNamespace = async () => {
resolve(currentContext.split('/')[0]);
}
});
};

const getCurrentToken = async (currentUser: User) => {
return new Promise((resolve, reject) => {
const getCurrentToken = async () =>
new Promise((resolve, reject) => {
if (currentContext === 'inClusterContext') {
const location =
currentUser?.authProvider?.config?.tokenFile ||
Expand All @@ -131,4 +128,3 @@ const getCurrentToken = async (currentUser: User) => {
resolve(currentUser?.token || '');
}
});
};
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { KubeFastifyInstance, AcceleratorProfileKind } from '../../../types';
import { FastifyRequest } from 'fastify';
import createError from 'http-errors';
import { KubeFastifyInstance, AcceleratorProfileKind } from '../../../types';
import { translateDisplayNameForK8s } from '../../../utils/resourceUtils';

export const postAcceleratorProfile = async (
fastify: KubeFastifyInstance,
request: FastifyRequest,
): Promise<{ success: boolean; error: string }> => {
const customObjectsApi = fastify.kube.customObjectsApi;
const namespace = fastify.kube.namespace;
const customObjectsApi = fastify.kube?.customObjectsApi;
const namespace = fastify.kube?.namespace;
const body = request.body as AcceleratorProfileKind['spec'];

const payload: AcceleratorProfileKind = {
apiVersion: 'dashboard.opendatahub.io/v1',
kind: 'AcceleratorProfile',
metadata: {
name: translateDisplayNameForK8s(body.displayName),
namespace: namespace,
namespace,
annotations: {
'opendatahub.io/modified-date': new Date().toISOString(),
},
Expand All @@ -25,22 +25,38 @@ export const postAcceleratorProfile = async (
};

try {
if (typeof customObjectsApi === 'undefined') {
throw new Error('customObjectsApi is undefined');
}
await customObjectsApi
.createNamespacedCustomObject(
'dashboard.opendatahub.io',
'v1',
namespace,
namespace ?? '',
'acceleratorprofiles',
payload,
)
.catch((e) => {
throw createError(e.statusCode, e?.body?.message);
});
return { success: true, error: null };
return { success: true, error: '' };
} catch (e) {
if (e.response?.statusCode !== 404) {
if (
typeof e === 'object' &&
!!e &&
'message' in e &&
!!e.message &&
typeof e.message === 'string' &&
'response' in e &&
!!e.response &&
typeof e.response === 'object' &&
'statusCode' in e.response &&
!!e.response.statusCode &&
typeof e.response.statusCode === 'number' &&
e.response.statusCode !== 404
) {
fastify.log.error(e, 'Unable to add accelerator profile.');
return { success: false, error: 'Unable to add accelerator profile: ' + e.message };
return { success: false, error: `Unable to add accelerator profile: ${e.message}` };
}
throw e;
}
Expand All @@ -50,8 +66,11 @@ export const deleteAcceleratorProfile = async (
fastify: KubeFastifyInstance,
request: FastifyRequest,
): Promise<{ success: boolean; error: string }> => {
const customObjectsApi = fastify.kube.customObjectsApi;
const namespace = fastify.kube.namespace;
if (typeof fastify.kube === 'undefined') {
throw new Error('fastify.kube is not defined.');
}
const { customObjectsApi } = fastify.kube;
const { namespace } = fastify.kube;
const params = request.params as { acceleratorProfileName: string };

try {
Expand All @@ -66,11 +85,24 @@ export const deleteAcceleratorProfile = async (
.catch((e) => {
throw createError(e.statusCode, e?.body?.message);
});
return { success: true, error: null };
return { success: true, error: '' };
} catch (e) {
if (e.response?.statusCode === 404) {
if (
typeof e === 'object' &&
!!e &&
'message' in e &&
!!e.message &&
typeof e.message === 'string' &&
'response' in e &&
!!e.response &&
typeof e.response === 'object' &&
'statusCode' in e.response &&
!!e.response.statusCode &&
typeof e.response.statusCode === 'number' &&
e.response.statusCode !== 404
) {
fastify.log.error(e, 'Unable to delete accelerator profile.');
return { success: false, error: 'Unable to delete accelerator profile: ' + e.message };
return { success: false, error: `Unable to delete accelerator profile: ${e.message}` };
}
throw e;
}
Expand All @@ -80,8 +112,11 @@ export const updateAcceleratorProfile = async (
fastify: KubeFastifyInstance,
request: FastifyRequest,
): Promise<{ success: boolean; error: string }> => {
const customObjectsApi = fastify.kube.customObjectsApi;
const namespace = fastify.kube.namespace;
if (typeof fastify.kube === 'undefined') {
throw new Error('fastify.kube is undefined');
}
const { customObjectsApi } = fastify.kube;
const { namespace } = fastify.kube;
const params = request.params as { acceleratorProfileName: string };
const body = request.body as Partial<AcceleratorProfileKind['spec']>;

Expand Down Expand Up @@ -139,11 +174,24 @@ export const updateAcceleratorProfile = async (
.catch((e) => {
throw createError(e.statusCode, e?.body?.message);
});
return { success: true, error: null };
return { success: true, error: '' };
} catch (e) {
if (e.response?.statusCode !== 404) {
if (
typeof e === 'object' &&
!!e &&
'message' in e &&
!!e.message &&
typeof e.message === 'string' &&
'response' in e &&
!!e.response &&
typeof e.response === 'object' &&
'statusCode' in e.response &&
!!e.response.statusCode &&
typeof e.response.statusCode === 'number' &&
e.response.statusCode !== 404
) {
fastify.log.error(e, 'Unable to update accelerator profile.');
return { success: false, error: 'Unable to update accelerator profile: ' + e.message };
return { success: false, error: `Unable to update accelerator profile: ${e.message}` };
}
throw e;
}
Expand Down
38 changes: 16 additions & 22 deletions backend/src/routes/api/accelerator-profiles/index.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import { secureAdminRoute } from '../../../utils/route-security';
import {
deleteAcceleratorProfile,
postAcceleratorProfile,
updateAcceleratorProfile,
} from './acceleratorProfilesUtils';
import { secureAdminRoute } from '../../../utils/route-security';

export default async (fastify: FastifyInstance): Promise<void> => {
fastify.delete(
'/:acceleratorProfileName',
secureAdminRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) => {
return deleteAcceleratorProfile(fastify, request)
.then((res) => {
return res;
})
secureAdminRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) =>
deleteAcceleratorProfile(fastify, request)
.then((res) => res)
.catch((res) => {
reply.send(res);
});
}),
}),
),
);

fastify.put(
'/:acceleratorProfileName',
secureAdminRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) => {
return updateAcceleratorProfile(fastify, request)
.then((res) => {
return res;
})
secureAdminRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) =>
updateAcceleratorProfile(fastify, request)
.then((res) => res)
.catch((res) => {
reply.send(res);
});
}),
}),
),
);

fastify.post(
'/',
secureAdminRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) => {
return postAcceleratorProfile(fastify, request)
.then((res) => {
return res;
})
secureAdminRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) =>
postAcceleratorProfile(fastify, request)
.then((res) => res)
.catch((res) => {
reply.send(res);
});
}),
}),
),
);
};
2 changes: 1 addition & 1 deletion backend/src/routes/api/accelerators/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KubeFastifyInstance, OauthFastifyRequest } from '../../../types';
import { getDetectedAccelerators } from './acceleratorUtils';
import { KubeFastifyInstance, OauthFastifyRequest } from '../../../types';
import { logRequestDetails } from '../../../utils/fileUtils';

export default async (fastify: KubeFastifyInstance): Promise<void> => {
Expand Down
14 changes: 6 additions & 8 deletions backend/src/routes/api/builds/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { KubeFastifyInstance } from '../../../types';
import { FastifyReply, FastifyRequest } from 'fastify';
import { listBuilds } from './list';
import { KubeFastifyInstance } from '../../../types';
import { secureRoute } from '../../../utils/route-security';

module.exports = async (fastify: KubeFastifyInstance) => {
fastify.get(
'/',
secureRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) => {
return listBuilds()
.then((res) => {
return res;
})
secureRoute(fastify)(async (request: FastifyRequest, reply: FastifyReply) =>
listBuilds()
.then((res) => res)
.catch((res) => {
reply.send(res);
});
}),
}),
),
);
};
4 changes: 1 addition & 3 deletions backend/src/routes/api/builds/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { getBuildStatuses } from '../../../utils/resourceUtils';
import { BuildStatus } from '../../../types';

export const listBuilds = async (): Promise<BuildStatus[]> => {
return Promise.resolve(getBuildStatuses());
};
export const listBuilds = async (): Promise<BuildStatus[]> => Promise.resolve(getBuildStatuses());
Loading

0 comments on commit a2e33eb

Please sign in to comment.