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:
parent
3394b74f10
commit
bba37d6111
@ -73,8 +73,8 @@ export class ReleaseStore extends ItemStore<HelmRelease> {
|
||||
}
|
||||
}
|
||||
|
||||
async loadSelectedNamespaces(): Promise<void> {
|
||||
return this.loadAll(namespaceStore.getContextNamespaces());
|
||||
async loadFromContextNamespaces(): Promise<void> {
|
||||
return this.loadAll(namespaceStore.contextNamespaces);
|
||||
}
|
||||
|
||||
async loadItems(namespaces: string[]) {
|
||||
@ -86,7 +86,7 @@ export class ReleaseStore extends ItemStore<HelmRelease> {
|
||||
async create(payload: IReleaseCreatePayload) {
|
||||
const response = await helmReleasesApi.create(payload);
|
||||
|
||||
if (this.isLoaded) this.loadSelectedNamespaces();
|
||||
if (this.isLoaded) this.loadFromContextNamespaces();
|
||||
|
||||
return response;
|
||||
}
|
||||
@ -94,7 +94,7 @@ export class ReleaseStore extends ItemStore<HelmRelease> {
|
||||
async update(name: string, namespace: string, payload: IReleaseUpdatePayload) {
|
||||
const response = await helmReleasesApi.update(name, namespace, payload);
|
||||
|
||||
if (this.isLoaded) this.loadSelectedNamespaces();
|
||||
if (this.isLoaded) this.loadFromContextNamespaces();
|
||||
|
||||
return response;
|
||||
}
|
||||
@ -102,7 +102,7 @@ export class ReleaseStore extends ItemStore<HelmRelease> {
|
||||
async rollback(name: string, namespace: string, revision: number) {
|
||||
const response = await helmReleasesApi.rollback(name, namespace, revision);
|
||||
|
||||
if (this.isLoaded) this.loadSelectedNamespaces();
|
||||
if (this.isLoaded) this.loadFromContextNamespaces();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ export class CrdResources extends React.Component<Props> {
|
||||
const { store } = this;
|
||||
|
||||
if (store && !store.isLoading && !store.isLoaded) {
|
||||
store.loadContextNamespaces();
|
||||
store.loadAllFromContextNamespaces();
|
||||
}
|
||||
})
|
||||
]);
|
||||
|
||||
@ -14,7 +14,7 @@ export interface KubeEventDetailsProps {
|
||||
@observer
|
||||
export class KubeEventDetails extends React.Component<KubeEventDetailsProps> {
|
||||
async componentDidMount() {
|
||||
eventStore.loadContextNamespaces();
|
||||
eventStore.loadAllFromContextNamespaces();
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
@ -32,8 +32,8 @@ export class NamespaceDetails extends React.Component<Props> {
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
resourceQuotaStore.loadContextNamespaces();
|
||||
limitRangeStore.loadContextNamespaces();
|
||||
resourceQuotaStore.loadAllFromContextNamespaces();
|
||||
limitRangeStore.loadAllFromContextNamespaces();
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
@ -82,7 +82,7 @@ export class NamespaceSelect extends React.Component<Props> {
|
||||
@observer
|
||||
export class NamespaceSelectFilter extends React.Component {
|
||||
@computed get placeholder(): React.ReactNode {
|
||||
const namespaces = namespaceStore.getContextNamespaces();
|
||||
const namespaces = namespaceStore.contextNamespaces;
|
||||
|
||||
switch (namespaces.length) {
|
||||
case 0:
|
||||
|
||||
@ -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 { KubeObjectStore, KubeObjectStoreLoadingParams } from "../../kube-object.store";
|
||||
import { Namespace, namespacesApi } from "../../api/endpoints/namespaces.api";
|
||||
import { createPageParam } from "../../navigation";
|
||||
import { apiManager } from "../../api/api-manager";
|
||||
import { clusterStore, getHostedCluster } from "../../../common/cluster-store";
|
||||
|
||||
const storage = createStorage<string[]>("context_namespaces", []);
|
||||
|
||||
@ -35,9 +34,6 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
|
||||
api = namespacesApi;
|
||||
|
||||
@observable private contextNs = observable.set<string>();
|
||||
@observable isReady = false;
|
||||
|
||||
whenReady = when(() => this.isReady);
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
@ -45,15 +41,12 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
|
||||
}
|
||||
|
||||
private async init() {
|
||||
await clusterStore.whenLoaded;
|
||||
if (!getHostedCluster()) return;
|
||||
await getHostedCluster().whenReady; // wait for cluster-state from main
|
||||
await this.resolveCluster();
|
||||
if (!this.cluster) return; // skip for non-cluster context window
|
||||
|
||||
this.setContext(this.initialNamespaces);
|
||||
this.autoLoadAllowedNamespaces();
|
||||
this.autoUpdateUrlAndLocalStorage();
|
||||
|
||||
this.isReady = true;
|
||||
}
|
||||
|
||||
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
|
||||
private get initialNamespaces(): string[] {
|
||||
const namespaces = new Set(this.allowedNamespaces);
|
||||
@ -103,27 +92,26 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
|
||||
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);
|
||||
|
||||
// show all namespaces when nothing selected
|
||||
if (!namespaces.length) {
|
||||
// return actual namespaces list since "allowedNamespaces" updating every 30s in cluster and thus might be stale
|
||||
if (this.isLoaded) {
|
||||
return this.items.map(namespace => namespace.getName());
|
||||
}
|
||||
|
||||
return this.allowedNamespaces;
|
||||
return this.allowedNamespaces; // show all namespaces when nothing selected
|
||||
}
|
||||
|
||||
return namespaces;
|
||||
}
|
||||
|
||||
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 (accessibleNamespaces.length > 0) {
|
||||
if (this.cluster?.accessibleNamespaces.length > 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
@ -152,7 +140,7 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
|
||||
}
|
||||
|
||||
@action
|
||||
resetContext(){
|
||||
resetContext() {
|
||||
this.contextNs.clear();
|
||||
}
|
||||
|
||||
|
||||
@ -29,7 +29,7 @@ export class NodeDetails extends React.Component<Props> {
|
||||
});
|
||||
|
||||
async componentDidMount() {
|
||||
podsStore.loadContextNamespaces();
|
||||
podsStore.loadAllFromContextNamespaces();
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
|
||||
@ -80,7 +80,7 @@ export class AddRoleBindingDialog extends React.Component<Props> {
|
||||
];
|
||||
|
||||
this.isLoading = true;
|
||||
await Promise.all(stores.map(store => store.loadContextNamespaces()));
|
||||
await Promise.all(stores.map(store => store.loadAllFromContextNamespaces()));
|
||||
this.isLoading = false;
|
||||
}
|
||||
|
||||
|
||||
@ -20,7 +20,7 @@ interface Props extends KubeObjectDetailsProps<CronJob> {
|
||||
@observer
|
||||
export class CronJobDetails extends React.Component<Props> {
|
||||
async componentDidMount() {
|
||||
jobStore.loadContextNamespaces();
|
||||
jobStore.loadAllFromContextNamespaces();
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
@ -30,7 +30,7 @@ export class DaemonSetDetails extends React.Component<Props> {
|
||||
});
|
||||
|
||||
componentDidMount() {
|
||||
podsStore.loadContextNamespaces();
|
||||
podsStore.loadAllFromContextNamespaces();
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
|
||||
@ -31,7 +31,7 @@ export class DeploymentDetails extends React.Component<Props> {
|
||||
});
|
||||
|
||||
componentDidMount() {
|
||||
podsStore.loadContextNamespaces();
|
||||
podsStore.loadAllFromContextNamespaces();
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
|
||||
@ -25,7 +25,7 @@ interface Props extends KubeObjectDetailsProps<Job> {
|
||||
@observer
|
||||
export class JobDetails extends React.Component<Props> {
|
||||
async componentDidMount() {
|
||||
podsStore.loadContextNamespaces();
|
||||
podsStore.loadAllFromContextNamespaces();
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
@ -27,7 +27,7 @@ export class OverviewStatuses extends React.Component {
|
||||
@autobind()
|
||||
renderWorkload(resource: KubeResource): React.ReactElement {
|
||||
const store = workloadStores[resource];
|
||||
const items = store.getAllByNs(namespaceStore.getContextNamespaces());
|
||||
const items = store.getAllByNs(namespaceStore.contextNamespaces);
|
||||
|
||||
return (
|
||||
<div className="workload" key={resource}>
|
||||
|
||||
@ -29,7 +29,7 @@ export class ReplicaSetDetails extends React.Component<Props> {
|
||||
});
|
||||
|
||||
async componentDidMount() {
|
||||
podsStore.loadContextNamespaces();
|
||||
podsStore.loadAllFromContextNamespaces();
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
|
||||
@ -30,7 +30,7 @@ export class StatefulSetDetails extends React.Component<Props> {
|
||||
});
|
||||
|
||||
componentDidMount() {
|
||||
podsStore.loadContextNamespaces();
|
||||
podsStore.loadAllFromContextNamespaces();
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
|
||||
@ -76,10 +76,9 @@ export class App extends React.Component {
|
||||
});
|
||||
whatInput.ask(); // Start to monitor user input device
|
||||
|
||||
await namespaceStore.whenReady;
|
||||
await kubeWatchApi.init({
|
||||
getCluster: getHostedCluster,
|
||||
getNamespaces: namespaceStore.getContextNamespaces,
|
||||
getCluster: () => getHostedCluster(),
|
||||
getNamespaces: () => namespaceStore.contextNamespaces,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -80,7 +80,7 @@ export class UpgradeChartStore extends DockTabStore<IChartUpgradeData> {
|
||||
const values = this.values.getData(tabId);
|
||||
|
||||
await Promise.all([
|
||||
!releaseStore.isLoaded && releaseStore.loadSelectedNamespaces(),
|
||||
!releaseStore.isLoaded && releaseStore.loadFromContextNamespaces(),
|
||||
!values && this.loadValues(tabId)
|
||||
]);
|
||||
}
|
||||
|
||||
@ -138,7 +138,8 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
|
||||
const { store, dependentStores } = this.props;
|
||||
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 } = {
|
||||
|
||||
@ -27,10 +27,11 @@ export class PageFiltersStore {
|
||||
this.syncWithContextNamespace();
|
||||
}
|
||||
|
||||
// todo: refactor
|
||||
protected syncWithContextNamespace() {
|
||||
const disposers = [
|
||||
reaction(() => this.getValues(FilterType.NAMESPACE), filteredNs => {
|
||||
if (filteredNs.length !== namespaceStore.getContextNamespaces().length) {
|
||||
if (filteredNs.length !== namespaceStore.contextNamespaces.length) {
|
||||
namespaceStore.setContext(filteredNs);
|
||||
}
|
||||
}),
|
||||
|
||||
@ -40,7 +40,7 @@ interface Props {
|
||||
@observer
|
||||
export class Sidebar extends React.Component<Props> {
|
||||
async componentDidMount() {
|
||||
crdStore.loadContextNamespaces();
|
||||
crdStore.loadAllFromContextNamespaces();
|
||||
}
|
||||
|
||||
renderCustomResources() {
|
||||
|
||||
@ -20,6 +20,7 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
||||
abstract api: KubeApi<T>;
|
||||
public readonly limit?: number;
|
||||
public readonly bufferSize: number = 50000;
|
||||
@observable.ref protected cluster: Cluster;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
@ -29,29 +30,27 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
||||
// TODO: detach / remove circular dependency
|
||||
@observable.ref private namespaceStore: NamespaceStore;
|
||||
|
||||
private async resolveNamespaceStore(): Promise<NamespaceStore> {
|
||||
protected async resolveNamespaceStore(): Promise<NamespaceStore> {
|
||||
const { namespaceStore } = await import("./components/+namespaces/namespace.store");
|
||||
|
||||
await namespaceStore.whenReady;
|
||||
this.namespaceStore = namespaceStore;
|
||||
|
||||
return namespaceStore;
|
||||
}
|
||||
|
||||
// TODO: detach / inject dependency as in kube-watch-api
|
||||
@observable.ref private cluster: Cluster;
|
||||
|
||||
private async resolveCluster(): Promise<Cluster> {
|
||||
const { getHostedCluster } = await import("../common/cluster-store");
|
||||
protected async resolveCluster(): Promise<Cluster> {
|
||||
const { getHostedCluster, clusterStore } = await import("../common/cluster-store");
|
||||
|
||||
await clusterStore.whenLoaded;
|
||||
this.cluster = getHostedCluster();
|
||||
await this.cluster.whenReady;
|
||||
|
||||
return this.cluster;
|
||||
}
|
||||
|
||||
// TODO: figure out how to transparently replace with this.items
|
||||
@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()));
|
||||
}
|
||||
@ -132,15 +131,15 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
||||
}
|
||||
|
||||
@action
|
||||
async loadAll(namespaces: string[] = [], { replace = false /*partial update*/ } = {}): Promise<void> {
|
||||
async loadAll(namespaces?: string[], { replace = false /*partial update*/ } = {}): Promise<void> {
|
||||
this.isLoading = true;
|
||||
|
||||
try {
|
||||
// load all available namespaces by default
|
||||
if (!namespaces.length) {
|
||||
if (!namespaces?.length) {
|
||||
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 });
|
||||
@ -177,8 +176,8 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
||||
return items;
|
||||
}
|
||||
|
||||
async loadContextNamespaces(): Promise<void> {
|
||||
return this.loadAll(this.namespaceStore?.getContextNamespaces());
|
||||
async loadAllFromContextNamespaces(): Promise<void> {
|
||||
return this.loadAll(this.namespaceStore?.contextNamespaces);
|
||||
}
|
||||
|
||||
protected resetOnError(error: any) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user