mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Remove last usages of request in our code (#6911)
* Improve the injectability of cluster metadata detection - Remove unnecessary and complex base class Signed-off-by: Sebastian Malton <sebastian@malton.name> * Remove dead code Signed-off-by: Sebastian Malton <sebastian@malton.name> * Remove dead code Signed-off-by: Sebastian Malton <sebastian@malton.name> * Remove last usages of request in our code Signed-off-by: Sebastian Malton <sebastian@malton.name> * Remove more deps Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix lensFetch Signed-off-by: Sebastian Malton <sebastian@malton.name> Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
8e65a0acd6
commit
305c4a5573
7 files changed
+98
-66
No files matched your search
@@ -4,16 +4,19 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { Cluster } from "../common/cluster/cluster";
|
||||
import nodeFetchModuleInjectable from "../common/fetch/fetch-module.injectable";
|
||||
import type { RequestMetricsParams } from "../common/k8s-api/endpoints/metrics.api/request-metrics.injectable";
|
||||
import { object } from "../common/utils";
|
||||
import k8sRequestInjectable from "./k8s-request.injectable";
|
||||
|
||||
export type GetMetrics = (cluster: Cluster, prometheusPath: string, queryParams: RequestMetricsParams & { query: string }) => Promise<any>;
|
||||
export type GetMetrics = (cluster: Cluster, prometheusPath: string, queryParams: RequestMetricsParams & { query: string }) => Promise<unknown>;
|
||||
|
||||
const getMetricsInjectable = getInjectable({
|
||||
id: "get-metrics",
|
||||
|
||||
instantiate: (di): GetMetrics => {
|
||||
const k8sRequest = di.inject(k8sRequestInjectable);
|
||||
const { FormData } = di.inject(nodeFetchModuleInjectable);
|
||||
|
||||
return async (
|
||||
cluster,
|
||||
@@ -22,13 +25,16 @@ const getMetricsInjectable = getInjectable({
|
||||
) => {
|
||||
const prometheusPrefix = cluster.preferences.prometheus?.prefix || "";
|
||||
const metricsPath = `/api/v1/namespaces/${prometheusPath}/proxy${prometheusPrefix}/api/v1/query_range`;
|
||||
const body = new FormData();
|
||||
|
||||
for (const [key, value] of object.entries(queryParams)) {
|
||||
body.set(key, value.toString());
|
||||
}
|
||||
|
||||
return k8sRequest(cluster, metricsPath, {
|
||||
timeout: 0,
|
||||
resolveWithFullResponse: false,
|
||||
json: true,
|
||||
method: "POST",
|
||||
form: queryParams,
|
||||
body,
|
||||
});
|
||||
};
|
||||
},
|
||||
|
||||
@@ -2,35 +2,49 @@
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import type { RequestPromiseOptions } from "request-promise-native";
|
||||
import request from "request-promise-native";
|
||||
import type { Cluster } from "../common/cluster/cluster";
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import lensProxyPortInjectable from "./lens-proxy/lens-proxy-port.injectable";
|
||||
import lensProxyCertificateInjectable from "../common/certificate/lens-proxy-certificate.injectable";
|
||||
import type { LensRequestInit } from "../common/fetch/lens-fetch.injectable";
|
||||
import lensFetchInjectable from "../common/fetch/lens-fetch.injectable";
|
||||
import { withTimeout } from "../common/fetch/timeout-controller";
|
||||
|
||||
export type K8sRequest = (cluster: Cluster, path: string, options?: RequestPromiseOptions) => Promise<any>;
|
||||
export interface K8sRequestInit extends LensRequestInit {
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export type K8sRequest = (cluster: Cluster, pathnameAndQuery: string, init?: K8sRequestInit) => Promise<unknown>;
|
||||
|
||||
const k8sRequestInjectable = getInjectable({
|
||||
id: "k8s-request",
|
||||
|
||||
instantiate: (di) => {
|
||||
const lensProxyPort = di.inject(lensProxyPortInjectable);
|
||||
const lensProxyCertificate = di.inject(lensProxyCertificateInjectable);
|
||||
instantiate: (di): K8sRequest => {
|
||||
const lensFetch = di.inject(lensFetchInjectable);
|
||||
|
||||
return async (
|
||||
cluster: Cluster,
|
||||
path: string,
|
||||
options: RequestPromiseOptions = {},
|
||||
cluster,
|
||||
pathnameAndQuery,
|
||||
{
|
||||
timeout = 30_000,
|
||||
signal,
|
||||
...init
|
||||
} = {},
|
||||
) => {
|
||||
const kubeProxyUrl = `https://127.0.0.1:${lensProxyPort.get()}/${cluster.id}`;
|
||||
const controller = timeout ? withTimeout(timeout) : undefined;
|
||||
|
||||
options.ca = lensProxyCertificate.get().cert;
|
||||
options.headers ??= {};
|
||||
options.json ??= true;
|
||||
options.timeout ??= 30000;
|
||||
if (controller) {
|
||||
signal?.addEventListener("abort", () => controller.abort());
|
||||
}
|
||||
|
||||
return request(kubeProxyUrl + path, options);
|
||||
const response = await lensFetch(`/${cluster.id}${pathnameAndQuery}`, {
|
||||
...init,
|
||||
signal: controller?.signal ?? signal,
|
||||
});
|
||||
|
||||
if (response.status < 200 || response.status >= 300) {
|
||||
throw new Error(`Failed to ${init.method ?? "get"} ${pathnameAndQuery} for clusterId=${cluster.id}: ${response.statusText}`, { cause: response });
|
||||
}
|
||||
|
||||
return response.json();
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user