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

adding disabled status to port forwards (WIP)

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
Jim Ehrismann 2021-11-26 17:31:13 -05:00
parent 43277d2af6
commit d620bf578c
2 changed files with 33 additions and 19 deletions

View File

@ -23,6 +23,7 @@
import type { ItemObject } from "../../common/item.store"; import type { ItemObject } from "../../common/item.store";
import { autoBind } from "../../common/utils"; import { autoBind } from "../../common/utils";
export type forwardedPortStatus = "Active" | "Disabled";
export interface ForwardedPort { export interface ForwardedPort {
clusterId?: string; clusterId?: string;
kind: string; kind: string;
@ -31,6 +32,7 @@ export interface ForwardedPort {
port: number; port: number;
forwardPort: number; forwardPort: number;
protocol?: string; protocol?: string;
status?: forwardedPortStatus;
} }
export class PortForwardItem implements ItemObject { export class PortForwardItem implements ItemObject {
@ -41,6 +43,7 @@ export class PortForwardItem implements ItemObject {
port: number; port: number;
forwardPort: number; forwardPort: number;
protocol: string; protocol: string;
status: forwardedPortStatus;
constructor(pf: ForwardedPort) { constructor(pf: ForwardedPort) {
this.clusterId = pf.clusterId; this.clusterId = pf.clusterId;
@ -50,6 +53,7 @@ export class PortForwardItem implements ItemObject {
this.port = pf.port; this.port = pf.port;
this.forwardPort = pf.forwardPort; this.forwardPort = pf.forwardPort;
this.protocol = pf.protocol ?? "http"; this.protocol = pf.protocol ?? "http";
this.status = pf.status;
autoBind(this); autoBind(this);
} }
@ -87,7 +91,7 @@ export class PortForwardItem implements ItemObject {
} }
getStatus() { getStatus() {
return "Active"; // to-do allow port-forward-items to be stopped (without removing them) return this.status;
} }
getSearchFields() { getSearchFields() {

View File

@ -27,6 +27,7 @@ import { ForwardedPort, PortForwardItem } from "./port-forward-item";
import { apiBase } from "../api"; import { apiBase } from "../api";
import { waitUntilFree } from "tcp-port-used"; import { waitUntilFree } from "tcp-port-used";
import logger from "../../common/logger"; import logger from "../../common/logger";
import { Notifications } from "../components/notifications";
export class PortForwardStore extends ItemStore<PortForwardItem> { export class PortForwardStore extends ItemStore<PortForwardItem> {
private storage = createStorage<ForwardedPort[] | undefined>("port_forwards", undefined); private storage = createStorage<ForwardedPort[] | undefined>("port_forwards", undefined);
@ -48,13 +49,21 @@ export class PortForwardStore extends ItemStore<PortForwardItem> {
if (Array.isArray(savedPortForwards)) { if (Array.isArray(savedPortForwards)) {
logger.info("[PORT-FORWARD-STORE] starting saved port-forwards"); logger.info("[PORT-FORWARD-STORE] starting saved port-forwards");
await Promise.all(savedPortForwards.map(addPortForward)); const results = await Promise.allSettled(savedPortForwards.map(addPortForward));
for (const result of results) {
if (result.status === "rejected") {
Notifications.info("One or more port-forwards could not be started");
return;
}
}
} }
} }
watch() { watch() {
return disposer( return disposer(
reaction(() => this.portForwards, () => this.loadAll()), reaction(() => this.portForwards.slice(), () => this.loadAll()),
); );
} }
@ -64,17 +73,13 @@ export class PortForwardStore extends ItemStore<PortForwardItem> {
this.storage.set(portForwards); this.storage.set(portForwards);
this.reset(); this.portForwards = [];
portForwards.map(pf => this.portForwards.push(new PortForwardItem(pf))); portForwards.map(pf => this.portForwards.push(new PortForwardItem(pf)));
return this.portForwards; return this.portForwards;
}); });
} }
reset() {
this.portForwards = [];
}
async removeSelectedItems() { async removeSelectedItems() {
return Promise.all(this.selectedItems.map(removePortForward)); return Promise.all(this.selectedItems.map(removePortForward));
} }
@ -102,20 +107,28 @@ export async function addPortForward(portForward: ForwardedPort): Promise<number
const { port, forwardPort } = portForward; const { port, forwardPort } = portForward;
let response: PortForwardResult; let response: PortForwardResult;
if (!portForward.status) {
portForward.status = "Active";
}
try { try {
const protocol = portForward.protocol ?? "http"; if (portForward.status === "Active") {
const protocol = portForward.protocol ?? "http";
response = await apiBase.post<PortForwardResult>(`/pods/port-forward/${portForward.namespace}/${portForward.kind}/${portForward.name}`, { query: { port, forwardPort, protocol }}); response = await apiBase.post<PortForwardResult>(`/pods/port-forward/${portForward.namespace}/${portForward.kind}/${portForward.name}`, { query: { port, forwardPort, protocol }});
// expecting the received port to be the specified port, unless the specified port is 0, which indicates any available port is suitable // expecting the received port to be the specified port, unless the specified port is 0, which indicates any available port is suitable
if (portForward.forwardPort && response?.port && response.port != +portForward.forwardPort) { if (portForward.forwardPort && response?.port && response.port != +portForward.forwardPort) {
logger.warn(`[PORT-FORWARD-STORE] specified ${portForward.forwardPort} got ${response.port}`); logger.warn(`[PORT-FORWARD-STORE] specified ${portForward.forwardPort} got ${response.port}`);
}
} }
} catch (error) { } catch (error) {
logger.warn("[PORT-FORWARD-STORE] Error adding port-forward:", error, portForward); logger.warn("[PORT-FORWARD-STORE] Error starting port-forward:", error, portForward);
throw (error); portForward.status = "Disabled";
throw(error);
} finally {
portForwardStore.portForwards.push(new PortForwardItem(portForward));
} }
portForwardStore.reset();
return response?.port; return response?.port;
} }
@ -141,8 +154,6 @@ export async function modifyPortForward(portForward: ForwardedPort, desiredPort:
portForward.forwardPort = desiredPort; portForward.forwardPort = desiredPort;
port = await addPortForward(portForward); port = await addPortForward(portForward);
portForwardStore.reset();
return port; return port;
} }
@ -157,7 +168,6 @@ export async function removePortForward(portForward: ForwardedPort) {
logger.warn("[PORT-FORWARD-STORE] Error removing port-forward:", error, portForward); logger.warn("[PORT-FORWARD-STORE] Error removing port-forward:", error, portForward);
throw (error); throw (error);
} }
portForwardStore.reset();
} }
export async function getPortForwards(clusterId?: string): Promise<ForwardedPort[]> { export async function getPortForwards(clusterId?: string): Promise<ForwardedPort[]> {