Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use annotation to fetch host for notebook routing #5

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'] ||

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we have service-mesh.opendatahub.io/public-gateway-host-external already in two places can we add it somewhere as a const?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so - the two references are found in the frontend/ and backend/ sections of the codebase respectively - and I don't think that the two share consts (from what I can see).

null
);
};