From 47e43acb17cc8bc5541847a8da7a42c9e128c83a Mon Sep 17 00:00:00 2001 From: Cameron Garrison Date: Mon, 26 Jun 2023 11:35:16 -0400 Subject: [PATCH] feat: use annotation to fetch host for notebook routing (#5) * update existing annotation reference * use ns annotation in backend route fetching --- backend/src/routes/api/notebooks/utils.ts | 20 ++++----- backend/src/utils/notebookUtils.ts | 52 ++++++++--------------- frontend/src/api/k8s/routes.ts | 5 ++- 3 files changed, 30 insertions(+), 47 deletions(-) diff --git a/backend/src/routes/api/notebooks/utils.ts b/backend/src/routes/api/notebooks/utils.ts index 4951c30894..04fc2e8b27 100644 --- a/backend/src/routes/api/notebooks/utils.ts +++ b/backend/src/routes/api/notebooks/utils.ts @@ -10,7 +10,7 @@ import { getNamespaces, getNotebook, getRoute, - getGatewayRoute, + getServiceMeshGwHost, updateNotebook, } from '../../../utils/notebookUtils'; import { FastifyRequest } from 'fastify'; @@ -30,25 +30,23 @@ export const getNotebookStatus = async ( let newNotebook: Notebook; if (isRunning && !notebook?.metadata.annotations?.['opendatahub.io/link']) { const disableServiceMesh = getDashboardConfig().spec.dashboardConfig.disableServiceMesh; - let route: Route; + let host: string; if (!disableServiceMesh) { - route = await getGatewayRoute(fastify, 'istio-system', 'odh-gateway').catch((e) => { + host = await getServiceMeshGwHost(fastify, namespace).catch((e) => { fastify.log.warn(`Failed getting route ${notebookName}: ${e.message}`); return undefined; }); } else { - route = await getRoute(fastify, namespace, notebookName).catch((e) => { + const route = await getRoute(fastify, namespace, notebookName).catch((e) => { fastify.log.warn(`Failed getting route ${notebookName}: ${e.message}`); return undefined; }); + if (route) { + host = route.spec.host; + } } - if (route) { - newNotebook = await patchNotebookRoute( - fastify, - route.spec.host, - namespace, - notebookName, - ).catch((e) => { + if (host) { + newNotebook = await patchNotebookRoute(fastify, host, namespace, notebookName).catch((e) => { fastify.log.warn(`Failed patching route to notebook ${notebookName}: ${e.message}`); return notebook; }); diff --git a/backend/src/utils/notebookUtils.ts b/backend/src/utils/notebookUtils.ts index c1fe866181..8649352f51 100644 --- a/backend/src/utils/notebookUtils.ts +++ b/backend/src/utils/notebookUtils.ts @@ -21,6 +21,7 @@ import { getUserName, usernameTranslate } from './userUtils'; import { createCustomError } from './requestUtils'; import { PatchUtils, + V1Namespace, V1PersistentVolumeClaim, V1Role, V1RoleBinding, @@ -68,52 +69,33 @@ export const getRoute = async ( return kubeResponse.body as Route; }; -interface RouteListResponse { - apiVersion: string; - kind: string; - metadata: { - resourceVersion: string; - }; - items: Route[]; -} - -export const getGatewayRoute = async ( +export const getServiceMeshGwHost = async ( fastify: KubeFastifyInstance, namespace: string, - gatewayName: string, -): Promise => { - const selector = `maistra.io/gateway-name=${gatewayName}`; - const kubeResponse = await fastify.kube.customObjectsApi - .listNamespacedCustomObject( - 'route.openshift.io', - 'v1', - namespace, - 'routes', - undefined, - undefined, - undefined, - selector, - ) - .catch((res) => { - const e = res.response.body; - const error = createCustomError('Error getting Gateway Route', e.message, e.code); - fastify.log.error(error); - throw error; - }); +): Promise => { + const kubeResponse = await fastify.kube.coreV1Api.readNamespace(namespace).catch((res) => { + const e = res.response.body; + const error = createCustomError('Error getting Namespace', e.message, e.code); + fastify.log.error(error); + throw error; + }); const body = kubeResponse.body as unknown; - const typedResponse = body as RouteListResponse; + const typedResponse = body as V1Namespace; - if (typedResponse.items.length === 0) { + const annotations = typedResponse.metadata?.annotations; + + if (!annotations || !annotations['service-mesh.opendatahub.io/public-gateway-host-external']) { const error = createCustomError( - 'Route not found', - `Could not find Route with label: ${selector}`, + 'Annotation not found', + `Could not find annotation 'service-mesh.opendatahub.io/public-gateway-host-external' for namespace: ${namespace}`, 404, ); fastify.log.error(error); throw error; } - return typedResponse.items[0]; + + return annotations['service-mesh.opendatahub.io/public-gateway-host-external']; }; export const createRBAC = async ( diff --git a/frontend/src/api/k8s/routes.ts b/frontend/src/api/k8s/routes.ts index 69dfc44a66..7ce435125e 100644 --- a/frontend/src/api/k8s/routes.ts +++ b/frontend/src/api/k8s/routes.ts @@ -20,5 +20,8 @@ export const getServiceMeshGwHost = async (namespace: string): Promise({ model: NamespaceModel, queryOptions }); - return project?.metadata?.annotations?.['opendatahub.io/service-mesh-gw-host'] || null; + return ( + project?.metadata?.annotations?.['service-mesh.opendatahub.io/public-gateway-host-external'] || + null + ); };