mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
predict the protocol based on the port name, some cleanup
Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
parent
1fbd9505f6
commit
264045cd37
@ -34,7 +34,7 @@ interface PortForwardArgs {
|
|||||||
name: string;
|
name: string;
|
||||||
port: number;
|
port: number;
|
||||||
forwardPort: number;
|
forwardPort: number;
|
||||||
protocol: string;
|
protocol?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const internalPortRegex = /^forwarding from (?<address>.+) ->/i;
|
const internalPortRegex = /^forwarding from (?<address>.+) ->/i;
|
||||||
@ -131,14 +131,15 @@ export class PortForwardRoute {
|
|||||||
namespace, port, forwardPort, protocol,
|
namespace, port, forwardPort, protocol,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!portForward) {
|
||||||
|
logger.info(`Creating a new port-forward ${namespace}/${resourceType}/${resourceName}:${port}`);
|
||||||
|
|
||||||
let thePort = 0;
|
let thePort = 0;
|
||||||
|
|
||||||
if (forwardPort > 0 && forwardPort < 65536) {
|
if (forwardPort > 0 && forwardPort < 65536) {
|
||||||
thePort = forwardPort;
|
thePort = forwardPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!portForward) {
|
|
||||||
logger.info(`Creating a new port-forward ${namespace}/${resourceType}/${resourceName}:${port}`);
|
|
||||||
portForward = new PortForward(await cluster.getProxyKubeconfigPath(), {
|
portForward = new PortForward(await cluster.getProxyKubeconfigPath(), {
|
||||||
clusterId: cluster.id,
|
clusterId: cluster.id,
|
||||||
kind: resourceType,
|
kind: resourceType,
|
||||||
@ -208,11 +209,10 @@ export class PortForwardRoute {
|
|||||||
const { namespace, resourceType, resourceName } = params;
|
const { namespace, resourceType, resourceName } = params;
|
||||||
const port = Number(query.get("port"));
|
const port = Number(query.get("port"));
|
||||||
const forwardPort = Number(query.get("forwardPort"));
|
const forwardPort = Number(query.get("forwardPort"));
|
||||||
const protocol = query.get("protocol");
|
|
||||||
|
|
||||||
const portForward = PortForward.getPortforward({
|
const portForward = PortForward.getPortforward({
|
||||||
clusterId: cluster.id, kind: resourceType, name: resourceName,
|
clusterId: cluster.id, kind: resourceType, name: resourceName,
|
||||||
namespace, port, forwardPort, protocol,
|
namespace, port, forwardPort,
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -50,7 +50,7 @@ export class PortForwardMenu extends React.Component<Props> {
|
|||||||
<span className="title">Open</span>
|
<span className="title">Open</span>
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuItem onClick={() => PortForwardDialog.open(portForward)}>
|
<MenuItem onClick={() => PortForwardDialog.open(portForward)}>
|
||||||
<Icon material="edit" tooltip="Change port" interactive={toolbar} />
|
<Icon material="edit" tooltip="Change port or protocol" interactive={toolbar} />
|
||||||
<span className="title">Edit</span>
|
<span className="title">Edit</span>
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@ -28,7 +28,7 @@ import { observable, makeObservable, reaction } from "mobx";
|
|||||||
import { cssNames } from "../../utils";
|
import { cssNames } from "../../utils";
|
||||||
import { Notifications } from "../notifications";
|
import { Notifications } from "../notifications";
|
||||||
import { Button } from "../button";
|
import { Button } from "../button";
|
||||||
import { addPortForward, getPortForward, openPortForward, PortForwardDialog, portForwardStore, removePortForward } from "../../port-forward";
|
import { addPortForward, getPortForward, openPortForward, PortForwardDialog, portForwardStore, predictProtocol, removePortForward } from "../../port-forward";
|
||||||
import type { ForwardedPort } from "../../port-forward";
|
import type { ForwardedPort } from "../../port-forward";
|
||||||
import logger from "../../../common/logger";
|
import logger from "../../../common/logger";
|
||||||
import { Spinner } from "../spinner";
|
import { Spinner } from "../spinner";
|
||||||
@ -85,6 +85,7 @@ export class ServicePortComponent extends React.Component<Props> {
|
|||||||
namespace: service.getNs(),
|
namespace: service.getNs(),
|
||||||
port: port.port,
|
port: port.port,
|
||||||
forwardPort: this.forwardPort,
|
forwardPort: this.forwardPort,
|
||||||
|
protocol: predictProtocol(port.name),
|
||||||
};
|
};
|
||||||
|
|
||||||
this.waiting = true;
|
this.waiting = true;
|
||||||
@ -140,6 +141,7 @@ export class ServicePortComponent extends React.Component<Props> {
|
|||||||
namespace: service.getNs(),
|
namespace: service.getNs(),
|
||||||
port: port.port,
|
port: port.port,
|
||||||
forwardPort: this.forwardPort,
|
forwardPort: this.forwardPort,
|
||||||
|
protocol: predictProtocol(port.name),
|
||||||
};
|
};
|
||||||
|
|
||||||
PortForwardDialog.open(portForward, { openInBrowser: true });
|
PortForwardDialog.open(portForward, { openInBrowser: true });
|
||||||
|
|||||||
@ -28,7 +28,7 @@ import { observable, makeObservable, reaction } from "mobx";
|
|||||||
import { cssNames } from "../../utils";
|
import { cssNames } from "../../utils";
|
||||||
import { Notifications } from "../notifications";
|
import { Notifications } from "../notifications";
|
||||||
import { Button } from "../button";
|
import { Button } from "../button";
|
||||||
import { addPortForward, getPortForward, openPortForward, PortForwardDialog, portForwardStore, removePortForward } from "../../port-forward";
|
import { addPortForward, getPortForward, openPortForward, PortForwardDialog, portForwardStore, predictProtocol, removePortForward } from "../../port-forward";
|
||||||
import type { ForwardedPort } from "../../port-forward";
|
import type { ForwardedPort } from "../../port-forward";
|
||||||
import logger from "../../../common/logger";
|
import logger from "../../../common/logger";
|
||||||
import { Spinner } from "../spinner";
|
import { Spinner } from "../spinner";
|
||||||
@ -89,6 +89,7 @@ export class PodContainerPort extends React.Component<Props> {
|
|||||||
namespace: pod.getNs(),
|
namespace: pod.getNs(),
|
||||||
port: port.containerPort,
|
port: port.containerPort,
|
||||||
forwardPort: this.forwardPort,
|
forwardPort: this.forwardPort,
|
||||||
|
protocol: predictProtocol(port.name),
|
||||||
};
|
};
|
||||||
|
|
||||||
this.waiting = true;
|
this.waiting = true;
|
||||||
@ -146,6 +147,7 @@ export class PodContainerPort extends React.Component<Props> {
|
|||||||
namespace: pod.getNs(),
|
namespace: pod.getNs(),
|
||||||
port: port.containerPort,
|
port: port.containerPort,
|
||||||
forwardPort: this.forwardPort,
|
forwardPort: this.forwardPort,
|
||||||
|
protocol: predictProtocol(port.name),
|
||||||
};
|
};
|
||||||
|
|
||||||
PortForwardDialog.open(portForward, { openInBrowser: true });
|
PortForwardDialog.open(portForward, { openInBrowser: true });
|
||||||
|
|||||||
@ -124,6 +124,7 @@ function getProtocolQuery(protocol: string) {
|
|||||||
if (protocol) {
|
if (protocol) {
|
||||||
return `&protocol=${protocol}`;
|
return `&protocol=${protocol}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,11 +144,8 @@ export async function modifyPortForward(portForward: ForwardedPort, desiredPort:
|
|||||||
let port = 0;
|
let port = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const protocol = portForward.protocol;
|
|
||||||
delete portForward.protocol;
|
|
||||||
await removePortForward(portForward);
|
await removePortForward(portForward);
|
||||||
portForward.forwardPort = desiredPort;
|
portForward.forwardPort = desiredPort;
|
||||||
portForward.protocol = protocol;
|
|
||||||
port = await addPortForward(portForward);
|
port = await addPortForward(portForward);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.warn(error); // don't care, caller must check
|
logger.warn(error); // don't care, caller must check
|
||||||
@ -160,7 +158,7 @@ export async function modifyPortForward(portForward: ForwardedPort, desiredPort:
|
|||||||
|
|
||||||
export async function removePortForward(portForward: ForwardedPort) {
|
export async function removePortForward(portForward: ForwardedPort) {
|
||||||
try {
|
try {
|
||||||
await apiBase.del(`/pods/port-forward/${portForward.namespace}/${portForward.kind}/${portForward.name}?port=${portForward.port}&forwardPort=${portForward.forwardPort}${getProtocolQuery(portForward.protocol)}`);
|
await apiBase.del(`/pods/port-forward/${portForward.namespace}/${portForward.kind}/${portForward.name}?port=${portForward.port}&forwardPort=${portForward.forwardPort}`);
|
||||||
await waitUntilFree(+portForward.forwardPort, 200, 1000);
|
await waitUntilFree(+portForward.forwardPort, 200, 1000);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.warn(error); // don't care, caller must check
|
logger.warn(error); // don't care, caller must check
|
||||||
@ -202,4 +200,8 @@ export function openPortForward(portForward: ForwardedPort) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function predictProtocol(name: string) {
|
||||||
|
return name === "https" ? "https" : "http";
|
||||||
|
}
|
||||||
|
|
||||||
export const portForwardStore = new PortForwardStore();
|
export const portForwardStore = new PortForwardStore();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user