Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to install packed extensions from URL or local file #1456

Merged
merged 23 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6624287
Option to install an extension from filesystem/url #1227 -- part 1 (UI)
ixrock Nov 19, 2020
7d28a43
DropFileInput: common component to handle droped files (replaced also…
ixrock Nov 19, 2020
724ee86
fix: install via url-string on input.submit
ixrock Nov 19, 2020
cc28424
ui tweaks & minor fixes
ixrock Nov 19, 2020
3df6c07
Merge remote-tracking branch 'origin/master' into extension_install_1277
ixrock Nov 19, 2020
97bcb49
more ui/ux tweaks & fixes
ixrock Nov 19, 2020
51f686c
layout fixes
ixrock Nov 19, 2020
f505dab
component renaming: `copy-to-click` => `copy-to-clipboard` => `clipbo…
ixrock Nov 19, 2020
78dcd5d
reworks -- part 1
ixrock Nov 20, 2020
9284611
fix downloading file, added common/utils/downloadFile
ixrock Nov 23, 2020
5093262
confirm before install, unpack tar first steps
ixrock Nov 23, 2020
88950b0
installation flow, extracting .tgz
ixrock Nov 23, 2020
de93812
Merge remote-tracking branch 'origin/master' into extension_install_1277
ixrock Nov 23, 2020
150c3dc
clean up, fix lint issues
ixrock Nov 23, 2020
8b6616a
update .azure-pipelines.yml
ixrock Nov 23, 2020
5fc0c63
fixes & refactoring
ixrock Nov 24, 2020
e460b3f
Merge remote-tracking branch 'origin/master' into extension_install_1277
ixrock Nov 24, 2020
12c3422
fix lint harder :/
ixrock Nov 24, 2020
d050d6c
fix validation
ixrock Nov 24, 2020
1c17420
fix validation harder
ixrock Nov 24, 2020
55c284f
responding to comments, fixed package validation
ixrock Nov 24, 2020
639bc1a
common/utils/tar.ts: reject with Error-type
ixrock Nov 24, 2020
4c3a8e2
fix: unit-tests
ixrock Nov 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"bundledHelmVersion": "3.3.4"
},
"engines": {
"node": ">=12.0 <13.0"
"node": ">=12 <=14"
},
"lingui": {
"locales": [
Expand Down Expand Up @@ -214,7 +214,7 @@
"@types/node": "^12.12.45",
"@types/proper-lockfile": "^4.1.1",
"@types/react-beautiful-dnd": "^13.0.0",
"@types/tar": "^4.0.3",
"@types/tar": "^4.0.4",
"array-move": "^3.0.0",
"chalk": "^4.1.0",
"command-exists": "1.2.9",
Expand Down Expand Up @@ -248,7 +248,7 @@
"serializr": "^2.0.3",
"shell-env": "^3.0.0",
"spdy": "^4.0.2",
"tar": "^6.0.2",
"tar": "^6.0.5",
"tcp-port-used": "^1.0.1",
"tempy": "^0.5.0",
"uuid": "^8.1.0",
Expand Down Expand Up @@ -310,7 +310,6 @@
"@types/sharp": "^0.26.0",
"@types/shelljs": "^0.8.8",
"@types/spdy": "^3.4.4",
"@types/tar": "^4.0.3",
"@types/tcp-port-used": "^1.0.0",
"@types/tempy": "^0.3.0",
"@types/terser-webpack-plugin": "^3.0.0",
Expand Down
36 changes: 36 additions & 0 deletions src/common/utils/downloadFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import request from "request";

export interface DownloadFileOptions {
url: string;
gzip?: boolean;
}

export interface DownloadFileTicket {
url: string;
promise: Promise<Buffer>;
cancel(): void;
}

export function downloadFile(opts: DownloadFileOptions): DownloadFileTicket {
const { url, gzip = true } = opts;
const fileChunks: Buffer[] = [];
const req = request(url, { gzip });
const promise: Promise<Buffer> = new Promise((resolve, reject) => {
req.on("data", (chunk: Buffer) => {
fileChunks.push(chunk);
});
req.on("complete", () => {
resolve(Buffer.concat(fileChunks));
});
req.on("error", err => {
reject({ url, err });
});
});
return {
url: url,
promise: promise,
cancel() {
req.abort();
}
};
}
1 change: 1 addition & 0 deletions src/common/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from "./saveToAppFiles";
export * from "./singleton";
export * from "./openExternal";
export * from "./rectify-array";
export * from "./downloadFile";
49 changes: 49 additions & 0 deletions src/common/utils/tar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Helper for working with tarball files (.tar, .tgz)
// Docs: https://github.com/npm/node-tar
import tar, { ExtractOptions, FileStat } from "tar";
import path from "path";

export interface ReadFileFromTarOpts {
fileName?: string;
fileMatcher?(path: string, entry: FileStat): boolean;
notFoundMessage?: string;
}

export function readFileFromTar(tarFilePath: string, opts: ReadFileFromTarOpts): Promise<Buffer> {
return new Promise(async (resolve, reject) => {
const fileChunks: Buffer[] = [];
const {
fileName,
fileMatcher = (path: string) => path === fileName,
notFoundMessage = "File not found",
} = opts;

await tar.list({
file: tarFilePath,
filter: fileMatcher,
onentry(entry: FileStat) {
entry.on("data", chunk => {
fileChunks.push(chunk);
});
entry.on("error", err => {
reject(`Reading ${entry.path} error: ${err}`);
});
entry.on("end", () => {
resolve(Buffer.concat(fileChunks));
});
},
});

if (!fileChunks.length) {
reject(notFoundMessage);
}
});
}

export function extractTar(filePath: string, opts: ExtractOptions & { sync?: boolean } = {}) {
return tar.extract({
file: filePath,
cwd: path.dirname(filePath),
...opts,
});
}
9 changes: 9 additions & 0 deletions src/extensions/extension-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ export class ExtensionManager {
}
}

getNpmPackageTarballUrl(packageName: string) {
try {
const command = [this.npmPath, "view", packageName, "dist.tarball", "--silent"];
return child_process.execSync(command.join(" "), { encoding: "utf8" }).trim();
} catch (err) {
return null;
}
}

protected installPackages(): Promise<void> {
return new Promise((resolve, reject) => {
const child = child_process.fork(this.npmPath, ["install", "--silent", "--no-audit", "--only=prod", "--prefer-offline", "--no-package-lock"], {
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/lens-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface LensExtensionManifest {
description?: string;
main?: string; // path to %ext/dist/main.js
renderer?: string; // path to %ext/dist/renderer.js
lens?: object; // fixme: add more required fields for validation
}

export class LensExtension {
Expand Down Expand Up @@ -95,3 +96,7 @@ export class LensExtension {
// mock
}
}

export function sanitizeExtensionName(name: string) {
return name.replace("@", "").replace("/", "-");
}
6 changes: 1 addition & 5 deletions src/extensions/registries/page-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from "path";
import { action } from "mobx";
import { compile } from "path-to-regexp";
import { BaseRegistry } from "./base-registry";
import { LensExtension } from "../lens-extension";
import { LensExtension, sanitizeExtensionName } from "../lens-extension";
import logger from "../../main/logger";
import { recitfy } from "../../common/utils";

Expand Down Expand Up @@ -45,10 +45,6 @@ export interface PageComponents {
Page: React.ComponentType<any>;
}

export function sanitizeExtensionName(name: string) {
return name.replace("@", "").replace("/", "-");
}

export function getExtensionPageUrl<P extends object>({ extensionId, pageId = "", params }: PageMenuTarget<P>): string {
const extensionBaseUrl = compile(`/extension/:name`)({
name: sanitizeExtensionName(extensionId), // compile only with extension-id first and define base path
Expand Down
8 changes: 0 additions & 8 deletions src/renderer/components/+add-cluster/add-cluster.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
.AddCluster {
.droppable {
box-shadow: 0 0 0 5px inset $primary;

> * {
pointer-events: none;
}
}

.hint {
margin-top: -$padding;
color: $textColorSecondary;
Expand Down
108 changes: 48 additions & 60 deletions src/renderer/components/+add-cluster/add-cluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { KubeConfig } from "@kubernetes/client-node";
import { _i18n } from "../../i18n";
import { t, Trans } from "@lingui/macro";
import { Select, SelectOption } from "../select";
import { Input } from "../input";
import { DropFileInput, Input } from "../input";
import { AceEditor } from "../ace-editor";
import { Button } from "../button";
import { Icon } from "../icon";
Expand Down Expand Up @@ -43,7 +43,6 @@ export class AddCluster extends React.Component {
@observable proxyServer = "";
@observable isWaiting = false;
@observable showSettings = false;
@observable dropAreaActive = false;

componentDidMount() {
clusterStore.setActive(null);
Expand Down Expand Up @@ -119,6 +118,11 @@ export class AddCluster extends React.Component {
}
};

onDropKubeConfig = (files: File[]) => {
this.sourceTab = KubeConfigSourceTab.FILE;
this.setKubeConfig(files[0].path);
};

@action
addClusters = () => {
let newClusters: ClusterModel[] = [];
Expand All @@ -137,7 +141,7 @@ export class AddCluster extends React.Component {
return true;
} catch (err) {
this.error = String(err.message);
if (err instanceof ExecValidationNotFoundError ) {
if (err instanceof ExecValidationNotFoundError) {
Notifications.error(<Trans>Error while adding cluster(s): {this.error}</Trans>);
return false;
} else {
Expand Down Expand Up @@ -228,7 +232,7 @@ export class AddCluster extends React.Component {
<Tab
value={KubeConfigSourceTab.FILE}
label={<Trans>Select kubeconfig file</Trans>}
active={this.sourceTab == KubeConfigSourceTab.FILE} />
active={this.sourceTab == KubeConfigSourceTab.FILE}/>
<Tab
value={KubeConfigSourceTab.TEXT}
label={<Trans>Paste as text</Trans>}
Expand Down Expand Up @@ -342,71 +346,55 @@ export class AddCluster extends React.Component {
return (
<div className={cssNames("kube-context flex gaps align-center", context)}>
<span>{context}</span>
{isNew && <Icon small material="fiber_new" />}
{isSelected && <Icon small material="check" className="box right" />}
{isNew && <Icon small material="fiber_new"/>}
{isSelected && <Icon small material="check" className="box right"/>}
</div>
);
};

render() {
const addDisabled = this.selectedContexts.length === 0;

return (
<WizardLayout
className="AddCluster"
infoPanel={this.renderInfo()}
contentClass={{ droppable: this.dropAreaActive }}
contentProps={{
onDragEnter: event => this.dropAreaActive = true,
onDragLeave: event => this.dropAreaActive = false,
onDragOver: event => {
event.preventDefault(); // enable onDrop()-callback
event.dataTransfer.dropEffect = "move";
},
onDrop: event => {
this.sourceTab = KubeConfigSourceTab.FILE;
this.dropAreaActive = false;
this.setKubeConfig(event.dataTransfer.files[0].path);
}
}}
>
<h2><Trans>Add Cluster</Trans></h2>
{this.renderKubeConfigSource()}
{this.renderContextSelector()}
<div className="cluster-settings">
<a href="#" onClick={() => this.showSettings = !this.showSettings}>
<Trans>Proxy settings</Trans>
</a>
</div>
{this.showSettings && (
<div className="proxy-settings">
<p>HTTP Proxy server. Used for communicating with Kubernetes API.</p>
<Input
autoFocus
value={this.proxyServer}
onChange={value => this.proxyServer = value}
theme="round-black"
<DropFileInput onDropFiles={this.onDropKubeConfig}>
<WizardLayout className="AddCluster" infoPanel={this.renderInfo()}>
<h2><Trans>Add Cluster</Trans></h2>
{this.renderKubeConfigSource()}
{this.renderContextSelector()}
<div className="cluster-settings">
<a href="#" onClick={() => this.showSettings = !this.showSettings}>
<Trans>Proxy settings</Trans>
</a>
</div>
{this.showSettings && (
<div className="proxy-settings">
<p>HTTP Proxy server. Used for communicating with Kubernetes API.</p>
<Input
autoFocus
value={this.proxyServer}
onChange={value => this.proxyServer = value}
theme="round-black"
/>
<small className="hint">
{'A HTTP proxy server URL (format: http://<address>:<port>).'}
</small>
</div>
)}
{this.error && (
<div className="error">{this.error}</div>
)}
<div className="actions-panel">
<Button
primary
disabled={addDisabled}
label={<Trans>Add cluster(s)</Trans>}
onClick={this.addClusters}
waiting={this.isWaiting}
tooltip={addDisabled ? _i18n._("Select at least one cluster to add.") : undefined}
tooltipOverrideDisabled
/>
<small className="hint">
{'A HTTP proxy server URL (format: http://<address>:<port>).'}
</small>
</div>
)}
{this.error && (
<div className="error">{this.error}</div>
)}
<div className="actions-panel">
<Button
primary
disabled={addDisabled}
label={<Trans>Add cluster(s)</Trans>}
onClick={this.addClusters}
waiting={this.isWaiting}
tooltip={addDisabled ? _i18n._("Select at least one cluster to add.") : undefined}
tooltipOverrideDisabled
/>
</div>
</WizardLayout>
</WizardLayout>
</DropFileInput>
);
}
}
Loading