mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
fix stack overflow and cycles in DI
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
375018c330
commit
26062aa20b
13 files changed
+156
-80
No files matched your search
@@ -4,11 +4,11 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import assert from "assert";
|
||||
import autoRegistrationEmitterInjectable from "../../../common/k8s-api/api-manager/auto-registration-emitter.injectable";
|
||||
import { kubeObjectStoreInjectionToken } from "../../../common/k8s-api/api-manager/manager.injectable";
|
||||
import customResourceDefinitionApiInjectable from "../../../common/k8s-api/endpoints/custom-resource-definition.api.injectable";
|
||||
import createStoresAndApisInjectable from "../../create-stores-apis.injectable";
|
||||
import { CustomResourceDefinitionStore } from "./definition.store";
|
||||
import initCustomResourceStoreInjectable from "./init-resource-store.injectable";
|
||||
|
||||
const customResourceDefinitionStoreInjectable = getInjectable({
|
||||
id: "custom-resource-definition-store",
|
||||
@@ -18,7 +18,7 @@ const customResourceDefinitionStoreInjectable = getInjectable({
|
||||
const api = di.inject(customResourceDefinitionApiInjectable);
|
||||
|
||||
return new CustomResourceDefinitionStore({
|
||||
initCustomResourceStore: di.inject(initCustomResourceStoreInjectable),
|
||||
autoRegistration: di.inject(autoRegistrationEmitterInjectable),
|
||||
}, api);
|
||||
},
|
||||
injectionToken: kubeObjectStoreInjectionToken,
|
||||
|
||||
@@ -9,10 +9,11 @@ import { KubeObjectStore } from "../../../common/k8s-api/kube-object.store";
|
||||
import { autoBind } from "../../utils";
|
||||
import type { CustomResourceDefinition, CustomResourceDefinitionApi } from "../../../common/k8s-api/endpoints/custom-resource-definition.api";
|
||||
import type { KubeObject } from "../../../common/k8s-api/kube-object";
|
||||
import type { InitCustomResourceStore } from "./init-resource-store.injectable";
|
||||
import type TypedEventEmitter from "typed-emitter";
|
||||
import type { LegacyAutoRegistration } from "../../../common/k8s-api/api-manager/auto-registration-emitter.injectable";
|
||||
|
||||
export interface CustomResourceDefinitionStoreDependencies {
|
||||
initCustomResourceStore: InitCustomResourceStore;
|
||||
readonly autoRegistration: TypedEventEmitter<LegacyAutoRegistration>;
|
||||
}
|
||||
|
||||
export class CustomResourceDefinitionStore extends KubeObjectStore<CustomResourceDefinition, CustomResourceDefinitionApi> {
|
||||
@@ -25,8 +26,14 @@ export class CustomResourceDefinitionStore extends KubeObjectStore<CustomResourc
|
||||
makeObservable(this);
|
||||
autoBind(this);
|
||||
|
||||
// auto-init stores for crd-s
|
||||
reaction(() => this.getItems(), items => items.forEach(this.dependencies.initCustomResourceStore));
|
||||
reaction(
|
||||
() => this.getItems(),
|
||||
crds => {
|
||||
for (const crd of crds) {
|
||||
this.dependencies.autoRegistration.emit("customResourceDefinition", crd);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
protected sortItems(items: CustomResourceDefinition[]) {
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import apiManagerInjectable from "../../../common/k8s-api/api-manager/manager.injectable";
|
||||
import type { CustomResourceDefinition } from "../../../common/k8s-api/endpoints";
|
||||
import { KubeApi } from "../../../common/k8s-api/kube-api";
|
||||
import { KubeObject } from "../../../common/k8s-api/kube-object";
|
||||
import { CustomResourceStore } from "./resource.store";
|
||||
|
||||
export type InitCustomResourceStore = (crd: CustomResourceDefinition) => void;
|
||||
|
||||
const initCustomResourceStoreInjectable = getInjectable({
|
||||
id: "init-custom-resource-store",
|
||||
instantiate: (di): InitCustomResourceStore => {
|
||||
const apiManager = di.inject(apiManagerInjectable);
|
||||
|
||||
return (crd) => {
|
||||
const objectConstructor = class extends KubeObject {
|
||||
static readonly kind = crd.getResourceKind();
|
||||
static readonly namespaced = crd.isNamespaced();
|
||||
static readonly apiBase = crd.getResourceApiBase();
|
||||
};
|
||||
|
||||
const api = apiManager.getApi(objectConstructor.apiBase)
|
||||
?? new KubeApi({ objectConstructor });
|
||||
|
||||
if (!apiManager.getStore(api)) {
|
||||
apiManager.registerStore(new CustomResourceStore(api));
|
||||
}
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default initCustomResourceStoreInjectable;
|
||||
@@ -1,14 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import type { KubeApi } from "../../../common/k8s-api/kube-api";
|
||||
import { KubeObjectStore } from "../../../common/k8s-api/kube-object.store";
|
||||
import type { KubeObject } from "../../../common/k8s-api/kube-object";
|
||||
|
||||
export class CustomResourceStore<K extends KubeObject> extends KubeObjectStore<K, KubeApi<K>> {
|
||||
constructor(api: KubeApi<K>) {
|
||||
super(api);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import assert from "assert";
|
||||
import apiManagerInjectable from "../../../common/k8s-api/api-manager/manager.injectable";
|
||||
import { kubeObjectStoreInjectionToken } from "../../../common/k8s-api/api-manager/manager.injectable";
|
||||
import ingressApiInjectable from "../../../common/k8s-api/endpoints/ingress.api.injectable";
|
||||
import createStoresAndApisInjectable from "../../create-stores-apis.injectable";
|
||||
import { IngressStore } from "./store";
|
||||
@@ -15,13 +15,10 @@ const ingressStoreInjectable = getInjectable({
|
||||
assert(di.inject(createStoresAndApisInjectable), "ingressStore is only available in certain environments");
|
||||
|
||||
const api = di.inject(ingressApiInjectable);
|
||||
const apiManager = di.inject(apiManagerInjectable);
|
||||
const store = new IngressStore(api);
|
||||
|
||||
apiManager.registerStore(store);
|
||||
|
||||
return store;
|
||||
return new IngressStore(api);
|
||||
},
|
||||
injectionToken: kubeObjectStoreInjectionToken,
|
||||
});
|
||||
|
||||
export default ingressStoreInjectable;
|
||||
@@ -55,7 +55,10 @@ export const NonInjectedClusterFrame = observer(({
|
||||
|
||||
return (
|
||||
<ErrorBoundary>
|
||||
<MainLayout sidebar={<Sidebar />} footer={<Dock />}>
|
||||
<MainLayout
|
||||
sidebar={<Sidebar />}
|
||||
footer={<Dock />}
|
||||
>
|
||||
{
|
||||
Component
|
||||
? <Component />
|
||||
|
||||
@@ -11,6 +11,7 @@ import hostedClusterInjectable from "../../../../common/cluster-store/hosted-clu
|
||||
import appEventBusInjectable from "../../../../common/app-event-bus/app-event-bus.injectable";
|
||||
import clusterFrameContextInjectable from "../../../cluster-frame-context/cluster-frame-context.injectable";
|
||||
import assert from "assert";
|
||||
import autoRegistrationInjectable from "../../../../common/k8s-api/api-manager/auto-registration.injectable";
|
||||
|
||||
const initClusterFrameInjectable = getInjectable({
|
||||
id: "init-cluster-frame",
|
||||
@@ -20,6 +21,14 @@ const initClusterFrameInjectable = getInjectable({
|
||||
|
||||
assert(hostedCluster, "This can only be injected within a cluster frame");
|
||||
|
||||
/**
|
||||
* This is injected here to initialize it for the side effect.
|
||||
*
|
||||
* The side effect CANNOT be within `apiManagerInjectable` itself since that causes circular
|
||||
* dependencies with the current need for legacy di use
|
||||
*/
|
||||
di.inject(autoRegistrationInjectable);
|
||||
|
||||
return initClusterFrame({
|
||||
hostedCluster,
|
||||
loadExtensions: di.inject(extensionLoaderInjectable).loadOnClusterRenderer,
|
||||
|
||||
Reference in new issue
Block a user