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

poll for pods and services disappearing to remove dead port-forwards

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
Jim Ehrismann 2021-10-08 20:21:34 -04:00
parent a3765211de
commit 5c5658c602
2 changed files with 24 additions and 3 deletions

View File

@ -29,9 +29,8 @@ import { Wizard, WizardStep } from "../components/wizard";
import { Input } from "../components/input"; import { Input } from "../components/input";
import { Notifications } from "../components/notifications"; import { Notifications } from "../components/notifications";
import { cssNames } from "../utils"; import { cssNames } from "../utils";
import { addPortForward, modifyPortForward } from "./port-forward.store"; import { addPortForward, modifyPortForward, openPortForward } from "./port-forward.store";
import type { ForwardedPort } from "./port-forward-item"; import type { ForwardedPort } from "./port-forward-item";
import { openPortForward } from ".";
interface Props extends Partial<DialogProps> { interface Props extends Partial<DialogProps> {
} }

View File

@ -28,6 +28,7 @@ import { apiBase } from "../api";
import { waitUntilFree } from "tcp-port-used"; import { waitUntilFree } from "tcp-port-used";
import { Notifications } from "../components/notifications"; import { Notifications } from "../components/notifications";
import logger from "../../common/logger"; import logger from "../../common/logger";
import { podsApi, serviceApi } from "../../common/k8s-api/endpoints";
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);
@ -56,9 +57,16 @@ export class PortForwardStore extends ItemStore<PortForwardItem> {
watch() { watch() {
return disposer( return disposer(
reaction(() => this.portForwards, () => this.loadAll()), reaction(() => this.portForwards, () => this.loadAll()),
this.reaper(),
); );
} }
reaper() {
const interval = setInterval(async () => await Promise.all(this.portForwards.map(reapPortForward)), 30 * 1000); // every 30 seconds
return () => clearInterval(interval);
}
loadAll() { loadAll() {
return this.loadItems(async () => { return this.loadItems(async () => {
let portForwards = await getPortForwards(); let portForwards = await getPortForwards();
@ -146,7 +154,7 @@ export async function removePortForward(portForward: ForwardedPort) {
portForwardStore.reset(); portForwardStore.reset();
} }
export async function getPortForwards(): Promise<ForwardedPort[]> { async function getPortForwards(): Promise<ForwardedPort[]> {
try { try {
const response = await apiBase.get<PortForwardsResult>(`/pods/port-forwards`); const response = await apiBase.get<PortForwardsResult>(`/pods/port-forwards`);
@ -158,6 +166,20 @@ export async function getPortForwards(): Promise<ForwardedPort[]> {
} }
} }
async function reapPortForward(portForward: ForwardedPort): Promise<void> {
const api = {
"service": serviceApi,
"pod": podsApi
}[portForward.kind];
try {
await api.get({name: portForward.name, namespace: portForward.namespace});
} catch(error) {
logger.debug(`port-forward resource ${portForward.name} not found, removing`, portForward);
removePortForward(portForward);
}
}
export function openPortForward(portForward: ForwardedPort) { export function openPortForward(portForward: ForwardedPort) {
const browseTo = `http://localhost:${portForward.forwardPort}`; const browseTo = `http://localhost:${portForward.forwardPort}`;