Skip to content

Commit

Permalink
Make PrometheusProviderRegistry fully injectable (#6592)
Browse files Browse the repository at this point in the history
* Stop using source code in build file

Signed-off-by: Sebastian Malton <[email protected]>

* Add new injectable version of binaryName

Signed-off-by: Sebastian Malton <[email protected]>

* Add new NormalizedPlatform type

Signed-off-by: Sebastian Malton <[email protected]>

* Switch legacy execHelm to use legacy global DI for binaryPath

Signed-off-by: Sebastian Malton <[email protected]>

* Remove dead code

Signed-off-by: Sebastian Malton <[email protected]>

* Introduce injectable for kube auth proxy certs

Signed-off-by: Sebastian Malton <[email protected]>

* Introduce injectable forms of PrometheusProviders

- Remove class requirement
- Make everything injectable

Signed-off-by: Sebastian Malton <[email protected]>

* Update tests to not use private functions

Signed-off-by: Sebastian Malton <[email protected]>

* Cleanup creating binary names and paths

Signed-off-by: Sebastian Malton <[email protected]>

Signed-off-by: Sebastian Malton <[email protected]>
  • Loading branch information
Nokel81 authored Nov 25, 2022
1 parent 30bfd6f commit 286e6c8
Show file tree
Hide file tree
Showing 32 changed files with 600 additions and 647 deletions.
10 changes: 9 additions & 1 deletion build/download_binaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ import type { SingleBar } from "cli-progress";
import { MultiBar } from "cli-progress";
import { extract } from "tar-stream";
import gunzip from "gunzip-maybe";
import { getBinaryName } from "../src/common/vars";
import { isErrnoException, setTimeoutFor } from "../src/common/utils";
import AbortController from "abort-controller";

type Response = FetchModule.Response;
type RequestInfo = FetchModule.RequestInfo;
type RequestInit = FetchModule.RequestInit;

const pipeline = promisify(_pipeline);

const getBinaryName = (binaryName: string, { forPlatform }: { forPlatform : string }) => {
if (forPlatform === "windows") {
return `${binaryName}.exe`;
}

return binaryName;
};

interface BinaryDownloaderArgs {
readonly version: string;
readonly platform: SupportedPlatform;
Expand Down
24 changes: 24 additions & 0 deletions src/common/utils/binary-name.injectable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import normalizedPlatformInjectable from "../vars/normalized-platform.injectable";

const binaryNameInjectable = getInjectable({
id: "binary-name",
instantiate: (di, binaryName) => {
const normalizedPlatform = di.inject(normalizedPlatformInjectable);

if (normalizedPlatform === "windows") {
return `${binaryName}.exe`;
}

return binaryName;
},
lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di, binaryName: string) => binaryName,
}),
});

export default binaryNameInjectable;
24 changes: 24 additions & 0 deletions src/common/utils/bundled-binary-path.injectable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import joinPathsInjectable from "../path/join-paths.injectable";
import baseBundledBinariesDirectoryInjectable from "../vars/base-bundled-binaries-dir.injectable";
import binaryNameInjectable from "./binary-name.injectable";

const bundledBinaryPathInjectable = getInjectable({
id: "bundled-binary-path",
instantiate: (di, name) => {
const joinPaths = di.inject(joinPathsInjectable);
const binaryName = di.inject(binaryNameInjectable, name);
const baseBundledBinariesDirectory = di.inject(baseBundledBinariesDirectoryInjectable);

return joinPaths(baseBundledBinariesDirectory, binaryName);
},
lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di, binaryName: string) => binaryName,
}),
});

export default bundledBinaryPathInjectable;
36 changes: 0 additions & 36 deletions src/common/utils/lazy-initialized.ts

This file was deleted.

69 changes: 0 additions & 69 deletions src/common/vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
*/

// App's common configuration for any process (main, renderer, build pipeline, etc.)
import path from "path";
import type { ThemeId } from "../renderer/themes/store";
import { lazyInitialized } from "./utils/lazy-initialized";

/**
* @deprecated Switch to using isMacInjectable
Expand Down Expand Up @@ -48,73 +46,6 @@ export const defaultThemeId: ThemeId = "lens-dark";
export const defaultFontSize = 12;
export const defaultTerminalFontFamily = "RobotoMono";
export const defaultEditorFontFamily = "RobotoMono";
/**
* @deprecated use `di.inject(normalizedPlatformInjectable)` instead
*/
export const normalizedPlatform = (() => {
switch (process.platform) {
case "darwin":
return "darwin";
case "linux":
return "linux";
case "win32":
return "windows";
default:
throw new Error(`platform=${process.platform} is unsupported`);
}
})();
/**
* @deprecated use `di.inject(bundledBinariesNormalizedArchInjectable)` instead
*/
export const normalizedArch = (() => {
switch (process.arch) {
case "arm64":
return "arm64";
case "x64":
case "amd64":
return "x64";
case "386":
case "x32":
case "ia32":
return "ia32";
default:
throw new Error(`arch=${process.arch} is unsupported`);
}
})();

export function getBinaryName(name: string, { forPlatform = normalizedPlatform } = {}): string {
if (forPlatform === "windows") {
return `${name}.exe`;
}

return name;
}

const resourcesDir = lazyInitialized(() => (
isProduction
? process.resourcesPath
: path.join(process.cwd(), "binaries", "client", normalizedPlatform)
));

/**
* @deprecated for being explicit side effect.
*/
export const baseBinariesDir = lazyInitialized(() => path.join(resourcesDir.get(), normalizedArch));

/**
* @deprecated for being explicit side effect.
*/
export const kubeAuthProxyBinaryName = getBinaryName("lens-k8s-proxy");

/**
* @deprecated for being explicit side effect.
*/
export const helmBinaryName = getBinaryName("helm");

/**
* @deprecated for being explicit side effect.
*/
export const helmBinaryPath = lazyInitialized(() => path.join(baseBinariesDir.get(), helmBinaryName));

// Apis
export const apiPrefix = "/api"; // local router apis
Expand Down
4 changes: 3 additions & 1 deletion src/common/vars/normalized-platform.injectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import { getInjectable } from "@ogre-tools/injectable";
import platformInjectable from "./platform.injectable";

export type NormalizedPlatform = "darwin" | "linux" | "windows";

const normalizedPlatformInjectable = getInjectable({
id: "normalized-platform",

instantiate: (di) => {
instantiate: (di): NormalizedPlatform => {
const platform = di.inject(platformInjectable);

switch (platform) {
Expand Down
Loading

0 comments on commit 286e6c8

Please sign in to comment.