Skip to content

Commit

Permalink
feat: use annotation to fetch host for notebook routing (#5)
Browse files Browse the repository at this point in the history
* update existing annotation reference

* use ns annotation in backend route fetching
  • Loading branch information
cam-garrison authored and bartoszmajsak committed Jun 28, 2023
1 parent 166e409 commit 47e43ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 47 deletions.
20 changes: 9 additions & 11 deletions backend/src/routes/api/notebooks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getNamespaces,
getNotebook,
getRoute,
getGatewayRoute,
getServiceMeshGwHost,
updateNotebook,
} from '../../../utils/notebookUtils';
import { FastifyRequest } from 'fastify';
Expand All @@ -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;
});
Expand Down
52 changes: 17 additions & 35 deletions backend/src/utils/notebookUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getUserName, usernameTranslate } from './userUtils';
import { createCustomError } from './requestUtils';
import {
PatchUtils,
V1Namespace,
V1PersistentVolumeClaim,
V1Role,
V1RoleBinding,
Expand Down Expand Up @@ -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<Route> => {
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<string> => {
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 (
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/api/k8s/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ export const getServiceMeshGwHost = async (namespace: string): Promise<string |
name: namespace,
};
const project = await k8sGetResource<NamespaceKind>({ 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
);
};

0 comments on commit 47e43ac

Please sign in to comment.