mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
added support for port-forwarding https
Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
parent
169f443c3b
commit
f70853b95e
@ -34,6 +34,7 @@ interface PortForwardArgs {
|
||||
name: string;
|
||||
port: number;
|
||||
forwardPort: number;
|
||||
protocol: string;
|
||||
}
|
||||
|
||||
const internalPortRegex = /^forwarding from (?<address>.+) ->/i;
|
||||
@ -47,7 +48,8 @@ class PortForward {
|
||||
pf.kind == forward.kind &&
|
||||
pf.name == forward.name &&
|
||||
pf.namespace == forward.namespace &&
|
||||
pf.port == forward.port
|
||||
pf.port == forward.port &&
|
||||
(!forward.protocol || pf.protocol == forward.protocol)
|
||||
));
|
||||
}
|
||||
|
||||
@ -58,6 +60,7 @@ class PortForward {
|
||||
public name: string;
|
||||
public port: number;
|
||||
public forwardPort: number;
|
||||
public protocol: string;
|
||||
|
||||
constructor(public kubeConfig: string, args: PortForwardArgs) {
|
||||
this.clusterId = args.clusterId;
|
||||
@ -66,6 +69,7 @@ class PortForward {
|
||||
this.name = args.name;
|
||||
this.port = args.port;
|
||||
this.forwardPort = args.forwardPort;
|
||||
this.protocol = args.protocol ?? "http";
|
||||
}
|
||||
|
||||
public async start() {
|
||||
@ -119,11 +123,12 @@ 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");
|
||||
|
||||
try {
|
||||
let portForward = PortForward.getPortforward({
|
||||
clusterId: cluster.id, kind: resourceType, name: resourceName,
|
||||
namespace, port, forwardPort,
|
||||
namespace, port, forwardPort, protocol,
|
||||
});
|
||||
|
||||
let thePort = 0;
|
||||
@ -141,6 +146,7 @@ export class PortForwardRoute {
|
||||
name: resourceName,
|
||||
port,
|
||||
forwardPort: thePort,
|
||||
protocol,
|
||||
});
|
||||
|
||||
const started = await portForward.start();
|
||||
@ -169,10 +175,11 @@ 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
|
||||
namespace, port, forwardPort, protocol,
|
||||
});
|
||||
|
||||
respondJson(response, { port: portForward?.forwardPort ?? null });
|
||||
@ -189,6 +196,7 @@ export class PortForwardRoute {
|
||||
name: f.name,
|
||||
port: f.port,
|
||||
forwardPort: f.forwardPort,
|
||||
protocol: f.protocol,
|
||||
})
|
||||
);
|
||||
|
||||
@ -200,10 +208,11 @@ 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,
|
||||
namespace, port, forwardPort, protocol,
|
||||
});
|
||||
|
||||
try {
|
||||
|
||||
@ -44,7 +44,8 @@ interface PortForwardDialogOpenOptions {
|
||||
const dialogState = observable.object({
|
||||
isOpen: false,
|
||||
data: null as ForwardedPort,
|
||||
openInBrowser: false
|
||||
useHttps: false,
|
||||
openInBrowser: false,
|
||||
});
|
||||
|
||||
@observer
|
||||
@ -60,6 +61,7 @@ export class PortForwardDialog extends Component<Props> {
|
||||
static open(portForward: ForwardedPort, options: PortForwardDialogOpenOptions = { openInBrowser: false }) {
|
||||
dialogState.isOpen = true;
|
||||
dialogState.data = portForward;
|
||||
dialogState.useHttps = Boolean(portForward.protocol === "https");
|
||||
dialogState.openInBrowser = options.openInBrowser;
|
||||
}
|
||||
|
||||
@ -96,6 +98,8 @@ export class PortForwardDialog extends Component<Props> {
|
||||
try {
|
||||
let port: number;
|
||||
|
||||
portForward.protocol = dialogState.useHttps ? "https" : "http";
|
||||
|
||||
if (currentPort) {
|
||||
port = await modifyPortForward(portForward, desiredPort);
|
||||
} else {
|
||||
@ -131,6 +135,13 @@ export class PortForwardDialog extends Component<Props> {
|
||||
onChange={this.changePort}
|
||||
/>
|
||||
</div>
|
||||
<Checkbox
|
||||
data-testid="port-forward-https"
|
||||
theme="light"
|
||||
label="https"
|
||||
value={dialogState.useHttps}
|
||||
onChange={value => dialogState.useHttps = value}
|
||||
/>
|
||||
<Checkbox
|
||||
data-testid="port-forward-open"
|
||||
theme="light"
|
||||
|
||||
@ -30,6 +30,7 @@ export interface ForwardedPort {
|
||||
name: string;
|
||||
port: number;
|
||||
forwardPort: number;
|
||||
protocol?: string;
|
||||
}
|
||||
|
||||
export class PortForwardItem implements ItemObject {
|
||||
@ -39,6 +40,7 @@ export class PortForwardItem implements ItemObject {
|
||||
name: string;
|
||||
port: number;
|
||||
forwardPort: number;
|
||||
protocol: string;
|
||||
|
||||
constructor(pf: ForwardedPort) {
|
||||
this.clusterId = pf.clusterId;
|
||||
@ -47,6 +49,7 @@ export class PortForwardItem implements ItemObject {
|
||||
this.name = pf.name;
|
||||
this.port = pf.port;
|
||||
this.forwardPort = pf.forwardPort;
|
||||
this.protocol = pf.protocol ?? "http";
|
||||
|
||||
autoBind(this);
|
||||
}
|
||||
@ -79,6 +82,10 @@ export class PortForwardItem implements ItemObject {
|
||||
return this.forwardPort;
|
||||
}
|
||||
|
||||
getProtocol() {
|
||||
return this.protocol;
|
||||
}
|
||||
|
||||
getStatus() {
|
||||
return "Active"; // to-do allow port-forward-items to be stopped (without removing them)
|
||||
}
|
||||
|
||||
@ -105,7 +105,9 @@ export async function addPortForward(portForward: ForwardedPort): Promise<number
|
||||
let response: PortForwardResult;
|
||||
|
||||
try {
|
||||
response = await apiBase.post<PortForwardResult>(`/pods/port-forward/${portForward.namespace}/${portForward.kind}/${portForward.name}?port=${portForward.port}&forwardPort=${portForward.forwardPort}`);
|
||||
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}`);
|
||||
@ -118,11 +120,18 @@ export async function addPortForward(portForward: ForwardedPort): Promise<number
|
||||
return response?.port;
|
||||
}
|
||||
|
||||
function getProtocolQuery(protocol: string) {
|
||||
if (protocol) {
|
||||
return `&protocol=${protocol}`;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
export async function getPortForward(portForward: ForwardedPort): Promise<number> {
|
||||
let response: PortForwardResult;
|
||||
|
||||
try {
|
||||
response = await apiBase.get<PortForwardResult>(`/pods/port-forward/${portForward.namespace}/${portForward.kind}/${portForward.name}?port=${portForward.port}&forwardPort=${portForward.forwardPort}`);
|
||||
response = await apiBase.get<PortForwardResult>(`/pods/port-forward/${portForward.namespace}/${portForward.kind}/${portForward.name}?port=${portForward.port}&forwardPort=${portForward.forwardPort}${getProtocolQuery(portForward.protocol)}`);
|
||||
} catch (error) {
|
||||
logger.warn(error); // don't care, caller must check
|
||||
}
|
||||
@ -134,8 +143,11 @@ export async function modifyPortForward(portForward: ForwardedPort, desiredPort:
|
||||
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
|
||||
@ -148,7 +160,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}`);
|
||||
await apiBase.del(`/pods/port-forward/${portForward.namespace}/${portForward.kind}/${portForward.name}?port=${portForward.port}&forwardPort=${portForward.forwardPort}${getProtocolQuery(portForward.protocol)}`);
|
||||
await waitUntilFree(+portForward.forwardPort, 200, 1000);
|
||||
} catch (error) {
|
||||
logger.warn(error); // don't care, caller must check
|
||||
@ -169,7 +181,7 @@ export async function getPortForwards(): Promise<ForwardedPort[]> {
|
||||
}
|
||||
|
||||
export function openPortForward(portForward: ForwardedPort) {
|
||||
const browseTo = `http://localhost:${portForward.forwardPort}`;
|
||||
const browseTo = `${portForward.protocol ?? "http"}://localhost:${portForward.forwardPort}`;
|
||||
|
||||
openExternal(browseTo)
|
||||
.catch(error => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user