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 28, 2024
1 parent 0c6256d commit ecbd421
Show file tree
Hide file tree
Showing 40 changed files with 352 additions and 297 deletions.
43 changes: 19 additions & 24 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, isKubeFastifyInstance } 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,7 @@ export default fp(async (fastify: FastifyInstance) => {

let currentToken;
try {
currentToken = await getCurrentToken(currentUser);
currentToken = await getCurrentToken();
} catch (e) {
currentToken = '';
fastify.log.error(e, 'Failed to retrieve current token');
Expand All @@ -46,25 +46,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 All @@ -83,19 +78,21 @@ export default fp(async (fastify: FastifyInstance) => {
});

// Initialize the watching of resources
initializeWatchedResources(fastify);
if (isKubeFastifyInstance(fastify)) {
initializeWatchedResources(fastify);

cleanupGPU(fastify).catch((e) =>
fastify.log.error(
`Unable to fully convert GPU to use accelerator profiles. ${
e.response?.body?.message || e.message || e
}`,
),
);
cleanupGPU(fastify).catch((e) =>
fastify.log.error(
`Unable to fully convert GPU to use accelerator profiles. ${
e.response?.body?.message || e.message || e
}`,
),
);
}
});

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 +110,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 +127,3 @@ const getCurrentToken = async (currentUser: User) => {
resolve(currentUser?.token || '');
}
});
};
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { KubeFastifyInstance, AcceleratorProfileKind } from '../../../types';
import { FastifyRequest } from 'fastify';
import createError from 'http-errors';
import { errorHandler } from '../../../utils';
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;
const { namespace } = fastify.kube;
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 @@ -36,11 +37,11 @@ export const postAcceleratorProfile = 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 (createError.isHttpError(e) && e.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: ${errorHandler(e)}` };
}
throw e;
}
Expand All @@ -50,8 +51,8 @@ export const deleteAcceleratorProfile = async (
fastify: KubeFastifyInstance,
request: FastifyRequest,
): Promise<{ success: boolean; error: string }> => {
const customObjectsApi = fastify.kube.customObjectsApi;
const namespace = fastify.kube.namespace;
const { customObjectsApi } = fastify.kube;
const { namespace } = fastify.kube;
const params = request.params as { acceleratorProfileName: string };

try {
Expand All @@ -66,11 +67,11 @@ 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 (createError.isHttpError(e) && e.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: ${errorHandler(e)}` };
}
throw e;
}
Expand All @@ -80,8 +81,8 @@ export const updateAcceleratorProfile = async (
fastify: KubeFastifyInstance,
request: FastifyRequest,
): Promise<{ success: boolean; error: string }> => {
const customObjectsApi = fastify.kube.customObjectsApi;
const namespace = fastify.kube.namespace;
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 +140,11 @@ 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 (createError.isHttpError(e) && e.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: ${errorHandler(e)}` };
}
throw e;
}
Expand Down
43 changes: 19 additions & 24 deletions backend/src/routes/api/accelerator-profiles/index.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import { secureAdminRoute } from '../../../utils/route-security';
import { FastifyReply, FastifyRequest } from 'fastify';
import {
deleteAcceleratorProfile,
postAcceleratorProfile,
updateAcceleratorProfile,
} from './acceleratorProfilesUtils';
import { secureAdminRoute } from '../../../utils/route-security';
import { KubeFastifyInstance } from '../../../types';

export default async (fastify: FastifyInstance): Promise<void> => {
export default async (fastify: KubeFastifyInstance): 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[]> => getBuildStatuses();
Loading

0 comments on commit ecbd421

Please sign in to comment.