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