Skip to content

Commit d9d58ba

Browse files
authored
add bridge (#18)
1 parent 27223dc commit d9d58ba

25 files changed

+849
-20
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Ignore artifacts:
22
dist/
33
node_modules/
4-
types/
4+
types/
5+
bridge/

bridge/environment.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env sh
2+
3+
BRIDGE_CLUSTER_ENDPOINT=$(oc whoami --show-server)
4+
export BRIDGE_CLUSTER_ENDPOINT
5+
6+
BRIDGE_CLUSTER_THANOS=$(oc -n openshift-config-managed get configmap monitoring-shared-config -o jsonpath='{.data.thanosPublicURL}')
7+
export BRIDGE_CLUSTER_THANOS
8+
9+
BRIDGE_CLUSTER_ALERTMANAGER=$(oc -n openshift-config-managed get configmap monitoring-shared-config -o jsonpath='{.data.alertmanagerPublicURL}')
10+
export BRIDGE_CLUSTER_ALERTMANAGER
11+
12+
BRIDGE_AUTH_BEARER_TOKEN=$(oc whoami --show-token)
13+
export BRIDGE_AUTH_BEARER_TOKEN

bridge/index.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env node
2+
3+
const express = require('express');
4+
const morgan = require('morgan');
5+
const { createProxyMiddleware } = require('http-proxy-middleware');
6+
const { Command, Option } = require('commander');
7+
const program = new Command();
8+
9+
// Parse user variables
10+
program
11+
.addOption(new Option('-p, --port <number>', 'proxy server port number').default(9090).env('BRIDGE_PORT'))
12+
.addOption(new Option('-u, --host <host>', 'proxy server host name').default('0.0.0.0').env('BRIDGE_HOST'))
13+
.addOption(new Option('-s, --staticFiles <dir>', 'the root directory from which to serve static assets').default('./docs'))
14+
.addOption(new Option('-e, --clusterEndpoint <endpoint>', 'drink size').makeOptionMandatory(true).env('BRIDGE_CLUSTER_ENDPOINT'))
15+
.addOption(new Option('-t, --clusterThanos <thanos>', 'drink size').makeOptionMandatory(true).env('BRIDGE_CLUSTER_THANOS'))
16+
.addOption(new Option('-a, --authBearerToken <auth>', 'port number').makeOptionMandatory(true).env('BRIDGE_AUTH_BEARER_TOKEN'));
17+
18+
program.parse();
19+
const options = program.opts();
20+
21+
// Printout options
22+
console.log({...options, authBearerToken: 'hidden'})
23+
24+
// Create Express Server
25+
const app = express();
26+
app.disable("x-powered-by");
27+
28+
// Logging
29+
app.use(morgan('dev'));
30+
31+
// Info GET endpoint
32+
app.get('/info', (req, res) => {
33+
res.json({app:'kubevirt ui bridge', k8s_server: '', metrics_server: ''});
34+
});
35+
36+
// Proxy endpoints
37+
app.use('/api/kubernetes', createProxyMiddleware({
38+
target: options.clusterEndpoint,
39+
secure: false,
40+
changeOrigin: true,
41+
ws: true,
42+
headers: {Authorization:`Bearer ${options.authBearerToken}`},
43+
pathRewrite: {
44+
[`^/api/kubernetes`]: '',
45+
},
46+
}));
47+
48+
app.use('/api/prometheus', createProxyMiddleware({
49+
target: options.clusterThanos,
50+
secure: false,
51+
changeOrigin: true,
52+
pathRewrite: {
53+
[`^/api/prometheus`]: '',
54+
},
55+
}));
56+
57+
// Serve static files
58+
app.use(express.static(options.staticFiles));
59+
60+
// Start the Proxy
61+
app.listen(options.port, options.host, () => {
62+
console.log(`Starting kubevirt ui bridge ${options.host}:${options.port}`);
63+
console.log(`On localhost http://localhost:${options.port}`);
64+
});

bridge/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "bridge",
3+
"version": "1.0.0",
4+
"description": "bridge k8s and prometheus api into the dev server",
5+
"main": "index.js",
6+
"repository": "https://github.com/kubevirt-ui/kubevirt-components/bridge",
7+
"author": "[email protected]",
8+
"license": "Apache 2",
9+
"private": false,
10+
"dependencies": {
11+
"commander": "^9.0.0",
12+
"express": "^4.17.2",
13+
"http-proxy-middleware": "^2.0.2",
14+
"morgan": "^1.10.0"
15+
},
16+
"scripts": {
17+
"start": "node index.js"
18+
}
19+
}

0 commit comments

Comments
 (0)