mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Factor out injectable for getting of cluster config data
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
3d4cc00dd7
commit
f2e2a40e53
@ -5,7 +5,7 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { ClusterStore } from "./cluster-store";
|
||||
import { createClusterInjectionToken } from "../cluster/create-cluster-injection-token";
|
||||
import readFileSyncInjectable from "../fs/read-file-sync.injectable";
|
||||
import readClusterConfigSyncInjectable from "./read-cluster-config.injectable";
|
||||
|
||||
const clusterStoreInjectable = getInjectable({
|
||||
id: "cluster-store",
|
||||
@ -15,7 +15,7 @@ const clusterStoreInjectable = getInjectable({
|
||||
|
||||
return ClusterStore.createInstance({
|
||||
createCluster: di.inject(createClusterInjectionToken),
|
||||
readFileSync: di.inject(readFileSyncInjectable),
|
||||
readClusterConfigSync: di.inject(readClusterConfigSyncInjectable),
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
@ -17,8 +17,7 @@ import type { ClusterModel, ClusterId, ClusterState } from "../cluster-types";
|
||||
import { requestInitialClusterStates } from "../../renderer/ipc";
|
||||
import { clusterStates } from "../ipc/cluster";
|
||||
import type { CreateCluster } from "../cluster/create-cluster-injection-token";
|
||||
import { loadConfigFromString, validateKubeConfig } from "../kube-helpers";
|
||||
import type { ReadFileSync } from "../fs/read-file-sync.injectable";
|
||||
import type { ReadClusterConfigSync } from "./read-cluster-config.injectable";
|
||||
|
||||
export interface ClusterStoreModel {
|
||||
clusters?: ClusterModel[];
|
||||
@ -26,7 +25,7 @@ export interface ClusterStoreModel {
|
||||
|
||||
interface Dependencies {
|
||||
createCluster: CreateCluster;
|
||||
readFileSync: ReadFileSync;
|
||||
readClusterConfigSync: ReadClusterConfigSync;
|
||||
}
|
||||
|
||||
export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
||||
@ -115,24 +114,15 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private createNewCluster(model: ClusterModel): Cluster {
|
||||
const kubeConfigData = this.dependencies.readFileSync(model.kubeConfigPath);
|
||||
const { config } = loadConfigFromString(kubeConfigData);
|
||||
const result = validateKubeConfig(config, model.contextName);
|
||||
|
||||
if (result.error) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return this.dependencies.createCluster(model, { clusterServerUrl: result.cluster.server });
|
||||
}
|
||||
|
||||
addCluster(clusterOrModel: ClusterModel | Cluster): Cluster {
|
||||
appEventBus.emit({ name: "cluster", action: "add" });
|
||||
|
||||
const cluster = clusterOrModel instanceof Cluster
|
||||
? clusterOrModel
|
||||
: this.createNewCluster(clusterOrModel);
|
||||
: this.dependencies.createCluster(
|
||||
clusterOrModel,
|
||||
this.dependencies.readClusterConfigSync(clusterOrModel),
|
||||
);
|
||||
|
||||
this.clusters.set(cluster.id, cluster);
|
||||
|
||||
@ -152,7 +142,10 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
||||
if (cluster) {
|
||||
cluster.updateModel(clusterModel);
|
||||
} else {
|
||||
cluster = this.createNewCluster(clusterModel);
|
||||
cluster = this.dependencies.createCluster(
|
||||
clusterModel,
|
||||
this.dependencies.readClusterConfigSync(clusterModel),
|
||||
);
|
||||
}
|
||||
newClusters.set(clusterModel.id, cluster);
|
||||
} catch (error) {
|
||||
|
||||
31
src/common/cluster-store/read-cluster-config.injectable.ts
Normal file
31
src/common/cluster-store/read-cluster-config.injectable.ts
Normal file
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { ClusterConfigData, ClusterModel } from "../cluster-types";
|
||||
import readFileSyncInjectable from "../fs/read-file-sync.injectable";
|
||||
import { loadConfigFromString, validateKubeConfig } from "../kube-helpers";
|
||||
|
||||
export type ReadClusterConfigSync = (model: ClusterModel) => ClusterConfigData;
|
||||
|
||||
const readClusterConfigSyncInjectable = getInjectable({
|
||||
id: "read-cluster-config-sync",
|
||||
instantiate: (di): ReadClusterConfigSync => {
|
||||
const readFileSync = di.inject(readFileSyncInjectable);
|
||||
|
||||
return ({ kubeConfigPath, contextName }) => {
|
||||
const kubeConfigData = readFileSync(kubeConfigPath);
|
||||
const { config } = loadConfigFromString(kubeConfigData);
|
||||
const result = validateKubeConfig(config, contextName);
|
||||
|
||||
if (result.error) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
return { clusterServerUrl: result.cluster.server };
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default readClusterConfigSyncInjectable;
|
||||
Loading…
Reference in New Issue
Block a user