loading k8s resources into stores per selected namespaces -- part 2

- fix: generating helm chart id

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2021-01-07 22:37:23 +02:00
1 parent 47bea52160
commit f71829ff14
8 files changed
+112 -47

No files matched your search

@@ -5,7 +5,7 @@ import { HelmRelease, helmReleasesApi, IReleaseCreatePayload, IReleaseUpdatePayl
import { ItemStore } from "../../item.store";
import { Secret } from "../../api/endpoints";
import { secretsStore } from "../+config-secrets/secrets.store";
import { getHostedCluster } from "../../../common/cluster-store";
import { namespaceStore } from "../+namespaces/namespace.store";
@autobind()
export class ReleaseStore extends ItemStore<HelmRelease> {
@@ -60,30 +60,24 @@ export class ReleaseStore extends ItemStore<HelmRelease> {
@action
async loadAll() {
this.isLoading = true;
let items;
let items: HelmRelease[];
try {
const { isAdmin, allowedNamespaces } = getHostedCluster();
items = await this.loadItems(!isAdmin ? allowedNamespaces : null);
} finally {
if (items) {
items = this.sortItems(items);
this.items.replace(items);
}
items = await this.loadItems(namespaceStore.getContextNamespaces());
items = this.sortItems(items);
this.items.replace(items);
this.isLoaded = true;
} catch (error) {
console.error(`Loading Helm Chart releases has failed: ${error}`);
} finally {
this.isLoading = false;
}
}
async loadItems(namespaces?: string[]) {
if (!namespaces) {
return helmReleasesApi.list();
} else {
return Promise
.all(namespaces.map(namespace => helmReleasesApi.list(namespace)))
.then(items => items.flat());
}
async loadItems(namespaces: string[]) {
return Promise
.all(namespaces.map(namespace => helmReleasesApi.list(namespace)))
.then(items => items.flat());
}
async create(payload: IReleaseCreatePayload) {
@@ -1,11 +1,11 @@
import { action, comparer, observable, reaction } from "mobx";
import { action, comparer, observable, reaction, when } 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 { isAllowedResource } from "../../../common/rbac";
import { getHostedCluster } from "../../../common/cluster-store";
import { clusterStore, getHostedCluster } from "../../../common/cluster-store";
const storage = createStorage<string[]>("context_namespaces", []);
@@ -21,14 +21,25 @@ export const namespaceUrlParam = createPageParam<string[]>({
@autobind()
export class NamespaceStore extends KubeObjectStore<Namespace> {
api = namespacesApi;
contextNs = observable.array<string>();
@observable contextNs = observable.array<string>();
@observable isReady = false;
whenReady = when(() => this.isReady);
constructor() {
super();
this.init();
}
private init() {
private async init() {
await clusterStore.whenLoaded;
if (!getHostedCluster()) return;
await getHostedCluster().whenReady; // wait for cluster-state from main
await this.loadAll(); // auto-load allowed namespaces
this.isReady = true;
this.setContext(this.initNamespaces);
return reaction(() => this.contextNs.toJS(), namespaces => {
@@ -40,13 +51,52 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
});
}
get initNamespaces() {
return namespaceUrlParam.get();
get allowedNamespaces(): string[] {
return getHostedCluster().allowedNamespaces;
}
// FIXME: page/app reload doesn't restore previously selected namespaces
get initNamespaces() {
const allowedNamespaces = new Set(this.allowedNamespaces);
const lastUsedNamespaces = new Set(storage.get());
// remove previously saved, but currently disallowed namespaces
lastUsedNamespaces.forEach(namespace => {
if (!allowedNamespaces.has(namespace)) {
lastUsedNamespaces.delete(namespace);
}
});
// return previously saved and currently allowed namespaces
if (lastUsedNamespaces.size) {
return Array.from(lastUsedNamespaces);
}
// otherwise select "default" or first allowed namespace
else {
if (allowedNamespaces.has("default")) {
return ["default"];
} else if (allowedNamespaces.size) {
return [Array.from(allowedNamespaces)[0]];
}
}
return [];
}
getContextNamespaces(): string[] {
let namespaces = this.contextNs.toJS();
if (!namespaces.length) {
return [...this.allowedNamespaces]; // show all namespaces when nothing selected
}
return namespaces;
}
/**
* @deprecated
*/
getContextParams() {
return {
namespaces: this.contextNs.toJS(),
namespaces: this.getContextNamespaces(),
};
}
@@ -61,6 +111,12 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
return super.subscribe(apis);
}
async loadAll() {
return super.loadAll({
namespaces: this.allowedNamespaces,
});
}
protected async loadItems({ isAdmin, namespaces }: KubeObjectStoreLoadingParams) {
if (!isAllowedResource("namespaces")) {
return namespaces.map(this.getDummyNamespace);
@@ -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.contextNs);
const items = store.getAllByNs(namespaceStore.getContextNamespaces());
return (
<div className="workload" key={resource}>
@@ -110,6 +110,7 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
]);
}
// FIXME: reload and re-subscribe stores when context namespaces changed
async componentDidMount() {
const { store, dependentStores, isClusterScoped } = this.props;
const stores = [store, ...dependentStores];