1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Replace usage of request in k8sRequest and cluster detectors

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-03 16:01:28 -04:00
parent f522d23662
commit 37955f03e2
6 changed files with 14 additions and 29 deletions

View File

@ -3,7 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { RequestPromiseOptions } from "request-promise-native";
import type { RequestInit } from "node-fetch";
import type { Cluster } from "../../common/cluster/cluster";
import type { K8sRequest } from "../k8s-request.injectable";
@ -19,7 +19,7 @@ export abstract class BaseClusterDetector {
abstract detect(): Promise<ClusterDetectionResult | null>;
protected async k8sRequest<T = any>(path: string, options: RequestPromiseOptions = {}): Promise<T> {
protected async k8sRequest(path: string, options?: RequestInit): Promise<unknown> {
return this._k8sRequest(this.cluster, path, options);
}
}

View File

@ -24,7 +24,7 @@ export class ClusterIdDetector extends BaseClusterDetector {
}
protected async getDefaultNamespaceId() {
const response = await this.k8sRequest("/api/v1/namespaces/default");
const response = await this.k8sRequest("/api/v1/namespaces/default") as { metadata: { uid: string }};
return response.metadata.uid;
}

View File

@ -96,7 +96,7 @@ export class DistributionDetector extends BaseClusterDetector {
}
public async getKubernetesVersion() {
const response = await this.k8sRequest("/version");
const response = await this.k8sRequest("/version") as { gitVersion: string };
return response.gitVersion;
}
@ -179,7 +179,7 @@ export class DistributionDetector extends BaseClusterDetector {
protected async isOpenshift() {
try {
const response = await this.k8sRequest("");
const response = await this.k8sRequest("") as { paths?: string[] };
return response.paths?.includes("/apis/project.openshift.io");
} catch (e) {

View File

@ -17,7 +17,7 @@ export class NodesCountDetector extends BaseClusterDetector {
}
protected async getNodeCount(): Promise<number> {
const response = await this.k8sRequest("/api/v1/nodes");
const response = await this.k8sRequest("/api/v1/nodes") as { items: unknown[] };
return response.items.length;
}

View File

@ -16,7 +16,7 @@ export class VersionDetector extends BaseClusterDetector {
}
public async getKubernetesVersion() {
const response = await this.k8sRequest("/version");
const response = await this.k8sRequest("/version") as { gitVersion: string };
return response.gitVersion;
}

View File

@ -2,36 +2,21 @@
* 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 { apiKubePrefix } from "../common/vars";
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 { RequestInit } from "node-fetch";
import lensFetchInjectable from "../common/fetch/lens-fetch.injectable";
export type K8sRequest = (cluster: Cluster, path: string, options?: RequestPromiseOptions) => Promise<any>;
export type K8sRequest = (cluster: Cluster, path: string, options?: RequestInit) => 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 = {},
) => {
const kubeProxyUrl = `https://127.0.0.1:${lensProxyPort.get()}/${cluster.id}`;
options.ca = lensProxyCertificate.get().cert;
options.headers ??= {};
options.json ??= true;
options.timeout ??= 30000;
return request(kubeProxyUrl + path, options);
};
return async (cluster, path, init) => lensFetch(`/${cluster.id}${apiKubePrefix}${path}`, init);
},
});