mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Simplify CRD KubeApi registrations
- Switch to auto injectable registrations Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
0f1f030a06
commit
886c812679
11 files changed
+163
-55
No files matched your search
+53
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* 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 { reaction } from "mobx";
|
||||
import { customResourceDefinitionApiInjectionToken } from "../../../common/k8s-api/api-manager/crd-api-token";
|
||||
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 maybeKubeApiInjectable from "../../../common/k8s-api/maybe-kube-api.injectable";
|
||||
import loggerInjectable from "../../../common/logger.injectable";
|
||||
import { injectableDifferencingRegistratorWith } from "../../../common/utils/registrator-helper";
|
||||
import customResourceDefinitionStoreInjectable from "../../components/+custom-resources/definition.store.injectable";
|
||||
import { beforeClusterFrameStartsSecondInjectionToken } from "../tokens";
|
||||
|
||||
const setupAutoCrdApiCreationsInjectable = getInjectable({
|
||||
id: "setup-auto-crd-api-creations",
|
||||
instantiate: (di) => ({
|
||||
run: () => {
|
||||
const customResourceDefinitionStore = di.inject(customResourceDefinitionStoreInjectable);
|
||||
const injectableDifferencingRegistrator = injectableDifferencingRegistratorWith(di);
|
||||
|
||||
reaction(
|
||||
() => customResourceDefinitionStore.getItems().map(toCrdApiInjectable),
|
||||
injectableDifferencingRegistrator,
|
||||
{
|
||||
fireImmediately: true,
|
||||
},
|
||||
);
|
||||
},
|
||||
}),
|
||||
injectionToken: beforeClusterFrameStartsSecondInjectionToken,
|
||||
});
|
||||
|
||||
export default setupAutoCrdApiCreationsInjectable;
|
||||
|
||||
const toCrdApiInjectable = (crd: CustomResourceDefinition) => getInjectable({
|
||||
id: `default-kube-api-for-custom-resource-definition-${crd.getResourceApiBase()}`,
|
||||
instantiate: (di) => {
|
||||
const objectConstructor = class extends KubeObject {
|
||||
static readonly kind = crd.getResourceKind();
|
||||
static readonly namespaced = crd.isNamespaced();
|
||||
static readonly apiBase = crd.getResourceApiBase();
|
||||
};
|
||||
|
||||
return new KubeApi({
|
||||
logger: di.inject(loggerInjectable),
|
||||
maybeKubeApi: di.inject(maybeKubeApiInjectable),
|
||||
}, { objectConstructor });
|
||||
},
|
||||
injectionToken: customResourceDefinitionApiInjectionToken,
|
||||
});
|
||||
-7
@@ -63,13 +63,6 @@ const setupAutoRegistrationInjectable = getInjectable({
|
||||
};
|
||||
|
||||
autoRegistrationEmitter
|
||||
.on("customResourceDefinition", (crd) => {
|
||||
if (initialized) {
|
||||
autoInitCustomResourceStore(crd);
|
||||
} else {
|
||||
beforeApiManagerInitializationCrds.push(crd);
|
||||
}
|
||||
})
|
||||
.on("kubeApi", (api) => {
|
||||
if (initialized) {
|
||||
autoInitKubeApi(api);
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
*/
|
||||
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/kube-object-store-token";
|
||||
import customResourceDefinitionApiInjectable from "../../../common/k8s-api/endpoints/custom-resource-definition.api.injectable";
|
||||
import loggerInjectable from "../../../common/logger.injectable";
|
||||
@@ -20,7 +19,6 @@ const customResourceDefinitionStoreInjectable = getInjectable({
|
||||
const api = di.inject(customResourceDefinitionApiInjectable);
|
||||
|
||||
return new CustomResourceDefinitionStore({
|
||||
autoRegistration: di.inject(autoRegistrationEmitterInjectable),
|
||||
context: di.inject(clusterFrameContextForClusterScopedResourcesInjectable),
|
||||
logger: di.inject(loggerInjectable),
|
||||
}, api);
|
||||
|
||||
@@ -3,37 +3,22 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { computed, reaction, makeObservable } from "mobx";
|
||||
import { computed, makeObservable } from "mobx";
|
||||
import type { KubeObjectStoreDependencies, KubeObjectStoreOptions } from "../../../common/k8s-api/kube-object.store";
|
||||
import { KubeObjectStore } from "../../../common/k8s-api/kube-object.store";
|
||||
import type { CustomResourceDefinition, CustomResourceDefinitionApi } from "../../../common/k8s-api/endpoints/custom-resource-definition.api";
|
||||
import type { KubeObject } from "../../../common/k8s-api/kube-object";
|
||||
import type TypedEventEmitter from "typed-emitter";
|
||||
import type { LegacyAutoRegistration } from "../../../common/k8s-api/api-manager/auto-registration-emitter.injectable";
|
||||
import autoBind from "auto-bind";
|
||||
|
||||
export interface CustomResourceDefinitionStoreDependencies extends KubeObjectStoreDependencies {
|
||||
readonly autoRegistration: TypedEventEmitter<LegacyAutoRegistration>;
|
||||
}
|
||||
|
||||
export class CustomResourceDefinitionStore extends KubeObjectStore<CustomResourceDefinition, CustomResourceDefinitionApi> {
|
||||
constructor(
|
||||
protected readonly dependencies: CustomResourceDefinitionStoreDependencies,
|
||||
dependencies: KubeObjectStoreDependencies,
|
||||
api: CustomResourceDefinitionApi,
|
||||
opts?: KubeObjectStoreOptions,
|
||||
) {
|
||||
super(dependencies, api, opts);
|
||||
makeObservable(this);
|
||||
autoBind(this);
|
||||
|
||||
reaction(
|
||||
() => this.getItems(),
|
||||
crds => {
|
||||
for (const crd of crds) {
|
||||
this.dependencies.autoRegistration.emit("customResourceDefinition", crd);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
protected sortItems(items: CustomResourceDefinition[]) {
|
||||
|
||||
Reference in new issue
Block a user