add-cluster -- part 2

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2020-07-17 11:25:00 +03:00
1 parent a0831b3283
commit 33d3181113
11 files changed
+163 -165

No files matched your search

+42 -5
View File
@@ -1,11 +1,21 @@
import { ipcRenderer } from "electron";
import type { WorkspaceId } from "./workspace-store";
import path from "path";
import filenamify from "filenamify";
import { app, ipcRenderer } from "electron";
import { copyFile, ensureDir, unlink } from "fs-extra";
import { action, computed, observable, toJS } from "mobx";
import { v4 as uuid } from "uuid"
import { appProto } from "./vars";
import { BaseStore } from "./base-store";
import { Cluster, ClusterState } from "../main/cluster";
import migrations from "../migrations/cluster-store"
import logger from "../main/logger";
import { tracker } from "./tracker";
export interface ClusterIconUpload {
clusterId: string;
name: string;
path: string;
}
export interface ClusterStoreModel {
activeCluster?: ClusterId; // last opened cluster
@@ -42,6 +52,10 @@ export interface ClusterPreferences {
}
export class ClusterStore extends BaseStore<ClusterStoreModel> {
static get iconsDir() {
return path.join(app.getPath("userData"), "icons");
}
private constructor() {
super({
configName: "lens-cluster-store",
@@ -81,9 +95,8 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
@action
addCluster(model: ClusterModel): Cluster {
const id = model.id || uuid();
const cluster = new Cluster({ ...model, id })
this.clusters.set(id, cluster);
const cluster = new Cluster(model);
this.clusters.set(model.id, cluster);
return cluster;
}
@@ -102,6 +115,30 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
})
}
@action
protected async uploadClusterIcon({ clusterId, ...upload }: ClusterIconUpload): Promise<string> {
const cluster = this.getById(clusterId);
if (cluster) {
tracker.event("cluster", "upload-icon");
const fileDest = path.join(ClusterStore.iconsDir, filenamify(cluster.contextName + "-" + upload.name))
await ensureDir(path.dirname(fileDest));
await copyFile(upload.path, fileDest)
cluster.preferences.icon = `${appProto}:///icons/${fileDest}`
return cluster.preferences.icon;
}
}
@action
protected resetClusterIcon(clusterId: ClusterId) {
const cluster = this.getById(clusterId);
if (cluster) {
tracker.event("cluster", "reset-icon")
const iconPath = path.join(ClusterStore.iconsDir, path.basename(cluster.preferences.icon));
unlink(iconPath).catch(() => null); // remove file
delete cluster.preferences.icon;
}
}
@action
protected fromStore({ activeCluster, clusters = [] }: ClusterStoreModel = {}) {
const currentClusters = this.clusters.toJS();
-14
View File
@@ -1,14 +0,0 @@
// IPC messages (all channels)
// All values must be unique
export enum ClusterIpcMessage {
ADD = "cluster-add",
STOP = "cluster-stop",
REMOVE = "cluster-remove",
REMOVE_WORKSPACE = "cluster-remove-all-from-workspace",
FEATURE_INSTALL = "cluster-feature-install",
FEATURE_UPGRADE = "cluster-feature-upgrade",
FEATURE_REMOVE = "cluster-feature-remove",
ICON_SAVE = "cluster-icon-save",
ICON_RESET = "cluster-icon-reset",
}
+3 -2
View File
@@ -141,11 +141,12 @@ export function getNodeWarningConditions(node: V1Node) {
}
// Write kubeconfigs to "embedded" store, i.e. "/Users/ixrock/Library/Application Support/Lens/kubeconfigs"
export function saveConfigToAppFiles(clusterId: string, kubeConfig: string): string {
export function saveConfigToAppFiles(clusterId: string, kubeConfig: KubeConfig | string): string {
const userData = (app || remote.app).getPath("userData");
const kubeConfigFile = path.join(userData, `kubeconfigs/${clusterId}`)
const kubeConfigContents = typeof kubeConfig == "string" ? kubeConfig : dumpConfigYaml(kubeConfig);
ensureDirSync(path.dirname(kubeConfigFile));
writeFileSync(kubeConfigFile, kubeConfig);
writeFileSync(kubeConfigFile, kubeConfigContents);
return kubeConfigFile;
}