diff --git a/src/main/cluster-detectors/base-cluster-detector.ts b/src/main/cluster-detectors/base-cluster-detector.ts index 1aca321dfe..2ff77f26cb 100644 --- a/src/main/cluster-detectors/base-cluster-detector.ts +++ b/src/main/cluster-detectors/base-cluster-detector.ts @@ -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; - protected async k8sRequest(path: string, options: RequestPromiseOptions = {}): Promise { + protected async k8sRequest(path: string, options?: RequestInit): Promise { return this._k8sRequest(this.cluster, path, options); } } diff --git a/src/main/cluster-detectors/cluster-id-detector.ts b/src/main/cluster-detectors/cluster-id-detector.ts index 8f1486fcb1..ac744e5174 100644 --- a/src/main/cluster-detectors/cluster-id-detector.ts +++ b/src/main/cluster-detectors/cluster-id-detector.ts @@ -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; } diff --git a/src/main/cluster-detectors/distribution-detector.ts b/src/main/cluster-detectors/distribution-detector.ts index dcdacfb253..d11f71c563 100644 --- a/src/main/cluster-detectors/distribution-detector.ts +++ b/src/main/cluster-detectors/distribution-detector.ts @@ -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) { diff --git a/src/main/cluster-detectors/nodes-count-detector.ts b/src/main/cluster-detectors/nodes-count-detector.ts index f9b3c02dbf..4fbf05e40a 100644 --- a/src/main/cluster-detectors/nodes-count-detector.ts +++ b/src/main/cluster-detectors/nodes-count-detector.ts @@ -17,7 +17,7 @@ export class NodesCountDetector extends BaseClusterDetector { } protected async getNodeCount(): Promise { - const response = await this.k8sRequest("/api/v1/nodes"); + const response = await this.k8sRequest("/api/v1/nodes") as { items: unknown[] }; return response.items.length; } diff --git a/src/main/cluster-detectors/version-detector.ts b/src/main/cluster-detectors/version-detector.ts index cc228734c5..6c836b851a 100644 --- a/src/main/cluster-detectors/version-detector.ts +++ b/src/main/cluster-detectors/version-detector.ts @@ -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; } diff --git a/src/main/k8s-request.injectable.ts b/src/main/k8s-request.injectable.ts index c00e70ec3f..898031850f 100644 --- a/src/main/k8s-request.injectable.ts +++ b/src/main/k8s-request.injectable.ts @@ -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; +export type K8sRequest = (cluster: Cluster, path: string, options?: RequestInit) => Promise; 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); }, });