fix KubeApi's not being registered

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2022-05-04 08:47:20 -04:00
1 parent 8499f26707
commit 25796b736d
7 files changed
+55 -20

No files matched your search

@@ -10,7 +10,7 @@ import type { KubeApi } from "../kube-api";
export interface LegacyAutoRegistration {
customResourceDefinition: (crd: CustomResourceDefinition) => void;
kubeApi: (api: KubeApi) => void;
kubeApi: (api: KubeApi<any, any>) => void;
}
/**
@@ -3,6 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { CustomResourceDefinition } from "../endpoints";
import { KubeApi } from "../kube-api";
import { KubeObject } from "../kube-object";
import autoRegistrationEmitterInjectable from "./auto-registration-emitter.injectable";
@@ -12,11 +13,12 @@ import { CustomResourceStore } from "./resource.store";
const autoRegistrationInjectable = getInjectable({
id: "api-manager-auto-registration",
instantiate: (di) => {
const apiManager = di.inject(apiManagerInjectable);
const autoRegistrationEmitter = di.inject(autoRegistrationEmitterInjectable);
const beforeApiManagerInitializationCrds: CustomResourceDefinition[] = [];
const beforeApiManagerInitializationApis: KubeApi[] = [];
let initialized = false;
autoRegistrationEmitter
.on("customResourceDefinition", (crd) => {
const autoInitCustomResourceStore = (crd: CustomResourceDefinition) => {
const objectConstructor = class extends KubeObject {
static readonly kind = crd.getResourceKind();
static readonly namespaced = crd.isNamespaced();
@@ -40,8 +42,32 @@ const autoRegistrationInjectable = getInjectable({
if (!apiManager.getStore(api)) {
apiManager.registerStore(new CustomResourceStore(api));
}
};
const autoInitKubeApi = (api: KubeApi) => {
apiManager.registerApi(api);
};
autoRegistrationEmitter
.on("customResourceDefinition", (crd) => {
if (initialized) {
autoInitCustomResourceStore(crd);
} else {
beforeApiManagerInitializationCrds.push(crd);
}
})
.on("kubeApi", (api) => apiManager.registerApi(api));
.on("kubeApi", (api) => {
if (initialized) {
autoInitKubeApi(api);
} else {
beforeApiManagerInitializationApis.push(api);
}
});
const apiManager = di.inject(apiManagerInjectable);
beforeApiManagerInitializationCrds.forEach(autoInitCustomResourceStore);
beforeApiManagerInitializationApis.forEach(autoInitKubeApi);
initialized = true;
},
});
@@ -3,6 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
import { createStoresAndApisInjectionToken } from "../create-stores-apis.token";
import type { KubeObjectStore } from "../kube-object.store";
import { ApiManager } from "./api-manager";
@@ -14,11 +15,14 @@ const apiManagerInjectable = getInjectable({
id: "api-manager",
instantiate: (di) => {
const apiManager = new ApiManager();
if (di.inject(createStoresAndApisInjectionToken)) {
const stores = di.injectMany(kubeObjectStoreInjectionToken);
for (const store of stores) {
apiManager.registerStore(store);
}
}
return apiManager;
},
+3 -3
View File
@@ -311,7 +311,7 @@ export interface DeleteResourceDescriptor extends ResourceDescriptor {
/**
* @deprecated In the new extension API, don't expose `KubeApi`'s constructor
*/
function legacyRegisterApi(api: KubeApi): void {
function legacyRegisterApi(api: KubeApi<any, any>): void {
// Try both just in case, because we might be in a testing environment
for (const env of [Environments.main, Environments.renderer]) {
const di = getEnvironmentSpecificLegacyGlobalDiForExtensionApi(env);
@@ -377,7 +377,7 @@ export class KubeApi<
this.apiResource = resource;
this.request = request;
this.objectConstructor = objectConstructor;
legacyRegisterApi(this as unknown as KubeApi);
legacyRegisterApi(this);
}
get apiVersionWithGroup() {
@@ -458,7 +458,7 @@ export class KubeApi<
if (this.apiVersionPreferred) {
this.apiBase = this.computeApiBase();
legacyRegisterApi(this as unknown as KubeApi);
legacyRegisterApi(this);
}
}
}
+12
View File
@@ -46,6 +46,7 @@ import assert from "assert";
import openDeleteClusterDialogInjectable from "./components/delete-cluster-dialog/open.injectable";
import { init } from "@sentry/electron/renderer";
import kubernetesClusterCategoryInjectable from "../common/catalog/categories/kubernetes-cluster.injectable";
import autoRegistrationInjectable from "../common/k8s-api/api-manager/auto-registration.injectable";
configurePackages(); // global packages
registerCustomThemes(); // monaco editor themes
@@ -78,6 +79,17 @@ export async function bootstrap(di: DiContainer) {
const rootNode = createRoot(rootElem);
const logPrefix = `[BOOTSTRAP-${process.isMainFrame ? "ROOT" : "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.
*
* This also MUST be done before anything else so that it can start listening for the events for
* auto initialization.
*/
di.inject(autoRegistrationInjectable);
// TODO: Remove temporal dependencies to make timing of initialization not important
di.inject(userStoreInjectable);
@@ -103,14 +103,16 @@ class NonInjectedKubeObjectMenu<TKubeObject extends KubeObject, Props extends Ku
}
render() {
const { remove, update, renderRemoveMessage, isEditable, isRemovable } = this;
const { remove, update, renderRemoveMessage, isEditable, isRemovable, store } = this;
const { className, editable, removable, object, ...menuProps } = this.props;
console.log(object, store);
return (
<MenuActions
className={cssNames("KubeObjectMenu", className)}
updateAction={object && isEditable ? (() => update(object)) : undefined}
removeAction={object && isRemovable ? (() => remove(object)) : undefined}
updateAction={(object && isEditable) ? (() => update(object)) : undefined}
removeAction={(object && isRemovable) ? (() => remove(object)) : undefined}
removeConfirmationMessage={renderRemoveMessage}
{...menuProps}
>
@@ -11,7 +11,6 @@ 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",
@@ -21,14 +20,6 @@ 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,