-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2992 from christianvogt/model-reg-proxy
refactor model registry proxy onto proxy util
- Loading branch information
Showing
2 changed files
with
67 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,18 @@ | ||
import httpProxy from '@fastify/http-proxy'; | ||
import { KubeFastifyInstance } from '../../../../types'; | ||
import { DEV_MODE, MODEL_REGISTRY_NAMESPACE } from '../../../../utils/constants'; | ||
import { getParam, setParam } from '../../../../utils/proxy'; | ||
import { MODEL_REGISTRY_NAMESPACE } from '../../../../utils/constants'; | ||
import { proxyService } from '../../../../utils/proxy'; | ||
|
||
export default async (fastify: KubeFastifyInstance): Promise<void> => { | ||
fastify.register(httpProxy, { | ||
upstream: '', | ||
prefix: '/:name', | ||
rewritePrefix: '', | ||
replyOptions: { | ||
// preHandler must set the `upstream` param | ||
getUpstream: (request) => getParam(request, 'upstream'), | ||
}, | ||
preHandler: (request, _, done) => { | ||
const name = getParam(request, 'name'); | ||
|
||
const upstream = DEV_MODE | ||
? // Use port forwarding for local development: | ||
// kubectl port-forward -n <namespace> svc/<service-name> <local.port>:<service.port> | ||
`http://${process.env.MODEL_REGISTRY_SERVICE_HOST}:${process.env.MODEL_REGISTRY_SERVICE_PORT}` | ||
: // Construct service URL | ||
`http://${name}.${MODEL_REGISTRY_NAMESPACE}.svc.cluster.local:8080`; | ||
|
||
// assign the `upstream` param so we can dynamically set the upstream URL for http-proxy | ||
setParam(request, 'upstream', upstream); | ||
|
||
fastify.log.info(`Proxy ${request.method} request ${request.url} to ${upstream}`); | ||
done(); | ||
}, | ||
}); | ||
}; | ||
export default proxyService( | ||
null, | ||
{ | ||
port: 8080, | ||
namespace: MODEL_REGISTRY_NAMESPACE, | ||
}, | ||
{ | ||
// Use port forwarding for local development: | ||
// kubectl port-forward -n odh-model-registries svc/<service-name> 8085:8080 | ||
host: process.env.MODEL_REGISTRY_SERVICE_HOST, | ||
port: process.env.MODEL_REGISTRY_SERVICE_PORT, | ||
}, | ||
null, | ||
false, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters