mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Initial port-forward implementation for services (#231)
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
1 parent
ec392c3828
commit
806d8c6716
7 files changed
+224
-10
No files matched your search
@@ -7,6 +7,7 @@ import { resourceApplierApi } from "./resource-applier-api"
|
||||
import { kubeconfigRoute } from "./routes/kubeconfig"
|
||||
import { metricsRoute } from "./routes/metrics"
|
||||
import { watchRoute } from "./routes/watch"
|
||||
import { portForwardRoute } from "./routes/port-forward"
|
||||
import { readFile } from "fs"
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
@@ -118,6 +119,9 @@ export class Router {
|
||||
// Metrics API
|
||||
this.router.add({ method: 'post', path: '/api/metrics' }, metricsRoute.routeMetrics.bind(metricsRoute))
|
||||
|
||||
// Port-forward API
|
||||
this.router.add({ method: 'post', path: '/api/services/{namespace}/{service}/port-forward/{port}' }, portForwardRoute.routeServicePortForward.bind(portForwardRoute))
|
||||
|
||||
// Helm API
|
||||
this.router.add({ method: 'get', path: '/api-helm/v2/charts' }, helmApi.listCharts.bind(helmApi))
|
||||
this.router.add({ method: 'get', path: '/api-helm/v2/charts/{repo}/{chart}' }, helmApi.getChart.bind(helmApi))
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
import { LensApiRequest } from "../router"
|
||||
import { LensApi } from "../lens-api"
|
||||
import { spawn, ChildProcessWithoutNullStreams } from "child_process"
|
||||
import { bundledKubectl } from "../kubectl"
|
||||
import { getFreePort } from "../port"
|
||||
import { shell } from "electron"
|
||||
import * as tcpPortUsed from "tcp-port-used"
|
||||
import logger from "../logger"
|
||||
|
||||
class PortForward {
|
||||
public static portForwards: PortForward[] = []
|
||||
|
||||
static getPortforward(forward: {clusterId: string; kind: string; name: string; namespace: string; port: string}) {
|
||||
return PortForward.portForwards.find((pf) => {
|
||||
return (
|
||||
pf.clusterId == forward.clusterId &&
|
||||
pf.kind == "service" &&
|
||||
pf.name == forward.name &&
|
||||
pf.namespace == forward.namespace &&
|
||||
pf.port == forward.port
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
public clusterId: string
|
||||
public process: ChildProcessWithoutNullStreams
|
||||
public kubeConfig: string
|
||||
public kind: string
|
||||
public namespace: string
|
||||
public name: string
|
||||
public port: string
|
||||
public localPort: number
|
||||
|
||||
constructor(obj: any) {
|
||||
Object.assign(this, obj)
|
||||
}
|
||||
|
||||
public async start() {
|
||||
this.localPort = await getFreePort(8000, 9999)
|
||||
const kubectlBin = await bundledKubectl.kubectlPath()
|
||||
const args = [
|
||||
"--kubeconfig", this.kubeConfig,
|
||||
"port-forward",
|
||||
"-n", this.namespace,
|
||||
`service/${this.name}`,
|
||||
`${this.localPort}:${this.port}`
|
||||
]
|
||||
|
||||
this.process = spawn(kubectlBin, args, {
|
||||
env: process.env
|
||||
})
|
||||
PortForward.portForwards.push(this)
|
||||
this.process.on("exit", () => {
|
||||
const index = PortForward.portForwards.indexOf(this)
|
||||
if (index > -1) {
|
||||
PortForward.portForwards.splice(index, 1)
|
||||
}
|
||||
})
|
||||
try {
|
||||
await tcpPortUsed.waitUntilUsed(this.localPort, 500, 3000)
|
||||
return true
|
||||
} catch (error) {
|
||||
this.process.kill()
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
public open() {
|
||||
shell.openExternal(`http://localhost:${this.localPort}`)
|
||||
}
|
||||
}
|
||||
|
||||
class PortForwardRoute extends LensApi {
|
||||
|
||||
public async routeServicePortForward(request: LensApiRequest) {
|
||||
const { params, response, cluster} = request
|
||||
|
||||
let portForward = PortForward.getPortforward({
|
||||
clusterId: cluster.id, kind: "service", name: params.service,
|
||||
namespace: params.namespace, port: params.port
|
||||
})
|
||||
if (!portForward) {
|
||||
logger.info(`Creating a new port-forward ${params.namespace}/${params.service}:${params.port}`)
|
||||
portForward = new PortForward({
|
||||
clusterId: cluster.id,
|
||||
kind: "service",
|
||||
namespace: params.namespace,
|
||||
name: params.service,
|
||||
port: params.port,
|
||||
kubeConfig: cluster.kubeconfigPath()
|
||||
})
|
||||
const started = await portForward.start()
|
||||
if (!started) {
|
||||
this.respondJson(response, {
|
||||
message: "Failed to open port-forward"
|
||||
}, 400)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
portForward.open()
|
||||
|
||||
this.respondJson(response, {})
|
||||
}
|
||||
}
|
||||
|
||||
export const portForwardRoute = new PortForwardRoute()
|
||||
Reference in new issue
Block a user