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

Remove last uses of loadConfigFromFile

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-07-07 14:37:59 -04:00
parent f0f06f53d2
commit 7e4de0a74e
3 changed files with 15 additions and 13 deletions

View File

@ -10,7 +10,7 @@ import type { KubeConfig } from "@kubernetes/client-node";
import { HttpError } from "@kubernetes/client-node"; import { HttpError } from "@kubernetes/client-node";
import type { Kubectl } from "../../main/kubectl/kubectl"; import type { Kubectl } from "../../main/kubectl/kubectl";
import type { KubeconfigManager } from "../../main/kubeconfig-manager/kubeconfig-manager"; import type { KubeconfigManager } from "../../main/kubeconfig-manager/kubeconfig-manager";
import { loadConfigFromFile } from "../kube-helpers"; import { loadConfigFromString } from "../kube-helpers";
import type { KubeApiResource, KubeResource } from "../rbac"; import type { KubeApiResource, KubeResource } from "../rbac";
import { apiResourceRecord, apiResources } from "../rbac"; import { apiResourceRecord, apiResources } from "../rbac";
import type { VersionDetector } from "../../main/cluster-detectors/version-detector"; import type { VersionDetector } from "../../main/cluster-detectors/version-detector";
@ -25,6 +25,7 @@ import type { CanI } from "./authorization-review.injectable";
import type { ListNamespaces } from "./list-namespaces.injectable"; import type { ListNamespaces } from "./list-namespaces.injectable";
import assert from "assert"; import assert from "assert";
import type { Logger } from "../logger"; import type { Logger } from "../logger";
import type { ReadFile } from "../fs/read-file.injectable";
export interface ClusterDependencies { export interface ClusterDependencies {
readonly directoryForKubeConfigs: string; readonly directoryForKubeConfigs: string;
@ -36,6 +37,7 @@ export interface ClusterDependencies {
createAuthorizationReview: (config: KubeConfig) => CanI; createAuthorizationReview: (config: KubeConfig) => CanI;
createListNamespaces: (config: KubeConfig) => ListNamespaces; createListNamespaces: (config: KubeConfig) => ListNamespaces;
createVersionDetector: (cluster: Cluster) => VersionDetector; createVersionDetector: (cluster: Cluster) => VersionDetector;
readFile: ReadFile;
} }
/** /**
@ -486,7 +488,8 @@ export class Cluster implements ClusterModel, ClusterState {
} }
async getKubeconfig(): Promise<KubeConfig> { async getKubeconfig(): Promise<KubeConfig> {
const { config } = await loadConfigFromFile(this.kubeConfigPath); const configData = await this.dependencies.readFile(this.kubeConfigPath);
const { config } = loadConfigFromString(configData);
return config; return config;
} }
@ -496,7 +499,8 @@ export class Cluster implements ClusterModel, ClusterState {
*/ */
async getProxyKubeconfig(): Promise<KubeConfig> { async getProxyKubeconfig(): Promise<KubeConfig> {
const proxyKCPath = await this.getProxyKubeconfigPath(); const proxyKCPath = await this.getProxyKubeconfigPath();
const { config } = await loadConfigFromFile(proxyKCPath); const configData = await this.dependencies.readFile(proxyKCPath);
const { config } = loadConfigFromString(configData);
return config; return config;
} }

View File

@ -5,11 +5,16 @@
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import fsInjectable from "./fs.injectable"; import fsInjectable from "./fs.injectable";
export type ReadFile = (filePath: string) => Promise<string>;
const readFileInjectable = getInjectable({ const readFileInjectable = getInjectable({
id: "read-file", id: "read-file",
instantiate: (di) => (filePath: string) => instantiate: (di): ReadFile => {
di.inject(fsInjectable).readFile(filePath, "utf-8"), const { readFile } = di.inject(fsInjectable);
return (filePath) => readFile(filePath, "utf-8");
},
}); });
export default readFileInjectable; export default readFileInjectable;

View File

@ -4,25 +4,18 @@
*/ */
import { KubeConfig } from "@kubernetes/client-node"; import { KubeConfig } from "@kubernetes/client-node";
import fse from "fs-extra";
import path from "path"; import path from "path";
import os from "os"; import os from "os";
import yaml from "js-yaml"; import yaml from "js-yaml";
import logger from "../main/logger"; import logger from "../main/logger";
import type { Cluster, Context, User } from "@kubernetes/client-node/dist/config_types"; import type { Cluster, Context, User } from "@kubernetes/client-node/dist/config_types";
import { newClusters, newContexts, newUsers } from "@kubernetes/client-node/dist/config_types"; import { newClusters, newContexts, newUsers } from "@kubernetes/client-node/dist/config_types";
import { isDefined, resolvePath } from "./utils"; import { isDefined } from "./utils";
import Joi from "joi"; import Joi from "joi";
import type { PartialDeep } from "type-fest"; import type { PartialDeep } from "type-fest";
export const kubeConfigDefaultPath = path.join(os.homedir(), ".kube", "config"); export const kubeConfigDefaultPath = path.join(os.homedir(), ".kube", "config");
export async function loadConfigFromFile(filePath: string): Promise<ConfigResult> {
const content = await fse.readFile(resolvePath(filePath), "utf-8");
return loadConfigFromString(content);
}
const clusterSchema = Joi.object({ const clusterSchema = Joi.object({
name: Joi name: Joi
.string() .string()