mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
/**
|
|
* 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 lensProxyPortNumberStateInjectable from "./lens-proxy-port-number-state.injectable";
|
|
|
|
export type K8sRequest = (cluster: Cluster, path: string, options?: RequestPromiseOptions) => Promise<any>;
|
|
|
|
const k8SRequestInjectable = getInjectable({
|
|
id: "k8s-request",
|
|
|
|
instantiate: (di) => {
|
|
const lensProxyPortNumberState = di.inject(lensProxyPortNumberStateInjectable);
|
|
|
|
return async (
|
|
cluster: Cluster,
|
|
path: string,
|
|
options: RequestPromiseOptions = {},
|
|
) => {
|
|
const kubeProxyUrl = `http://localhost:${lensProxyPortNumberState.get()}${apiKubePrefix}`;
|
|
|
|
options.headers ??= {};
|
|
options.json ??= true;
|
|
options.timeout ??= 30000;
|
|
options.headers.Host = `${cluster.id}.${new URL(kubeProxyUrl).host}`; // required in ClusterManager.getClusterForRequest()
|
|
|
|
return request(kubeProxyUrl + path, options);
|
|
};
|
|
},
|
|
});
|
|
|
|
export default k8SRequestInjectable;
|