1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Added ability to add custom port on pod and service port forwarding

- pod-container-port fixed init async and active port checking
- service-port-component fixed init async and active port checking
- port-forward-route promise and response fix

Signed-off-by: rdeepc <12953177+rdeepc@users.noreply.github.com>
This commit is contained in:
rdeepc 2021-07-10 00:33:44 -04:00
parent a6b2a3972f
commit 91ab6bf3ce
3 changed files with 25 additions and 23 deletions

View File

@ -45,10 +45,10 @@ class PortForward {
static getPortforward(forward: PortForwardArgs) { static getPortforward(forward: PortForwardArgs) {
return PortForward.portForwards.find((pf) => ( return PortForward.portForwards.find((pf) => (
pf.clusterId == forward.clusterId && pf.clusterId == forward.clusterId &&
pf.kind == forward.kind && pf.kind == forward.kind &&
pf.name == forward.name && pf.name == forward.name &&
pf.namespace == forward.namespace && pf.namespace == forward.namespace &&
pf.port == forward.port pf.port == forward.port
)); ));
} }
@ -59,7 +59,7 @@ class PortForward {
public name: string; public name: string;
public port: string; public port: string;
public internalPort?: number; public internalPort?: number;
public forwardPort: string; public forwardPort?: string;
constructor(public kubeConfig: string, args: PortForwardArgs) { constructor(public kubeConfig: string, args: PortForwardArgs) {
this.clusterId = args.clusterId; this.clusterId = args.clusterId;
@ -156,7 +156,7 @@ export class PortForwardRoute {
portForward.open(); portForward.open();
respondJson(response, {port: portForward.forwardPort}); respondJson(response, {port: portForward.forwardPort});
} catch (e) { } catch (error) {
return respondJson(response, { return respondJson(response, {
message: `Failed to forward port ${port} to ${forwardPort}` message: `Failed to forward port ${port} to ${forwardPort}`
}, 400); }, 400);
@ -172,11 +172,7 @@ export class PortForwardRoute {
namespace, port, forwardPort namespace, port, forwardPort
}); });
let forwardedPort = -1; const forwardedPort = portForward?.forwardPort?? null;
if (portForward) {
forwardedPort = Number(portForward.forwardPort);
}
respondJson(response, {port: forwardedPort}); respondJson(response, {port: forwardedPort});
} }
@ -190,8 +186,11 @@ export class PortForwardRoute {
namespace, port, forwardPort, namespace, port, forwardPort,
}); });
portForward.stop().then( () => { try {
await portForward.stop();
respondJson(response, {status: true}); respondJson(response, {status: true});
}).catch(error => logger.error(error)); } catch (error) {
logger.error(error);
}
} }
} }

View File

@ -43,7 +43,7 @@ interface PortForwardResult {
@observer @observer
export class ServicePortComponent extends React.Component<Props> { export class ServicePortComponent extends React.Component<Props> {
@observable waiting = false; @observable waiting = false;
@observable forwardPort = -1; @observable forwardPort: number | null;
@observable isPortForwarded = false; @observable isPortForwarded = false;
constructor(props: Props) { constructor(props: Props) {
@ -53,7 +53,9 @@ export class ServicePortComponent extends React.Component<Props> {
} }
init() { init() {
this.checkExistingPortForwarding().then(); this.checkExistingPortForwarding().catch(error => {
console.error(error);
});
} }
async checkExistingPortForwarding() { async checkExistingPortForwarding() {
@ -62,7 +64,7 @@ export class ServicePortComponent extends React.Component<Props> {
const activePort = response.port; const activePort = response.port;
if (activePort && activePort != -1) { if (activePort) {
this.forwardPort = activePort; this.forwardPort = activePort;
this.isPortForwarded = true; this.isPortForwarded = true;
} }
@ -78,7 +80,6 @@ export class ServicePortComponent extends React.Component<Props> {
this.forwardPort = response.port; this.forwardPort = response.port;
this.isPortForwarded = true; this.isPortForwarded = true;
} catch(error) { } catch(error) {
Notifications.error(error); Notifications.error(error);
} finally { } finally {
@ -104,7 +105,7 @@ export class ServicePortComponent extends React.Component<Props> {
render() { render() {
const { port } = this.props; const { port } = this.props;
if (this.forwardPort == -1) { if (this.forwardPort == null) {
this.forwardPort = port.port; this.forwardPort = port.port;
} }

View File

@ -47,7 +47,7 @@ interface PortForwardResult {
@observer @observer
export class PodContainerPort extends React.Component<Props> { export class PodContainerPort extends React.Component<Props> {
@observable waiting = false; @observable waiting = false;
@observable forwardPort = -1; @observable forwardPort: number | null;
@observable isPortForwarded = false; @observable isPortForwarded = false;
constructor(props: Props) { constructor(props: Props) {
@ -57,7 +57,9 @@ export class PodContainerPort extends React.Component<Props> {
} }
init() { init() {
this.checkExistingPortForwarding().then(); this.checkExistingPortForwarding().catch(error => {
console.error(error);
});
} }
async checkExistingPortForwarding() { async checkExistingPortForwarding() {
@ -66,7 +68,7 @@ export class PodContainerPort extends React.Component<Props> {
const activePort = response.port; const activePort = response.port;
if (activePort && activePort != -1) { if (activePort) {
this.forwardPort = activePort; this.forwardPort = activePort;
this.isPortForwarded = true; this.isPortForwarded = true;
} }
@ -109,14 +111,14 @@ export class PodContainerPort extends React.Component<Props> {
const { name, containerPort, protocol } = port; const { name, containerPort, protocol } = port;
const text = `${name ? `${name}: ` : ""}${containerPort}/${protocol}`; const text = `${name ? `${name}: ` : ""}${containerPort}/${protocol}`;
if (this.forwardPort == -1) { if (this.forwardPort == null) {
this.forwardPort = containerPort; this.forwardPort = containerPort;
} }
const portForwardAction = async () => { const portForwardAction = async () => {
if (this.isPortForwarded) { if (this.isPortForwarded) {
await this.stopPortForward(); await this.stopPortForward();
}else { } else {
await this.portForward(); await this.portForward();
} }
}; };