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;
|
||||
port: number;
|
||||
forwardPort: number;
|
||||
protocol: string;
|
||||
protocol?: string;
|
||||
}
|
||||
|
||||
const internalPortRegex = /^forwarding from (?<address>.+) ->/i;
|
||||
@ -131,14 +131,15 @@ export class PortForwardRoute {
|
||||
namespace, port, forwardPort, protocol,
|
||||
});
|
||||
|
||||
let thePort = 0;
|
||||
|
||||
if (forwardPort > 0 && forwardPort < 65536) {
|
||||
thePort = forwardPort;
|
||||
}
|
||||
|
||||
if (!portForward) {
|
||||
logger.info(`Creating a new port-forward ${namespace}/${resourceType}/${resourceName}:${port}`);
|
||||
|
||||
let thePort = 0;
|
||||
|
||||
if (forwardPort > 0 && forwardPort < 65536) {
|
||||
thePort = forwardPort;
|
||||
}
|
||||
|
||||
portForward = new PortForward(await cluster.getProxyKubeconfigPath(), {
|
||||
clusterId: cluster.id,
|
||||
kind: resourceType,
|
||||
@ -208,11 +209,10 @@ export class PortForwardRoute {
|
||||
const { namespace, resourceType, resourceName } = params;
|
||||
const port = Number(query.get("port"));
|
||||
const forwardPort = Number(query.get("forwardPort"));
|
||||
const protocol = query.get("protocol");
|
||||
|
||||
const portForward = PortForward.getPortforward({
|
||||
clusterId: cluster.id, kind: resourceType, name: resourceName,
|
||||
namespace, port, forwardPort, protocol,
|
||||
namespace, port, forwardPort,
|
||||
});
|
||||
|
||||
try {
|
||||
|
||||
@ -50,7 +50,7 @@ export class PortForwardMenu extends React.Component<Props> {
|
||||
<span className="title">Open</span>
|
||||
</MenuItem>
|
||||
<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>
|
||||
</MenuItem>
|
||||
</>
|
||||
|
||||
@ -28,7 +28,7 @@ import { observable, makeObservable, reaction } from "mobx";
|
||||
import { cssNames } from "../../utils";
|
||||
import { Notifications } from "../notifications";
|
||||
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 logger from "../../../common/logger";
|
||||
import { Spinner } from "../spinner";
|
||||
@ -85,6 +85,7 @@ export class ServicePortComponent extends React.Component<Props> {
|
||||
namespace: service.getNs(),
|
||||
port: port.port,
|
||||
forwardPort: this.forwardPort,
|
||||
protocol: predictProtocol(port.name),
|
||||
};
|
||||
|
||||
this.waiting = true;
|
||||
@ -140,6 +141,7 @@ export class ServicePortComponent extends React.Component<Props> {
|
||||
namespace: service.getNs(),
|
||||
port: port.port,
|
||||
forwardPort: this.forwardPort,
|
||||
protocol: predictProtocol(port.name),
|
||||
};
|
||||
|
||||
PortForwardDialog.open(portForward, { openInBrowser: true });
|
||||
|
||||
@ -28,7 +28,7 @@ import { observable, makeObservable, reaction } from "mobx";
|
||||
import { cssNames } from "../../utils";
|
||||
import { Notifications } from "../notifications";
|
||||
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 logger from "../../../common/logger";
|
||||
import { Spinner } from "../spinner";
|
||||
@ -89,6 +89,7 @@ export class PodContainerPort extends React.Component<Props> {
|
||||
namespace: pod.getNs(),
|
||||
port: port.containerPort,
|
||||
forwardPort: this.forwardPort,
|
||||
protocol: predictProtocol(port.name),
|
||||
};
|
||||
|
||||
this.waiting = true;
|
||||
@ -146,6 +147,7 @@ export class PodContainerPort extends React.Component<Props> {
|
||||
namespace: pod.getNs(),
|
||||
port: port.containerPort,
|
||||
forwardPort: this.forwardPort,
|
||||
protocol: predictProtocol(port.name),
|
||||
};
|
||||
|
||||
PortForwardDialog.open(portForward, { openInBrowser: true });
|
||||
|
||||
@ -88,7 +88,7 @@ export class PortForwardStore extends ItemStore<PortForwardItem> {
|
||||
if (index === -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return this.getItems()[index];
|
||||
}
|
||||
}
|
||||
@ -108,7 +108,7 @@ export async function addPortForward(portForward: ForwardedPort): Promise<number
|
||||
const protocol = portForward.protocol ?? "http";
|
||||
|
||||
response = await apiBase.post<PortForwardResult>(`/pods/port-forward/${portForward.namespace}/${portForward.kind}/${portForward.name}?port=${portForward.port}&forwardPort=${portForward.forwardPort}&protocol=${protocol}`);
|
||||
|
||||
|
||||
if (response?.port && response.port != +portForward.forwardPort) {
|
||||
logger.warn(`specified ${portForward.forwardPort} got ${response.port}`);
|
||||
}
|
||||
@ -124,6 +124,7 @@ function getProtocolQuery(protocol: string) {
|
||||
if (protocol) {
|
||||
return `&protocol=${protocol}`;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
@ -141,13 +142,10 @@ export async function getPortForward(portForward: ForwardedPort): Promise<number
|
||||
|
||||
export async function modifyPortForward(portForward: ForwardedPort, desiredPort: number): Promise<number> {
|
||||
let port = 0;
|
||||
|
||||
|
||||
try {
|
||||
const protocol = portForward.protocol;
|
||||
delete portForward.protocol;
|
||||
await removePortForward(portForward);
|
||||
portForward.forwardPort = desiredPort;
|
||||
portForward.protocol = protocol;
|
||||
port = await addPortForward(portForward);
|
||||
} catch (error) {
|
||||
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) {
|
||||
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);
|
||||
} catch (error) {
|
||||
logger.warn(error); // don't care, caller must check
|
||||
@ -175,7 +173,7 @@ export async function getPortForwards(): Promise<ForwardedPort[]> {
|
||||
return response.portForwards;
|
||||
} catch (error) {
|
||||
logger.warn(error); // don't care, caller must check
|
||||
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@ -202,4 +200,8 @@ export function openPortForward(portForward: ForwardedPort) {
|
||||
|
||||
}
|
||||
|
||||
export function predictProtocol(name: string) {
|
||||
return name === "https" ? "https" : "http";
|
||||
}
|
||||
|
||||
export const portForwardStore = new PortForwardStore();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user