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

more refactoring, clean up, responding to comments

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2021-02-05 02:30:48 +02:00
parent 3394b74f10
commit bba37d6111
21 changed files with 53 additions and 65 deletions

View File

@ -73,8 +73,8 @@ export class ReleaseStore extends ItemStore<HelmRelease> {
} }
} }
async loadSelectedNamespaces(): Promise<void> { async loadFromContextNamespaces(): Promise<void> {
return this.loadAll(namespaceStore.getContextNamespaces()); return this.loadAll(namespaceStore.contextNamespaces);
} }
async loadItems(namespaces: string[]) { async loadItems(namespaces: string[]) {
@ -86,7 +86,7 @@ export class ReleaseStore extends ItemStore<HelmRelease> {
async create(payload: IReleaseCreatePayload) { async create(payload: IReleaseCreatePayload) {
const response = await helmReleasesApi.create(payload); const response = await helmReleasesApi.create(payload);
if (this.isLoaded) this.loadSelectedNamespaces(); if (this.isLoaded) this.loadFromContextNamespaces();
return response; return response;
} }
@ -94,7 +94,7 @@ export class ReleaseStore extends ItemStore<HelmRelease> {
async update(name: string, namespace: string, payload: IReleaseUpdatePayload) { async update(name: string, namespace: string, payload: IReleaseUpdatePayload) {
const response = await helmReleasesApi.update(name, namespace, payload); const response = await helmReleasesApi.update(name, namespace, payload);
if (this.isLoaded) this.loadSelectedNamespaces(); if (this.isLoaded) this.loadFromContextNamespaces();
return response; return response;
} }
@ -102,7 +102,7 @@ export class ReleaseStore extends ItemStore<HelmRelease> {
async rollback(name: string, namespace: string, revision: number) { async rollback(name: string, namespace: string, revision: number) {
const response = await helmReleasesApi.rollback(name, namespace, revision); const response = await helmReleasesApi.rollback(name, namespace, revision);
if (this.isLoaded) this.loadSelectedNamespaces(); if (this.isLoaded) this.loadFromContextNamespaces();
return response; return response;
} }

View File

@ -30,7 +30,7 @@ export class CrdResources extends React.Component<Props> {
const { store } = this; const { store } = this;
if (store && !store.isLoading && !store.isLoaded) { if (store && !store.isLoading && !store.isLoaded) {
store.loadContextNamespaces(); store.loadAllFromContextNamespaces();
} }
}) })
]); ]);

View File

@ -14,7 +14,7 @@ export interface KubeEventDetailsProps {
@observer @observer
export class KubeEventDetails extends React.Component<KubeEventDetailsProps> { export class KubeEventDetails extends React.Component<KubeEventDetailsProps> {
async componentDidMount() { async componentDidMount() {
eventStore.loadContextNamespaces(); eventStore.loadAllFromContextNamespaces();
} }
render() { render() {

View File

@ -32,8 +32,8 @@ export class NamespaceDetails extends React.Component<Props> {
} }
componentDidMount() { componentDidMount() {
resourceQuotaStore.loadContextNamespaces(); resourceQuotaStore.loadAllFromContextNamespaces();
limitRangeStore.loadContextNamespaces(); limitRangeStore.loadAllFromContextNamespaces();
} }
render() { render() {

View File

@ -82,7 +82,7 @@ export class NamespaceSelect extends React.Component<Props> {
@observer @observer
export class NamespaceSelectFilter extends React.Component { export class NamespaceSelectFilter extends React.Component {
@computed get placeholder(): React.ReactNode { @computed get placeholder(): React.ReactNode {
const namespaces = namespaceStore.getContextNamespaces(); const namespaces = namespaceStore.contextNamespaces;
switch (namespaces.length) { switch (namespaces.length) {
case 0: case 0:

View File

@ -1,10 +1,9 @@
import { action, comparer, computed, IReactionDisposer, IReactionOptions, observable, reaction, toJS, when } from "mobx"; import { action, comparer, computed, IReactionDisposer, IReactionOptions, observable, reaction } from "mobx";
import { autobind, createStorage } from "../../utils"; import { autobind, createStorage } from "../../utils";
import { KubeObjectStore, KubeObjectStoreLoadingParams } from "../../kube-object.store"; import { KubeObjectStore, KubeObjectStoreLoadingParams } from "../../kube-object.store";
import { Namespace, namespacesApi } from "../../api/endpoints/namespaces.api"; import { Namespace, namespacesApi } from "../../api/endpoints/namespaces.api";
import { createPageParam } from "../../navigation"; import { createPageParam } from "../../navigation";
import { apiManager } from "../../api/api-manager"; import { apiManager } from "../../api/api-manager";
import { clusterStore, getHostedCluster } from "../../../common/cluster-store";
const storage = createStorage<string[]>("context_namespaces", []); const storage = createStorage<string[]>("context_namespaces", []);
@ -35,9 +34,6 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
api = namespacesApi; api = namespacesApi;
@observable private contextNs = observable.set<string>(); @observable private contextNs = observable.set<string>();
@observable isReady = false;
whenReady = when(() => this.isReady);
constructor() { constructor() {
super(); super();
@ -45,15 +41,12 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
} }
private async init() { private async init() {
await clusterStore.whenLoaded; await this.resolveCluster();
if (!getHostedCluster()) return; if (!this.cluster) return; // skip for non-cluster context window
await getHostedCluster().whenReady; // wait for cluster-state from main
this.setContext(this.initialNamespaces); this.setContext(this.initialNamespaces);
this.autoLoadAllowedNamespaces(); this.autoLoadAllowedNamespaces();
this.autoUpdateUrlAndLocalStorage(); this.autoUpdateUrlAndLocalStorage();
this.isReady = true;
} }
public onContextChange(callback: (contextNamespaces: string[]) => void, opts: IReactionOptions = {}): IReactionDisposer { public onContextChange(callback: (contextNamespaces: string[]) => void, opts: IReactionOptions = {}): IReactionDisposer {
@ -79,10 +72,6 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
}); });
} }
@computed get allowedNamespaces(): string[] {
return toJS(getHostedCluster().allowedNamespaces);
}
@computed @computed
private get initialNamespaces(): string[] { private get initialNamespaces(): string[] {
const namespaces = new Set(this.allowedNamespaces); const namespaces = new Set(this.allowedNamespaces);
@ -103,27 +92,26 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
return []; return [];
} }
getContextNamespaces(): string[] { @computed get allowedNamespaces(): string[] {
return Array.from(new Set([
...(this.cluster?.allowedNamespaces ?? []), // loaded names from main, updating every 30s and thus might be stale
...this.items.map(item => item.getName()), // loaded names from hosted cluster
].flat()));
}
@computed get contextNamespaces(): string[] {
const namespaces = Array.from(this.contextNs); const namespaces = Array.from(this.contextNs);
// show all namespaces when nothing selected
if (!namespaces.length) { if (!namespaces.length) {
// return actual namespaces list since "allowedNamespaces" updating every 30s in cluster and thus might be stale return this.allowedNamespaces; // show all namespaces when nothing selected
if (this.isLoaded) {
return this.items.map(namespace => namespace.getName());
}
return this.allowedNamespaces;
} }
return namespaces; return namespaces;
} }
getSubscribeApis() { getSubscribeApis() {
const { accessibleNamespaces } = getHostedCluster();
// if user has given static list of namespaces let's not start watches because watch adds stuff that's not wanted // if user has given static list of namespaces let's not start watches because watch adds stuff that's not wanted
if (accessibleNamespaces.length > 0) { if (this.cluster?.accessibleNamespaces.length > 0) {
return []; return [];
} }
@ -152,7 +140,7 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
} }
@action @action
resetContext(){ resetContext() {
this.contextNs.clear(); this.contextNs.clear();
} }

View File

@ -29,7 +29,7 @@ export class NodeDetails extends React.Component<Props> {
}); });
async componentDidMount() { async componentDidMount() {
podsStore.loadContextNamespaces(); podsStore.loadAllFromContextNamespaces();
} }
componentWillUnmount() { componentWillUnmount() {

View File

@ -80,7 +80,7 @@ export class AddRoleBindingDialog extends React.Component<Props> {
]; ];
this.isLoading = true; this.isLoading = true;
await Promise.all(stores.map(store => store.loadContextNamespaces())); await Promise.all(stores.map(store => store.loadAllFromContextNamespaces()));
this.isLoading = false; this.isLoading = false;
} }

View File

@ -20,7 +20,7 @@ interface Props extends KubeObjectDetailsProps<CronJob> {
@observer @observer
export class CronJobDetails extends React.Component<Props> { export class CronJobDetails extends React.Component<Props> {
async componentDidMount() { async componentDidMount() {
jobStore.loadContextNamespaces(); jobStore.loadAllFromContextNamespaces();
} }
render() { render() {

View File

@ -30,7 +30,7 @@ export class DaemonSetDetails extends React.Component<Props> {
}); });
componentDidMount() { componentDidMount() {
podsStore.loadContextNamespaces(); podsStore.loadAllFromContextNamespaces();
} }
componentWillUnmount() { componentWillUnmount() {

View File

@ -31,7 +31,7 @@ export class DeploymentDetails extends React.Component<Props> {
}); });
componentDidMount() { componentDidMount() {
podsStore.loadContextNamespaces(); podsStore.loadAllFromContextNamespaces();
} }
componentWillUnmount() { componentWillUnmount() {

View File

@ -25,7 +25,7 @@ interface Props extends KubeObjectDetailsProps<Job> {
@observer @observer
export class JobDetails extends React.Component<Props> { export class JobDetails extends React.Component<Props> {
async componentDidMount() { async componentDidMount() {
podsStore.loadContextNamespaces(); podsStore.loadAllFromContextNamespaces();
} }
render() { render() {

View File

@ -27,7 +27,7 @@ export class OverviewStatuses extends React.Component {
@autobind() @autobind()
renderWorkload(resource: KubeResource): React.ReactElement { renderWorkload(resource: KubeResource): React.ReactElement {
const store = workloadStores[resource]; const store = workloadStores[resource];
const items = store.getAllByNs(namespaceStore.getContextNamespaces()); const items = store.getAllByNs(namespaceStore.contextNamespaces);
return ( return (
<div className="workload" key={resource}> <div className="workload" key={resource}>

View File

@ -29,7 +29,7 @@ export class ReplicaSetDetails extends React.Component<Props> {
}); });
async componentDidMount() { async componentDidMount() {
podsStore.loadContextNamespaces(); podsStore.loadAllFromContextNamespaces();
} }
componentWillUnmount() { componentWillUnmount() {

View File

@ -30,7 +30,7 @@ export class StatefulSetDetails extends React.Component<Props> {
}); });
componentDidMount() { componentDidMount() {
podsStore.loadContextNamespaces(); podsStore.loadAllFromContextNamespaces();
} }
componentWillUnmount() { componentWillUnmount() {

View File

@ -76,10 +76,9 @@ export class App extends React.Component {
}); });
whatInput.ask(); // Start to monitor user input device whatInput.ask(); // Start to monitor user input device
await namespaceStore.whenReady;
await kubeWatchApi.init({ await kubeWatchApi.init({
getCluster: getHostedCluster, getCluster: () => getHostedCluster(),
getNamespaces: namespaceStore.getContextNamespaces, getNamespaces: () => namespaceStore.contextNamespaces,
}); });
} }

View File

@ -80,7 +80,7 @@ export class UpgradeChartStore extends DockTabStore<IChartUpgradeData> {
const values = this.values.getData(tabId); const values = this.values.getData(tabId);
await Promise.all([ await Promise.all([
!releaseStore.isLoaded && releaseStore.loadSelectedNamespaces(), !releaseStore.isLoaded && releaseStore.loadFromContextNamespaces(),
!values && this.loadValues(tabId) !values && this.loadValues(tabId)
]); ]);
} }

View File

@ -138,7 +138,8 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
const { store, dependentStores } = this.props; const { store, dependentStores } = this.props;
const stores = Array.from(new Set([store, ...dependentStores])); const stores = Array.from(new Set([store, ...dependentStores]));
stores.forEach(store => store.loadAll(namespaceStore.getContextNamespaces())); // loads context namespaces by default (see also: `<NamespaceSelectFilter/>`)
stores.forEach(store => store.loadAll(namespaceStore.contextNamespaces));
} }
private filterCallbacks: { [type: string]: ItemsFilter } = { private filterCallbacks: { [type: string]: ItemsFilter } = {

View File

@ -27,10 +27,11 @@ export class PageFiltersStore {
this.syncWithContextNamespace(); this.syncWithContextNamespace();
} }
// todo: refactor
protected syncWithContextNamespace() { protected syncWithContextNamespace() {
const disposers = [ const disposers = [
reaction(() => this.getValues(FilterType.NAMESPACE), filteredNs => { reaction(() => this.getValues(FilterType.NAMESPACE), filteredNs => {
if (filteredNs.length !== namespaceStore.getContextNamespaces().length) { if (filteredNs.length !== namespaceStore.contextNamespaces.length) {
namespaceStore.setContext(filteredNs); namespaceStore.setContext(filteredNs);
} }
}), }),

View File

@ -40,7 +40,7 @@ interface Props {
@observer @observer
export class Sidebar extends React.Component<Props> { export class Sidebar extends React.Component<Props> {
async componentDidMount() { async componentDidMount() {
crdStore.loadContextNamespaces(); crdStore.loadAllFromContextNamespaces();
} }
renderCustomResources() { renderCustomResources() {

View File

@ -20,6 +20,7 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
abstract api: KubeApi<T>; abstract api: KubeApi<T>;
public readonly limit?: number; public readonly limit?: number;
public readonly bufferSize: number = 50000; public readonly bufferSize: number = 50000;
@observable.ref protected cluster: Cluster;
constructor() { constructor() {
super(); super();
@ -29,29 +30,27 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
// TODO: detach / remove circular dependency // TODO: detach / remove circular dependency
@observable.ref private namespaceStore: NamespaceStore; @observable.ref private namespaceStore: NamespaceStore;
private async resolveNamespaceStore(): Promise<NamespaceStore> { protected async resolveNamespaceStore(): Promise<NamespaceStore> {
const { namespaceStore } = await import("./components/+namespaces/namespace.store"); const { namespaceStore } = await import("./components/+namespaces/namespace.store");
await namespaceStore.whenReady;
this.namespaceStore = namespaceStore; this.namespaceStore = namespaceStore;
return namespaceStore; return namespaceStore;
} }
// TODO: detach / inject dependency as in kube-watch-api protected async resolveCluster(): Promise<Cluster> {
@observable.ref private cluster: Cluster; const { getHostedCluster, clusterStore } = await import("../common/cluster-store");
private async resolveCluster(): Promise<Cluster> {
const { getHostedCluster } = await import("../common/cluster-store");
await clusterStore.whenLoaded;
this.cluster = getHostedCluster(); this.cluster = getHostedCluster();
await this.cluster.whenReady;
return this.cluster; return this.cluster;
} }
// TODO: figure out how to transparently replace with this.items // TODO: figure out how to transparently replace with this.items
@computed get contextItems(): T[] { @computed get contextItems(): T[] {
const contextNamespaces = this.namespaceStore?.getContextNamespaces() ?? []; // not loaded const contextNamespaces = this.namespaceStore?.contextNamespaces ?? []; // not loaded
return this.items.filter((item: T) => !item.getNs() || contextNamespaces.includes(item.getId())); return this.items.filter((item: T) => !item.getNs() || contextNamespaces.includes(item.getId()));
} }
@ -132,15 +131,15 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
} }
@action @action
async loadAll(namespaces: string[] = [], { replace = false /*partial update*/ } = {}): Promise<void> { async loadAll(namespaces?: string[], { replace = false /*partial update*/ } = {}): Promise<void> {
this.isLoading = true; this.isLoading = true;
try { try {
// load all available namespaces by default // load all available namespaces by default
if (!namespaces.length) { if (!namespaces?.length) {
const namespaceStore = await this.resolveNamespaceStore(); const namespaceStore = await this.resolveNamespaceStore();
namespaces = namespaceStore.getContextNamespaces(); namespaces = namespaceStore.allowedNamespaces; // load all by default if list not provided
} }
const items = await this.loadItems({ namespaces, api: this.api }); const items = await this.loadItems({ namespaces, api: this.api });
@ -177,8 +176,8 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
return items; return items;
} }
async loadContextNamespaces(): Promise<void> { async loadAllFromContextNamespaces(): Promise<void> {
return this.loadAll(this.namespaceStore?.getContextNamespaces()); return this.loadAll(this.namespaceStore?.contextNamespaces);
} }
protected resetOnError(error: any) { protected resetOnError(error: any) {