cluster-store refactoring -- part 1

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2020-07-06 14:31:34 +03:00
1 parent d4bbaf4f34
commit afb54694f4
14 files changed
+168 -461

No files matched your search

+15 -10
View File
@@ -1,8 +1,8 @@
import { KubeConfig } from "@kubernetes/client-node"
import { PromiseIpc } from "electron-promise-ipc"
import http from "http"
import { Cluster, ClusterBaseInfo } from "./cluster"
import { clusterStore } from "../common/cluster-store"
import { Cluster } from "./cluster"
import { ClusterModel, clusterStore } from "../common/cluster-store"
import * as k8s from "./k8s"
import logger from "./logger"
import { LensProxy } from "./proxy"
@@ -14,6 +14,8 @@ import filenamify from "filenamify"
import { v4 as uuid } from "uuid"
import { apiPrefix } from "../common/vars";
// todo: refactor + reuse parts of cluster-store more heavily
export type FeatureInstallRequest = {
name: string;
clusterId: string;
@@ -32,7 +34,10 @@ export type ClusterIconUpload = {
}
export class ClusterManager {
public static readonly clusterIconDir = path.join(app.getPath("userData"), "icons")
static get clusterIconDir(){
return path.join(app.getPath("userData"), "icons")
}
protected promiseIpc: any
protected proxyServer: LensProxy
protected port: number
@@ -83,7 +88,7 @@ export class ClusterManager {
return kc;
}
protected async addNewCluster(clusterData: ClusterBaseInfo): Promise<Cluster> {
protected async addNewCluster(clusterData: ClusterModel): Promise<Cluster> {
return new Promise(async (resolve, reject) => {
try {
const kc = this.loadKubeConfig(clusterData.kubeConfigPath)
@@ -110,7 +115,7 @@ export class ClusterManager {
}
protected listenEvents() {
this.promiseIpc.on("addCluster", async (clusterData: ClusterBaseInfo) => {
this.promiseIpc.on("addCluster", async (clusterData: ClusterModel) => {
logger.debug(`IPC: addCluster`)
const cluster = await this.addNewCluster(clusterData)
return {
@@ -174,10 +179,10 @@ export class ClusterManager {
}
try {
const clusterIcon = await this.uploadClusterIcon(cluster, fileUpload.name, fileUpload.path)
clusterStore.reloadCluster(cluster);
// clusterStore.reloadCluster(cluster);
if(!cluster.preferences) cluster.preferences = {};
cluster.preferences.icon = clusterIcon
clusterStore.saveCluster(cluster);
// clusterStore.saveCluster(cluster);
return {success: true, cluster: cluster.toClusterInfo(), message: ""}
} catch(error) {
return {success: false, message: error}
@@ -189,7 +194,7 @@ export class ClusterManager {
const cluster = this.getCluster(id)
if (cluster && cluster.preferences) {
cluster.preferences.icon = null;
clusterStore.saveCluster(cluster)
// clusterStore.saveCluster(cluster)
return {success: true, cluster: cluster.toClusterInfo(), message: ""}
} else {
return {success: false, message: "Cluster not found"}
@@ -221,7 +226,7 @@ export class ClusterManager {
logger.debug(`IPC: clusterStored: ${clusterId}`)
const cluster = this.clusters.get(clusterId)
if (cluster) {
clusterStore.reloadCluster(cluster);
// clusterStore.reloadCluster(cluster);
cluster.stopServer()
}
});
@@ -244,7 +249,7 @@ export class ClusterManager {
const cluster = this.clusters.get(id)
if (cluster) {
cluster.stopServer()
clusterStore.removeCluster(cluster.id);
clusterStore.removeById(cluster.id);
this.clusters.delete(cluster.id)
}
return Array.from(this.clusters.values())
+8 -48
View File
@@ -1,7 +1,7 @@
import { ContextHandler } from "./context-handler"
import { FeatureStatusMap } from "./feature"
import * as k8s from "./k8s"
import { clusterStore } from "../common/cluster-store"
import { ClusterId, ClusterModel, ClusterPreferences } from "../common/cluster-store"
import logger from "./logger"
import { AuthorizationV1Api, CoreV1Api, KubeConfig, V1ResourceAttributes } from "@kubernetes/client-node"
import * as fm from "./feature-manager";
@@ -10,6 +10,7 @@ import { KubeconfigManager } from "./kubeconfig-manager"
import { PromiseIpc } from "electron-promise-ipc"
import request from "request-promise-native"
import { apiPrefix } from "../common/vars";
import type { ClusterInfo } from "../renderer/_vue/store/modules/clusters";
enum ClusterStatus {
AccessGranted = 2,
@@ -17,49 +18,8 @@ enum ClusterStatus {
Offline = 0
}
export interface ClusterBaseInfo {
id: string;
kubeConfig?: string;
kubeConfigPath: string;
contextName: string;
preferences?: ClusterPreferences;
port?: number;
workspace?: string;
}
export interface ClusterInfo extends ClusterBaseInfo {
url: string;
apiUrl: string;
online?: boolean;
accessible?: boolean;
failureReason?: string;
nodes?: number;
version?: string;
distribution?: string;
isAdmin?: boolean;
features?: FeatureStatusMap;
kubeCtl?: Kubectl;
contextName: string;
}
export type ClusterPreferences = {
terminalCWD?: string;
clusterName?: string;
prometheus?: {
namespace: string;
service: string;
port: number;
prefix: string;
};
prometheusProvider?: {
type: string;
};
icon?: string;
httpsProxy?: string;
}
export class Cluster implements ClusterInfo {
public id: string;
export class Cluster implements ClusterModel {
public id: ClusterId;
public workspace: string;
public contextHandler: ContextHandler;
public contextName: string;
@@ -84,8 +44,8 @@ export class Cluster implements ClusterInfo {
protected kubeconfigManager: KubeconfigManager;
constructor(clusterInfo: ClusterBaseInfo) {
if (clusterInfo) Object.assign(this, clusterInfo)
constructor(jsonModel: ClusterModel) {
if (jsonModel) Object.assign(this, jsonModel)
if (!this.preferences) this.preferences = {}
}
@@ -129,7 +89,7 @@ export class Cluster implements ClusterInfo {
}
public async refreshCluster() {
clusterStore.reloadCluster(this)
// clusterStore.reloadCluster(this)
this.contextHandler.setClusterPreferences(this.preferences)
const connectionStatus = await this.getConnectionStatus()
@@ -155,7 +115,7 @@ export class Cluster implements ClusterInfo {
}
public save() {
clusterStore.saveCluster(this)
// clusterStore.saveCluster(this)
}
public toClusterInfo(): ClusterInfo {
+3 -2
View File
@@ -5,9 +5,10 @@ import * as url from "url"
import logger from "./logger"
import { getFreePort } from "./port"
import { KubeAuthProxy } from "./kube-auth-proxy"
import { Cluster, ClusterPreferences } from "./cluster"
import { Cluster } from "./cluster"
import { prometheusProviders } from "../common/prometheus-providers"
import { PrometheusService, PrometheusProvider } from "./prometheus/provider-registry"
import type { PrometheusService, PrometheusProvider } from "./prometheus/provider-registry"
import type { ClusterPreferences } from "../common/cluster-store";
export class ContextHandler {
public contextName: string
+18 -10
View File
@@ -1,15 +1,9 @@
// Main process
import "../common/system-ca"
import { app, dialog, protocol } from "electron"
import { isMac, vueAppName, isDevelopment } from "../common/vars";
if (isDevelopment) {
const appName = 'LensDev';
const appData = app.getPath('appData');
app.setName(appName);
app.setPath('userData', path.join(appData, appName));
}
import "../common/prometheus-providers"
import { app, dialog, protocol } from "electron"
import { isDevelopment, isMac, vueAppName } from "../common/vars";
import { PromiseIpc } from "electron-promise-ipc"
import path from "path"
import { format as formatUrl } from "url"
@@ -17,7 +11,6 @@ import logger from "./logger"
import initMenu from "./menu"
import * as proxy from "./proxy"
import { WindowManager } from "./window-manager";
import { clusterStore } from "../common/cluster-store"
import { ClusterManager } from "./cluster-manager";
import AppUpdater from "./app-updater"
import { shellSync } from "./shell-sync"
@@ -25,8 +18,17 @@ import { getFreePort } from "./port"
import { mangleProxyEnv } from "./proxy-env"
import { findMainWebContents } from "./webcontents"
import { registerStaticProtocol } from "../common/register-static";
import { clusterStore } from "../common/cluster-store"
import { userStore } from "../common/user-store";
import { tracker } from "../common/tracker";
if (isDevelopment) {
const appName = "LensDev";
const appData = app.getPath("appData");
app.setName(appName);
app.setPath("userData", path.join(appData, appName));
}
mangleProxyEnv()
if (app.commandLine.getSwitchValue("proxy-server") !== "") {
process.env.HTTPS_PROXY = app.commandLine.getSwitchValue("proxy-server")
@@ -69,8 +71,14 @@ async function main() {
app.quit();
}
// preload required stores
await Promise.all([
userStore.load(),
clusterStore.load(),
]);
// create cluster manager
clusterManager = new ClusterManager(clusterStore.getAllClusterObjects(), port)
clusterManager = new ClusterManager(clusterStore.clusters, port)
// run proxy
try {
proxyServer = proxy.listen(port, clusterManager)
+4 -2
View File
@@ -46,14 +46,16 @@ if(isDevelopment) {
if(process.platform === "win32") bundledPath = `${bundledPath}.exe`
export class Kubectl {
public kubectlVersion: string
protected directory: string
protected url: string
protected path: string
protected dirname: string
public static readonly kubectlDir = path.join((app || remote.app).getPath("userData"), "binaries", "kubectl")
static get kubectlDir(){
return path.join((app || remote.app).getPath("userData"), "binaries", "kubectl")
}
public static readonly bundledKubectlPath = bundledPath
public static readonly bundledKubectlVersion: string = bundledVersion
private static bundledInstance: Kubectl;