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

Refreshing tab pods if pod amount is changed in store

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2021-01-25 14:23:40 +03:00
parent 355fbc9134
commit 0cd865a0d4
5 changed files with 174 additions and 21 deletions

View File

@ -5,18 +5,18 @@
import { podsStore } from "../../+workloads-pods/pods.store"; import { podsStore } from "../../+workloads-pods/pods.store";
import { Pod } from "../../../api/endpoints"; import { Pod } from "../../../api/endpoints";
import { dockStore } from "../dock.store"; import { dockStore } from "../dock.store";
import { LogTabStore } from "../log-tab.store"; import { logTabStore } from "../log-tab.store";
import { deploymentPod1, deploymentPod2, dockerPod } from "./pod.mock"; import { deploymentPod1, deploymentPod2, deploymentPod3, dockerPod } from "./pod.mock";
let logTabStore: LogTabStore = null;
podsStore.items.push(new Pod(dockerPod)); podsStore.items.push(new Pod(dockerPod));
podsStore.items.push(new Pod(deploymentPod1)); podsStore.items.push(new Pod(deploymentPod1));
podsStore.items.push(new Pod(deploymentPod2)); podsStore.items.push(new Pod(deploymentPod2));
describe("log tab store", () => { describe("log tab store", () => {
beforeEach(async () => { afterEach(() => {
logTabStore = new LogTabStore(); logTabStore.reset();
dockStore.reset();
}); });
it("creates log tab without sibling pods", () => { it("creates log tab without sibling pods", () => {
@ -55,4 +55,59 @@ describe("log tab store", () => {
previous: false previous: false
}); });
}); });
it("removes item from pods list if pod deleted from store", () => {
const selectedPod = new Pod(deploymentPod1);
const selectedContainer = selectedPod.getInitContainers()[0];
logTabStore.createPodTab({
selectedPod,
selectedContainer
});
podsStore.items.pop();
expect(logTabStore.getData(dockStore.selectedTabId)).toEqual({
pods: [selectedPod],
selectedPod,
selectedContainer,
showTimestamps: false,
previous: false
});
});
it("adds item into pods list if new sibling pod added to store", () => {
const selectedPod = new Pod(deploymentPod1);
const selectedContainer = selectedPod.getInitContainers()[0];
logTabStore.createPodTab({
selectedPod,
selectedContainer
});
podsStore.items.push(new Pod(deploymentPod3));
expect(logTabStore.getData(dockStore.selectedTabId)).toEqual({
pods: [selectedPod, deploymentPod3],
selectedPod,
selectedContainer,
showTimestamps: false,
previous: false
});
});
it("closes tab if no pods left in store", () => {
const selectedPod = new Pod(deploymentPod1);
const selectedContainer = selectedPod.getInitContainers()[0];
logTabStore.createPodTab({
selectedPod,
selectedContainer
});
podsStore.items.clear();
expect(logTabStore.getData(dockStore.selectedTabId)).toBeUndefined();
expect(dockStore.getTabById(dockStore.selectedTabId)).toBeUndefined();
});
}); });

View File

@ -153,3 +153,51 @@ export const deploymentPod2 = {
startTime: "dummy", startTime: "dummy",
} }
}; };
export const deploymentPod3 = {
apiVersion: "v1",
kind: "dummy",
metadata: {
uid: "deploymentPod3",
name: "deploymentPod3",
creationTimestamp: "dummy",
resourceVersion: "dummy",
namespace: "default",
ownerReferences: [{
apiVersion: "v1",
kind: "Deployment",
name: "super-deployment",
uid: "uuid",
controller: true,
blockOwnerDeletion: true,
}]
},
spec: {
containers: [
{
name: "node-exporter",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull"
},
{
name: "node-exporter-1",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull"
}
],
serviceAccountName: "dummy",
serviceAccount: "dummy",
},
status: {
phase: "Running",
conditions: [{
type: "Running",
status: "Running",
lastProbeTime: 1,
lastTransitionTime: "Some time",
}],
hostIP: "dummy",
podIP: "dummy",
startTime: "dummy",
}
};

View File

@ -1,6 +1,6 @@
import "./log-resource-selector.scss"; import "./log-resource-selector.scss";
import React from "react"; import React, { useEffect } from "react";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import { Pod } from "../../api/endpoints"; import { Pod } from "../../api/endpoints";
@ -33,24 +33,20 @@ export const LogResourceSelector = observer((props: Props) => {
reload(); reload();
}; };
const selectFirstContainer = () => {
save({ selectedContainer: pod.getAllContainers()[0] });
};
const renameTab = () => {
dockStore.renameTab(tabId, `Pod ${pod.getName()}`);
};
const onPodChange = (option: SelectOption) => { const onPodChange = (option: SelectOption) => {
const selectedPod = podsStore.getByName(option.value, pod.getNs()); const selectedPod = podsStore.getByName(option.value, pod.getNs());
if (!selectedPod) { save({ selectedPod });
return;
}
save({
selectedPod,
selectedContainer: selectedPod.getAllContainers()[0]
});
dockStore.renameTab(tabId, `Pod ${option.value}`);
reload();
}; };
const getSelectOptions = (items: string[]) => { const getSelectOptions = (items: string[]) => {
return items.map(item => { return items.map(item => {
return { return {
@ -78,6 +74,12 @@ export const LogResourceSelector = observer((props: Props) => {
} }
]; ];
useEffect(() => {
selectFirstContainer();
renameTab();
reload();
}, [selectedPod]);
return ( return (
<div className="LogResourceSelector flex gaps align-center"> <div className="LogResourceSelector flex gaps align-center">
<span>Namespace</span> <Badge data-testid="namespace-badge" label={pod.getNs()}/> <span>Namespace</span> <Badge data-testid="namespace-badge" label={pod.getNs()}/>

View File

@ -1,4 +1,5 @@
import uniqueId from "lodash/uniqueId"; import uniqueId from "lodash/uniqueId";
import { reaction } from "mobx";
import { podsStore } from "../+workloads-pods/pods.store"; import { podsStore } from "../+workloads-pods/pods.store";
import { IPodContainer, Pod } from "../../api/endpoints"; import { IPodContainer, Pod } from "../../api/endpoints";
@ -28,9 +29,13 @@ export class LogTabStore extends DockTabStore<LogTabData> {
super({ super({
storageName: "pod_logs" storageName: "pod_logs"
}); });
reaction(() => podsStore.items.length, () => {
this.updateTabsData();
});
} }
public createPodTab({ selectedPod, selectedContainer }: PodLogsTabData) { createPodTab({ selectedPod, selectedContainer }: PodLogsTabData): void {
const podOwner = selectedPod.getOwnerRefs()[0]; const podOwner = selectedPod.getOwnerRefs()[0];
const pods = podsStore.getPodsByOwnerId(podOwner?.uid); const pods = podsStore.getPodsByOwnerId(podOwner?.uid);
const title = `Pod ${selectedPod.getName()}`; const title = `Pod ${selectedPod.getName()}`;
@ -42,7 +47,7 @@ export class LogTabStore extends DockTabStore<LogTabData> {
}); });
} }
public createWorkloadTab({ workload }: WorkloadLogsTabData) { createWorkloadTab({ workload }: WorkloadLogsTabData): void {
const pods = podsStore.getPodsByOwnerId(workload.getId()); const pods = podsStore.getPodsByOwnerId(workload.getId());
if (!pods.length) return; if (!pods.length) return;
@ -58,6 +63,10 @@ export class LogTabStore extends DockTabStore<LogTabData> {
}); });
} }
private get tabId() {
return dockStore.selectedTabId;
}
private createDockTab(tabParams: Partial<IDockTab>) { private createDockTab(tabParams: Partial<IDockTab>) {
dockStore.createTab({ dockStore.createTab({
kind: TabKind.POD_LOGS, kind: TabKind.POD_LOGS,
@ -75,6 +84,41 @@ export class LogTabStore extends DockTabStore<LogTabData> {
previous: false previous: false
}); });
} }
private updateTabsData() {
this.data.forEach((value, tabId) => {
this.updatePodsData(tabId);
});
}
private updatePodsData(tabId: string) {
const tabData = this.getData(tabId);
const selectedPod = new Pod(tabData.selectedPod);
const owner = selectedPod.getOwnerRefs()[0];
const pods = podsStore.getPodsByOwnerId(owner?.uid);
let newSelectedPod = selectedPod;
if (!pods.length) {
this.closeTab(tabId);
return;
}
if (!pods.includes(selectedPod)) {
newSelectedPod = pods[0];
}
this.setData(tabId, {
...tabData,
selectedPod: newSelectedPod,
pods
});
}
private closeTab(tabId: string) {
this.clearData(tabId);
dockStore.closeTab(tabId);
}
} }
export const logTabStore = new LogTabStore(); export const logTabStore = new LogTabStore();

View File

@ -42,6 +42,10 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
} }
} }
getById(id: string) {
return this.items.find(item => item.getId() === id);
}
getByName(name: string, namespace?: string): T { getByName(name: string, namespace?: string): T {
return this.items.find(item => { return this.items.find(item => {
return item.getName() === name && ( return item.getName() === name && (