mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Restructure to monorepo (#6907)
* wip: restructure to monorepo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor create-release-pr to a package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * github workflow fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix typo Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * add webpack-env types to core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix github workflows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * refactor/fix integration tests Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * lint fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * yarn run dev Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * eslint settings for vscode Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * move templates to right package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * open-lens build fixes Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * integration test fix Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix nx task dependencies Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * use bash shell for unit tests in test workflow Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix test:unit for windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix win-ca webpack error in open-lens Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix build:app on windows Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove ELECTRON_BUILDER_EXTRA_ARGS Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * sync src/ from master Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * remove Makefile from core Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
1 parent
0066ca4ea2
commit
2657df2293
3321 files changed
+28699
-11447
No files matched your search
@@ -0,0 +1,173 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import type { KubeObjectStore } from "../kube-object.store";
|
||||
|
||||
import type { IComputedValue } from "mobx";
|
||||
import { autorun, action, observable } from "mobx";
|
||||
import type { KubeApi } from "../kube-api";
|
||||
import type { KubeObject, ObjectReference } from "../kube-object";
|
||||
import { parseKubeApi, createKubeApiURL } from "../kube-api-parse";
|
||||
import { chain, find } from "../../utils/iter";
|
||||
|
||||
export type RegisterableStore<Store> = Store extends KubeObjectStore<any, any, any>
|
||||
? Store
|
||||
: never;
|
||||
export type RegisterableApi<Api> = Api extends KubeApi<any, any>
|
||||
? Api
|
||||
: never;
|
||||
export type KubeObjectStoreFrom<Api> = Api extends KubeApi<infer KubeObj, infer ApiData>
|
||||
? KubeObjectStore<KubeObj, Api, ApiData>
|
||||
: never;
|
||||
|
||||
export type FindApiCallback = (api: KubeApi<KubeObject>) => boolean;
|
||||
|
||||
interface Dependencies {
|
||||
readonly apis: IComputedValue<KubeApi[]>;
|
||||
readonly stores: IComputedValue<KubeObjectStore[]>;
|
||||
}
|
||||
|
||||
export class ApiManager {
|
||||
private readonly externalApis = observable.array<KubeApi>();
|
||||
private readonly externalStores = observable.array<KubeObjectStore>();
|
||||
|
||||
private readonly apis = observable.map<string, KubeApi>();
|
||||
|
||||
constructor(private readonly dependencies: Dependencies) {
|
||||
// NOTE: this is done to preserve the old behaviour of an API being discoverable using all previous apiBases
|
||||
autorun(() => {
|
||||
const apis = chain(this.dependencies.apis.get().values())
|
||||
.concat(this.externalApis.values());
|
||||
const removedApis = new Set(this.apis.values());
|
||||
|
||||
for (const api of apis) {
|
||||
removedApis.delete(api);
|
||||
this.apis.set(api.apiBase, api);
|
||||
}
|
||||
|
||||
for (const api of removedApis) {
|
||||
for (const [apiBase, storedApi] of this.apis) {
|
||||
if (storedApi === api) {
|
||||
this.apis.delete(apiBase);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getApi(pathOrCallback: string | FindApiCallback) {
|
||||
if (typeof pathOrCallback === "function") {
|
||||
return find(this.apis.values(), pathOrCallback);
|
||||
}
|
||||
|
||||
const { apiBase } = parseKubeApi(pathOrCallback);
|
||||
|
||||
return this.apis.get(apiBase);
|
||||
}
|
||||
|
||||
getApiByKind(kind: string, apiVersion: string) {
|
||||
return this.getApi(api => api.kind === kind && api.apiVersionWithGroup === apiVersion);
|
||||
}
|
||||
|
||||
registerApi<Api>(api: RegisterableApi<Api>): void;
|
||||
/**
|
||||
* @deprecated Just register the `api` by itself
|
||||
*/
|
||||
registerApi<Api>(apiBase: string, api: RegisterableApi<Api>): void;
|
||||
registerApi<Api>(...args: [RegisterableApi<Api>] | [string, RegisterableApi<Api>]) {
|
||||
if (args.length === 1) {
|
||||
this.externalApis.push(args[0]);
|
||||
} else {
|
||||
this.externalApis.push(args[1]);
|
||||
}
|
||||
}
|
||||
|
||||
unregisterApi(apiOrBase: string | KubeApi<KubeObject>) {
|
||||
if (typeof apiOrBase === "string") {
|
||||
const api = this.externalApis.find(api => api.apiBase === apiOrBase);
|
||||
|
||||
if (api) {
|
||||
this.externalApis.remove(api);
|
||||
}
|
||||
} else {
|
||||
this.unregisterApi(apiOrBase.apiBase);
|
||||
}
|
||||
}
|
||||
|
||||
registerStore<KubeObj>(store: RegisterableStore<KubeObj>): void;
|
||||
/**
|
||||
* @deprecated KubeObjectStore's should only every be about a single KubeApi type
|
||||
*/
|
||||
registerStore<KubeObj>(store: RegisterableStore<KubeObj>, apis: KubeApi<KubeObject>[]): void;
|
||||
|
||||
@action
|
||||
registerStore<KubeObj>(store: RegisterableStore<KubeObj>): void {
|
||||
this.externalStores.push(store);
|
||||
}
|
||||
|
||||
getStore(api: string | undefined): KubeObjectStore | undefined;
|
||||
getStore<Api>(api: RegisterableApi<Api>): KubeObjectStoreFrom<Api> | undefined;
|
||||
/**
|
||||
* @deprecated use an actual cast instead of hiding it with this unused type param
|
||||
*/
|
||||
getStore<Store extends KubeObjectStore>(api: string | KubeApi): Store | undefined ;
|
||||
getStore(apiOrBase: string | KubeApi | undefined): KubeObjectStore | undefined {
|
||||
if (!apiOrBase) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const { apiBase } = typeof apiOrBase === "string"
|
||||
? parseKubeApi(apiOrBase)
|
||||
: apiOrBase;
|
||||
const api = this.getApi(apiBase);
|
||||
|
||||
if (!api) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return chain(this.dependencies.stores.get().values())
|
||||
.concat(this.externalStores.values())
|
||||
.find(store => store.api.apiBase === api.apiBase);
|
||||
}
|
||||
|
||||
lookupApiLink(ref: ObjectReference, parentObject?: KubeObject): string {
|
||||
const {
|
||||
kind, apiVersion = "v1", name,
|
||||
namespace = parentObject?.getNs(),
|
||||
} = ref;
|
||||
|
||||
if (!kind) return "";
|
||||
|
||||
// search in registered apis by 'kind' & 'apiVersion'
|
||||
const api = this.getApi(api => api.kind === kind && api.apiVersionWithGroup == apiVersion);
|
||||
|
||||
if (api) {
|
||||
return api.formatUrlForNotListing({ namespace, name });
|
||||
}
|
||||
|
||||
// lookup api by generated resource link
|
||||
const apiPrefixes = ["/apis", "/api"];
|
||||
const resource = kind.endsWith("s") ? `${kind.toLowerCase()}es` : `${kind.toLowerCase()}s`;
|
||||
|
||||
for (const apiPrefix of apiPrefixes) {
|
||||
const apiLink = createKubeApiURL({ apiPrefix, apiVersion, name, namespace, resource });
|
||||
|
||||
if (this.getApi(apiLink)) {
|
||||
return apiLink;
|
||||
}
|
||||
}
|
||||
|
||||
// resolve by kind only (hpa's might use refs to older versions of resources for example)
|
||||
const apiByKind = this.getApi(api => api.kind === kind);
|
||||
|
||||
if (apiByKind) {
|
||||
return apiByKind.formatUrlForNotListing({ name, namespace });
|
||||
}
|
||||
|
||||
// otherwise generate link with default prefix
|
||||
// resource still might exists in k8s, but api is not registered in the app
|
||||
return createKubeApiURL({ apiVersion, name, namespace, resource });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* 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 EventEmitter from "events";
|
||||
import type TypedEventEmitter from "typed-emitter";
|
||||
import type { CustomResourceDefinition } from "../endpoints";
|
||||
import type { KubeApi } from "../kube-api";
|
||||
|
||||
export interface LegacyAutoRegistration {
|
||||
customResourceDefinition: (crd: CustomResourceDefinition) => void;
|
||||
kubeApi: (api: KubeApi<any, any>) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is used to remove dependency cycles from auto registering of instances
|
||||
*
|
||||
* - Custom Resource Definitions get their own registered store (will need in the future)
|
||||
* - All KubeApi's get auto registered (this should be changed in the future)
|
||||
*/
|
||||
const autoRegistrationEmitterInjectable = getInjectable({
|
||||
id: "auto-registration-emitter",
|
||||
instantiate: (): TypedEventEmitter<LegacyAutoRegistration> => new EventEmitter(),
|
||||
});
|
||||
|
||||
export default autoRegistrationEmitterInjectable;
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
export * from "./api-manager";
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
import type { KubeObjectStore } from "../kube-object.store";
|
||||
|
||||
export const kubeObjectStoreInjectionToken = getInjectionToken<KubeObjectStore<any, any, any>>({
|
||||
id: "kube-object-store-token",
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* 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 { storesAndApisCanBeCreatedInjectionToken } from "../stores-apis-can-be-created.token";
|
||||
import { ApiManager } from "./api-manager";
|
||||
import { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx";
|
||||
import { kubeObjectStoreInjectionToken } from "./kube-object-store-token";
|
||||
import { kubeApiInjectionToken } from "../kube-api/kube-api-injection-token";
|
||||
import { computed } from "mobx";
|
||||
|
||||
const apiManagerInjectable = getInjectable({
|
||||
id: "api-manager",
|
||||
instantiate: (di) => {
|
||||
const computedInjectMany = di.inject(computedInjectManyInjectable);
|
||||
const storesAndApisCanBeCreated = di.inject(storesAndApisCanBeCreatedInjectionToken);
|
||||
|
||||
return new ApiManager({
|
||||
apis: storesAndApisCanBeCreated
|
||||
? computedInjectMany(kubeApiInjectionToken)
|
||||
: computed(() => []),
|
||||
stores: storesAndApisCanBeCreated
|
||||
? computedInjectMany(kubeObjectStoreInjectionToken)
|
||||
: computed(() => []),
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
export default apiManagerInjectable;
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import type { KubeApi } from "../kube-api";
|
||||
import type { KubeObjectStoreDependencies } from "../kube-object.store";
|
||||
import { KubeObjectStore } from "../kube-object.store";
|
||||
import type { KubeObject } from "../kube-object";
|
||||
|
||||
export class CustomResourceStore<K extends KubeObject> extends KubeObjectStore<K, KubeApi<K>> {
|
||||
constructor(deps: KubeObjectStoreDependencies, api: KubeApi<K>) {
|
||||
super(deps, api);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user