Skip to content

Commit

Permalink
Update mlmd and pipelines service to use constructUrl for URL generat…
Browse files Browse the repository at this point in the history
…ion (#3177)

* Update mlmd and pipelines service to use constructUrl for URL generation

* Update constructUrl for mlmd and pipelines service
  • Loading branch information
Gkrumbach07 authored Sep 17, 2024
1 parent 0ad3e20 commit 5613032
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
3 changes: 1 addition & 2 deletions backend/src/routes/api/service/mlmd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export default proxyService<DSPipelineKind>(
plural: 'datasciencepipelinesapplications',
},
{
port: 8443,
prefix: 'ds-pipeline-md-',
constructUrl: (resource) => resource.status?.components.mlmdProxy.url,
},
{
// Use port forwarding for local development:
Expand Down
3 changes: 1 addition & 2 deletions backend/src/routes/api/service/pipelines/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export default proxyService<DSPipelineKind>(
plural: 'datasciencepipelinesapplications',
},
{
port: 8443,
prefix: 'ds-pipeline-',
constructUrl: (resource) => resource.status?.components.apiServer.url,
},
{
// Use port forwarding for local development:
Expand Down
10 changes: 10 additions & 0 deletions backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,16 @@ export type DSPipelineKind = K8sResourceCommon & {
};
status?: {
conditions?: K8sCondition[];
components: {
apiServer: {
externalUrl: string;
url: string;
};
mlmdProxy: {
externalUrl: string;
url: string;
};
};
};
};

Expand Down
27 changes: 17 additions & 10 deletions backend/src/utils/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isK8sStatus, passThroughResource } from '../routes/api/k8s/pass-through
import { DEV_MODE } from './constants';
import { createCustomError } from './requestUtils';
import { getAccessToken, getDirectCallOptions } from './directCallUtils';
import { EitherNotBoth } from '../typeHelpers';

export const getParam = <F extends FastifyRequest<any, any>>(req: F, name: string): string =>
(req.params as { [key: string]: string })[name];
Expand All @@ -25,16 +26,20 @@ const notFoundError = (kind: string, name: string, e?: any, overrideMessage?: st
404,
);
};

export const proxyService =
<K extends K8sResourceCommon = never>(
model: { apiGroup: string; apiVersion: string; plural: string; kind: string } | null,
service: {
port: number | string;
prefix?: string;
suffix?: string;
namespace?: string;
},
service: EitherNotBoth<
{
port: number | string;
prefix?: string;
suffix?: string;
namespace?: string;
},
{
constructUrl: (resource: K) => string;
}
>,
local: {
host: string;
port: number | string;
Expand Down Expand Up @@ -70,15 +75,17 @@ export const proxyService =
const namespace = service.namespace ?? getParam(request, 'namespace');
const name = getParam(request, 'name');

const doServiceRequest = () => {
const doServiceRequest = (resource?: K) => {
const scheme = tls ? 'https' : 'http';

const upstream = DEV_MODE
? // Use port forwarding for local development:
// kubectl port-forward -n <namespace> svc/<service-name> <local.port>:<service.port>
`${scheme}://${local.host}:${local.port}`
: // Construct service URL
`${scheme}://${service.prefix || ''}${name}${
service.constructUrl
? service.constructUrl(resource)
: `${scheme}://${service.prefix || ''}${name}${
service.suffix ?? ''
}.${namespace}.svc.cluster.local:${service.port}`;

Expand Down Expand Up @@ -108,7 +115,7 @@ export const proxyService =
request.headers.authorization = `Bearer ${token}`;
}

doServiceRequest();
doServiceRequest(resource);
} else {
done(notFoundError(model.kind, name, undefined, 'service unavailable'));
}
Expand Down

0 comments on commit 5613032

Please sign in to comment.