mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Merge remote-tracking branch 'origin/master' into feature/ingress_class_api_view
# Conflicts: # src/common/rbac.ts
This commit is contained in:
commit
4c44b76f24
363 files changed
+3866
-5580
No files matched your search
@@ -15,6 +15,12 @@ import type { ResourceApplyingStack } from "../../common/k8s/resource-stack";
|
||||
import { asLegacyGlobalFunctionForExtensionApi } from "../as-legacy-globals-for-extension-api/as-legacy-global-function-for-extension-api";
|
||||
import { asLegacyGlobalForExtensionApi } from "../as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api";
|
||||
import type { KubernetesCluster } from "./catalog";
|
||||
import type { KubeApiDataFrom, KubeObjectStoreOptions } from "../../common/k8s-api/kube-object.store";
|
||||
import { KubeObjectStore as InternalKubeObjectStore } from "../../common/k8s-api/kube-object.store";
|
||||
import type { KubeJsonApiDataFor, KubeObject } from "../../common/k8s-api/kube-object";
|
||||
import type { KubeApi } from "../../common/k8s-api/kube-api";
|
||||
import clusterFrameContextForNamespacedResourcesInjectable from "../../renderer/cluster-frame-context/for-namespaced-resources.injectable";
|
||||
import type { ClusterContext } from "../../renderer/cluster-frame-context/cluster-frame-context";
|
||||
|
||||
export const apiManager = asLegacyGlobalForExtensionApi(apiManagerInjectable);
|
||||
export const forCluster = asLegacyGlobalFunctionForExtensionApi(createKubeApiForClusterInjectable);
|
||||
@@ -72,8 +78,44 @@ export {
|
||||
type KubeJsonApiData,
|
||||
} from "../../common/k8s-api/kube-json-api";
|
||||
|
||||
export abstract class KubeObjectStore<
|
||||
K extends KubeObject = KubeObject,
|
||||
A extends KubeApi<K, D> = KubeApi<K, KubeJsonApiDataFor<K>>,
|
||||
D extends KubeJsonApiDataFor<K> = KubeApiDataFrom<K, A>,
|
||||
> extends InternalKubeObjectStore<K, A, D> {
|
||||
/**
|
||||
* @deprecated This is no longer used and shouldn't have been every really used
|
||||
*/
|
||||
static readonly context = {
|
||||
set: (ctx: ClusterContext) => {
|
||||
console.warn("Setting KubeObjectStore.context is no longer supported");
|
||||
void ctx;
|
||||
},
|
||||
get: () => asLegacyGlobalForExtensionApi(clusterFrameContextForNamespacedResourcesInjectable),
|
||||
};
|
||||
|
||||
get context() {
|
||||
return this.dependencies.context;
|
||||
}
|
||||
|
||||
constructor(api: A, opts?: KubeObjectStoreOptions);
|
||||
/**
|
||||
* @deprecated Supply API instance through constructor
|
||||
*/
|
||||
constructor();
|
||||
constructor(api?: A, opts?: KubeObjectStoreOptions) {
|
||||
super(
|
||||
{
|
||||
context: asLegacyGlobalForExtensionApi(clusterFrameContextForNamespacedResourcesInjectable),
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
api!,
|
||||
opts,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
KubeObjectStore,
|
||||
type JsonPatch,
|
||||
type KubeObjectStoreLoadAllParams,
|
||||
type KubeObjectStoreLoadingParams,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* 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 { LensExtensionConstructor, LensExtensionManifest } from "../lens-extension";
|
||||
|
||||
export interface BundledExtension {
|
||||
readonly manifest: LensExtensionManifest;
|
||||
main: () => LensExtensionConstructor | null;
|
||||
renderer: () => LensExtensionConstructor | null;
|
||||
}
|
||||
|
||||
export const bundledExtensionInjectionToken = getInjectionToken<BundledExtension>({
|
||||
id: "bundled-extension-path",
|
||||
});
|
||||
@@ -27,7 +27,6 @@ import getRelativePathInjectable from "../../common/path/get-relative-path.injec
|
||||
import joinPathsInjectable from "../../common/path/join-paths.injectable";
|
||||
import removePathInjectable from "../../common/fs/remove.injectable";
|
||||
import homeDirectoryPathInjectable from "../../common/os/home-directory-path.injectable";
|
||||
import applicationInformationInjectable from "../../common/vars/application-information.injectable";
|
||||
import lensResourcesDirInjectable from "../../common/vars/lens-resources-dir.injectable";
|
||||
|
||||
const extensionDiscoveryInjectable = getInjectable({
|
||||
@@ -58,7 +57,6 @@ const extensionDiscoveryInjectable = getInjectable({
|
||||
getRelativePath: di.inject(getRelativePathInjectable),
|
||||
joinPaths: di.inject(joinPathsInjectable),
|
||||
homeDirectoryPath: di.inject(homeDirectoryPathInjectable),
|
||||
applicationInformation: di.inject(applicationInformationInjectable),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -30,7 +30,6 @@ import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable"
|
||||
import type { GetRelativePath } from "../../common/path/get-relative-path.injectable";
|
||||
import type { RemovePath } from "../../common/fs/remove.injectable";
|
||||
import type TypedEventEmitter from "typed-emitter";
|
||||
import type { ApplicationInformation } from "../../common/vars/application-information.injectable";
|
||||
|
||||
interface Dependencies {
|
||||
readonly extensionLoader: ExtensionLoader;
|
||||
@@ -42,7 +41,6 @@ interface Dependencies {
|
||||
readonly isProduction: boolean;
|
||||
readonly fileSystemSeparator: string;
|
||||
readonly homeDirectoryPath: string;
|
||||
readonly applicationInformation: ApplicationInformation;
|
||||
isCompatibleExtension: (manifest: LensExtensionManifest) => boolean;
|
||||
installExtension: (name: string) => Promise<void>;
|
||||
readJsonFile: ReadJson;
|
||||
@@ -384,42 +382,16 @@ export class ExtensionDiscovery {
|
||||
}
|
||||
|
||||
async ensureExtensions(): Promise<Map<LensExtensionId, InstalledExtension>> {
|
||||
const bundledExtensions = await this.loadBundledExtensions();
|
||||
const userExtensions = await this.loadFromFolder(this.localFolderPath, bundledExtensions.map((extension) => extension.manifest.name));
|
||||
const extensions = bundledExtensions.concat(userExtensions);
|
||||
const userExtensions = await this.loadFromFolder(this.localFolderPath);
|
||||
|
||||
return this.extensions = new Map(extensions.map(extension => [extension.id, extension]));
|
||||
return this.extensions = new Map(userExtensions.map(extension => [extension.id, extension]));
|
||||
}
|
||||
|
||||
async loadBundledExtensions(): Promise<InstalledExtension[]> {
|
||||
const extensions: InstalledExtension[] = [];
|
||||
const extensionNames = this.dependencies.applicationInformation.config.extensions || [];
|
||||
|
||||
for (const dirName of extensionNames) {
|
||||
const absPath = this.dependencies.joinPaths(__dirname, "..", "..", "node_modules", dirName);
|
||||
const extension = await this.loadExtensionFromFolder(absPath, { isBundled: true });
|
||||
|
||||
if (!extension) {
|
||||
throw new Error(`Couldn't load bundled extension: ${dirName}`);
|
||||
}
|
||||
|
||||
extensions.push(extension);
|
||||
}
|
||||
this.dependencies.logger.debug(`${logModule}: ${extensions.length} extensions loaded`, { extensions });
|
||||
|
||||
return extensions;
|
||||
}
|
||||
|
||||
async loadFromFolder(folderPath: string, bundledExtensions: string[]): Promise<InstalledExtension[]> {
|
||||
async loadFromFolder(folderPath: string): Promise<InstalledExtension[]> {
|
||||
const extensions: InstalledExtension[] = [];
|
||||
const paths = await this.dependencies.readDirectory(folderPath);
|
||||
|
||||
for (const fileName of paths) {
|
||||
// do not allow to override bundled extensions
|
||||
if (bundledExtensions.includes(fileName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const absPath = this.dependencies.joinPaths(folderPath, fileName);
|
||||
|
||||
try {
|
||||
|
||||
@@ -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";
|
||||
|
||||
export const extensionEntryPointNameInjectionToken = getInjectionToken<"main" | "renderer">({
|
||||
id: "extension-entry-point-name-token",
|
||||
});
|
||||
@@ -12,6 +12,8 @@ import extensionInjectable from "./extension/extension.injectable";
|
||||
import loggerInjectable from "../../common/logger.injectable";
|
||||
import joinPathsInjectable from "../../common/path/join-paths.injectable";
|
||||
import getDirnameOfPathInjectable from "../../common/path/get-dirname.injectable";
|
||||
import { bundledExtensionInjectionToken } from "../extension-discovery/bundled-extension-token";
|
||||
import { extensionEntryPointNameInjectionToken } from "./entry-point-name";
|
||||
|
||||
const extensionLoaderInjectable = getInjectable({
|
||||
id: "extension-loader",
|
||||
@@ -21,6 +23,8 @@ const extensionLoaderInjectable = getInjectable({
|
||||
createExtensionInstance: di.inject(createExtensionInstanceInjectionToken),
|
||||
extensionInstances: di.inject(extensionInstancesInjectable),
|
||||
getExtension: (instance: LensExtension) => di.inject(extensionInjectable, instance),
|
||||
bundledExtensions: di.injectMany(bundledExtensionInjectionToken),
|
||||
extensionEntryPointName: di.inject(extensionEntryPointNameInjectionToken),
|
||||
logger: di.inject(loggerInjectable),
|
||||
joinPaths: di.inject(joinPathsInjectable),
|
||||
getDirnameOfPath: di.inject(getDirnameOfPathInjectable),
|
||||
|
||||
@@ -21,12 +21,15 @@ import type { Extension } from "./extension/extension.injectable";
|
||||
import type { Logger } from "../../common/logger";
|
||||
import type { JoinPaths } from "../../common/path/join-paths.injectable";
|
||||
import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable";
|
||||
import type { BundledExtension } from "../extension-discovery/bundled-extension-token";
|
||||
|
||||
const logModule = "[EXTENSIONS-LOADER]";
|
||||
|
||||
interface Dependencies {
|
||||
readonly extensionInstances: ObservableMap<LensExtensionId, LensExtension>;
|
||||
readonly bundledExtensions: BundledExtension[];
|
||||
readonly logger: Logger;
|
||||
readonly extensionEntryPointName: "main" | "renderer";
|
||||
updateExtensionsState: (extensionsState: Record<LensExtensionId, LensExtensionState>) => void;
|
||||
createExtensionInstance: CreateExtensionInstance;
|
||||
getExtension: (instance: LensExtension) => Extension;
|
||||
@@ -34,6 +37,12 @@ interface Dependencies {
|
||||
getDirnameOfPath: GetDirnameOfPath;
|
||||
}
|
||||
|
||||
interface ExtensionBeingActivated {
|
||||
instance: LensExtension;
|
||||
installedExtension: InstalledExtension;
|
||||
activated: Promise<void>;
|
||||
}
|
||||
|
||||
export interface ExtensionLoading {
|
||||
isBundled: boolean;
|
||||
loaded: Promise<void>;
|
||||
@@ -248,14 +257,84 @@ export class ExtensionLoader {
|
||||
});
|
||||
}
|
||||
|
||||
protected async loadExtensions(installedExtensions: Map<string, InstalledExtension>) {
|
||||
protected async loadBundledExtensions() {
|
||||
return this.dependencies.bundledExtensions
|
||||
.map(extension => {
|
||||
try {
|
||||
const LensExtensionClass = extension[this.dependencies.extensionEntryPointName]();
|
||||
|
||||
if (!LensExtensionClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const installedExtension: InstalledExtension = {
|
||||
absolutePath: "irrelevant",
|
||||
id: extension.manifest.name,
|
||||
isBundled: true,
|
||||
isCompatible: true,
|
||||
isEnabled: true,
|
||||
manifest: extension.manifest,
|
||||
manifestPath: "irrelevant",
|
||||
};
|
||||
const instance = this.dependencies.createExtensionInstance(
|
||||
LensExtensionClass,
|
||||
installedExtension,
|
||||
);
|
||||
|
||||
this.dependencies.extensionInstances.set(extension.manifest.name, instance);
|
||||
|
||||
return {
|
||||
instance,
|
||||
installedExtension,
|
||||
activated: instance.activate(),
|
||||
} as ExtensionBeingActivated;
|
||||
} catch (err) {
|
||||
this.dependencies.logger.error(`${logModule}: error loading extension`, { ext: extension, err });
|
||||
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter(isDefined);
|
||||
}
|
||||
|
||||
protected async loadExtensions(extensions: ExtensionBeingActivated[]): Promise<ExtensionLoading[]> {
|
||||
// We first need to wait until each extension's `onActivate` is resolved or rejected,
|
||||
// as this might register new catalog categories. Afterwards we can safely .enable the extension.
|
||||
await Promise.all(
|
||||
extensions.map(extension =>
|
||||
// If extension activation fails, log error
|
||||
extension.activated.catch((error) => {
|
||||
this.dependencies.logger.error(`${logModule}: activation extension error`, { ext: extension.installedExtension, error });
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
extensions.forEach(({ instance }) => {
|
||||
const extension = this.dependencies.getExtension(instance);
|
||||
|
||||
extension.register();
|
||||
});
|
||||
|
||||
return extensions.map(extension => {
|
||||
const loaded = extension.instance.enable().catch((err) => {
|
||||
this.dependencies.logger.error(`${logModule}: failed to enable`, { ext: extension, err });
|
||||
});
|
||||
|
||||
return {
|
||||
isBundled: extension.installedExtension.isBundled,
|
||||
loaded,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
protected async loadUserExtensions(installedExtensions: Map<string, InstalledExtension>) {
|
||||
// Steps of the function:
|
||||
// 1. require and call .activate for each Extension
|
||||
// 2. Wait until every extension's onActivate has been resolved
|
||||
// 3. Call .enable for each extension
|
||||
// 4. Return ExtensionLoading[]
|
||||
|
||||
const extensions = [...installedExtensions.entries()]
|
||||
return [...installedExtensions.entries()]
|
||||
.map(([extId, extension]) => {
|
||||
const alreadyInit = this.dependencies.extensionInstances.has(extId) || this.nonInstancesByName.has(extension.manifest.name);
|
||||
|
||||
@@ -280,7 +359,7 @@ export class ExtensionLoader {
|
||||
instance,
|
||||
installedExtension: extension,
|
||||
activated: instance.activate(),
|
||||
};
|
||||
} as ExtensionBeingActivated;
|
||||
} catch (err) {
|
||||
this.dependencies.logger.error(`${logModule}: error loading extension`, { ext: extension, err });
|
||||
}
|
||||
@@ -290,52 +369,33 @@ export class ExtensionLoader {
|
||||
|
||||
return null;
|
||||
})
|
||||
// Remove null values
|
||||
.filter(isDefined);
|
||||
|
||||
// We first need to wait until each extension's `onActivate` is resolved or rejected,
|
||||
// as this might register new catalog categories. Afterwards we can safely .enable the extension.
|
||||
await Promise.all(
|
||||
extensions.map(extension =>
|
||||
// If extension activation fails, log error
|
||||
extension.activated.catch((error) => {
|
||||
this.dependencies.logger.error(`${logModule}: activation extension error`, { ext: extension.installedExtension, error });
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
extensions.forEach(({ instance }) => {
|
||||
const extension = this.dependencies.getExtension(instance);
|
||||
|
||||
extension.register();
|
||||
});
|
||||
|
||||
// Return ExtensionLoading[]
|
||||
return extensions.map(extension => {
|
||||
const loaded = extension.instance.enable().catch((err) => {
|
||||
this.dependencies.logger.error(`${logModule}: failed to enable`, { ext: extension, err });
|
||||
});
|
||||
|
||||
return {
|
||||
isBundled: extension.installedExtension.isBundled,
|
||||
loaded,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
autoInitExtensions() {
|
||||
async autoInitExtensions() {
|
||||
this.dependencies.logger.info(`${logModule}: auto initializing extensions`);
|
||||
|
||||
// Setup reaction to load extensions on JSON changes
|
||||
reaction(() => this.toJSON(), installedExtensions => this.loadExtensions(installedExtensions));
|
||||
const bundledExtensions = await this.loadBundledExtensions();
|
||||
const userExtensions = await this.loadUserExtensions(this.toJSON());
|
||||
const loadedExtensions = await this.loadExtensions([
|
||||
...bundledExtensions,
|
||||
...userExtensions,
|
||||
]);
|
||||
|
||||
// Load initial extensions
|
||||
return this.loadExtensions(this.toJSON());
|
||||
// Setup reaction to load extensions on JSON changes
|
||||
reaction(() => this.toJSON(), installedExtensions => {
|
||||
void (async () => {
|
||||
const userExtensions = await this.loadUserExtensions(installedExtensions);
|
||||
|
||||
await this.loadExtensions(userExtensions);
|
||||
})();
|
||||
});
|
||||
|
||||
return loadedExtensions;
|
||||
}
|
||||
|
||||
protected requireExtension(extension: InstalledExtension): LensExtensionConstructor | null {
|
||||
const entryPointName = ipcRenderer ? "renderer" : "main";
|
||||
const extRelativePath = extension.manifest[entryPointName];
|
||||
const extRelativePath = extension.manifest[this.dependencies.extensionEntryPointName];
|
||||
|
||||
if (!extRelativePath) {
|
||||
return null;
|
||||
@@ -344,11 +404,11 @@ export class ExtensionLoader {
|
||||
const extAbsolutePath = this.dependencies.joinPaths(this.dependencies.getDirnameOfPath(extension.manifestPath), extRelativePath);
|
||||
|
||||
try {
|
||||
return __non_webpack_require__(extAbsolutePath).default;
|
||||
return require(/* webpackIgnore: true */ extAbsolutePath).default;
|
||||
} catch (error) {
|
||||
const message = (error instanceof Error ? error.stack : undefined) || error;
|
||||
|
||||
this.dependencies.logger.error(`${logModule}: can't load ${entryPointName} for "${extension.manifest.name}": ${message}`, { extension });
|
||||
this.dependencies.logger.error(`${logModule}: can't load ${this.dependencies.extensionEntryPointName} for "${extension.manifest.name}": ${message}`, { extension });
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
dist/
|
||||
__mocks__/
|
||||
@@ -1,31 +0,0 @@
|
||||
{
|
||||
"name": "@k8slens/extensions",
|
||||
"productName": "OpenLens extensions",
|
||||
"description": "OpenLens - Open Source Kubernetes IDE: extensions",
|
||||
"version": "6.0.0",
|
||||
"copyright": "© 2022 OpenLens Authors",
|
||||
"license": "MIT",
|
||||
"main": "dist/src/extensions/extension-api.js",
|
||||
"types": "dist/src/extensions/extension-api.d.ts",
|
||||
"files": [
|
||||
"dist/**/*.ts",
|
||||
"__mocks__/*.ts",
|
||||
"dist/**/*.js"
|
||||
],
|
||||
"author": {
|
||||
"name": "OpenLens Authors"
|
||||
},
|
||||
"dependencies": {
|
||||
"@material-ui/core": "^4.12.3",
|
||||
"@types/node": "^16.11.58",
|
||||
"@types/react": "^17.0.45",
|
||||
"@types/react-dom": "^17.0.16",
|
||||
"@types/react-router": "^5.1.18",
|
||||
"@types/react-router-dom": "^5.3.3",
|
||||
"conf": "^7.0.1",
|
||||
"mobx": "^6.5.0",
|
||||
"mobx-react": "^7.3.0",
|
||||
"react-select": "^5.3.2",
|
||||
"typed-emitter": "^1.3.1"
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,7 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import type { KubeResource } from "../../common/rbac";
|
||||
import isAllowedResourceInjectable from "../../common/utils/is-allowed-resource.injectable";
|
||||
import { castArray } from "lodash/fp";
|
||||
import { apiResourceRecord } from "../../common/rbac";
|
||||
import { getLegacyGlobalDiForExtensionApi } from "../as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
|
||||
import clusterRoleBindingApiInjectable from "../../common/k8s-api/endpoints/cluster-role-binding.api.injectable";
|
||||
import clusterRoleApiInjectable from "../../common/k8s-api/endpoints/cluster-role.api.injectable";
|
||||
@@ -37,13 +36,22 @@ import namespaceApiInjectable from "../../common/k8s-api/endpoints/namespace.api
|
||||
import kubeEventApiInjectable from "../../common/k8s-api/endpoints/events.api.injectable";
|
||||
import roleBindingApiInjectable from "../../common/k8s-api/endpoints/role-binding.api.injectable";
|
||||
import customResourceDefinitionApiInjectable from "../../common/k8s-api/endpoints/custom-resource-definition.api.injectable";
|
||||
import { shouldShowResourceInjectionToken } from "../../common/cluster-store/allowed-resources-injection-token";
|
||||
|
||||
export function isAllowedResource(resource: KubeResource | KubeResource[]) {
|
||||
const resources = castArray(resource);
|
||||
export function isAllowedResource(resources: KubeResource | KubeResource[]) {
|
||||
const di = getLegacyGlobalDiForExtensionApi();
|
||||
|
||||
return resources.every((resourceName: any) => {
|
||||
const _isAllowedResource = di.inject(isAllowedResourceInjectable, resourceName);
|
||||
return [resources].flat().every((resourceName) => {
|
||||
const resource = apiResourceRecord[resourceName];
|
||||
|
||||
if (!resource) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const _isAllowedResource = di.inject(shouldShowResourceInjectionToken, {
|
||||
apiName: resourceName,
|
||||
group: resource.group,
|
||||
});
|
||||
|
||||
// Note: Legacy isAllowedResource does not advertise reactivity
|
||||
return _isAllowedResource.get();
|
||||
|
||||
Reference in new issue
Block a user