From 5005d34c2e13d07d7cf1981d11b2e43e8117a125 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 23 Nov 2020 12:54:39 -0500 Subject: [PATCH 1/5] change BaseRegistry to only have one type parameter (#1474) * change BaseRegistry to only have one type parameter Signed-off-by: Sebastian Malton --- src/common/utils/index.ts | 3 +++ src/common/utils/rectify-array.ts | 8 ++++++++ src/extensions/registries/base-registry.ts | 13 +++++++------ src/extensions/registries/page-registry.ts | 8 +++++--- src/renderer/utils/index.ts | 1 - 5 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 src/common/utils/rectify-array.ts diff --git a/src/common/utils/index.ts b/src/common/utils/index.ts index e43863284d..330b98fcf7 100644 --- a/src/common/utils/index.ts +++ b/src/common/utils/index.ts @@ -1,5 +1,7 @@ // Common utils (main OR renderer) +export const noop: any = () => { /* empty */ }; + export * from "./app-version"; export * from "./autobind"; export * from "./base64"; @@ -12,3 +14,4 @@ export * from "./splitArray"; export * from "./saveToAppFiles"; export * from "./singleton"; export * from "./openExternal"; +export * from "./rectify-array"; diff --git a/src/common/utils/rectify-array.ts b/src/common/utils/rectify-array.ts new file mode 100644 index 0000000000..48feb3a165 --- /dev/null +++ b/src/common/utils/rectify-array.ts @@ -0,0 +1,8 @@ +/** + * rectify condences the single item or array of T type, to an array. + * @param items either one item or an array of items + * @returns a list of items + */ +export function recitfy(items: T | T[]): T[] { + return Array.isArray(items) ? items : [items]; +} diff --git a/src/extensions/registries/base-registry.ts b/src/extensions/registries/base-registry.ts index 73dbd373f0..ff8760151c 100644 --- a/src/extensions/registries/base-registry.ts +++ b/src/extensions/registries/base-registry.ts @@ -1,20 +1,21 @@ // Base class for extensions-api registries import { action, observable } from "mobx"; import { LensExtension } from "../lens-extension"; +import { recitfy } from "../../common/utils"; -export class BaseRegistry { +export class BaseRegistry { private items = observable([], { deep: false }); - getItems(): I[] { - return this.items.toJS() as I[]; + getItems(): T[] { + return this.items.toJS(); } add(items: T | T[], ext?: LensExtension): () => void; // allow method overloading with required "ext" @action add(items: T | T[]) { - const normalizedItems = (Array.isArray(items) ? items : [items]); - this.items.push(...normalizedItems); - return () => this.remove(...normalizedItems); + const itemArray = recitfy(items); + this.items.push(...itemArray); + return () => this.remove(...itemArray); } @action diff --git a/src/extensions/registries/page-registry.ts b/src/extensions/registries/page-registry.ts index 4b9c872715..0b385c02c4 100644 --- a/src/extensions/registries/page-registry.ts +++ b/src/extensions/registries/page-registry.ts @@ -7,6 +7,7 @@ import { compile } from "path-to-regexp"; import { BaseRegistry } from "./base-registry"; import { LensExtension } from "../lens-extension"; import logger from "../../main/logger"; +import { recitfy } from "../../common/utils"; export interface PageRegistration { /** @@ -59,12 +60,13 @@ export function getExtensionPageUrl

({ extensionId, pageId = "" return extPageRoutePath; } -export class PageRegistry extends BaseRegistry { +export class PageRegistry extends BaseRegistry { @action - add(items: PageRegistration[], ext: LensExtension) { + add(items: PageRegistration | PageRegistration[], ext: LensExtension) { + const itemArray = recitfy(items); let registeredPages: RegisteredPage[] = []; try { - registeredPages = items.map(page => ({ + registeredPages = itemArray.map(page => ({ ...page, extensionId: ext.name, routePath: getExtensionPageUrl({ extensionId: ext.name, pageId: page.id ?? page.routePath }), diff --git a/src/renderer/utils/index.ts b/src/renderer/utils/index.ts index 149d9aa1e8..f76547371d 100755 --- a/src/renderer/utils/index.ts +++ b/src/renderer/utils/index.ts @@ -1,6 +1,5 @@ // Common usage utils & helpers -export const noop: any = Function(); export const isElectron = !!navigator.userAgent.match(/Electron/); export * from "../../common/utils"; From daade3b899a38f73787085126fd7da3085796411 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 23 Nov 2020 12:55:00 -0500 Subject: [PATCH 2/5] add extensions to lint & lint:fix (#1490) Signed-off-by: Sebastian Malton --- .eslintrc.js | 5 ++++- extensions/example-extension/page.tsx | 2 +- extensions/example-extension/renderer.tsx | 4 ++-- .../kube-object-event-status/renderer.tsx | 2 +- extensions/license-menu-item/main.ts | 2 +- .../metrics-cluster-feature/renderer.tsx | 2 +- .../src/metrics-feature.ts | 4 ++-- extensions/node-menu/renderer.tsx | 2 +- extensions/pod-menu/renderer.tsx | 2 +- .../src/telemetry-preferences-store.ts | 2 +- extensions/telemetry/src/tracker.ts | 20 +++++++++---------- package.json | 2 +- 12 files changed, 26 insertions(+), 23 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 913430d291..5a377b06e0 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,5 +1,8 @@ module.exports = { - ignorePatterns: ["src/extensions/npm/extensions/dist/**/*"], + ignorePatterns: [ + "**/node_modules/**/*", + "**/dist/**/*", + ], overrides: [ { files: [ diff --git a/extensions/example-extension/page.tsx b/extensions/example-extension/page.tsx index 2fe28ea49c..e9eb361ceb 100644 --- a/extensions/example-extension/page.tsx +++ b/extensions/example-extension/page.tsx @@ -11,7 +11,7 @@ export class ExamplePage extends React.Component<{ extension: LensRendererExtens deactivate = () => { const { extension } = this.props; extension.disable(); - } + }; render() { const doodleStyle = { diff --git a/extensions/example-extension/renderer.tsx b/extensions/example-extension/renderer.tsx index ffec687e40..2ddeadb92a 100644 --- a/extensions/example-extension/renderer.tsx +++ b/extensions/example-extension/renderer.tsx @@ -12,7 +12,7 @@ export default class ExampleExtension extends LensRendererExtension { Page: () => , } } - ] + ]; clusterPageMenus = [ { @@ -22,5 +22,5 @@ export default class ExampleExtension extends LensRendererExtension { Icon: ExampleIcon, } } - ] + ]; } diff --git a/extensions/kube-object-event-status/renderer.tsx b/extensions/kube-object-event-status/renderer.tsx index 6224464a56..e5b3fb665a 100644 --- a/extensions/kube-object-event-status/renderer.tsx +++ b/extensions/kube-object-event-status/renderer.tsx @@ -38,5 +38,5 @@ export default class EventResourceStatusRendererExtension extends LensRendererEx apiVersions: ["batch/v1"], resolve: (cronJob: K8sApi.CronJob) => resolveStatusForCronJobs(cronJob) }, - ] + ]; } diff --git a/extensions/license-menu-item/main.ts b/extensions/license-menu-item/main.ts index ca84041036..1b3a35e2bf 100644 --- a/extensions/license-menu-item/main.ts +++ b/extensions/license-menu-item/main.ts @@ -9,5 +9,5 @@ export default class LicenseLensMainExtension extends LensMainExtension { Util.openExternal("https://k8slens.dev/licenses/eula.md"); } } - ] + ]; } diff --git a/extensions/metrics-cluster-feature/renderer.tsx b/extensions/metrics-cluster-feature/renderer.tsx index a285328b12..9192b10364 100644 --- a/extensions/metrics-cluster-feature/renderer.tsx +++ b/extensions/metrics-cluster-feature/renderer.tsx @@ -19,5 +19,5 @@ export default class ClusterMetricsFeatureExtension extends LensRendererExtensio }, feature: new MetricsFeature() } - ] + ]; } diff --git a/extensions/metrics-cluster-feature/src/metrics-feature.ts b/extensions/metrics-cluster-feature/src/metrics-feature.ts index 777f36b35a..4787280b61 100644 --- a/extensions/metrics-cluster-feature/src/metrics-feature.ts +++ b/extensions/metrics-cluster-feature/src/metrics-feature.ts @@ -25,8 +25,8 @@ export interface MetricsConfiguration { } export class MetricsFeature extends ClusterFeature.Feature { - name = "metrics" - latestVersion = "v2.17.2-lens1" + name = "metrics"; + latestVersion = "v2.17.2-lens1"; config: MetricsConfiguration = { persistence: { diff --git a/extensions/node-menu/renderer.tsx b/extensions/node-menu/renderer.tsx index ebe6cf46ce..902f576633 100644 --- a/extensions/node-menu/renderer.tsx +++ b/extensions/node-menu/renderer.tsx @@ -11,5 +11,5 @@ export default class NodeMenuRendererExtension extends LensRendererExtension { MenuItem: (props: NodeMenuProps) => } } - ] + ]; } diff --git a/extensions/pod-menu/renderer.tsx b/extensions/pod-menu/renderer.tsx index e13195ee68..4dd4eca1a0 100644 --- a/extensions/pod-menu/renderer.tsx +++ b/extensions/pod-menu/renderer.tsx @@ -19,5 +19,5 @@ export default class PodMenuRendererExtension extends LensRendererExtension { MenuItem: (props: PodLogsMenuProps) => } } - ] + ]; } diff --git a/extensions/telemetry/src/telemetry-preferences-store.ts b/extensions/telemetry/src/telemetry-preferences-store.ts index e20ef1ede8..93ec94f9e5 100644 --- a/extensions/telemetry/src/telemetry-preferences-store.ts +++ b/extensions/telemetry/src/telemetry-preferences-store.ts @@ -3,7 +3,7 @@ import { toJS } from "mobx"; export type TelemetryPreferencesModel = { enabled: boolean; -} +}; export class TelemetryPreferencesStore extends Store.ExtensionStore { enabled = true; diff --git a/extensions/telemetry/src/tracker.ts b/extensions/telemetry/src/tracker.ts index d6fadf4ee4..8c2fdab8e4 100644 --- a/extensions/telemetry/src/tracker.ts +++ b/extensions/telemetry/src/tracker.ts @@ -7,22 +7,22 @@ import { reaction, IReactionDisposer } from "mobx"; import { comparer } from "mobx"; export class Tracker extends Util.Singleton { - static readonly GA_ID = "UA-159377374-1" - static readonly SEGMENT_KEY = "YENwswyhlOgz8P7EFKUtIZ2MfON7Yxqb" - protected eventHandlers: Array<(ev: EventBus.AppEvent ) => void> = [] - protected started = false - protected visitor: ua.Visitor - protected analytics: Analytics + static readonly GA_ID = "UA-159377374-1"; + static readonly SEGMENT_KEY = "YENwswyhlOgz8P7EFKUtIZ2MfON7Yxqb"; + protected eventHandlers: Array<(ev: EventBus.AppEvent ) => void> = []; + protected started = false; + protected visitor: ua.Visitor; + protected analytics: Analytics; protected machineId: string = null; protected ip: string = null; protected appVersion: string; protected locale: string; protected userAgent: string; protected anonymousId: string; - protected os: string - protected disposers: IReactionDisposer[] + protected os: string; + protected disposers: IReactionDisposer[]; - protected reportInterval: NodeJS.Timeout + protected reportInterval: NodeJS.Timeout; private constructor() { super(); @@ -63,7 +63,7 @@ export class Tracker extends Util.Singleton { const newExtensions = currentExtensions.filter(x => !previousExtensions.includes(x)); newExtensions.forEach(ext => { this.event("extension", "enable", { extension: ext }); - }) + }); previousExtensions = currentExtensions; }, { equals: comparer.structural })); } diff --git a/package.json b/package.json index 8f4faf20f7..013a46aed8 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "download:kubectl": "yarn run ts-node build/download_kubectl.ts", "download:helm": "yarn run ts-node build/download_helm.ts", "build:tray-icons": "yarn run ts-node build/build_tray_icon.ts", - "lint": "yarn run eslint $@ --ext js,ts,tsx --max-warnings=0 src/ integration/ __mocks__/ build/", + "lint": "yarn run eslint $@ --ext js,ts,tsx --max-warnings=0 src/ integration/ __mocks__/ build/ extensions/", "lint:fix": "yarn run lint --fix", "mkdocs-serve-local": "docker build -t mkdocs-serve-local:latest mkdocs/ && docker run --rm -it -p 8000:8000 -v ${PWD}:/docs mkdocs-serve-local:latest", "typedocs-extensions-api": "yarn run typedoc --ignoreCompilerErrors --readme docs/extensions/typedoc-readme.md.tpl --name @k8slens/extensions --out docs/extensions/api --mode library --excludePrivate --hideBreadcrumbs --includes src/ src/extensions/extension-api.ts" From cd4660b85b50292ce2b9930ffed059db10041a85 Mon Sep 17 00:00:00 2001 From: Jim Ehrismann <40840436+jim-docker@users.noreply.github.com> Date: Mon, 23 Nov 2020 17:06:19 -0500 Subject: [PATCH 3/5] Doc/renderer extension guide (#1476) * lens renderer extension guide * renderer extension guide (pages and page menus) Signed-off-by: Jim Ehrismann --- docs/extensions/guides/main-extension.md | 2 +- docs/extensions/guides/renderer-extension.md | 424 +++++++++++++++++++ 2 files changed, 425 insertions(+), 1 deletion(-) diff --git a/docs/extensions/guides/main-extension.md b/docs/extensions/guides/main-extension.md index c9ad9e378b..e1249da0d4 100644 --- a/docs/extensions/guides/main-extension.md +++ b/docs/extensions/guides/main-extension.md @@ -20,7 +20,7 @@ export default class ExampleExtensionMain extends LensMainExtension { } ``` -There are two methods that you can implement to facilitate running your custom code. `onActivate()` is called when your extension has been successfully enabled. By overriding `onActivate()` you can initiate your custom code. `onDeactivate()` is called when the extension is disabled (typically from the [Lens Extensions Page]()) and when implemented gives you a chance to clean up after your extension, if necessary. The example above simply logs messages when the extension is enabled and disabled. Note that to see standard output from the main process there must be a console connected to it. This is typically achieved by starting Lens from the command prompt. +There are two methods that you can implement to facilitate running your custom code. `onActivate()` is called when your extension has been successfully enabled. By implementing `onActivate()` you can initiate your custom code. `onDeactivate()` is called when the extension is disabled (typically from the [Lens Extensions Page]()) and when implemented gives you a chance to clean up after your extension, if necessary. The example above simply logs messages when the extension is enabled and disabled. Note that to see standard output from the main process there must be a console connected to it. This is typically achieved by starting Lens from the command prompt. The following example is a little more interesting in that it accesses some Lens state data and periodically logs the name of the currently active cluster in Lens. diff --git a/docs/extensions/guides/renderer-extension.md b/docs/extensions/guides/renderer-extension.md index fc4f9b8bdd..8d02fb9cef 100644 --- a/docs/extensions/guides/renderer-extension.md +++ b/docs/extensions/guides/renderer-extension.md @@ -1 +1,425 @@ # Renderer Extension + +The renderer extension api is the interface to Lens' renderer process (Lens runs in main and renderer processes). It allows you to access, configure, and customize Lens data, add custom Lens UI elements, and generally run custom code in Lens' renderer process. The custom Lens UI elements that can be added include global pages, cluster pages, cluster page menus, cluster features, app preferences, status bar items, KubeObject menu items, and KubeObject details items. These UI elements are based on React components. + +## `LensRendererExtension` Class + +To create a renderer extension simply extend the `LensRendererExtension` class: + +``` typescript +import { LensRendererExtension } from "@k8slens/extensions"; + +export default class ExampleExtensionMain extends LensRendererExtension { + onActivate() { + console.log('custom renderer process extension code started'); + } + + onDeactivate() { + console.log('custom renderer process extension de-activated'); + } +} +``` + +There are two methods that you can implement to facilitate running your custom code. `onActivate()` is called when your extension has been successfully enabled. By implementing `onActivate()` you can initiate your custom code. `onDeactivate()` is called when the extension is disabled (typically from the [Lens Extensions Page]()) and when implemented gives you a chance to clean up after your extension, if necessary. The example above simply logs messages when the extension is enabled and disabled. + +### `clusterPages` + +Cluster pages appear as part of the cluster dashboard. They are accessible from the side bar, and are shown in the menu list after *Custom Resources*. It is conventional to use a cluster page to show information or provide functionality pertaining to the active cluster, along with custom data and functionality your extension may have. However, it is not limited to the active cluster. Also, your extension can gain access to the Kubernetes resources in the active cluster in a straightforward manner using the [`clusterStore`](../stores#clusterstore). + +The following example adds a cluster page definition to a `LensRendererExtension` subclass: + +``` typescript +import { LensRendererExtension } from "@k8slens/extensions"; +import { ExampleIcon, ExamplePage } from "./page" +import React from "react" + +export default class ExampleExtension extends LensRendererExtension { + clusterPages = [ + { + id: "hello", + components: { + Page: () => , + } + } + ]; +} +``` + +Cluster pages are objects matching the `PageRegistration` interface. The `id` field identiifies the page, and at its simplest is just a string identifier, as shown in the example above. The 'id' field can also convey route path details, such as variable parameters provided to a page ([See example below]()). The `components` field matches the `PageComponents` interface for wich there is one field, `Page`. `Page` is of type ` React.ComponentType`, which gives you great flexibility in defining the appearance and behaviour of your page. For the example above `ExamplePage` can be defined in `page.tsx`: + +``` typescript +import { LensRendererExtension } from "@k8slens/extensions"; +import React from "react" + +export class ExamplePage extends React.Component<{ extension: LensRendererExtension }> { + render() { + return ( +

+

Hello world!

+
+ ) + } +} +``` + +Note that the `ExamplePage` class defines a property named `extension`. This allows the `ExampleExtension` object to be passed in React-style in the cluster page definition, so that `ExamplePage` can access any `ExampleExtension` subclass data. + +### `clusterPageMenus` + +The above example code shows how to create a cluster page but not how to make it available to the Lens user. Cluster pages are typically made available through a menu item in the cluster dashboard sidebar. Expanding on the above example a cluster page menu is added to the `ExampleExtension` definition: + +``` typescript +import { LensRendererExtension } from "@k8slens/extensions"; +import { ExampleIcon, ExamplePage } from "./page" +import React from "react" + +export default class ExampleExtension extends LensRendererExtension { + clusterPages = [ + { + id: "hello", + components: { + Page: () => , + } + } + ]; + + clusterPageMenus = [ + { + target: { pageId: "hello" }, + title: "Hello World", + components: { + Icon: ExampleIcon, + } + }, + ]; +} +``` + +Cluster page menus are objects matching the `ClusterPageMenuRegistration` interface. They define the appearance of the cluster page menu item in the cluster dashboard sidebar and the behaviour when the cluster page menu item is activated (typically by a mouse click). The example above uses the `target` field to set the behaviour as a link to the cluster page with `id` of `"hello"`. This is done by setting `target`'s `pageId` field to `"hello"`. The cluster page menu item's appearance is defined by setting the `title` field to the text that is to be displayed in the cluster dashboard sidebar. The `components` field is used to set an icon that appears to the left of the `title` text in the sidebar. Thus when the `"Hello World"` menu item is activated the cluster dashboard will show the contents of `ExamplePage`. This example requires the definition of another React-based component, `ExampleIcon`, which has been added to `page.tsx`: + +``` typescript +import { LensRendererExtension, Component } from "@k8slens/extensions"; +import React from "react" + +export function ExampleIcon(props: Component.IconProps) { + return +} + +export class ExamplePage extends React.Component<{ extension: LensRendererExtension }> { + render() { + return ( +
+

Hello world!

+
+ ) + } +} +``` + +`ExampleIcon` introduces one of Lens' built-in components available to extension developers, the `Component.Icon`. Built in are the [Material Design](https://material.io) [icons](https://material.io/resources/icons/). One can be selected by name via the `material` field. `ExampleIcon` also sets a tooltip, shown when the Lens user hovers over the icon with a mouse, by setting the `tooltip` field. + +A cluster page menu can also be used to define a foldout submenu in the cluster dashboard sidebar. This enables the grouping of cluster pages. The following example shows how to specify a submenu having two menu items: + +``` typescript +import { LensRendererExtension } from "@k8slens/extensions"; +import { ExampleIcon, ExamplePage } from "./page" +import React from "react" + +export default class ExampleExtension extends LensRendererExtension { + clusterPages = [ + { + id: "hello", + components: { + Page: () => , + } + }, + { + id: "bonjour", + components: { + Page: () => , + } + } + ]; + + clusterPageMenus = [ + { + id: "example", + title: "Greetings", + components: { + Icon: ExampleIcon, + } + }, + { + parentId: "example", + target: { pageId: "hello" }, + title: "Hello World", + components: { + Icon: ExampleIcon, + } + }, + { + parentId: "example", + target: { pageId: "bonjour" }, + title: "Bonjour le monde", + components: { + Icon: ExempleIcon, + } + } + ]; +} +``` + +The above defines two cluster pages and three cluster page menu objects. The cluster page definitons are straightforward. The first cluster page menu object defines the parent of a foldout submenu. Setting the `id` field in a cluster page menu definition implies that it is defining a foldout submenu. Also note that the `target` field is not specified (it is ignored if the `id` field is specified). This cluster page menu object specifies the `title` and `components` fields, which are used in displaying the menu item in the cluster dashboard sidebar. Initially the submenu is hidden. Activating this menu item toggles on and off the appearance of the submenu below it. The remaining two cluster page menu objects define the contents of the submenu. A cluster page menu object is defined to be a submenu item by setting the `parentId` field to the id of the parent of a foldout submenu, `"example"` in this case + +### `globalPages` + +Global pages appear independently of the cluster dashboard and they fill the Lens UI space. A global page is typically triggered from the cluster menu using a [global page menu](#globalpagemenus). They can also be triggered by a [custom app menu selection](../main-extension#appmenus) from a Main Extension or a [custom status bar item](#statusbaritems). Global pages can appear even when there is no active cluster, unlike cluster pages. It is conventional to use a global page to show information and provide functionality relevant across clusters, along with custom data and functionality that your extension may have. + +The following example defines a `LensRendererExtension` subclass with a single global page definition: + +``` typescript +import { LensRendererExtension } from '@k8slens/extensions'; +import { HelpPage } from './page'; +import React from 'react'; + +export default class HelpExtension extends LensRendererExtension { + globalPages = [ + { + id: "help", + components: { + Page: () => , + } + } + ]; +} +``` + +Global pages are objects matching the `PageRegistration` interface. The `id` field identiifies the page, and at its simplest is just a string identifier, as shown in the example above. The 'id' field can also convey route path details, such as variable parameters provided to a page ([See example below]()). The `components` field matches the `PageComponents` interface for which there is one field, `Page`. `Page` is of type ` React.ComponentType`, which gives you great flexibility in defining the appearance and behaviour of your page. For the example above `HelpPage` can be defined in `page.tsx`: + +``` typescript +import { LensRendererExtension } from "@k8slens/extensions"; +import React from "react" + +export class HelpPage extends React.Component<{ extension: LensRendererExtension }> { + render() { + return ( +
+

Help yourself

+
+ ) + } +} +``` + +Note that the `HelpPage` class defines a property named `extension`. This allows the `HelpExtension` object to be passed in React-style in the global page definition, so that `HelpPage` can access any `HelpExtension` subclass data. + +This example code shows how to create a global page but not how to make it available to the Lens user. Global pages are typically made available through a number of ways. Menu items can be added to the Lens app menu system and set to open a global page when activated (See [`appMenus` in the Main Extension guide](../main-extension#appmenus)). Interactive elements can be placed on the status bar (the blue strip along the bottom of the Lens UI) and can be configured to link to a global page when activated (See [`statusBarItems`](#statusbaritems)). As well, global pages can be made accessible from the cluster menu, which is the vertical strip along the left side of the Lens UI showing the available cluster icons, and the Add Cluster icon. Global page menu icons that are defined using [`globalPageMenus`](#globalpagemenus) appear below the Add Cluster icon. + +### `globalPageMenus` + +Global page menus connect a global page to the cluster menu, which is the vertical strip along the left side of the Lens UI showing the available cluster icons, and the Add Cluster icon. Expanding on the example from [`globalPages`](#globalPages) a global page menu is added to the `HelpExtension` definition: + +``` typescript +import { LensRendererExtension } from "@k8slens/extensions"; +import { HelpIcon, HelpPage } from "./page" +import React from "react" + +export default class HelpExtension extends LensRendererExtension { + clusterPages = [ + { + id: "help", + components: { + Page: () => , + } + } + ]; + + globalPageMenus = [ + { + target: { pageId: "help" }, + title: "Help", + components: { + Icon: HelpIcon, + } + }, + ]; +} +``` + +Global page menus are objects matching the `PageMenuRegistration` interface. They define the appearance of the global page menu item in the cluster menu and the behaviour when the global page menu item is activated (typically by a mouse click). The example above uses the `target` field to set the behaviour as a link to the global page with `id` of `"help"`. This is done by setting `target`'s `pageId` field to `"help"`. The global page menu item's appearance is defined by setting the `title` field to the text that is to be displayed as a tooltip in the cluster menu. The `components` field is used to set an icon that appears in the cluster menu. Thus when the `"Help"` icon is activated the contents of `ExamplePage` will be shown. This example requires the definition of another React-based component, `HelpIcon`, which has been added to `page.tsx`: + +``` typescript +import { LensRendererExtension, Component } from "@k8slens/extensions"; +import React from "react" + +export function HelpIcon(props: Component.IconProps) { + return +} + +export class HelpPage extends React.Component<{ extension: LensRendererExtension }> { + render() { + return ( +
+

Help

+
+ ) + } +} +``` + +`HelpIcon` introduces one of Lens' built-in components available to extension developers, the `Component.Icon`. Built in are the [Material Design](https://material.io) [icons](https://material.io/resources/icons/). One can be selected by name via the `material` field. + + + + +********************************************************************* +WIP below! +********************************************************************* + + + +### `clusterFeatures` + +Cluster features are Kubernetes resources that can applied and managed to the active cluster. They can be installed/uninstalled from the [cluster settings page](). +The following example shows how to add a cluster feature: + +``` typescript +import { LensRendererExtension } from "@k8slens/extensions" +import { MetricsFeature } from "./src/metrics-feature" +import React from "react" + +export default class ClusterMetricsFeatureExtension extends LensRendererExtension { + clusterFeatures = [ + { + title: "Metrics Stack", + components: { + Description: () => { + return ( + + Enable timeseries data visualization (Prometheus stack) for your cluster. + Install this only if you don't have existing Prometheus stack installed. + You can see preview of manifests here. + + ) + } + }, + feature: new MetricsFeature() + } + ]; +} +``` +The `title` and `components.Description` fields appear on the cluster settings page. The cluster feature must extend the abstract class `ClusterFeature.Feature`, and specifically implement the following methods: + +``` typescript + abstract install(cluster: Cluster): Promise; + abstract upgrade(cluster: Cluster): Promise; + abstract uninstall(cluster: Cluster): Promise; + abstract updateStatus(cluster: Cluster): Promise; +``` + +### `appPreferences` + +The Preferences page is essentially a global page. Extensions can add custom preferences to the Preferences page, thus providing a single location for users to configure global, for Lens and extensions alike. + +``` typescript +import React from "react" +import { LensRendererExtension } from "@k8slens/extensions" +import { myCustomPreferencesStore } from "./src/my-custom-preferences-store" +import { MyCustomPreferenceHint, MyCustomPreferenceInput } from "./src/my-custom-preference" + + +export default class ExampleRendererExtension extends LensRendererExtension { + appPreferences = [ + { + title: "My Custom Preference", + components: { + Hint: () => , + Input: () => + } + } + ]; +} +``` + +### `statusBarItems` + +The Status bar is the blue strip along the bottom of the Lens UI. Status bar items are `React.ReactNode` types, which can be used to convey status information, or act as a link to a global page. + +The following example adds a status bar item definition, as well as a global page definition, to a `LensRendererExtension` subclass, and configures the status bar item to navigate to the global upon a mouse click: + +``` typescript +import { LensRendererExtension, Navigation } from '@k8slens/extensions'; +import { MyStatusBarIcon, MyPage } from './page'; +import React from 'react'; + +export default class ExtensionRenderer extends LensRendererExtension { + globalPages = [ + { + path: "/my-extension-path", + hideInMenu: true, + components: { + Page: () => , + }, + }, + ]; + + statusBarItems = [ + { + item: ( +
Navigation.navigate(this.globalPages[0].path)} + > + + My Status Bar Item +
+ ), + }, + ]; +} +``` + +### `kubeObjectMenuItems` + +An extension can add custom menu items (including actions) for specified Kubernetes resource kinds/apiVersions. These menu items appear under the `...` for each listed resource, and on the title bar of the details page for a specific resource. + +``` typescript +import React from "react" +import { LensRendererExtension } from "@k8slens/extensions"; +import { CustomMenuItem, CustomMenuItemProps } from "./src/custom-menu-item" + +export default class ExampleExtension extends LensRendererExtension { + kubeObjectMenuItems = [ + { + kind: "Node", + apiVersions: ["v1"], + components: { + MenuItem: (props: CustomMenuItemProps) => + } + } + ]; +} + +``` + +### `kubeObjectDetailItems` + +An extension can add custom details (content) for specified Kubernetes resource kinds/apiVersions. These custom details appear on the details page for a specific resource. + +``` typescript +import React from "react" +import { LensRendererExtension } from "@k8slens/extensions"; +import { CustomKindDetails, CustomKindDetailsProps } from "./src/custom-kind-details" + +export default class ExampleExtension extends LensRendererExtension { + kubeObjectMenuItems = [ + { + kind: "CustomKind", + apiVersions: ["custom.acme.org/v1"], + components: { + Details: (props: CustomKindDetailsProps) => + } + } + ]; +} +``` \ No newline at end of file From 39226092044a61c491c5c51d8af68a2bdd09b653 Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Tue, 24 Nov 2020 09:54:19 +0200 Subject: [PATCH 4/5] Fix azure pipeline yarn cache (#1491) Signed-off-by: Jari Kolehmainen --- .azure-pipelines.yml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/.azure-pipelines.yml b/.azure-pipelines.yml index 86c83c39a1..33dfe83206 100644 --- a/.azure-pipelines.yml +++ b/.azure-pipelines.yml @@ -30,10 +30,9 @@ jobs: displayName: Install Node.js - task: Cache@2 inputs: - key: yarn | $(Agent.OS) | yarn.lock + key: 'yarn | "$(Agent.OS)"" | yarn.lock' restoreKeys: | yarn | "$(Agent.OS)" - yarn path: $(YARN_CACHE_FOLDER) displayName: Cache Yarn packages - script: make node_modules @@ -70,10 +69,9 @@ jobs: displayName: Install Node.js - task: Cache@2 inputs: - key: yarn | $(Agent.OS) | yarn.lock + key: 'yarn | "$(Agent.OS)" | yarn.lock' restoreKeys: | yarn | "$(Agent.OS)" - yarn path: $(YARN_CACHE_FOLDER) displayName: Cache Yarn packages - script: make node_modules @@ -116,10 +114,9 @@ jobs: displayName: Install Node.js - task: Cache@2 inputs: - key: yarn | $(Agent.OS) | yarn.lock + key: 'yarn | "$(Agent.OS)" | yarn.lock' restoreKeys: | yarn | "$(Agent.OS)" - yarn path: $(YARN_CACHE_FOLDER) displayName: Cache Yarn packages - script: make node_modules From c79cee031114ebb28f524db6706cf6502f605477 Mon Sep 17 00:00:00 2001 From: Panu Horsmalahti Date: Tue, 24 Nov 2020 10:48:40 +0200 Subject: [PATCH 5/5] Watch for added/removed local extensions (#1482) Signed-off-by: Panu Horsmalahti --- package.json | 2 + src/extensions/extension-discovery.ts | 329 ++++++++++++++++++ src/extensions/extension-installer.ts | 69 ++++ src/extensions/extension-loader.ts | 43 ++- src/extensions/extension-manager.ts | 172 --------- src/extensions/lens-extension.ts | 3 +- src/main/index.ts | 19 +- .../components/+extensions/extensions.tsx | 4 +- yarn.lock | 7 +- 9 files changed, 457 insertions(+), 191 deletions(-) create mode 100644 src/extensions/extension-discovery.ts create mode 100644 src/extensions/extension-installer.ts delete mode 100644 src/extensions/extension-manager.ts diff --git a/package.json b/package.json index 013a46aed8..1a0cec8f2c 100644 --- a/package.json +++ b/package.json @@ -216,7 +216,9 @@ "@types/react-beautiful-dnd": "^13.0.0", "@types/tar": "^4.0.3", "array-move": "^3.0.0", + "await-lock": "^2.1.0", "chalk": "^4.1.0", + "chokidar": "^3.4.3", "command-exists": "1.2.9", "conf": "^7.0.1", "crypto-js": "^4.0.0", diff --git a/src/extensions/extension-discovery.ts b/src/extensions/extension-discovery.ts new file mode 100644 index 0000000000..bb1d1db420 --- /dev/null +++ b/src/extensions/extension-discovery.ts @@ -0,0 +1,329 @@ +import chokidar from "chokidar"; +import { EventEmitter } from "events"; +import fs from "fs-extra"; +import os from "os"; +import path from "path"; +import { getBundledExtensions } from "../common/utils/app-version"; +import logger from "../main/logger"; +import { extensionInstaller, PackageJson } from "./extension-installer"; +import type { LensExtensionId, LensExtensionManifest } from "./lens-extension"; + +export interface InstalledExtension { + readonly manifest: LensExtensionManifest; + readonly manifestPath: string; + readonly isBundled: boolean; // defined in project root's package.json + isEnabled: boolean; + } + +const logModule = "[EXTENSION-DISCOVERY]"; +const manifestFilename = "package.json"; + +/** + * Returns true if the lstat is for a directory-like file (e.g. isDirectory or symbolic link) + * @param lstat the stats to compare + */ +const isDirectoryLike = (lstat: fs.Stats) => lstat.isDirectory() || lstat.isSymbolicLink(); + +/** + * Discovers installed bundled and local extensions from the filesystem. + * Also watches for added and removed local extensions by watching the directory. + * Uses ExtensionInstaller to install dependencies for all of the extensions. + * This is also done when a new extension is copied to the local extensions directory. + * .init() must be called to start the directory watching. + * The class emits events for added and removed extensions: + * - "add": When extension is added. The event is of type InstalledExtension + * - "remove": When extension is removed. The event is of type LensExtensionId + */ +export class ExtensionDiscovery { + protected bundledFolderPath: string; + + private loadStarted = false; + + // This promise is resolved when .load() is finished. + // This allows operations to be added after .load() success. + private loaded: Promise; + + // These are called to either resolve or reject this.loaded promise + private resolveLoaded: () => void; + private rejectLoaded: (error: any) => void; + + public events: EventEmitter; + + constructor() { + this.loaded = new Promise((resolve, reject) => { + this.resolveLoaded = resolve; + this.rejectLoaded = reject; + }); + + this.events = new EventEmitter(); + } + + // Each extension is added as a single dependency to this object, which is written as package.json. + // Each dependency key is the name of the dependency, and + // each dependency value is the non-symlinked path to the dependency (folder). + protected packagesJson: PackageJson = { + dependencies: {} + }; + + get localFolderPath(): string { + return path.join(os.homedir(), ".k8slens", "extensions"); + } + + get packageJsonPath() { + return path.join(extensionInstaller.extensionPackagesRoot, manifestFilename); + } + + get inTreeTargetPath() { + return path.join(extensionInstaller.extensionPackagesRoot, "extensions"); + } + + get inTreeFolderPath(): string { + return path.resolve(__static, "../extensions"); + } + + get nodeModulesPath(): string { + return path.join(extensionInstaller.extensionPackagesRoot, "node_modules"); + } + + /** + * Initializes the class and setups the file watcher for added/removed local extensions. + */ + init() { + this.watchExtensions(); + } + + /** + * Watches for added/removed local extensions. + * Dependencies are installed automatically after an extension folder is copied. + */ + async watchExtensions() { + logger.info(`${logModule} watching extension add/remove in ${this.localFolderPath}`); + + // Wait until .load() has been called and has been resolved + await this.loaded; + + // chokidar works better than fs.watch + chokidar.watch(this.localFolderPath, { + // Dont watch recursively into subdirectories + depth: 0, + // Try to wait until the file has been completely copied. + // The OS might emit an event for added file even it's not completely written to the filesysten. + awaitWriteFinish: { + // Wait 300ms until the file size doesn't change to consider the file written. + // For a small file like package.json this should be plenty of time. + stabilityThreshold: 300 + } + }) + // Extension add is detected by watching "package.json" add + .on("add", this.handleWatchFileAdd) + // Extension remove is detected by watching " unlink + .on("unlinkDir", this.handleWatchUnlinkDir); + } + + handleWatchFileAdd = async (filePath: string) => { + if (path.basename(filePath) === manifestFilename) { + try { + const absPath = path.dirname(filePath); + + // this.loadExtensionFromPath updates this.packagesJson + const extension = await this.loadExtensionFromPath(absPath); + + if (extension) { + // Install dependencies for the new extension + await this.installPackages(); + + logger.info(`${logModule} Added extension ${extension.manifest.name}`); + this.events.emit("add", extension); + } + } catch (error) { + console.error(error); + } + } + }; + + handleWatchUnlinkDir = async (filePath: string) => { + // filePath is the non-symlinked path to the extension folder + // this.packagesJson.dependencies value is the non-symlinked path to the extension folder + // LensExtensionId in extension-loader is the symlinked path to the extension folder manifest file + + // Check that the removed path is directly under this.localFolderPath + // Note that the watcher can create unlink events for subdirectories of the extension + const extensionFolderName = path.basename(filePath); + + if (path.relative(this.localFolderPath, filePath) === extensionFolderName) { + const extensionName: string | undefined = Object + .entries(this.packagesJson.dependencies) + .find(([_name, extensionFolder]) => filePath === extensionFolder)?.[0]; + + if (extensionName !== undefined) { + delete this.packagesJson.dependencies[extensionName]; + + // Reinstall dependencies to remove the extension from package.json + await this.installPackages(); + + // The path to the manifest file is the lens extension id + // Note that we need to use the symlinked path + const lensExtensionId = path.join(this.nodeModulesPath, extensionName, "package.json"); + + logger.info(`${logModule} removed extension ${extensionName}`); + this.events.emit("remove", lensExtensionId as LensExtensionId); + } else { + logger.warn(`${logModule} extension ${extensionFolderName} not found, can't remove`); + } + } + }; + + async load(): Promise> { + if (this.loadStarted) { + // The class is simplified by only supporting .load() to be called once + throw new Error("ExtensionDiscovery.load() can be only be called once"); + } + + this.loadStarted = true; + + try { + logger.info(`${logModule} loading extensions from ${extensionInstaller.extensionPackagesRoot}`); + + if (fs.existsSync(path.join(extensionInstaller.extensionPackagesRoot, "package-lock.json"))) { + await fs.remove(path.join(extensionInstaller.extensionPackagesRoot, "package-lock.json")); + } + + try { + await fs.access(this.inTreeFolderPath, fs.constants.W_OK); + this.bundledFolderPath = this.inTreeFolderPath; + } catch { + // we need to copy in-tree extensions so that we can symlink them properly on "npm install" + await fs.remove(this.inTreeTargetPath); + await fs.ensureDir(this.inTreeTargetPath); + await fs.copy(this.inTreeFolderPath, this.inTreeTargetPath); + this.bundledFolderPath = this.inTreeTargetPath; + } + + await fs.ensureDir(this.nodeModulesPath); + await fs.ensureDir(this.localFolderPath); + + const extensions = await this.loadExtensions(); + + // resolve the loaded promise + this.resolveLoaded(); + + return extensions; + } catch (error) { + this.rejectLoaded(error); + } + } + + protected async getByManifest(manifestPath: string, { isBundled = false, isEnabled = isBundled }: { + isBundled?: boolean; + isEnabled?: boolean; + } = {}): Promise { + let manifestJson: LensExtensionManifest; + + try { + // check manifest file for existence + fs.accessSync(manifestPath, fs.constants.F_OK); + + manifestJson = __non_webpack_require__(manifestPath); + this.packagesJson.dependencies[manifestJson.name] = path.dirname(manifestPath); + + return { + manifestPath: path.join(this.nodeModulesPath, manifestJson.name, "package.json"), + manifest: manifestJson, + isBundled, + isEnabled, + }; + } catch (error) { + logger.error(`${logModule}: can't install extension at ${manifestPath}: ${error}`, { manifestJson }); + + return null; + } + } + + async loadExtensions(): Promise> { + const bundledExtensions = await this.loadBundledExtensions(); + const localExtensions = await this.loadFromFolder(this.localFolderPath); + await this.installPackages(); + const extensions = bundledExtensions.concat(localExtensions); + + return new Map(extensions.map(ext => [ext.manifestPath, ext])); + } + + /** + * Write package.json to file system and install dependencies. + */ + installPackages() { + return extensionInstaller.installPackages(this.packageJsonPath, this.packagesJson); + } + + async loadBundledExtensions() { + const extensions: InstalledExtension[] = []; + const folderPath = this.bundledFolderPath; + const bundledExtensions = getBundledExtensions(); + const paths = await fs.readdir(folderPath); + + for (const fileName of paths) { + if (!bundledExtensions.includes(fileName)) { + continue; + } + + const absPath = path.resolve(folderPath, fileName); + const extension = await this.loadExtensionFromPath(absPath, { isBundled: true }); + + if (extension) { + extensions.push(extension); + } + } + logger.debug(`${logModule}: ${extensions.length} extensions loaded`, { folderPath, extensions }); + + return extensions; + } + + async loadFromFolder(folderPath: string): Promise { + const bundledExtensions = getBundledExtensions(); + const extensions: InstalledExtension[] = []; + const paths = await fs.readdir(folderPath); + + for (const fileName of paths) { + // do not allow to override bundled extensions + if (bundledExtensions.includes(fileName)) { + continue; + } + + const absPath = path.resolve(folderPath, fileName); + + if (!fs.existsSync(absPath)) { + continue; + } + + const lstat = await fs.lstat(absPath); + + // skip non-directories + if (!isDirectoryLike(lstat)) { + continue; + } + + const extension = await this.loadExtensionFromPath(absPath); + + if (extension) { + extensions.push(extension); + } + } + + logger.debug(`${logModule}: ${extensions.length} extensions loaded`, { folderPath, extensions }); + return extensions; + } + + /** + * Loads extension from absolute path, updates this.packagesJson to include it and returns the extension. + */ + async loadExtensionFromPath(absPath: string, { isBundled = false, isEnabled = isBundled }: { + isBundled?: boolean; + isEnabled?: boolean; + } = {}): Promise { + const manifestPath = path.resolve(absPath, manifestFilename); + + return this.getByManifest(manifestPath, { isBundled, isEnabled }); + } +} + +export const extensionDiscovery = new ExtensionDiscovery(); \ No newline at end of file diff --git a/src/extensions/extension-installer.ts b/src/extensions/extension-installer.ts new file mode 100644 index 0000000000..46a7a31e6f --- /dev/null +++ b/src/extensions/extension-installer.ts @@ -0,0 +1,69 @@ +import AwaitLock from 'await-lock'; +import child_process from "child_process"; +import fs from "fs-extra"; +import path from "path"; +import logger from "../main/logger"; +import { extensionPackagesRoot } from "./extension-loader"; + +const logModule = "[EXTENSION-INSTALLER]"; + +type Dependencies = { + [name: string]: string; +}; + +// Type for the package.json file that is written by ExtensionInstaller +export type PackageJson = { + dependencies: Dependencies; +}; + +/** + * Installs dependencies for extensions + */ +export class ExtensionInstaller { + private installLock = new AwaitLock(); + + get extensionPackagesRoot() { + return extensionPackagesRoot(); + } + + get npmPath() { + return __non_webpack_require__.resolve('npm/bin/npm-cli'); + } + + installDependencies(): Promise { + return new Promise((resolve, reject) => { + logger.info(`${logModule} installing dependencies at ${extensionPackagesRoot()}`); + const child = child_process.fork(this.npmPath, ["install", "--silent", "--no-audit", "--only=prod", "--prefer-offline", "--no-package-lock"], { + cwd: extensionPackagesRoot(), + silent: true + }); + child.on("close", () => { + resolve(); + }); + child.on("error", (err) => { + reject(err); + }); + }); + } + + /** + * Write package.json to the file system and execute npm install for it. + */ + async installPackages(packageJsonPath: string, packagesJson: PackageJson): Promise { + // Mutual exclusion to install packages in sequence + await this.installLock.acquireAsync(); + + try { + // Write the package.json which will be installed in .installDependencies() + await fs.writeFile(path.join(packageJsonPath), JSON.stringify(packagesJson, null, 2), { + mode: 0o600 + }); + + await this.installDependencies(); + } finally { + this.installLock.release(); + } + } +} + +export const extensionInstaller = new ExtensionInstaller(); diff --git a/src/extensions/extension-loader.ts b/src/extensions/extension-loader.ts index c71fe90db2..e51caa298b 100644 --- a/src/extensions/extension-loader.ts +++ b/src/extensions/extension-loader.ts @@ -1,20 +1,25 @@ +import { app, ipcRenderer, remote } from "electron"; +import { action, computed, observable, reaction, toJS, when } from "mobx"; +import path from "path"; +import { broadcastMessage, handleRequest, requestMain, subscribeToBroadcast } from "../common/ipc"; +import logger from "../main/logger"; +import type { InstalledExtension } from "./extension-discovery"; +import { extensionsStore } from "./extensions-store"; import type { LensExtension, LensExtensionConstructor, LensExtensionId } from "./lens-extension"; import type { LensMainExtension } from "./lens-main-extension"; import type { LensRendererExtension } from "./lens-renderer-extension"; -import type { InstalledExtension } from "./extension-manager"; -import path from "path"; -import { broadcastMessage, handleRequest, requestMain, subscribeToBroadcast } from "../common/ipc"; -import { action, computed, observable, reaction, toJS, when } from "mobx"; -import logger from "../main/logger"; -import { app, ipcRenderer, remote } from "electron"; import * as registries from "./registries"; -import { extensionsStore } from "./extensions-store"; // lazy load so that we get correct userData export function extensionPackagesRoot() { return path.join((app || remote.app).getPath("userData")); } +const logModule = "[EXTENSIONS-LOADER]"; + +/** + * Loads installed extensions to the Lens application + */ export class ExtensionLoader { protected extensions = observable.map(); protected instances = observable.map(); @@ -47,6 +52,17 @@ export class ExtensionLoader { this.extensions.replace(extensions); } + addExtension(extension: InstalledExtension) { + this.extensions.set(extension.manifestPath as LensExtensionId, extension); + } + + removeExtension(lensExtensionId: LensExtensionId) { + // TODO: Remove the extension properly (from menus etc.) + if (!this.extensions.delete(lensExtensionId)) { + throw new Error(`Can't remove extension ${lensExtensionId}, doesn't exist.`); + } + } + protected async initMain() { this.isLoaded = true; this.loadOnMain(); @@ -77,14 +93,14 @@ export class ExtensionLoader { } loadOnMain() { - logger.info('[EXTENSIONS-LOADER]: load on main'); + logger.info(`${logModule}: load on main`); this.autoInitExtensions((ext: LensMainExtension) => [ registries.menuRegistry.add(ext.appMenus) ]); } loadOnClusterManagerRenderer() { - logger.info('[EXTENSIONS-LOADER]: load on main renderer (cluster manager)'); + logger.info(`${logModule}: load on main renderer (cluster manager)`); this.autoInitExtensions((ext: LensRendererExtension) => [ registries.globalPageRegistry.add(ext.globalPages, ext), registries.globalPageMenuRegistry.add(ext.globalPageMenus, ext), @@ -95,7 +111,7 @@ export class ExtensionLoader { } loadOnClusterRenderer() { - logger.info('[EXTENSIONS-LOADER]: load on cluster renderer (dashboard)'); + logger.info(`${logModule}: load on cluster renderer (dashboard)`); this.autoInitExtensions((ext: LensRendererExtension) => [ registries.clusterPageRegistry.add(ext.clusterPages, ext), registries.clusterPageMenuRegistry.add(ext.clusterPageMenus, ext), @@ -118,14 +134,15 @@ export class ExtensionLoader { instance.enable(); this.instances.set(extId, instance); } catch (err) { - logger.error(`[EXTENSION-LOADER]: activation extension error`, { ext, err }); + logger.error(`${logModule}: activation extension error`, { ext, err }); } } else if (!ext.isEnabled && instance) { + logger.info(`${logModule} deleting extension ${extId}`); try { instance.disable(); this.instances.delete(extId); } catch (err) { - logger.error(`[EXTENSION-LOADER]: deactivation extension error`, { ext, err }); + logger.error(`${logModule}: deactivation extension error`, { ext, err }); } } } @@ -146,7 +163,7 @@ export class ExtensionLoader { return __non_webpack_require__(extEntrypoint).default; } } catch (err) { - console.error(`[EXTENSION-LOADER]: can't load extension main at ${extEntrypoint}: ${err}`, { extension }); + console.error(`${logModule}: can't load extension main at ${extEntrypoint}: ${err}`, { extension }); console.trace(err); } } diff --git a/src/extensions/extension-manager.ts b/src/extensions/extension-manager.ts deleted file mode 100644 index 0e51eeb666..0000000000 --- a/src/extensions/extension-manager.ts +++ /dev/null @@ -1,172 +0,0 @@ -import type { LensExtensionId, LensExtensionManifest } from "./lens-extension"; -import path from "path"; -import os from "os"; -import fs from "fs-extra"; -import child_process from "child_process"; -import logger from "../main/logger"; -import { extensionPackagesRoot } from "./extension-loader"; -import { getBundledExtensions } from "../common/utils/app-version"; - -export interface InstalledExtension { - readonly manifest: LensExtensionManifest; - readonly manifestPath: string; - readonly isBundled: boolean; // defined in project root's package.json - isEnabled: boolean; -} - -type Dependencies = { - [name: string]: string; -}; - -type PackageJson = { - dependencies: Dependencies; -}; - -export class ExtensionManager { - - protected bundledFolderPath: string; - - protected packagesJson: PackageJson = { - dependencies: {} - }; - - get extensionPackagesRoot() { - return extensionPackagesRoot(); - } - - get inTreeTargetPath() { - return path.join(this.extensionPackagesRoot, "extensions"); - } - - get inTreeFolderPath(): string { - return path.resolve(__static, "../extensions"); - } - - get nodeModulesPath(): string { - return path.join(this.extensionPackagesRoot, "node_modules"); - } - - get localFolderPath(): string { - return path.join(os.homedir(), ".k8slens", "extensions"); - } - - get npmPath() { - return __non_webpack_require__.resolve('npm/bin/npm-cli'); - } - - get packageJsonPath() { - return path.join(this.extensionPackagesRoot, "package.json"); - } - - async load(): Promise> { - logger.info("[EXTENSION-MANAGER] loading extensions from " + this.extensionPackagesRoot); - if (fs.existsSync(path.join(this.extensionPackagesRoot, "package-lock.json"))) { - await fs.remove(path.join(this.extensionPackagesRoot, "package-lock.json")); - } - try { - await fs.access(this.inTreeFolderPath, fs.constants.W_OK); - this.bundledFolderPath = this.inTreeFolderPath; - } catch { - // we need to copy in-tree extensions so that we can symlink them properly on "npm install" - await fs.remove(this.inTreeTargetPath); - await fs.ensureDir(this.inTreeTargetPath); - await fs.copy(this.inTreeFolderPath, this.inTreeTargetPath); - this.bundledFolderPath = this.inTreeTargetPath; - } - await fs.ensureDir(this.nodeModulesPath); - await fs.ensureDir(this.localFolderPath); - return await this.loadExtensions(); - } - - protected async getByManifest(manifestPath: string, { isBundled = false } = {}): Promise { - let manifestJson: LensExtensionManifest; - try { - fs.accessSync(manifestPath, fs.constants.F_OK); // check manifest file for existence - manifestJson = __non_webpack_require__(manifestPath); - this.packagesJson.dependencies[manifestJson.name] = path.dirname(manifestPath); - - logger.info("[EXTENSION-MANAGER] installed extension " + manifestJson.name); - return { - manifestPath: path.join(this.nodeModulesPath, manifestJson.name, "package.json"), - manifest: manifestJson, - isBundled: isBundled, - isEnabled: isBundled, - }; - } catch (err) { - logger.error(`[EXTENSION-MANAGER]: can't install extension at ${manifestPath}: ${err}`, { manifestJson }); - } - } - - protected installPackages(): Promise { - return new Promise((resolve, reject) => { - const child = child_process.fork(this.npmPath, ["install", "--silent", "--no-audit", "--only=prod", "--prefer-offline", "--no-package-lock"], { - cwd: extensionPackagesRoot(), - silent: true - }); - child.on("close", () => { - resolve(); - }); - child.on("error", (err) => { - reject(err); - }); - }); - } - - async loadExtensions() { - const bundledExtensions = await this.loadBundledExtensions(); - const localExtensions = await this.loadFromFolder(this.localFolderPath); - await fs.writeFile(path.join(this.packageJsonPath), JSON.stringify(this.packagesJson, null, 2), { mode: 0o600 }); - await this.installPackages(); - const extensions = bundledExtensions.concat(localExtensions); - return new Map(extensions.map(ext => [ext.manifestPath, ext])); - } - - async loadBundledExtensions() { - const extensions: InstalledExtension[] = []; - const folderPath = this.bundledFolderPath; - const bundledExtensions = getBundledExtensions(); - const paths = await fs.readdir(folderPath); - for (const fileName of paths) { - if (!bundledExtensions.includes(fileName)) { - continue; - } - const absPath = path.resolve(folderPath, fileName); - const manifestPath = path.resolve(absPath, "package.json"); - const ext = await this.getByManifest(manifestPath, { isBundled: true }).catch(() => null); - if (ext) { - extensions.push(ext); - } - } - logger.debug(`[EXTENSION-MANAGER]: ${extensions.length} extensions loaded`, { folderPath, extensions }); - return extensions; - } - - async loadFromFolder(folderPath: string): Promise { - const bundledExtensions = getBundledExtensions(); - const extensions: InstalledExtension[] = []; - const paths = await fs.readdir(folderPath); - for (const fileName of paths) { - if (bundledExtensions.includes(fileName)) { // do no allow to override bundled extensions - continue; - } - const absPath = path.resolve(folderPath, fileName); - if (!fs.existsSync(absPath)) { - continue; - } - const lstat = await fs.lstat(absPath); - if (!lstat.isDirectory() && !lstat.isSymbolicLink()) { // skip non-directories - continue; - } - const manifestPath = path.resolve(absPath, "package.json"); - const ext = await this.getByManifest(manifestPath).catch(() => null); - if (ext) { - extensions.push(ext); - } - } - - logger.debug(`[EXTENSION-MANAGER]: ${extensions.length} extensions loaded`, { folderPath, extensions }); - return extensions; - } -} - -export const extensionManager = new ExtensionManager(); diff --git a/src/extensions/lens-extension.ts b/src/extensions/lens-extension.ts index 444cf449d6..fc7f3ff0df 100644 --- a/src/extensions/lens-extension.ts +++ b/src/extensions/lens-extension.ts @@ -1,4 +1,4 @@ -import type { InstalledExtension } from "./extension-manager"; +import type { InstalledExtension } from "./extension-discovery"; import { action, observable, reaction } from "mobx"; import logger from "../main/logger"; @@ -27,6 +27,7 @@ export class LensExtension { } get id(): LensExtensionId { + // This is the symlinked path under node_modules return this.manifestPath; } diff --git a/src/main/index.ts b/src/main/index.ts index 2087432d7b..b6f9fd969e 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -21,8 +21,9 @@ import { userStore } from "../common/user-store"; import { workspaceStore } from "../common/workspace-store"; import { appEventBus } from "../common/event-bus"; import { extensionLoader } from "../extensions/extension-loader"; -import { extensionManager } from "../extensions/extension-manager"; import { extensionsStore } from "../extensions/extensions-store"; +import { InstalledExtension, extensionDiscovery } from "../extensions/extension-discovery"; +import type { LensExtensionId } from "../extensions/lens-extension"; const workingDir = path.join(app.getPath("appData"), appName); let proxyPort: number; @@ -79,8 +80,22 @@ app.on("ready", async () => { } extensionLoader.init(); + + extensionDiscovery.init(); windowManager = WindowManager.getInstance(proxyPort); - extensionLoader.initExtensions(await extensionManager.load()); // call after windowManager to see splash earlier + + // call after windowManager to see splash earlier + const extensions = await extensionDiscovery.load(); + + // Subscribe to extensions that are copied or deleted to/from the extensions folder + extensionDiscovery.events.on("add", (extension: InstalledExtension) => { + extensionLoader.addExtension(extension); + }); + extensionDiscovery.events.on("remove", (lensExtensionId: LensExtensionId) => { + extensionLoader.removeExtension(lensExtensionId); + }); + + extensionLoader.initExtensions(extensions); setTimeout(() => { appEventBus.emit({ name: "service", action: "start" }); diff --git a/src/renderer/components/+extensions/extensions.tsx b/src/renderer/components/+extensions/extensions.tsx index 875861a8dd..a8b9c54f51 100644 --- a/src/renderer/components/+extensions/extensions.tsx +++ b/src/renderer/components/+extensions/extensions.tsx @@ -11,7 +11,7 @@ import { Input } from "../input"; import { Icon } from "../icon"; import { PageLayout } from "../layout/page-layout"; import { extensionLoader } from "../../../extensions/extension-loader"; -import { extensionManager } from "../../../extensions/extension-manager"; +import { extensionDiscovery } from "../../../extensions/extension-discovery"; @observer export class Extensions extends React.Component { @@ -29,7 +29,7 @@ export class Extensions extends React.Component { } get extensionsPath() { - return extensionManager.localFolderPath; + return extensionDiscovery.localFolderPath; } renderInfo() { diff --git a/yarn.lock b/yarn.lock index 72b2025686..7c5ff7abc9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3262,6 +3262,11 @@ atob@^2.1.2: resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9" integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg== +await-lock@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/await-lock/-/await-lock-2.1.0.tgz#bc78c51d229a34d5d90965a1c94770e772c6145e" + integrity sha512-t7Zm5YGgEEc/3eYAicF32m/TNvL+XOeYZy9CvBUeJY/szM7frLolFylhrlZNWV/ohWhcUXygrBGjYmoQdxF4CQ== + aws-sign2@~0.7.0: version "0.7.0" resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.7.0.tgz#b46e890934a9591f2d2f6f86d7e6a9f1b3fe76a8" @@ -4116,7 +4121,7 @@ chokidar@^3.2.2: optionalDependencies: fsevents "~2.1.2" -chokidar@^3.4.1: +chokidar@^3.4.1, chokidar@^3.4.3: version "3.4.3" resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.4.3.tgz#c1df38231448e45ca4ac588e6c79573ba6a57d5b" integrity sha512-DtM3g7juCXQxFVSNPNByEC2+NImtBuxQQvWlHunpJIS5Ocr0lG306cC7FCi7cEA0fzmybPUIl4txBIobk1gGOQ==