mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Revert "Load k8s resources only for selected namespaces (#1918)"
This reverts commit f8c111ddd8.
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
parent
6f7cb4d568
commit
46fa5bbc11
@ -7,38 +7,37 @@ export type KubeResource =
|
|||||||
"endpoints" | "customresourcedefinitions" | "horizontalpodautoscalers" | "podsecuritypolicies" | "poddisruptionbudgets";
|
"endpoints" | "customresourcedefinitions" | "horizontalpodautoscalers" | "podsecuritypolicies" | "poddisruptionbudgets";
|
||||||
|
|
||||||
export interface KubeApiResource {
|
export interface KubeApiResource {
|
||||||
kind: string; // resource type (e.g. "Namespace")
|
resource: KubeResource; // valid resource name
|
||||||
apiName: KubeResource; // valid api resource name (e.g. "namespaces")
|
|
||||||
group?: string; // api-group
|
group?: string; // api-group
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: auto-populate all resources dynamically (see: kubectl api-resources -o=wide -v=7)
|
// TODO: auto-populate all resources dynamically (see: kubectl api-resources -o=wide -v=7)
|
||||||
export const apiResources: KubeApiResource[] = [
|
export const apiResources: KubeApiResource[] = [
|
||||||
{ kind: "ConfigMap", apiName: "configmaps" },
|
{ resource: "configmaps" },
|
||||||
{ kind: "CronJob", apiName: "cronjobs", group: "batch" },
|
{ resource: "cronjobs", group: "batch" },
|
||||||
{ kind: "CustomResourceDefinition", apiName: "customresourcedefinitions", group: "apiextensions.k8s.io" },
|
{ resource: "customresourcedefinitions", group: "apiextensions.k8s.io" },
|
||||||
{ kind: "DaemonSet", apiName: "daemonsets", group: "apps" },
|
{ resource: "daemonsets", group: "apps" },
|
||||||
{ kind: "Deployment", apiName: "deployments", group: "apps" },
|
{ resource: "deployments", group: "apps" },
|
||||||
{ kind: "Endpoint", apiName: "endpoints" },
|
{ resource: "endpoints" },
|
||||||
{ kind: "Event", apiName: "events" },
|
{ resource: "events" },
|
||||||
{ kind: "HorizontalPodAutoscaler", apiName: "horizontalpodautoscalers" },
|
{ resource: "horizontalpodautoscalers" },
|
||||||
{ kind: "Ingress", apiName: "ingresses", group: "networking.k8s.io" },
|
{ resource: "ingresses", group: "networking.k8s.io" },
|
||||||
{ kind: "Job", apiName: "jobs", group: "batch" },
|
{ resource: "jobs", group: "batch" },
|
||||||
{ kind: "Namespace", apiName: "namespaces" },
|
{ resource: "limitranges" },
|
||||||
{ kind: "LimitRange", apiName: "limitranges" },
|
{ resource: "namespaces" },
|
||||||
{ kind: "NetworkPolicy", apiName: "networkpolicies", group: "networking.k8s.io" },
|
{ resource: "networkpolicies", group: "networking.k8s.io" },
|
||||||
{ kind: "Node", apiName: "nodes" },
|
{ resource: "nodes" },
|
||||||
{ kind: "PersistentVolume", apiName: "persistentvolumes" },
|
{ resource: "persistentvolumes" },
|
||||||
{ kind: "PersistentVolumeClaim", apiName: "persistentvolumeclaims" },
|
{ resource: "persistentvolumeclaims" },
|
||||||
{ kind: "Pod", apiName: "pods" },
|
{ resource: "pods" },
|
||||||
{ kind: "PodDisruptionBudget", apiName: "poddisruptionbudgets" },
|
{ resource: "poddisruptionbudgets" },
|
||||||
{ kind: "PodSecurityPolicy", apiName: "podsecuritypolicies" },
|
{ resource: "podsecuritypolicies" },
|
||||||
{ kind: "ResourceQuota", apiName: "resourcequotas" },
|
{ resource: "resourcequotas" },
|
||||||
{ kind: "ReplicaSet", apiName: "replicasets", group: "apps" },
|
{ resource: "replicasets", group: "apps" },
|
||||||
{ kind: "Secret", apiName: "secrets" },
|
{ resource: "secrets" },
|
||||||
{ kind: "Service", apiName: "services" },
|
{ resource: "services" },
|
||||||
{ kind: "StatefulSet", apiName: "statefulsets", group: "apps" },
|
{ resource: "statefulsets", group: "apps" },
|
||||||
{ kind: "StorageClass", apiName: "storageclasses", group: "storage.k8s.io" },
|
{ resource: "storageclasses", group: "storage.k8s.io" },
|
||||||
];
|
];
|
||||||
|
|
||||||
export function isAllowedResource(resources: KubeResource | KubeResource[]) {
|
export function isAllowedResource(resources: KubeResource | KubeResource[]) {
|
||||||
|
|||||||
@ -190,7 +190,7 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
*/
|
*/
|
||||||
@observable metadata: ClusterMetadata = {};
|
@observable metadata: ClusterMetadata = {};
|
||||||
/**
|
/**
|
||||||
* List of allowed namespaces verified via K8S::SelfSubjectAccessReview api
|
* List of allowed namespaces
|
||||||
*
|
*
|
||||||
* @observable
|
* @observable
|
||||||
*/
|
*/
|
||||||
@ -203,7 +203,7 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
*/
|
*/
|
||||||
@observable allowedResources: string[] = [];
|
@observable allowedResources: string[] = [];
|
||||||
/**
|
/**
|
||||||
* List of accessible namespaces provided by user in the Cluster Settings
|
* List of accessible namespaces
|
||||||
*
|
*
|
||||||
* @observable
|
* @observable
|
||||||
*/
|
*/
|
||||||
@ -224,7 +224,7 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
* @computed
|
* @computed
|
||||||
*/
|
*/
|
||||||
@computed get name() {
|
@computed get name() {
|
||||||
return this.preferences.clusterName || this.contextName;
|
return this.preferences.clusterName || this.contextName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -279,8 +279,7 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
* @param port port where internal auth proxy is listening
|
* @param port port where internal auth proxy is listening
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@action
|
@action async init(port: number) {
|
||||||
async init(port: number) {
|
|
||||||
try {
|
try {
|
||||||
this.initializing = true;
|
this.initializing = true;
|
||||||
this.contextHandler = new ContextHandler(this);
|
this.contextHandler = new ContextHandler(this);
|
||||||
@ -335,8 +334,7 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
* @param force force activation
|
* @param force force activation
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@action
|
@action async activate(force = false) {
|
||||||
async activate(force = false) {
|
|
||||||
if (this.activated && !force) {
|
if (this.activated && !force) {
|
||||||
return this.pushState();
|
return this.pushState();
|
||||||
}
|
}
|
||||||
@ -375,8 +373,7 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@action
|
@action async reconnect() {
|
||||||
async reconnect() {
|
|
||||||
logger.info(`[CLUSTER]: reconnect`, this.getMeta());
|
logger.info(`[CLUSTER]: reconnect`, this.getMeta());
|
||||||
this.contextHandler?.stopServer();
|
this.contextHandler?.stopServer();
|
||||||
await this.contextHandler?.ensureServer();
|
await this.contextHandler?.ensureServer();
|
||||||
@ -403,8 +400,7 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
* @internal
|
* @internal
|
||||||
* @param opts refresh options
|
* @param opts refresh options
|
||||||
*/
|
*/
|
||||||
@action
|
@action async refresh(opts: ClusterRefreshOptions = {}) {
|
||||||
async refresh(opts: ClusterRefreshOptions = {}) {
|
|
||||||
logger.info(`[CLUSTER]: refresh`, this.getMeta());
|
logger.info(`[CLUSTER]: refresh`, this.getMeta());
|
||||||
await this.whenInitialized;
|
await this.whenInitialized;
|
||||||
await this.refreshConnectionStatus();
|
await this.refreshConnectionStatus();
|
||||||
@ -424,8 +420,7 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@action
|
@action async refreshMetadata() {
|
||||||
async refreshMetadata() {
|
|
||||||
logger.info(`[CLUSTER]: refreshMetadata`, this.getMeta());
|
logger.info(`[CLUSTER]: refreshMetadata`, this.getMeta());
|
||||||
const metadata = await detectorRegistry.detectForCluster(this);
|
const metadata = await detectorRegistry.detectForCluster(this);
|
||||||
const existingMetadata = this.metadata;
|
const existingMetadata = this.metadata;
|
||||||
@ -436,8 +431,7 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@action
|
@action async refreshConnectionStatus() {
|
||||||
async refreshConnectionStatus() {
|
|
||||||
const connectionStatus = await this.getConnectionStatus();
|
const connectionStatus = await this.getConnectionStatus();
|
||||||
|
|
||||||
this.online = connectionStatus > ClusterStatus.Offline;
|
this.online = connectionStatus > ClusterStatus.Offline;
|
||||||
@ -447,8 +441,7 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@action
|
@action async refreshAllowedResources() {
|
||||||
async refreshAllowedResources() {
|
|
||||||
this.allowedNamespaces = await this.getAllowedNamespaces();
|
this.allowedNamespaces = await this.getAllowedNamespaces();
|
||||||
this.allowedResources = await this.getAllowedResources();
|
this.allowedResources = await this.getAllowedResources();
|
||||||
}
|
}
|
||||||
@ -675,7 +668,7 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
for (const namespace of this.allowedNamespaces.slice(0, 10)) {
|
for (const namespace of this.allowedNamespaces.slice(0, 10)) {
|
||||||
if (!this.resourceAccessStatuses.get(apiResource)) {
|
if (!this.resourceAccessStatuses.get(apiResource)) {
|
||||||
const result = await this.canI({
|
const result = await this.canI({
|
||||||
resource: apiResource.apiName,
|
resource: apiResource.resource,
|
||||||
group: apiResource.group,
|
group: apiResource.group,
|
||||||
verb: "list",
|
verb: "list",
|
||||||
namespace
|
namespace
|
||||||
@ -690,19 +683,9 @@ export class Cluster implements ClusterModel, ClusterState {
|
|||||||
|
|
||||||
return apiResources
|
return apiResources
|
||||||
.filter((resource) => this.resourceAccessStatuses.get(resource))
|
.filter((resource) => this.resourceAccessStatuses.get(resource))
|
||||||
.map(apiResource => apiResource.apiName);
|
.map(apiResource => apiResource.resource);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isAllowedResource(kind: string): boolean {
|
|
||||||
const apiResource = apiResources.find(resource => resource.kind === kind || resource.apiName === kind);
|
|
||||||
|
|
||||||
if (apiResource) {
|
|
||||||
return this.allowedResources.includes(apiResource.apiName);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true; // allowed by default for other resources
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import { apiPrefix, isDevelopment } from "../../common/vars";
|
|||||||
import { getHostedCluster } from "../../common/cluster-store";
|
import { getHostedCluster } from "../../common/cluster-store";
|
||||||
|
|
||||||
export interface IKubeWatchEvent<T = any> {
|
export interface IKubeWatchEvent<T = any> {
|
||||||
type: "ADDED" | "MODIFIED" | "DELETED" | "ERROR";
|
type: "ADDED" | "MODIFIED" | "DELETED";
|
||||||
object?: T;
|
object?: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,41 +62,27 @@ export class KubeWatchApi {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: use POST to send apis for subscribing (list could be huge)
|
protected getQuery(): Partial<IKubeWatchRouteQuery> {
|
||||||
// TODO: try to use normal fetch res.body stream to consume watch-api updates
|
const { isAdmin, allowedNamespaces } = getHostedCluster();
|
||||||
// https://github.com/lensapp/lens/issues/1898
|
|
||||||
protected async getQuery() {
|
|
||||||
const { namespaceStore } = await import("../components/+namespaces/namespace.store");
|
|
||||||
|
|
||||||
await namespaceStore.whenReady;
|
|
||||||
const { isAdmin } = getHostedCluster();
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
api: this.activeApis.map(api => {
|
api: this.activeApis.map(api => {
|
||||||
if (isAdmin && !api.isNamespaced) {
|
if (isAdmin) return api.getWatchUrl();
|
||||||
return api.getWatchUrl();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (api.isNamespaced) {
|
return allowedNamespaces.map(namespace => api.getWatchUrl(namespace));
|
||||||
return namespaceStore.getContextNamespaces().map(namespace => api.getWatchUrl(namespace));
|
|
||||||
}
|
|
||||||
|
|
||||||
return [];
|
|
||||||
}).flat()
|
}).flat()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: maybe switch to websocket to avoid often reconnects
|
// todo: maybe switch to websocket to avoid often reconnects
|
||||||
@autobind()
|
@autobind()
|
||||||
protected async connect() {
|
protected connect() {
|
||||||
if (this.evtSource) this.disconnect(); // close previous connection
|
if (this.evtSource) this.disconnect(); // close previous connection
|
||||||
|
|
||||||
const query = await this.getQuery();
|
if (!this.activeApis.length) {
|
||||||
|
|
||||||
if (!this.activeApis.length || !query.api.length) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const query = this.getQuery();
|
||||||
const apiUrl = `${apiPrefix}/watch?${stringify(query)}`;
|
const apiUrl = `${apiPrefix}/watch?${stringify(query)}`;
|
||||||
|
|
||||||
this.evtSource = new EventSource(apiUrl);
|
this.evtSource = new EventSource(apiUrl);
|
||||||
@ -172,10 +158,6 @@ export class KubeWatchApi {
|
|||||||
|
|
||||||
addListener(store: KubeObjectStore, callback: (evt: IKubeWatchEvent) => void) {
|
addListener(store: KubeObjectStore, callback: (evt: IKubeWatchEvent) => void) {
|
||||||
const listener = (evt: IKubeWatchEvent<KubeJsonApiData>) => {
|
const listener = (evt: IKubeWatchEvent<KubeJsonApiData>) => {
|
||||||
if (evt.type === "ERROR") {
|
|
||||||
return; // e.g. evt.object.message == "too old resource version"
|
|
||||||
}
|
|
||||||
|
|
||||||
const { namespace, resourceVersion } = evt.object.metadata;
|
const { namespace, resourceVersion } = evt.object.metadata;
|
||||||
const api = apiManager.getApiByKind(evt.object.kind, evt.object.apiVersion);
|
const api = apiManager.getApiByKind(evt.object.kind, evt.object.apiVersion);
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import { HelmRelease, helmReleasesApi, IReleaseCreatePayload, IReleaseUpdatePayl
|
|||||||
import { ItemStore } from "../../item.store";
|
import { ItemStore } from "../../item.store";
|
||||||
import { Secret } from "../../api/endpoints";
|
import { Secret } from "../../api/endpoints";
|
||||||
import { secretsStore } from "../+config-secrets/secrets.store";
|
import { secretsStore } from "../+config-secrets/secrets.store";
|
||||||
import { namespaceStore } from "../+namespaces/namespace.store";
|
import { getHostedCluster } from "../../../common/cluster-store";
|
||||||
|
|
||||||
@autobind()
|
@autobind()
|
||||||
export class ReleaseStore extends ItemStore<HelmRelease> {
|
export class ReleaseStore extends ItemStore<HelmRelease> {
|
||||||
@ -60,23 +60,30 @@ export class ReleaseStore extends ItemStore<HelmRelease> {
|
|||||||
@action
|
@action
|
||||||
async loadAll() {
|
async loadAll() {
|
||||||
this.isLoading = true;
|
this.isLoading = true;
|
||||||
|
let items;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const items = await this.loadItems(namespaceStore.getContextNamespaces());
|
const { isAdmin, allowedNamespaces } = getHostedCluster();
|
||||||
|
|
||||||
this.items.replace(this.sortItems(items));
|
items = await this.loadItems(!isAdmin ? allowedNamespaces : null);
|
||||||
this.isLoaded = true;
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Loading Helm Chart releases has failed: ${error}`);
|
|
||||||
} finally {
|
} finally {
|
||||||
|
if (items) {
|
||||||
|
items = this.sortItems(items);
|
||||||
|
this.items.replace(items);
|
||||||
|
}
|
||||||
|
this.isLoaded = true;
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadItems(namespaces: string[]) {
|
async loadItems(namespaces?: string[]) {
|
||||||
return Promise
|
if (!namespaces) {
|
||||||
.all(namespaces.map(namespace => helmReleasesApi.list(namespace)))
|
return helmReleasesApi.list();
|
||||||
.then(items => items.flat());
|
} else {
|
||||||
|
return Promise
|
||||||
|
.all(namespaces.map(namespace => helmReleasesApi.list(namespace)))
|
||||||
|
.then(items => items.flat());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(payload: IReleaseCreatePayload) {
|
async create(payload: IReleaseCreatePayload) {
|
||||||
|
|||||||
@ -1,120 +1,53 @@
|
|||||||
import { action, comparer, IReactionDisposer, IReactionOptions, observable, reaction, toJS, when } from "mobx";
|
import { action, comparer, observable, reaction } from "mobx";
|
||||||
import { autobind, createStorage } from "../../utils";
|
import { autobind, createStorage } from "../../utils";
|
||||||
import { KubeObjectStore, KubeObjectStoreLoadingParams } from "../../kube-object.store";
|
import { KubeObjectStore } from "../../kube-object.store";
|
||||||
import { Namespace, namespacesApi } from "../../api/endpoints/namespaces.api";
|
import { Namespace, namespacesApi } from "../../api/endpoints";
|
||||||
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";
|
import { isAllowedResource } from "../../../common/rbac";
|
||||||
|
import { getHostedCluster } from "../../../common/cluster-store";
|
||||||
|
|
||||||
const storage = createStorage<string[]>("context_namespaces");
|
const storage = createStorage<string[]>("context_namespaces", []);
|
||||||
|
|
||||||
export const namespaceUrlParam = createPageParam<string[]>({
|
export const namespaceUrlParam = createPageParam<string[]>({
|
||||||
name: "namespaces",
|
name: "namespaces",
|
||||||
isSystem: true,
|
isSystem: true,
|
||||||
multiValues: true,
|
multiValues: true,
|
||||||
get defaultValue() {
|
get defaultValue() {
|
||||||
return storage.get() ?? []; // initial namespaces coming from URL or local-storage (default)
|
return storage.get(); // initial namespaces coming from URL or local-storage (default)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export function getDummyNamespace(name: string) {
|
|
||||||
return new Namespace({
|
|
||||||
kind: Namespace.kind,
|
|
||||||
apiVersion: "v1",
|
|
||||||
metadata: {
|
|
||||||
name,
|
|
||||||
uid: "",
|
|
||||||
resourceVersion: "",
|
|
||||||
selfLink: `/api/v1/namespaces/${name}`
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@autobind()
|
@autobind()
|
||||||
export class NamespaceStore extends KubeObjectStore<Namespace> {
|
export class NamespaceStore extends KubeObjectStore<Namespace> {
|
||||||
api = namespacesApi;
|
api = namespacesApi;
|
||||||
|
contextNs = observable.array<string>();
|
||||||
@observable contextNs = observable.array<string>();
|
|
||||||
@observable isReady = false;
|
|
||||||
|
|
||||||
whenReady = when(() => this.isReady);
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async init() {
|
private init() {
|
||||||
await clusterStore.whenLoaded;
|
this.setContext(this.initNamespaces);
|
||||||
if (!getHostedCluster()) return;
|
|
||||||
await getHostedCluster().whenReady; // wait for cluster-state from main
|
|
||||||
|
|
||||||
this.setContext(this.initialNamespaces);
|
return reaction(() => this.contextNs.toJS(), namespaces => {
|
||||||
this.autoLoadAllowedNamespaces();
|
|
||||||
this.autoUpdateUrlAndLocalStorage();
|
|
||||||
|
|
||||||
this.isReady = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public onContextChange(callback: (contextNamespaces: string[]) => void, opts: IReactionOptions = {}): IReactionDisposer {
|
|
||||||
return reaction(() => this.contextNs.toJS(), callback, {
|
|
||||||
equals: comparer.shallow,
|
|
||||||
...opts,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private autoUpdateUrlAndLocalStorage(): IReactionDisposer {
|
|
||||||
return this.onContextChange(namespaces => {
|
|
||||||
storage.set(namespaces); // save to local-storage
|
storage.set(namespaces); // save to local-storage
|
||||||
namespaceUrlParam.set(namespaces, { replaceHistory: true }); // update url
|
namespaceUrlParam.set(namespaces, { replaceHistory: true }); // update url
|
||||||
}, {
|
}, {
|
||||||
fireImmediately: true,
|
fireImmediately: true,
|
||||||
|
equals: comparer.identity,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private autoLoadAllowedNamespaces(): IReactionDisposer {
|
get initNamespaces() {
|
||||||
return reaction(() => this.allowedNamespaces, () => this.loadAll(), {
|
return namespaceUrlParam.get();
|
||||||
fireImmediately: true,
|
|
||||||
equals: comparer.shallow,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get allowedNamespaces(): string[] {
|
getContextParams() {
|
||||||
return toJS(getHostedCluster().allowedNamespaces);
|
return {
|
||||||
}
|
namespaces: this.contextNs.toJS(),
|
||||||
|
};
|
||||||
private get initialNamespaces(): string[] {
|
|
||||||
const allowed = new Set(this.allowedNamespaces);
|
|
||||||
const prevSelected = storage.get();
|
|
||||||
|
|
||||||
if (Array.isArray(prevSelected)) {
|
|
||||||
return prevSelected.filter(namespace => allowed.has(namespace));
|
|
||||||
}
|
|
||||||
|
|
||||||
// otherwise select "default" or first allowed namespace
|
|
||||||
if (allowed.has("default")) {
|
|
||||||
return ["default"];
|
|
||||||
} else if (allowed.size) {
|
|
||||||
return [Array.from(allowed)[0]];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
getContextNamespaces(): string[] {
|
|
||||||
const namespaces = this.contextNs.toJS();
|
|
||||||
|
|
||||||
// show all namespaces when nothing selected
|
|
||||||
if (!namespaces.length) {
|
|
||||||
if (this.isLoaded) {
|
|
||||||
// return actual namespaces list since "allowedNamespaces" updating every 30s in cluster and thus might be stale
|
|
||||||
return this.items.map(namespace => namespace.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.allowedNamespaces;
|
|
||||||
}
|
|
||||||
|
|
||||||
return namespaces;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(apis = [this.api]) {
|
subscribe(apis = [this.api]) {
|
||||||
@ -128,18 +61,31 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
|
|||||||
return super.subscribe(apis);
|
return super.subscribe(apis);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async loadItems(params: KubeObjectStoreLoadingParams) {
|
protected async loadItems(namespaces?: string[]) {
|
||||||
const { allowedNamespaces } = this;
|
if (!isAllowedResource("namespaces")) {
|
||||||
|
if (namespaces) return namespaces.map(this.getDummyNamespace);
|
||||||
|
|
||||||
let namespaces = await super.loadItems(params);
|
return [];
|
||||||
|
|
||||||
namespaces = namespaces.filter(namespace => allowedNamespaces.includes(namespace.getName()));
|
|
||||||
|
|
||||||
if (!namespaces.length && allowedNamespaces.length > 0) {
|
|
||||||
return allowedNamespaces.map(getDummyNamespace);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return namespaces;
|
if (namespaces) {
|
||||||
|
return Promise.all(namespaces.map(name => this.api.get({ name })));
|
||||||
|
} else {
|
||||||
|
return super.loadItems();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected getDummyNamespace(name: string) {
|
||||||
|
return new Namespace({
|
||||||
|
kind: "Namespace",
|
||||||
|
apiVersion: "v1",
|
||||||
|
metadata: {
|
||||||
|
name,
|
||||||
|
uid: "",
|
||||||
|
resourceVersion: "",
|
||||||
|
selfLink: `/api/v1/namespaces/${name}`
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
@ -159,6 +105,12 @@ export class NamespaceStore extends KubeObjectStore<Namespace> {
|
|||||||
else this.contextNs.push(namespace);
|
else this.contextNs.push(namespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@action
|
||||||
|
reset() {
|
||||||
|
super.reset();
|
||||||
|
this.contextNs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
async remove(item: Namespace) {
|
async remove(item: Namespace) {
|
||||||
await super.remove(item);
|
await super.remove(item);
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import difference from "lodash/difference";
|
import difference from "lodash/difference";
|
||||||
import uniqBy from "lodash/uniqBy";
|
import uniqBy from "lodash/uniqBy";
|
||||||
import { clusterRoleBindingApi, IRoleBindingSubject, RoleBinding, roleBindingApi } from "../../api/endpoints";
|
import { clusterRoleBindingApi, IRoleBindingSubject, RoleBinding, roleBindingApi } from "../../api/endpoints";
|
||||||
import { KubeObjectStore, KubeObjectStoreLoadingParams } from "../../kube-object.store";
|
import { KubeObjectStore } from "../../kube-object.store";
|
||||||
import { autobind } from "../../utils";
|
import { autobind } from "../../utils";
|
||||||
import { apiManager } from "../../api/api-manager";
|
import { apiManager } from "../../api/api-manager";
|
||||||
|
|
||||||
@ -26,13 +26,15 @@ export class RoleBindingsStore extends KubeObjectStore<RoleBinding> {
|
|||||||
return clusterRoleBindingApi.get(params);
|
return clusterRoleBindingApi.get(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async loadItems(params: KubeObjectStoreLoadingParams): Promise<RoleBinding[]> {
|
protected loadItems(namespaces?: string[]) {
|
||||||
const items = await Promise.all([
|
if (namespaces) {
|
||||||
super.loadItems({ ...params, api: clusterRoleBindingApi }),
|
return Promise.all(
|
||||||
super.loadItems({ ...params, api: roleBindingApi }),
|
namespaces.map(namespace => roleBindingApi.list({ namespace }))
|
||||||
]);
|
).then(items => items.flat());
|
||||||
|
} else {
|
||||||
return items.flat();
|
return Promise.all([clusterRoleBindingApi.list(), roleBindingApi.list()])
|
||||||
|
.then(items => items.flat());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async createItem(params: { name: string; namespace?: string }, data?: Partial<RoleBinding>) {
|
protected async createItem(params: { name: string; namespace?: string }, data?: Partial<RoleBinding>) {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { clusterRoleApi, Role, roleApi } from "../../api/endpoints";
|
import { clusterRoleApi, Role, roleApi } from "../../api/endpoints";
|
||||||
import { autobind } from "../../utils";
|
import { autobind } from "../../utils";
|
||||||
import { KubeObjectStore, KubeObjectStoreLoadingParams } from "../../kube-object.store";
|
import { KubeObjectStore } from "../../kube-object.store";
|
||||||
import { apiManager } from "../../api/api-manager";
|
import { apiManager } from "../../api/api-manager";
|
||||||
|
|
||||||
@autobind()
|
@autobind()
|
||||||
@ -24,13 +24,15 @@ export class RolesStore extends KubeObjectStore<Role> {
|
|||||||
return clusterRoleApi.get(params);
|
return clusterRoleApi.get(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async loadItems(params: KubeObjectStoreLoadingParams): Promise<Role[]> {
|
protected loadItems(namespaces?: string[]): Promise<Role[]> {
|
||||||
const items = await Promise.all([
|
if (namespaces) {
|
||||||
super.loadItems({ ...params, api: clusterRoleApi }),
|
return Promise.all(
|
||||||
super.loadItems({ ...params, api: roleApi }),
|
namespaces.map(namespace => roleApi.list({ namespace }))
|
||||||
]);
|
).then(items => items.flat());
|
||||||
|
} else {
|
||||||
return items.flat();
|
return Promise.all([clusterRoleApi.list(), roleApi.list()])
|
||||||
|
.then(items => items.flat());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async createItem(params: { name: string; namespace?: string }, data?: Partial<Role>) {
|
protected async createItem(params: { name: string; namespace?: string }, data?: Partial<Role>) {
|
||||||
|
|||||||
@ -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.contextNs);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="workload" key={resource}>
|
<div className="workload" key={resource}>
|
||||||
|
|||||||
@ -17,65 +17,81 @@ import { cronJobStore } from "../+workloads-cronjobs/cronjob.store";
|
|||||||
import { Events } from "../+events";
|
import { Events } from "../+events";
|
||||||
import { KubeObjectStore } from "../../kube-object.store";
|
import { KubeObjectStore } from "../../kube-object.store";
|
||||||
import { isAllowedResource } from "../../../common/rbac";
|
import { isAllowedResource } from "../../../common/rbac";
|
||||||
import { namespaceStore } from "../+namespaces/namespace.store";
|
|
||||||
|
|
||||||
interface Props extends RouteComponentProps<IWorkloadsOverviewRouteParams> {
|
interface Props extends RouteComponentProps<IWorkloadsOverviewRouteParams> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class WorkloadsOverview extends React.Component<Props> {
|
export class WorkloadsOverview extends React.Component<Props> {
|
||||||
@observable isLoading = false;
|
|
||||||
@observable isUnmounting = false;
|
@observable isUnmounting = false;
|
||||||
|
|
||||||
async componentDidMount() {
|
async componentDidMount() {
|
||||||
const stores: KubeObjectStore[] = [
|
const stores: KubeObjectStore[] = [];
|
||||||
isAllowedResource("pods") && podsStore,
|
|
||||||
isAllowedResource("deployments") && deploymentStore,
|
|
||||||
isAllowedResource("daemonsets") && daemonSetStore,
|
|
||||||
isAllowedResource("statefulsets") && statefulSetStore,
|
|
||||||
isAllowedResource("replicasets") && replicaSetStore,
|
|
||||||
isAllowedResource("jobs") && jobStore,
|
|
||||||
isAllowedResource("cronjobs") && cronJobStore,
|
|
||||||
isAllowedResource("events") && eventStore,
|
|
||||||
].filter(Boolean);
|
|
||||||
|
|
||||||
const unsubscribeMap = new Map<KubeObjectStore, () => void>();
|
if (isAllowedResource("pods")) {
|
||||||
|
stores.push(podsStore);
|
||||||
|
}
|
||||||
|
|
||||||
const loadStores = async () => {
|
if (isAllowedResource("deployments")) {
|
||||||
this.isLoading = true;
|
stores.push(deploymentStore);
|
||||||
|
}
|
||||||
|
|
||||||
for (const store of stores) {
|
if (isAllowedResource("daemonsets")) {
|
||||||
if (this.isUnmounting) break;
|
stores.push(daemonSetStore);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
if (isAllowedResource("statefulsets")) {
|
||||||
await store.loadAll();
|
stores.push(statefulSetStore);
|
||||||
unsubscribeMap.get(store)?.(); // unsubscribe previous watcher
|
}
|
||||||
unsubscribeMap.set(store, store.subscribe());
|
|
||||||
} catch (error) {
|
|
||||||
console.error("loading store error", error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.isLoading = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
namespaceStore.onContextChange(loadStores, {
|
if (isAllowedResource("replicasets")) {
|
||||||
fireImmediately: true,
|
stores.push(replicaSetStore);
|
||||||
});
|
}
|
||||||
|
|
||||||
await when(() => this.isUnmounting && !this.isLoading);
|
if (isAllowedResource("jobs")) {
|
||||||
unsubscribeMap.forEach(dispose => dispose());
|
stores.push(jobStore);
|
||||||
unsubscribeMap.clear();
|
}
|
||||||
|
|
||||||
|
if (isAllowedResource("cronjobs")) {
|
||||||
|
stores.push(cronJobStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isAllowedResource("events")) {
|
||||||
|
stores.push(eventStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
const unsubscribeList: Array<() => void> = [];
|
||||||
|
|
||||||
|
for (const store of stores) {
|
||||||
|
await store.loadAll();
|
||||||
|
unsubscribeList.push(store.subscribe());
|
||||||
|
}
|
||||||
|
|
||||||
|
await when(() => this.isUnmounting);
|
||||||
|
unsubscribeList.forEach(dispose => dispose());
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
this.isUnmounting = true;
|
this.isUnmounting = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get contents() {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<OverviewStatuses/>
|
||||||
|
{ isAllowedResource("events") && <Events
|
||||||
|
compact
|
||||||
|
hideFilters
|
||||||
|
className="box grow"
|
||||||
|
/> }
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="WorkloadsOverview flex column gaps">
|
<div className="WorkloadsOverview flex column gaps">
|
||||||
<OverviewStatuses/>
|
{this.contents}
|
||||||
{isAllowedResource("events") && <Events compact hideFilters className="box grow"/>}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,8 @@ import { lookupApiLink } from "../../api/kube-api";
|
|||||||
import { KubeObjectStatusIcon } from "../kube-object-status-icon";
|
import { KubeObjectStatusIcon } from "../kube-object-status-icon";
|
||||||
import { Badge } from "../badge";
|
import { Badge } from "../badge";
|
||||||
|
|
||||||
enum columnId {
|
|
||||||
|
enum sortBy {
|
||||||
name = "name",
|
name = "name",
|
||||||
namespace = "namespace",
|
namespace = "namespace",
|
||||||
containers = "containers",
|
containers = "containers",
|
||||||
@ -76,15 +77,15 @@ export class Pods extends React.Component<Props> {
|
|||||||
tableId = "workloads_pods"
|
tableId = "workloads_pods"
|
||||||
isConfigurable
|
isConfigurable
|
||||||
sortingCallbacks={{
|
sortingCallbacks={{
|
||||||
[columnId.name]: (pod: Pod) => pod.getName(),
|
[sortBy.name]: (pod: Pod) => pod.getName(),
|
||||||
[columnId.namespace]: (pod: Pod) => pod.getNs(),
|
[sortBy.namespace]: (pod: Pod) => pod.getNs(),
|
||||||
[columnId.containers]: (pod: Pod) => pod.getContainers().length,
|
[sortBy.containers]: (pod: Pod) => pod.getContainers().length,
|
||||||
[columnId.restarts]: (pod: Pod) => pod.getRestartsCount(),
|
[sortBy.restarts]: (pod: Pod) => pod.getRestartsCount(),
|
||||||
[columnId.owners]: (pod: Pod) => pod.getOwnerRefs().map(ref => ref.kind),
|
[sortBy.owners]: (pod: Pod) => pod.getOwnerRefs().map(ref => ref.kind),
|
||||||
[columnId.qos]: (pod: Pod) => pod.getQosClass(),
|
[sortBy.qos]: (pod: Pod) => pod.getQosClass(),
|
||||||
[columnId.node]: (pod: Pod) => pod.getNodeName(),
|
[sortBy.node]: (pod: Pod) => pod.getNodeName(),
|
||||||
[columnId.age]: (pod: Pod) => pod.metadata.creationTimestamp,
|
[sortBy.age]: (pod: Pod) => pod.metadata.creationTimestamp,
|
||||||
[columnId.status]: (pod: Pod) => pod.getStatusMessage(),
|
[sortBy.status]: (pod: Pod) => pod.getStatusMessage(),
|
||||||
}}
|
}}
|
||||||
searchFilters={[
|
searchFilters={[
|
||||||
(pod: Pod) => pod.getSearchFields(),
|
(pod: Pod) => pod.getSearchFields(),
|
||||||
@ -94,16 +95,16 @@ export class Pods extends React.Component<Props> {
|
|||||||
]}
|
]}
|
||||||
renderHeaderTitle="Pods"
|
renderHeaderTitle="Pods"
|
||||||
renderTableHeader={[
|
renderTableHeader={[
|
||||||
{ title: "Name", className: "name", sortBy: columnId.name, id: columnId.name },
|
{ title: "Name", className: "name", sortBy: sortBy.name },
|
||||||
{ className: "warning", showWithColumn: columnId.name },
|
{ className: "warning", showWithColumn: "name" },
|
||||||
{ title: "Namespace", className: "namespace", sortBy: columnId.namespace, id: columnId.namespace },
|
{ title: "Namespace", className: "namespace", sortBy: sortBy.namespace },
|
||||||
{ title: "Containers", className: "containers", sortBy: columnId.containers, id: columnId.containers },
|
{ title: "Containers", className: "containers", sortBy: sortBy.containers },
|
||||||
{ title: "Restarts", className: "restarts", sortBy: columnId.restarts, id: columnId.restarts },
|
{ title: "Restarts", className: "restarts", sortBy: sortBy.restarts },
|
||||||
{ title: "Controlled By", className: "owners", sortBy: columnId.owners, id: columnId.owners },
|
{ title: "Controlled By", className: "owners", sortBy: sortBy.owners },
|
||||||
{ title: "Node", className: "node", sortBy: columnId.node, id: columnId.node },
|
{ title: "Node", className: "node", sortBy: sortBy.node },
|
||||||
{ title: "QoS", className: "qos", sortBy: columnId.qos, id: columnId.qos },
|
{ title: "QoS", className: "qos", sortBy: sortBy.qos },
|
||||||
{ title: "Age", className: "age", sortBy: columnId.age, id: columnId.age },
|
{ title: "Age", className: "age", sortBy: sortBy.age },
|
||||||
{ title: "Status", className: "status", sortBy: columnId.status, id: columnId.status },
|
{ title: "Status", className: "status", sortBy: sortBy.status },
|
||||||
]}
|
]}
|
||||||
renderTableContents={(pod: Pod) => [
|
renderTableContents={(pod: Pod) => [
|
||||||
<Badge flat key="name" label={pod.getName()} tooltip={pod.getName()} />,
|
<Badge flat key="name" label={pod.getName()} tooltip={pod.getName()} />,
|
||||||
|
|||||||
@ -36,14 +36,3 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.ItemListLayoutVisibilityMenu {
|
|
||||||
.MenuItem {
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.Checkbox {
|
|
||||||
width: 100%;
|
|
||||||
padding: var(--spacing);
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -129,7 +129,7 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
|
|||||||
|
|
||||||
if (!isClusterScoped) {
|
if (!isClusterScoped) {
|
||||||
disposeOnUnmount(this, [
|
disposeOnUnmount(this, [
|
||||||
namespaceStore.onContextChange(() => this.loadStores())
|
reaction(() => namespaceStore.items.toJS(), () => this.loadStores())
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -440,9 +440,11 @@ export class ItemListLayout extends React.Component<ItemListLayoutProps> {
|
|||||||
return <TableCell key={cellProps.id ?? index} {...cellProps} />;
|
return <TableCell key={cellProps.id ?? index} {...cellProps} />;
|
||||||
}
|
}
|
||||||
})}
|
})}
|
||||||
<TableCell className="menu">
|
{isConfigurable && (
|
||||||
{isConfigurable && this.renderColumnVisibilityMenu()}
|
<TableCell className="menu">
|
||||||
</TableCell>
|
{this.renderColumnVisibilityMenu()}
|
||||||
|
</TableCell>
|
||||||
|
)}
|
||||||
</TableHead>
|
</TableHead>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,14 +34,14 @@ export class PageFiltersStore {
|
|||||||
namespaceStore.setContext(filteredNs);
|
namespaceStore.setContext(filteredNs);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
namespaceStore.onContextChange(namespaces => {
|
reaction(() => namespaceStore.contextNs.toJS(), contextNs => {
|
||||||
const filteredNs = this.getValues(FilterType.NAMESPACE);
|
const filteredNs = this.getValues(FilterType.NAMESPACE);
|
||||||
const isChanged = namespaces.length !== filteredNs.length;
|
const isChanged = contextNs.length !== filteredNs.length;
|
||||||
|
|
||||||
if (isChanged) {
|
if (isChanged) {
|
||||||
this.filters.replace([
|
this.filters.replace([
|
||||||
...this.filters.filter(({ type }) => type !== FilterType.NAMESPACE),
|
...this.filters.filter(({ type }) => type !== FilterType.NAMESPACE),
|
||||||
...namespaces.map(ns => ({ type: FilterType.NAMESPACE, value: ns })),
|
...contextNs.map(ns => ({ type: FilterType.NAMESPACE, value: ns })),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
|
|||||||
4
src/renderer/components/item-object-list/table-menu.scss
Normal file
4
src/renderer/components/item-object-list/table-menu.scss
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.MenuCheckbox {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
@ -74,7 +74,7 @@ export class TableCell extends React.Component<TableCellProps> {
|
|||||||
const content = displayBooleans(displayBoolean, title || children);
|
const content = displayBooleans(displayBoolean, title || children);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div {...cellProps} className={classNames} onClick={this.onClick}>
|
<div {...cellProps} id={className} className={classNames} onClick={this.onClick}>
|
||||||
{this.renderCheckbox()}
|
{this.renderCheckbox()}
|
||||||
{_nowrap ? <div className="content">{content}</div> : content}
|
{_nowrap ? <div className="content">{content}</div> : content}
|
||||||
{this.renderSortIcon()}
|
{this.renderSortIcon()}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ export interface ItemObject {
|
|||||||
|
|
||||||
@autobind()
|
@autobind()
|
||||||
export abstract class ItemStore<T extends ItemObject = ItemObject> {
|
export abstract class ItemStore<T extends ItemObject = ItemObject> {
|
||||||
abstract loadAll(...args: any[]): Promise<void>;
|
abstract loadAll(): Promise<void>;
|
||||||
|
|
||||||
protected defaultSorting = (item: T) => item.getName();
|
protected defaultSorting = (item: T) => item.getName();
|
||||||
|
|
||||||
@ -40,7 +40,8 @@ export abstract class ItemStore<T extends ItemObject = ItemObject> {
|
|||||||
|
|
||||||
if (item) {
|
if (item) {
|
||||||
return item;
|
return item;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
const items = this.sortItems([...this.items, newItem]);
|
const items = this.sortItems([...this.items, newItem]);
|
||||||
|
|
||||||
this.items.replace(items);
|
this.items.replace(items);
|
||||||
@ -82,7 +83,8 @@ export abstract class ItemStore<T extends ItemObject = ItemObject> {
|
|||||||
const index = this.items.findIndex(item => item === existingItem);
|
const index = this.items.findIndex(item => item === existingItem);
|
||||||
|
|
||||||
this.items.splice(index, 1, item);
|
this.items.splice(index, 1, item);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
let items = [...this.items, item];
|
let items = [...this.items, item];
|
||||||
|
|
||||||
if (sortItems) items = this.sortItems(items);
|
if (sortItems) items = this.sortItems(items);
|
||||||
@ -128,7 +130,8 @@ export abstract class ItemStore<T extends ItemObject = ItemObject> {
|
|||||||
toggleSelection(item: T) {
|
toggleSelection(item: T) {
|
||||||
if (this.isSelected(item)) {
|
if (this.isSelected(item)) {
|
||||||
this.unselect(item);
|
this.unselect(item);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
this.select(item);
|
this.select(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,7 +142,8 @@ export abstract class ItemStore<T extends ItemObject = ItemObject> {
|
|||||||
|
|
||||||
if (allSelected) {
|
if (allSelected) {
|
||||||
visibleItems.forEach(this.unselect);
|
visibleItems.forEach(this.unselect);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
visibleItems.forEach(this.select);
|
visibleItems.forEach(this.select);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import type { Cluster } from "../main/cluster";
|
|
||||||
import { action, observable, reaction } from "mobx";
|
import { action, observable, reaction } from "mobx";
|
||||||
import { autobind } from "./utils";
|
import { autobind } from "./utils";
|
||||||
import { KubeObject } from "./api/kube-object";
|
import { KubeObject } from "./api/kube-object";
|
||||||
@ -7,11 +6,7 @@ import { ItemStore } from "./item.store";
|
|||||||
import { apiManager } from "./api/api-manager";
|
import { apiManager } from "./api/api-manager";
|
||||||
import { IKubeApiQueryParams, KubeApi } from "./api/kube-api";
|
import { IKubeApiQueryParams, KubeApi } from "./api/kube-api";
|
||||||
import { KubeJsonApiData } from "./api/kube-json-api";
|
import { KubeJsonApiData } from "./api/kube-json-api";
|
||||||
|
import { getHostedCluster } from "../common/cluster-store";
|
||||||
export interface KubeObjectStoreLoadingParams {
|
|
||||||
namespaces: string[];
|
|
||||||
api?: KubeApi;
|
|
||||||
}
|
|
||||||
|
|
||||||
@autobind()
|
@autobind()
|
||||||
export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemStore<T> {
|
export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemStore<T> {
|
||||||
@ -80,26 +75,14 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async resolveCluster(): Promise<Cluster> {
|
protected async loadItems(allowedNamespaces?: string[]): Promise<T[]> {
|
||||||
const { getHostedCluster } = await import("../common/cluster-store");
|
if (!this.api.isNamespaced || !allowedNamespaces) {
|
||||||
|
return this.api.list({}, this.query);
|
||||||
return getHostedCluster();
|
} else {
|
||||||
}
|
return Promise
|
||||||
|
.all(allowedNamespaces.map(namespace => this.api.list({ namespace })))
|
||||||
protected async loadItems({ namespaces, api }: KubeObjectStoreLoadingParams): Promise<T[]> {
|
.then(items => items.flat());
|
||||||
const cluster = await this.resolveCluster();
|
|
||||||
|
|
||||||
if (cluster.isAllowedResource(api.kind)) {
|
|
||||||
if (api.isNamespaced) {
|
|
||||||
return Promise
|
|
||||||
.all(namespaces.map(namespace => api.list({ namespace })))
|
|
||||||
.then(items => items.flat());
|
|
||||||
}
|
|
||||||
|
|
||||||
return api.list({}, this.query);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected filterItemsOnLoad(items: T[]) {
|
protected filterItemsOnLoad(items: T[]) {
|
||||||
@ -107,35 +90,30 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
|||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
async loadAll({ namespaces: contextNamespaces }: { namespaces?: string[] } = {}) {
|
async loadAll() {
|
||||||
this.isLoading = true;
|
this.isLoading = true;
|
||||||
|
let items: T[];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!contextNamespaces) {
|
const { allowedNamespaces, accessibleNamespaces, isAdmin } = getHostedCluster();
|
||||||
const { namespaceStore } = await import("./components/+namespaces/namespace.store");
|
|
||||||
|
|
||||||
contextNamespaces = namespaceStore.getContextNamespaces();
|
if (isAdmin && accessibleNamespaces.length == 0) {
|
||||||
|
items = await this.loadItems();
|
||||||
|
} else {
|
||||||
|
items = await this.loadItems(allowedNamespaces);
|
||||||
}
|
}
|
||||||
|
|
||||||
let items = await this.loadItems({ namespaces: contextNamespaces, api: this.api });
|
|
||||||
|
|
||||||
items = this.filterItemsOnLoad(items);
|
items = this.filterItemsOnLoad(items);
|
||||||
items = this.sortItems(items);
|
|
||||||
|
|
||||||
this.items.replace(items);
|
|
||||||
this.isLoaded = true;
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Loading store items failed", { error, store: this });
|
|
||||||
this.resetOnError(error);
|
|
||||||
} finally {
|
} finally {
|
||||||
|
if (items) {
|
||||||
|
items = this.sortItems(items);
|
||||||
|
this.items.replace(items);
|
||||||
|
}
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
|
this.isLoaded = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected resetOnError(error: any) {
|
|
||||||
if (error) this.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async loadItem(params: { name: string; namespace?: string }): Promise<T> {
|
protected async loadItem(params: { name: string; namespace?: string }): Promise<T> {
|
||||||
return this.api.get(params);
|
return this.api.get(params);
|
||||||
}
|
}
|
||||||
@ -220,7 +198,7 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
|||||||
// create latest non-observable copy of items to apply updates in one action (==single render)
|
// create latest non-observable copy of items to apply updates in one action (==single render)
|
||||||
const items = this.items.toJS();
|
const items = this.items.toJS();
|
||||||
|
|
||||||
for (const { type, object } of this.eventsBuffer.clear()) {
|
for (const {type, object} of this.eventsBuffer.clear()) {
|
||||||
const index = items.findIndex(item => item.getId() === object.metadata?.uid);
|
const index = items.findIndex(item => item.getId() === object.metadata?.uid);
|
||||||
const item = items[index];
|
const item = items[index];
|
||||||
const api = apiManager.getApiByKind(object.kind, object.apiVersion);
|
const api = apiManager.getApiByKind(object.kind, object.apiVersion);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user