mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Lens app source code (#119)
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
1 parent
936cbd53d4
commit
1d0815abd2
797 files changed
+73714
-215
No files matched your search
@@ -0,0 +1,19 @@
|
||||
import { autoUpdater } from "electron-updater"
|
||||
import logger from "./logger"
|
||||
|
||||
export default class AppUpdater {
|
||||
|
||||
protected updateInterval: number = (1000 * 60 * 60 * 24) // once a day
|
||||
|
||||
constructor() {
|
||||
autoUpdater.logger = logger
|
||||
}
|
||||
|
||||
public start() {
|
||||
setInterval(() => {
|
||||
autoUpdater.checkForUpdatesAndNotify()
|
||||
}, this.updateInterval)
|
||||
|
||||
return autoUpdater.checkForUpdatesAndNotify()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
import { KubeConfig } from "@kubernetes/client-node"
|
||||
import { PromiseIpc } from "electron-promise-ipc"
|
||||
import * as http from "http"
|
||||
import { Cluster, ClusterBaseInfo } from "./cluster"
|
||||
import { clusterStore } from "../common/cluster-store"
|
||||
import * as k8s from "./k8s"
|
||||
import logger from "./logger"
|
||||
import { LensProxy } from "./proxy"
|
||||
import { app } from "electron"
|
||||
import * as path from "path"
|
||||
import { promises } from "fs"
|
||||
import { ensureDir } from "fs-extra"
|
||||
import * as filenamify from "filenamify"
|
||||
import { v4 as uuid } from "uuid"
|
||||
|
||||
declare const __static: string;
|
||||
|
||||
export type FeatureInstallRequest = {
|
||||
name: string;
|
||||
clusterId: string;
|
||||
config: any;
|
||||
}
|
||||
|
||||
export type FeatureInstallResponse = {
|
||||
success: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export type ClusterIconUpload = {
|
||||
path: string;
|
||||
name: string;
|
||||
clusterId: string;
|
||||
}
|
||||
|
||||
export class ClusterManager {
|
||||
public static readonly clusterIconDir = path.join(app.getPath("userData"), "icons")
|
||||
protected promiseIpc: any
|
||||
protected proxyServer: LensProxy
|
||||
protected port: number
|
||||
protected clusters: Map<string, Cluster>;
|
||||
|
||||
constructor(clusters: Cluster[], port: number) {
|
||||
this.promiseIpc = new PromiseIpc({ timeout: 2000 })
|
||||
this.port = port
|
||||
this.clusters = new Map()
|
||||
clusters.forEach((clusterInfo) => {
|
||||
try {
|
||||
const kc = this.loadKubeConfig(clusterInfo.kubeConfig)
|
||||
logger.debug(`Starting to load target definitions for ${ kc.currentContext }`)
|
||||
const cluster = new Cluster({
|
||||
id: clusterInfo.id,
|
||||
port: this.port,
|
||||
kubeConfig: clusterInfo.kubeConfig,
|
||||
preferences: clusterInfo.preferences,
|
||||
workspace: clusterInfo.workspace
|
||||
})
|
||||
cluster.init(kc)
|
||||
logger.debug(`Created cluster[id: ${ cluster.id }] for context ${ cluster.contextName }`)
|
||||
this.clusters.set(cluster.id, cluster)
|
||||
} catch(error) {
|
||||
logger.error(`Error while initializing ${clusterInfo.contextName}`)
|
||||
}
|
||||
});
|
||||
logger.debug("clusters after constructor:" + this.clusters.size)
|
||||
this.listenEvents()
|
||||
}
|
||||
|
||||
public getClusters() {
|
||||
return this.clusters.values()
|
||||
}
|
||||
|
||||
public getCluster(id: string) {
|
||||
return this.clusters.get(id)
|
||||
}
|
||||
|
||||
public stop() {
|
||||
const clusters = Array.from(this.getClusters())
|
||||
clusters.map(cluster => cluster.stopServer())
|
||||
}
|
||||
|
||||
protected loadKubeConfig(config: string): KubeConfig {
|
||||
const kc = new KubeConfig();
|
||||
kc.loadFromString(config);
|
||||
return kc;
|
||||
}
|
||||
|
||||
protected async addNewCluster(clusterData: ClusterBaseInfo): Promise<Cluster> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
const configs: KubeConfig[] = k8s.loadAndSplitConfig(clusterData.kubeConfig)
|
||||
if(configs.length == 0) {
|
||||
reject("No cluster contexts defined")
|
||||
}
|
||||
configs.forEach(c => {
|
||||
k8s.valideConfig(c)
|
||||
const cluster = new Cluster({
|
||||
id: uuid(),
|
||||
port: this.port,
|
||||
kubeConfig: k8s.dumpConfigYaml(c),
|
||||
preferences: clusterData.preferences,
|
||||
workspace: clusterData.workspace
|
||||
})
|
||||
cluster.init(c)
|
||||
cluster.save()
|
||||
this.clusters.set(cluster.id, cluster)
|
||||
resolve(cluster)
|
||||
});
|
||||
} catch(error) {
|
||||
logger.error(error)
|
||||
reject(error)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected listenEvents() {
|
||||
this.promiseIpc.on("addCluster", async (clusterData: ClusterBaseInfo) => {
|
||||
logger.debug(`IPC: addCluster`)
|
||||
const cluster = await this.addNewCluster(clusterData)
|
||||
return {
|
||||
addedCluster: this.clusterResponse(cluster),
|
||||
allClusters: Array.from(this.getClusters()).map((cluster: Cluster) => this.clusterResponse(cluster))
|
||||
}
|
||||
});
|
||||
|
||||
this.promiseIpc.on("getClusters", async (workspaceId: string) => {
|
||||
logger.debug(`IPC: getClusters, workspace ${workspaceId}`)
|
||||
const workspaceClusters = Array.from(this.getClusters()).filter((cluster) => cluster.workspace === workspaceId)
|
||||
return workspaceClusters.map((cluster: Cluster) => this.clusterResponse(cluster))
|
||||
});
|
||||
|
||||
this.promiseIpc.on("getCluster", async (id: string) => {
|
||||
logger.debug(`IPC: getCluster`)
|
||||
const cluster = this.getCluster(id)
|
||||
if (cluster) {
|
||||
await cluster.refreshCluster()
|
||||
return this.clusterResponse(cluster)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
});
|
||||
|
||||
this.promiseIpc.on("installFeature", async (installReq: FeatureInstallRequest) => {
|
||||
logger.debug(`IPC: installFeature for ${installReq.name}`)
|
||||
const cluster = this.clusters.get(installReq.clusterId)
|
||||
try {
|
||||
await cluster.installFeature(installReq.name, installReq.config)
|
||||
return {success: true, message: ""}
|
||||
} catch(error) {
|
||||
return {success: false, message: error}
|
||||
}
|
||||
});
|
||||
|
||||
this.promiseIpc.on("uninstallFeature", async (installReq: FeatureInstallRequest) => {
|
||||
logger.debug(`IPC: uninstallFeature for ${installReq.name}`)
|
||||
const cluster = this.clusters.get(installReq.clusterId)
|
||||
|
||||
await cluster.uninstallFeature(installReq.name)
|
||||
return {success: true, message: ""}
|
||||
});
|
||||
|
||||
this.promiseIpc.on("saveClusterIcon", async (fileUpload: ClusterIconUpload) => {
|
||||
logger.debug(`IPC: saveClusterIcon for ${fileUpload.clusterId}`)
|
||||
const cluster = this.getCluster(fileUpload.clusterId)
|
||||
if (!cluster) {
|
||||
return {success: false, message: "Cluster not found"}
|
||||
}
|
||||
try {
|
||||
const clusterIcon = await this.uploadClusterIcon(cluster, fileUpload.name, fileUpload.path)
|
||||
clusterStore.reloadCluster(cluster);
|
||||
if(!cluster.preferences) cluster.preferences = {};
|
||||
cluster.preferences.icon = clusterIcon
|
||||
clusterStore.storeCluster(cluster);
|
||||
return {success: true, cluster: this.clusterResponse(cluster), message: ""}
|
||||
} catch(error) {
|
||||
return {success: false, message: error}
|
||||
}
|
||||
});
|
||||
|
||||
this.promiseIpc.on("resetClusterIcon", async (id: string) => {
|
||||
logger.debug(`IPC: resetClusterIcon`)
|
||||
const cluster = this.getCluster(id)
|
||||
if (cluster && cluster.preferences) {
|
||||
cluster.preferences.icon = null;
|
||||
clusterStore.storeCluster(cluster)
|
||||
return {success: true, cluster: this.clusterResponse(cluster), message: ""}
|
||||
} else {
|
||||
return {success: false, message: "Cluster not found"}
|
||||
}
|
||||
});
|
||||
|
||||
this.promiseIpc.on("refreshCluster", async (clusterId: string) => {
|
||||
const cluster = this.clusters.get(clusterId)
|
||||
await cluster.refreshCluster()
|
||||
return this.clusterResponse(cluster)
|
||||
});
|
||||
|
||||
this.promiseIpc.on("stopCluster", (clusterId: string) => {
|
||||
logger.debug(`IPC: stopCluster: ${clusterId}`)
|
||||
const cluster = this.clusters.get(clusterId)
|
||||
if (cluster) {
|
||||
cluster.stopServer()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
});
|
||||
|
||||
this.promiseIpc.on("removeCluster", (ctx: string) => {
|
||||
logger.debug(`IPC: removeCluster: ${ctx}`)
|
||||
return this.removeCluster(ctx).map((cluster: Cluster) => this.clusterResponse(cluster))
|
||||
});
|
||||
|
||||
this.promiseIpc.on("clusterStored", (clusterId: string) => {
|
||||
logger.debug(`IPC: clusterStored: ${clusterId}`)
|
||||
const cluster = this.clusters.get(clusterId)
|
||||
if (cluster) {
|
||||
clusterStore.reloadCluster(cluster);
|
||||
cluster.stopServer()
|
||||
}
|
||||
});
|
||||
|
||||
this.promiseIpc.on("preferencesSaved", () => {
|
||||
logger.debug(`IPC: preferencesSaved`)
|
||||
this.clusters.forEach((cluster) => {
|
||||
cluster.stopServer()
|
||||
})
|
||||
});
|
||||
|
||||
this.promiseIpc.on("getClusterEvents", async (clusterId: string) => {
|
||||
const cluster = this.clusters.get(clusterId)
|
||||
return cluster.getEventCount();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public removeCluster(id: string): Cluster[] {
|
||||
const cluster = this.clusters.get(id)
|
||||
if (cluster) {
|
||||
cluster.stopServer()
|
||||
clusterStore.removeCluster(cluster.id);
|
||||
this.clusters.delete(cluster.id)
|
||||
}
|
||||
return Array.from(this.clusters.values())
|
||||
}
|
||||
|
||||
public getClusterForRequest(req: http.IncomingMessage): Cluster {
|
||||
let cluster: Cluster = null
|
||||
|
||||
// lens-server is connecting to 127.0.0.1:<port>/<uid>
|
||||
if (req.headers.host.startsWith("127.0.0.1")) {
|
||||
const clusterId = req.url.split("/")[1]
|
||||
if (clusterId) {
|
||||
cluster = this.clusters.get(clusterId)
|
||||
if (cluster) {
|
||||
// we need to swap path prefix so that request is proxied to kube api
|
||||
req.url = req.url.replace(`/${clusterId}`, "/api-kube")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const id = req.headers.host.split(".")[0]
|
||||
cluster = this.clusters.get(id)
|
||||
}
|
||||
|
||||
return cluster;
|
||||
}
|
||||
|
||||
// TODO: remove this
|
||||
protected clusterResponse(cluster: Cluster) {
|
||||
return cluster.toClusterInfo()
|
||||
}
|
||||
|
||||
protected async uploadClusterIcon(cluster: Cluster, fileName: string, src: string): Promise<string> {
|
||||
await ensureDir(ClusterManager.clusterIconDir)
|
||||
fileName = filenamify(cluster.contextName + "-" + fileName)
|
||||
const dest = path.join(ClusterManager.clusterIconDir, fileName)
|
||||
await promises.copyFile(src, dest)
|
||||
return "store:///icons/" + fileName
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
import { ContextHandler } from "./context-handler"
|
||||
import { FeatureStatusMap } from "./feature"
|
||||
import * as k8s from "./k8s"
|
||||
import { clusterStore } from "../common/cluster-store"
|
||||
import logger from "./logger"
|
||||
import { KubeConfig, CoreV1Api } from "@kubernetes/client-node"
|
||||
import * as fm from "./feature-manager";
|
||||
import { Kubectl } from "./kubectl";
|
||||
import { PromiseIpc } from "electron-promise-ipc"
|
||||
import * as request from "request-promise-native"
|
||||
import { KubeconfigManager } from "./kubeconfig-manager"
|
||||
|
||||
|
||||
enum ClusterStatus {
|
||||
AccessGranted = 2,
|
||||
AccessDenied = 1,
|
||||
Offline = 0
|
||||
}
|
||||
|
||||
export interface ClusterBaseInfo {
|
||||
id: string;
|
||||
kubeConfig: 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;
|
||||
};
|
||||
icon?: string;
|
||||
httpsProxy?: string;
|
||||
}
|
||||
|
||||
export class Cluster implements ClusterInfo {
|
||||
public id: string;
|
||||
public workspace: string;
|
||||
public contextHandler: ContextHandler;
|
||||
public contextName: string;
|
||||
public url: string;
|
||||
public port: number;
|
||||
public apiUrl: string;
|
||||
public online: boolean;
|
||||
public accessible: boolean;
|
||||
public failureReason: string;
|
||||
public nodes: number;
|
||||
public version: string;
|
||||
public distribution: string;
|
||||
public isAdmin: boolean;
|
||||
public features: FeatureStatusMap;
|
||||
public kubeCtl: Kubectl
|
||||
public kubeConfig: string;
|
||||
public eventCount: number;
|
||||
public preferences: ClusterPreferences;
|
||||
|
||||
protected eventPoller: NodeJS.Timeout;
|
||||
protected promiseIpc = new PromiseIpc({ timeout: 2000 })
|
||||
|
||||
protected kubeconfigManager: KubeconfigManager;
|
||||
|
||||
constructor(clusterInfo: ClusterBaseInfo) {
|
||||
if (clusterInfo) Object.assign(this, clusterInfo)
|
||||
if (!this.preferences) this.preferences = {}
|
||||
this.kubeconfigManager = new KubeconfigManager(this.kubeConfig)
|
||||
}
|
||||
|
||||
public kubeconfigPath() {
|
||||
return this.kubeconfigManager.getPath()
|
||||
}
|
||||
|
||||
public async init(kc: KubeConfig) {
|
||||
this.contextHandler = new ContextHandler(kc, this)
|
||||
this.contextName = kc.currentContext
|
||||
this.url = this.contextHandler.url
|
||||
this.apiUrl = kc.getCurrentCluster().server
|
||||
await this.contextHandler.init()
|
||||
}
|
||||
|
||||
public stopServer() {
|
||||
this.contextHandler.stopServer()
|
||||
clearInterval(this.eventPoller);
|
||||
}
|
||||
|
||||
public async installFeature(name: string, config: any) {
|
||||
await fm.installFeature(name, this, config)
|
||||
return this.refreshCluster()
|
||||
}
|
||||
|
||||
public async uninstallFeature(name: string) {
|
||||
await fm.uninstallFeature(name, this)
|
||||
return this.refreshCluster()
|
||||
}
|
||||
|
||||
public async refreshCluster() {
|
||||
clusterStore.reloadCluster(this)
|
||||
this.contextHandler.setClusterPreferences(this.preferences)
|
||||
|
||||
const connectionStatus = await this.getConnectionStatus()
|
||||
if (connectionStatus == ClusterStatus.AccessGranted) {
|
||||
this.accessible = true
|
||||
} else {
|
||||
this.accessible = false
|
||||
}
|
||||
if (connectionStatus > ClusterStatus.Offline) {
|
||||
this.online = true
|
||||
} else {
|
||||
this.online = false
|
||||
}
|
||||
if (this.accessible) {
|
||||
this.distribution = this.detectKubernetesDistribution(this.version)
|
||||
this.features = await fm.getFeatures(this.contextHandler)
|
||||
this.isAdmin = await this.isClusterAdmin()
|
||||
if (this.isAdmin) {
|
||||
this.nodes = await this.getNodeCount()
|
||||
}
|
||||
this.kubeCtl = new Kubectl(this.version)
|
||||
}
|
||||
this.eventCount = await this.getEventCount();
|
||||
}
|
||||
|
||||
public updateKubeconfig(kubeconfig: string) {
|
||||
const storedCluster = clusterStore.getCluster(this.id)
|
||||
if (!storedCluster) { return }
|
||||
|
||||
this.kubeConfig = kubeconfig
|
||||
this.save()
|
||||
}
|
||||
|
||||
public save() {
|
||||
clusterStore.storeCluster(this)
|
||||
}
|
||||
|
||||
public toClusterInfo(): ClusterInfo {
|
||||
const clusterInfo: ClusterInfo = {
|
||||
id: this.id,
|
||||
workspace: this.workspace,
|
||||
url: this.url,
|
||||
contextName: this.contextHandler.kc.currentContext,
|
||||
apiUrl: this.apiUrl,
|
||||
online: this.online,
|
||||
accessible: this.accessible,
|
||||
failureReason: this.failureReason,
|
||||
nodes: this.nodes,
|
||||
version: this.version,
|
||||
distribution: this.distribution,
|
||||
isAdmin: this.isAdmin,
|
||||
features: this.features,
|
||||
kubeCtl: this.kubeCtl,
|
||||
kubeConfig: this.kubeConfig,
|
||||
preferences: this.preferences
|
||||
}
|
||||
return clusterInfo;
|
||||
}
|
||||
|
||||
protected async k8sRequest(path: string, opts?: request.RequestPromiseOptions) {
|
||||
const options = Object.assign({
|
||||
json: true, timeout: 10000
|
||||
}, (opts || {}))
|
||||
if (!options.headers) { options.headers = {} }
|
||||
options.headers.host = `${this.id}.localhost:${this.port}`
|
||||
|
||||
return request(`http://127.0.0.1:${this.port}/api-kube${path}`, options)
|
||||
}
|
||||
|
||||
protected async getConnectionStatus() {
|
||||
try {
|
||||
const response = await this.k8sRequest("/version")
|
||||
this.version = response.gitVersion
|
||||
this.failureReason = null
|
||||
return ClusterStatus.AccessGranted;
|
||||
} catch(error) {
|
||||
logger.error(`Failed to connect to cluster ${this.contextName}: ${JSON.stringify(error)}`)
|
||||
if (error.statusCode) {
|
||||
if (error.statusCode >= 400 && error.statusCode < 500) {
|
||||
this.failureReason = "Invalid credentials";
|
||||
return ClusterStatus.AccessDenied;
|
||||
} else {
|
||||
this.failureReason = error.error || error.message;
|
||||
return ClusterStatus.Offline;
|
||||
}
|
||||
} else if (error.failed === true) {
|
||||
if (error.timedOut === true) {
|
||||
this.failureReason = "Connection timed out";
|
||||
return ClusterStatus.Offline;
|
||||
} else {
|
||||
this.failureReason = "Failed to fetch credentials";
|
||||
return ClusterStatus.AccessDenied;
|
||||
}
|
||||
}
|
||||
this.failureReason = error.message;
|
||||
return ClusterStatus.Offline;
|
||||
}
|
||||
}
|
||||
|
||||
protected async isClusterAdmin(): Promise<boolean> {
|
||||
const requestOpts: request.RequestPromiseOptions = {
|
||||
body: {
|
||||
kind: "SelfSubjectAccessReview",
|
||||
apiVersion: "authorization.k8s.io/v1",
|
||||
spec: {
|
||||
resourceAttributes: {
|
||||
namespace: "kube-system",
|
||||
resource: "*",
|
||||
verb: "create",
|
||||
}
|
||||
}
|
||||
},
|
||||
method: "post"
|
||||
}
|
||||
try {
|
||||
const response = await this.k8sRequest("/apis/authorization.k8s.io/v1/selfsubjectaccessreviews", requestOpts)
|
||||
return response.status.allowed === true
|
||||
} catch(error) {
|
||||
logger.error(`failed to request selfSubjectAccessReview: ${error.message}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
protected detectKubernetesDistribution(kubernetesVersion: string): string {
|
||||
if (kubernetesVersion.includes("gke")) {
|
||||
return "gke"
|
||||
}
|
||||
else if (kubernetesVersion.includes("eks")) {
|
||||
return "eks"
|
||||
}
|
||||
else if (kubernetesVersion.includes("IKS")) {
|
||||
return "iks"
|
||||
}
|
||||
else if(this.apiUrl.endsWith("azmk8s.io")) {
|
||||
return "aks"
|
||||
}
|
||||
else if(this.apiUrl.endsWith("k8s.ondigitalocean.com")) {
|
||||
return "digitalocean"
|
||||
}
|
||||
else if (this.contextHandler.contextName.startsWith("minikube")) {
|
||||
return "minikube"
|
||||
}
|
||||
else if (kubernetesVersion.includes("+")) {
|
||||
return "custom"
|
||||
}
|
||||
|
||||
return "vanilla"
|
||||
}
|
||||
|
||||
protected async getNodeCount() {
|
||||
try {
|
||||
const response = await this.k8sRequest("/api/v1/nodes")
|
||||
return response.items.length
|
||||
} catch(error) {
|
||||
logger.debug(`failed to request node list: ${error.message}`)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
public async getEventCount(): Promise<number> {
|
||||
if (!this.isAdmin) {
|
||||
return 0;
|
||||
}
|
||||
const client = this.contextHandler.kc.makeApiClient(CoreV1Api);
|
||||
try {
|
||||
const response = await client.listEventForAllNamespaces(false, null, null, null, 1000);
|
||||
const uniqEventSources = new Set();
|
||||
const warnings = response.body.items.filter(e => e.type !== 'Normal');
|
||||
for (const w of warnings) {
|
||||
if(w.involvedObject.kind === 'Pod') {
|
||||
try {
|
||||
const pod = (await client.readNamespacedPod(w.involvedObject.name, w.involvedObject.namespace)).body;
|
||||
logger.debug(`checking pod ${w.involvedObject.namespace}/${w.involvedObject.name}`)
|
||||
if(k8s.podHasIssues(pod)) {
|
||||
uniqEventSources.add(w.involvedObject.uid);
|
||||
}
|
||||
continue;
|
||||
} catch (error) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
uniqEventSources.add(w.involvedObject.uid);
|
||||
}
|
||||
}
|
||||
let nodeNotificationCount = 0;
|
||||
const nodes = (await client.listNode()).body.items;
|
||||
nodes.map(n => {
|
||||
nodeNotificationCount = nodeNotificationCount + k8s.getNodeWarningConditions(n).length
|
||||
});
|
||||
return uniqEventSources.size + nodeNotificationCount;
|
||||
} catch (error) {
|
||||
logger.error("Failed to fetch event count: " + JSON.stringify(error))
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
import { app } from "electron"
|
||||
import { KubeConfig } from "@kubernetes/client-node"
|
||||
import { readFileSync } from "fs"
|
||||
import * as http from "http"
|
||||
import { ServerOptions } from "http-proxy"
|
||||
import * as url from "url"
|
||||
import { v4 as uuid } from "uuid"
|
||||
import logger from "./logger"
|
||||
import { getFreePort } from "./port"
|
||||
import { LensServer } from "./lens-server"
|
||||
import { KubeAuthProxy } from "./kube-auth-proxy"
|
||||
import { Cluster, ClusterPreferences } from "./cluster"
|
||||
import { userStore } from "../common/user-store"
|
||||
|
||||
export class ContextHandler {
|
||||
public contextName: string
|
||||
public id: string
|
||||
public url: string
|
||||
public kc: KubeConfig
|
||||
public certData: string
|
||||
public authCertData: string
|
||||
public cluster: Cluster
|
||||
|
||||
protected apiTarget: ServerOptions
|
||||
protected proxyTarget: ServerOptions
|
||||
protected clusterUrl: url.UrlWithStringQuery
|
||||
protected localServer: LensServer
|
||||
protected proxyServer: KubeAuthProxy
|
||||
|
||||
protected clientCert: string
|
||||
protected clientKey: string
|
||||
protected secureApiConnection = true
|
||||
protected defaultNamespace: string
|
||||
protected port: number
|
||||
protected proxyPort: number
|
||||
protected kubernetesApi: string
|
||||
protected prometheusPath: string
|
||||
protected clusterName: string
|
||||
|
||||
constructor(kc: KubeConfig, cluster: Cluster) {
|
||||
this.id = cluster.id
|
||||
this.kc = new KubeConfig()
|
||||
this.kc.users = [
|
||||
{
|
||||
name: kc.getCurrentUser().name,
|
||||
token: this.id
|
||||
}
|
||||
]
|
||||
this.kc.contexts = [
|
||||
{
|
||||
name: kc.currentContext,
|
||||
cluster: kc.getCurrentCluster().name,
|
||||
user: kc.getCurrentUser().name,
|
||||
namespace: kc.getContextObject(kc.currentContext).namespace
|
||||
}
|
||||
]
|
||||
this.kc.setCurrentContext(kc.currentContext)
|
||||
|
||||
this.cluster = cluster
|
||||
this.clusterUrl = url.parse(kc.getCurrentCluster().server)
|
||||
this.contextName = kc.currentContext;
|
||||
this.defaultNamespace = kc.getContextObject(kc.currentContext).namespace
|
||||
this.url = `http://${this.id}.localhost:${cluster.port}/`
|
||||
this.kubernetesApi = `http://127.0.0.1:${cluster.port}/${this.id}`
|
||||
this.setClusterPreferences(cluster.preferences)
|
||||
this.kc.clusters = [
|
||||
{
|
||||
name: kc.getCurrentCluster().name,
|
||||
server: this.kubernetesApi,
|
||||
skipTLSVerify: true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
public setClusterPreferences(clusterPreferences?: ClusterPreferences) {
|
||||
if (clusterPreferences && clusterPreferences.prometheus) {
|
||||
const prom = clusterPreferences.prometheus
|
||||
this.prometheusPath = `${prom.namespace}/services/${prom.service}:${prom.port}`
|
||||
} else {
|
||||
this.prometheusPath = "lens-metrics/services/prometheus:80"
|
||||
}
|
||||
if(clusterPreferences && clusterPreferences.clusterName) {
|
||||
this.clusterName = clusterPreferences.clusterName;
|
||||
} else {
|
||||
this.clusterName = this.contextName;
|
||||
}
|
||||
}
|
||||
|
||||
public async init() {
|
||||
const currentCluster = this.kc.getCurrentCluster()
|
||||
if (currentCluster.caFile) {
|
||||
this.certData = readFileSync(currentCluster.caFile).toString()
|
||||
} else if (currentCluster.caData) {
|
||||
this.certData = Buffer.from(currentCluster.caData, "base64").toString("ascii")
|
||||
}
|
||||
const user = this.kc.getCurrentUser()
|
||||
if (user.authProvider && user.authProvider.name === "oidc") {
|
||||
const authConfig = user.authProvider.config
|
||||
if (authConfig["idp-certificate-authority"]) {
|
||||
this.authCertData = readFileSync(authConfig["idp-certificate-authority"]).toString()
|
||||
} else if (authConfig["idp-certificate-authority-data"]) {
|
||||
this.authCertData = Buffer.from(authConfig["idp-certificate-authority-data"], "base64").toString("ascii")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async getApiTarget() {
|
||||
if (this.apiTarget) { return this.apiTarget }
|
||||
|
||||
this.apiTarget = {
|
||||
changeOrigin: true,
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
"Host": this.clusterUrl.hostname
|
||||
},
|
||||
target: {
|
||||
port: await this.resolveProxyPort(),
|
||||
protocol: "http://",
|
||||
host: "localhost",
|
||||
path: this.clusterUrl.path
|
||||
},
|
||||
}
|
||||
|
||||
return this.apiTarget
|
||||
}
|
||||
|
||||
public async getProxyTarget() {
|
||||
if (this.proxyTarget) {
|
||||
return this.proxyTarget;
|
||||
}
|
||||
|
||||
this.proxyTarget = {
|
||||
changeOrigin: true,
|
||||
secure: false,
|
||||
target: {
|
||||
host: this.clusterUrl.host,
|
||||
hostname: "localhost",
|
||||
path: "/",
|
||||
port: await this.resolvePort(),
|
||||
protocol: "http://",
|
||||
},
|
||||
}
|
||||
|
||||
return this.proxyTarget;
|
||||
}
|
||||
|
||||
protected async resolvePort(): Promise<number> {
|
||||
if (this.port) return this.port
|
||||
|
||||
let serverPort: number = null
|
||||
try {
|
||||
serverPort = await getFreePort(49153, 49900) // the proxy will usually already be on 49152 so skip that
|
||||
} catch(error) {
|
||||
logger.error(error)
|
||||
throw(error)
|
||||
}
|
||||
this.port = serverPort
|
||||
|
||||
return serverPort
|
||||
}
|
||||
|
||||
protected async resolveProxyPort(): Promise<number> {
|
||||
if (this.proxyPort) return this.proxyPort
|
||||
|
||||
let serverPort: number = null
|
||||
try {
|
||||
serverPort = await getFreePort(49901, 65535)
|
||||
} catch(error) {
|
||||
logger.error(error)
|
||||
throw(error)
|
||||
}
|
||||
this.proxyPort = serverPort
|
||||
|
||||
return serverPort
|
||||
}
|
||||
|
||||
public applyHeaders(req: http.IncomingMessage) {
|
||||
req.headers["authorization"] = `Bearer ${this.id}`
|
||||
}
|
||||
|
||||
public async withTemporaryKubeconfig(callback: (kubeconfig: string) => Promise<any>) {
|
||||
try {
|
||||
await callback(this.cluster.kubeconfigPath())
|
||||
} catch(error) {
|
||||
throw(error)
|
||||
}
|
||||
}
|
||||
|
||||
protected initServer(serverUrl: string, port: number) {
|
||||
const userPrefs = userStore.getPreferences()
|
||||
const envs = {
|
||||
KUBE_CLUSTER_URL: serverUrl,
|
||||
KUBE_CLUSTER_NAME: this.clusterName,
|
||||
KUBERNETES_TLS_SKIP: "true",
|
||||
KUBERNETES_NAMESPACE: this.defaultNamespace,
|
||||
SESSION_SECRET: this.id,
|
||||
LOCAL_SERVER_PORT: port.toString(),
|
||||
KUBE_METRICS_URL: `${serverUrl}/api/v1/namespaces/${this.prometheusPath}/proxy`,
|
||||
STATS_NAMESPACE_DEFAULT: this.prometheusPath.split("/")[0],
|
||||
CHARTS_ENABLED: "true",
|
||||
LENS_VERSION: app.getVersion(),
|
||||
LENS_THEME: `kontena-${userPrefs.colorTheme}`,
|
||||
NODE_ENV: "production",
|
||||
}
|
||||
logger.debug(`spinning up lens-server process with env: ${JSON.stringify(envs)}`)
|
||||
this.localServer = new LensServer(serverUrl, envs)
|
||||
}
|
||||
|
||||
public async ensureServer() {
|
||||
if (!this.localServer) {
|
||||
const currentCluster = this.kc.getCurrentCluster()
|
||||
const clusterUrl = url.parse(currentCluster.server)
|
||||
const serverPort = await this.resolvePort()
|
||||
logger.info(`initializing server for ${clusterUrl.host} on port ${serverPort}`)
|
||||
this.initServer(this.kubernetesApi, serverPort)
|
||||
await this.localServer.run()
|
||||
}
|
||||
if (!this.proxyServer) {
|
||||
const proxyPort = await this.resolveProxyPort()
|
||||
const proxyEnv = Object.assign({}, process.env)
|
||||
if (this.cluster.preferences && this.cluster.preferences.httpsProxy) {
|
||||
proxyEnv.HTTPS_PROXY = this.cluster.preferences.httpsProxy
|
||||
}
|
||||
this.proxyServer = new KubeAuthProxy(this.cluster, proxyPort, proxyEnv)
|
||||
await this.proxyServer.run()
|
||||
}
|
||||
}
|
||||
|
||||
public stopServer() {
|
||||
if (this.localServer) {
|
||||
this.localServer.exit()
|
||||
this.localServer = null
|
||||
}
|
||||
if (this.proxyServer) {
|
||||
this.proxyServer.exit()
|
||||
this.proxyServer = null
|
||||
}
|
||||
}
|
||||
|
||||
public proxyServerError() {
|
||||
if (!this.proxyServer) { return null }
|
||||
|
||||
return this.proxyServer.lastError
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { ContextHandler } from "./context-handler"
|
||||
import logger from "./logger";
|
||||
import { Cluster } from "./cluster";
|
||||
import { Feature, FeatureStatusMap } from "./feature"
|
||||
import { MetricsFeature } from "../features/metrics"
|
||||
import { UserModeFeature } from "../features/user-mode"
|
||||
|
||||
const ALL_FEATURES: any = {
|
||||
'metrics': new MetricsFeature(null),
|
||||
'user-mode': new UserModeFeature(null),
|
||||
}
|
||||
|
||||
export async function getFeatures(clusterContext: ContextHandler): Promise<FeatureStatusMap> {
|
||||
return new Promise<FeatureStatusMap>(async (resolve, reject) => {
|
||||
const result: FeatureStatusMap = {};
|
||||
logger.debug(`features for ${clusterContext.contextName}`);
|
||||
for (const key in ALL_FEATURES) {
|
||||
logger.debug(`feature ${key}`);
|
||||
if (ALL_FEATURES.hasOwnProperty(key)) {
|
||||
logger.debug("getting feature status...");
|
||||
const feature = ALL_FEATURES[key] as Feature;
|
||||
|
||||
const status = await feature.featureStatus(clusterContext.kc);
|
||||
result[feature.name] = status
|
||||
|
||||
} else {
|
||||
logger.error("ALL_FEATURES.hasOwnProperty(key) returned FALSE ?!?!?!?!")
|
||||
|
||||
}
|
||||
}
|
||||
logger.debug(`getFeatures resolving with features: ${JSON.stringify(result)}`);
|
||||
resolve(result);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export async function installFeature(name: string, cluster: Cluster, config: any) {
|
||||
const feature = ALL_FEATURES[name] as Feature
|
||||
// TODO Figure out how to handle config stuff
|
||||
await feature.install(cluster)
|
||||
}
|
||||
|
||||
|
||||
|
||||
export async function uninstallFeature(name: string, cluster: Cluster) {
|
||||
const feature = ALL_FEATURES[name] as Feature
|
||||
|
||||
await feature.uninstall(cluster)
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import * as fs from "fs";
|
||||
import * as path from "path"
|
||||
import * as hb from "handlebars"
|
||||
import { ResourceApplier } from "./resource-applier"
|
||||
import { KubeConfig, CoreV1Api, Watch } from "@kubernetes/client-node"
|
||||
import logger from "./logger";
|
||||
import { Cluster } from "./cluster";
|
||||
|
||||
export type FeatureStatus = {
|
||||
currentVersion: string;
|
||||
installed: boolean;
|
||||
latestVersion: string;
|
||||
canUpgrade: boolean;
|
||||
// TODO We need bunch of other stuff too: upgradeable, latestVersion, ...
|
||||
};
|
||||
|
||||
export type FeatureStatusMap = {
|
||||
[name: string]: FeatureStatus;
|
||||
}
|
||||
|
||||
export abstract class Feature {
|
||||
name: string;
|
||||
config: any;
|
||||
latestVersion: string;
|
||||
|
||||
constructor(config: any) {
|
||||
if(config) {
|
||||
this.config = config;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Return types for these?
|
||||
async install(cluster: Cluster): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
// Read and process yamls through handlebar
|
||||
const resources = this.renderTemplates();
|
||||
|
||||
// Apply processed manifests
|
||||
cluster.contextHandler.withTemporaryKubeconfig(async (kubeconfigPath) => {
|
||||
const resourceApplier = new ResourceApplier(cluster, kubeconfigPath)
|
||||
try {
|
||||
await resourceApplier.kubectlApplyAll(resources)
|
||||
resolve(true)
|
||||
} catch(error) {
|
||||
reject(error)
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
upgrade(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
abstract async uninstall(cluster: Cluster): Promise<boolean>;
|
||||
|
||||
abstract async featureStatus(kc: KubeConfig): Promise<FeatureStatus>;
|
||||
|
||||
protected async deleteNamespace(kc: KubeConfig, name: string) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const client = kc.makeApiClient(CoreV1Api)
|
||||
const result = await client.deleteNamespace("lens-metrics", 'false', undefined, undefined, undefined, "Foreground");
|
||||
const nsVersion = result.body.metadata.resourceVersion;
|
||||
const nsWatch = new Watch(kc);
|
||||
const req = nsWatch.watch('/api/v1/namespaces', {resourceVersion: nsVersion, fieldSelector: "metadata.name=lens-metrics"},
|
||||
(type, obj) => {
|
||||
if(type === 'DELETED') {
|
||||
logger.debug(`namespace ${name} finally gone`)
|
||||
req.abort();
|
||||
resolve()
|
||||
}
|
||||
},
|
||||
(err) => {
|
||||
if(err) {
|
||||
reject(err)
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected renderTemplates(): string[] {
|
||||
console.log("starting to render resources...");
|
||||
const resources: string[] = [];
|
||||
fs.readdirSync(this.manifestPath()).forEach((f) => {
|
||||
const file = path.join(this.manifestPath(), f);
|
||||
console.log("processing file:", file)
|
||||
const raw = fs.readFileSync(file);
|
||||
console.log("raw file loaded");
|
||||
if(f.endsWith('.hb')) {
|
||||
console.log("processing HB template");
|
||||
const template = hb.compile(raw.toString());
|
||||
resources.push(template(this.config));
|
||||
console.log("HB template done");
|
||||
} else {
|
||||
console.log("using as raw, no HB detected");
|
||||
resources.push(raw.toString());
|
||||
}
|
||||
});
|
||||
|
||||
return resources;
|
||||
}
|
||||
|
||||
protected manifestPath() {
|
||||
return path.join(__dirname, '..', 'features', this.name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import * as fs from "fs"
|
||||
|
||||
export function ensureDir(dirname: string) {
|
||||
if (!fs.existsSync(dirname)) {
|
||||
fs.mkdirSync(dirname)
|
||||
}
|
||||
}
|
||||
|
||||
export function randomFileName(name: string) {
|
||||
return `${Math.random().toString(36).substring(2, 15)}-${Math.random().toString(36).substring(2, 15)}-${name}`
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
import { LensApiRequest } from "./router"
|
||||
import { helmService } from "./helm-service"
|
||||
import { LensApi } from "./lens-api"
|
||||
import logger from "./logger"
|
||||
|
||||
class HelmApi extends LensApi {
|
||||
public async listCharts(request: LensApiRequest) {
|
||||
const { response } = request
|
||||
const charts = await helmService.listCharts()
|
||||
this.respondJson(response, charts)
|
||||
}
|
||||
|
||||
public async getChart(request: LensApiRequest) {
|
||||
const { params, query, response } = request
|
||||
const chart = await helmService.getChart(params.repo, params.chart, query.get("version"))
|
||||
this.respondJson(response, chart)
|
||||
}
|
||||
|
||||
public async getChartValues(request: LensApiRequest) {
|
||||
const { params, query, response } = request
|
||||
const values = await helmService.getChartValues(params.repo, params.chart, query.get("version"))
|
||||
this.respondJson(response, values)
|
||||
}
|
||||
|
||||
public async installChart(request: LensApiRequest) {
|
||||
const { payload, cluster, response } = request
|
||||
try {
|
||||
const result = await helmService.installChart(cluster, payload)
|
||||
this.respondJson(response, result, 201)
|
||||
} catch (error) {
|
||||
logger.debug(error)
|
||||
this.respondText(response, error, 422)
|
||||
}
|
||||
}
|
||||
|
||||
public async updateRelease(request: LensApiRequest) {
|
||||
const { cluster, params, payload, response } = request
|
||||
try {
|
||||
const result = await helmService.updateRelease(cluster, params.release, params.namespace, payload )
|
||||
this.respondJson(response, result)
|
||||
} catch (error) {
|
||||
logger.debug(error)
|
||||
this.respondText(response, error, 422)
|
||||
}
|
||||
}
|
||||
|
||||
public async rollbackRelease(request: LensApiRequest) {
|
||||
const { cluster, params, payload, response } = request
|
||||
try {
|
||||
const result = await helmService.rollback(cluster, params.release, params.namespace, payload.revision)
|
||||
this.respondJson(response, result)
|
||||
} catch (error) {
|
||||
logger.debug(error)
|
||||
this.respondText(response, error)
|
||||
}
|
||||
}
|
||||
|
||||
public async listReleases(request: LensApiRequest) {
|
||||
const { cluster, params, response } = request
|
||||
try {
|
||||
const result = await helmService.listReleases(cluster, params.namespace)
|
||||
this.respondJson(response, result)
|
||||
} catch(error) {
|
||||
logger.debug(error)
|
||||
this.respondText(response, error)
|
||||
}
|
||||
}
|
||||
|
||||
public async getRelease(request: LensApiRequest) {
|
||||
const { cluster, params, response } = request
|
||||
try {
|
||||
const result = await helmService.getRelease(cluster, params.release, params.namespace)
|
||||
this.respondJson(response, result)
|
||||
} catch (error) {
|
||||
logger.debug(error)
|
||||
this.respondText(response, error, 422)
|
||||
}
|
||||
}
|
||||
|
||||
public async getReleaseValues(request: LensApiRequest) {
|
||||
const { cluster, params, response } = request
|
||||
try {
|
||||
const result = await helmService.getReleaseValues(cluster, params.release, params.namespace)
|
||||
this.respondText(response, result)
|
||||
} catch (error) {
|
||||
logger.debug(error)
|
||||
this.respondText(response, error, 422)
|
||||
}
|
||||
}
|
||||
|
||||
public async getReleaseHistory(request: LensApiRequest) {
|
||||
const { cluster, params, response } = request
|
||||
try {
|
||||
const result = await helmService.getReleaseHistory(cluster, params.release, params.namespace)
|
||||
this.respondJson(response, result)
|
||||
} catch (error) {
|
||||
logger.debug(error)
|
||||
this.respondText(response, error, 422)
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteRelease(request: LensApiRequest) {
|
||||
const { cluster, params, response } = request
|
||||
try {
|
||||
const result = await helmService.deleteRelease(cluster, params.release, params.namespace)
|
||||
this.respondJson(response, result)
|
||||
} catch (error) {
|
||||
logger.debug(error)
|
||||
this.respondText(response, error, 422)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const helmApi = new HelmApi()
|
||||
@@ -0,0 +1,73 @@
|
||||
import * as fs from "fs";
|
||||
import * as yaml from "js-yaml";
|
||||
import { HelmRepo, HelmRepoManager } from "./helm-repo-manager"
|
||||
import logger from "./logger";
|
||||
import { promiseExec } from "./promise-exec"
|
||||
import { helmCli } from "./helm-cli"
|
||||
|
||||
type CachedYaml = {
|
||||
entries: any;
|
||||
}
|
||||
|
||||
export class HelmChartManager {
|
||||
protected cache: any
|
||||
protected repo: HelmRepo
|
||||
constructor(repo: HelmRepo){
|
||||
this.cache = HelmRepoManager.cache
|
||||
this.repo = repo
|
||||
}
|
||||
|
||||
public async chart(name: string) {
|
||||
const charts = await this.charts()
|
||||
return charts[name]
|
||||
}
|
||||
|
||||
public async charts(): Promise<any> {
|
||||
try {
|
||||
const cachedYaml = await this.cachedYaml()
|
||||
return cachedYaml["entries"]
|
||||
} catch(error) {
|
||||
logger.error(error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
public async getReadme(name: string, version = "") {
|
||||
const helm = await helmCli.binaryPath()
|
||||
if(version && version != "") {
|
||||
const { stdout, stderr} = await promiseExec(`"${helm}" show readme ${this.repo.name}/${name} --version ${version}`).catch((error) => { throw(error.stderr)})
|
||||
return stdout
|
||||
} else {
|
||||
const { stdout, stderr} = await promiseExec(`"${helm}" show readme ${this.repo.name}/${name}`).catch((error) => { throw(error.stderr)})
|
||||
return stdout
|
||||
}
|
||||
}
|
||||
|
||||
public async getValues(name: string, version = "") {
|
||||
const helm = await helmCli.binaryPath()
|
||||
if(version && version != "") {
|
||||
const { stdout, stderr} = await promiseExec(`"${helm}" show values ${this.repo.name}/${name} --version ${version}`).catch((error) => { throw(error.stderr)})
|
||||
|
||||
return stdout
|
||||
} else {
|
||||
const { stdout, stderr} = await promiseExec(`"${helm}" show values ${this.repo.name}/${name}`).catch((error) => { throw(error.stderr)})
|
||||
|
||||
return stdout
|
||||
}
|
||||
}
|
||||
|
||||
protected async cachedYaml(): Promise<CachedYaml> {
|
||||
if (!(this.repo.name in this.cache)) {
|
||||
const cacheFile = await fs.promises.readFile(this.repo.cacheFilePath, 'utf-8')
|
||||
const data = yaml.safeLoad(cacheFile)
|
||||
for(const key in data["entries"]) {
|
||||
data["entries"][key].forEach((version: any) => {
|
||||
version['repo'] = this.repo.name
|
||||
version['created'] = Date.parse(version.created).toString()
|
||||
})
|
||||
}
|
||||
this.cache[this.repo.name] = Buffer.from(JSON.stringify(data))
|
||||
}
|
||||
return JSON.parse(this.cache[this.repo.name].toString())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import * as path from "path"
|
||||
import { LensBinary, LensBinaryOpts } from "./lens-binary"
|
||||
|
||||
export class HelmCli extends LensBinary {
|
||||
|
||||
public constructor(version: string) {
|
||||
const opts: LensBinaryOpts = {
|
||||
version,
|
||||
originalBinaryName: "helm",
|
||||
newBinaryName: "helm3"
|
||||
}
|
||||
super(opts)
|
||||
}
|
||||
protected getTarName(): string|null {
|
||||
return `${this.binaryName}-v${this.binaryVersion}-${this.platformName}-${this.arch}.tar.gz`
|
||||
}
|
||||
|
||||
protected getUrl() {
|
||||
return `https://get.helm.sh/helm-v${this.binaryVersion}-${this.platformName}-${this.arch}.tar.gz`
|
||||
}
|
||||
|
||||
protected getBinaryPath() {
|
||||
return path.join(this.dirname, this.platformName+"-"+this.arch, this.binaryName)
|
||||
}
|
||||
|
||||
protected getOriginalBinaryPath() {
|
||||
return path.join(this.dirname, this.platformName+"-"+this.arch, this.originalBinaryName)
|
||||
}
|
||||
}
|
||||
|
||||
export const helmCli = new HelmCli("3.1.2")
|
||||
@@ -0,0 +1,112 @@
|
||||
import * as tempy from "tempy";
|
||||
import * as fs from "fs";
|
||||
import * as yaml from "js-yaml";
|
||||
import * as camelcaseKeys from "camelcase-keys";
|
||||
import { promiseExec} from "./promise-exec"
|
||||
import { helmCli } from "./helm-cli";
|
||||
import { Cluster } from "./cluster";
|
||||
|
||||
export class HelmReleaseManager {
|
||||
|
||||
public async listReleases(pathToKubeconfig: string, namespace?: string) {
|
||||
const helm = await helmCli.binaryPath()
|
||||
const namespaceFlag = namespace ? `-n ${namespace}` : "--all-namespaces"
|
||||
const { stdout, stderr } = await promiseExec(`"${helm}" ls --output json ${namespaceFlag} --kubeconfig ${pathToKubeconfig}`).catch((error) => { throw(error.stderr)})
|
||||
|
||||
const output = JSON.parse(stdout)
|
||||
if (output.length == 0) {
|
||||
return output
|
||||
}
|
||||
const result: any = []
|
||||
output.forEach((release: any, index: number) => {
|
||||
output[index] = camelcaseKeys(release)
|
||||
});
|
||||
return output
|
||||
}
|
||||
|
||||
|
||||
public async installChart(chart: string, values: any, name: string, namespace: string, version: string, pathToKubeconfig: string){
|
||||
const helm = await helmCli.binaryPath()
|
||||
const fileName = tempy.file({name: "values.yaml"})
|
||||
await fs.promises.writeFile(fileName, yaml.safeDump(values))
|
||||
try {
|
||||
let generateName = ""
|
||||
if (!name) {
|
||||
generateName = "--generate-name"
|
||||
name = ""
|
||||
}
|
||||
const { stdout, stderr } = await promiseExec(`"${helm}" install ${name} ${chart} --version ${version} -f ${fileName} --namespace ${namespace} --kubeconfig ${pathToKubeconfig} ${generateName}`).catch((error) => { throw(error.stderr)})
|
||||
const releaseName = stdout.split("\n")[0].split(' ')[1].trim()
|
||||
return {
|
||||
log: stdout,
|
||||
release: {
|
||||
name: releaseName,
|
||||
namespace: namespace
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await fs.promises.unlink(fileName)
|
||||
}
|
||||
}
|
||||
|
||||
public async upgradeRelease(name: string, chart: string, values: any, namespace: string, version: string, cluster: Cluster){
|
||||
const helm = await helmCli.binaryPath()
|
||||
const fileName = tempy.file({name: "values.yaml"})
|
||||
await fs.promises.writeFile(fileName, yaml.safeDump(values))
|
||||
|
||||
try {
|
||||
const { stdout, stderr } = await promiseExec(`"${helm}" upgrade ${name} ${chart} --version ${version} -f ${fileName} --namespace ${namespace} --kubeconfig ${cluster.kubeconfigPath()}`).catch((error) => { throw(error.stderr)})
|
||||
return {
|
||||
log: stdout,
|
||||
release: this.getRelease(name, namespace, cluster)
|
||||
}
|
||||
} finally {
|
||||
await fs.promises.unlink(fileName)
|
||||
}
|
||||
}
|
||||
|
||||
public async getRelease(name: string, namespace: string, cluster: Cluster) {
|
||||
const helm = await helmCli.binaryPath()
|
||||
const {stdout, stderr} = await promiseExec(`"${helm}" status ${name} --output json --namespace ${namespace} --kubeconfig ${cluster.kubeconfigPath()}`).catch((error) => { throw(error.stderr)})
|
||||
const release = JSON.parse(stdout)
|
||||
release.resources = await this.getResources(name, namespace, cluster)
|
||||
return release
|
||||
}
|
||||
|
||||
public async deleteRelease(name: string, namespace: string, pathToKubeconfig: string) {
|
||||
const helm = await helmCli.binaryPath()
|
||||
const { stdout, stderr } = await promiseExec(`"${helm}" delete ${name} --namespace ${namespace} --kubeconfig ${pathToKubeconfig}`).catch((error) => { throw(error.stderr)})
|
||||
|
||||
return stdout
|
||||
}
|
||||
|
||||
public async getValues(name: string, namespace: string, pathToKubeconfig: string) {
|
||||
const helm = await helmCli.binaryPath()
|
||||
const { stdout, stderr } = await promiseExec(`"${helm}" get values ${name} --all --output yaml --namespace ${namespace} --kubeconfig ${pathToKubeconfig}`).catch((error) => { throw(error.stderr)})
|
||||
return stdout
|
||||
}
|
||||
|
||||
public async getHistory(name: string, namespace: string, pathToKubeconfig: string) {
|
||||
const helm = await helmCli.binaryPath()
|
||||
const {stdout, stderr} = await promiseExec(`"${helm}" history ${name} --output json --namespace ${namespace} --kubeconfig ${pathToKubeconfig}`).catch((error) => { throw(error.stderr)})
|
||||
return JSON.parse(stdout)
|
||||
}
|
||||
|
||||
public async rollback(name: string, namespace: string, revision: number, pathToKubeconfig: string) {
|
||||
const helm = await helmCli.binaryPath()
|
||||
const {stdout, stderr} = await promiseExec(`"${helm}" rollback ${name} ${revision} --namespace ${namespace} --kubeconfig ${pathToKubeconfig}`).catch((error) => { throw(error.stderr)})
|
||||
return stdout
|
||||
}
|
||||
|
||||
protected async getResources(name: string, namespace: string, cluster: Cluster) {
|
||||
const helm = await helmCli.binaryPath()
|
||||
const kubectl = await cluster.kubeCtl.kubectlPath()
|
||||
const pathToKubeconfig = cluster.kubeconfigPath()
|
||||
const { stdout } = await promiseExec(`"${helm}" get manifest ${name} --namespace ${namespace} --kubeconfig ${pathToKubeconfig} | "${kubectl}" get -n ${namespace} --kubeconfig ${pathToKubeconfig} -f - -o=json`).catch((error) => {
|
||||
return { stdout: JSON.stringify({items: []})}
|
||||
})
|
||||
return stdout
|
||||
}
|
||||
}
|
||||
|
||||
export const releaseManager = new HelmReleaseManager()
|
||||
@@ -0,0 +1,155 @@
|
||||
import * as fs from "fs";
|
||||
import logger from "./logger";
|
||||
import * as yaml from "js-yaml";
|
||||
import { promiseExec } from "./promise-exec";
|
||||
import { helmCli } from "./helm-cli";
|
||||
|
||||
type HelmEnv = {
|
||||
[key: string]: string | undefined;
|
||||
}
|
||||
|
||||
export type HelmRepo = {
|
||||
name: string;
|
||||
url: string;
|
||||
cacheFilePath?: string;
|
||||
}
|
||||
|
||||
export class HelmRepoManager {
|
||||
private static instance: HelmRepoManager;
|
||||
public static cache = {}
|
||||
protected helmEnv: HelmEnv
|
||||
protected initialized: boolean
|
||||
|
||||
static getInstance(): HelmRepoManager {
|
||||
if(!HelmRepoManager.instance) {
|
||||
HelmRepoManager.instance = new HelmRepoManager()
|
||||
}
|
||||
return HelmRepoManager.instance;
|
||||
}
|
||||
|
||||
private constructor() {
|
||||
// use singleton getInstance()
|
||||
}
|
||||
|
||||
public async init() {
|
||||
const helm = await helmCli.binaryPath()
|
||||
if (!this.initialized) {
|
||||
this.helmEnv = await this.parseHelmEnv()
|
||||
await this.update()
|
||||
this.initialized = true
|
||||
}
|
||||
}
|
||||
|
||||
protected async parseHelmEnv() {
|
||||
const helm = await helmCli.binaryPath()
|
||||
const { stdout } = await promiseExec(`"${helm}" env`).catch((error) => { throw(error.stderr)})
|
||||
const lines = stdout.split(/\r?\n/) // split by new line feed
|
||||
const env: HelmEnv = {}
|
||||
lines.forEach((line: string) => {
|
||||
const [key, value] = line.split("=")
|
||||
if (key && value) {
|
||||
env[key] = value.replace(/"/g, "") // strip quotas
|
||||
}
|
||||
})
|
||||
return env
|
||||
}
|
||||
|
||||
public async repositories(): Promise<Array<HelmRepo>> {
|
||||
if(!this.initialized) {
|
||||
await this.init()
|
||||
}
|
||||
const repositoryFilePath = this.helmEnv.HELM_REPOSITORY_CONFIG
|
||||
const repoFile = await fs.promises.readFile(repositoryFilePath, 'utf8').catch(async (error) => {
|
||||
return null
|
||||
})
|
||||
if(!repoFile) {
|
||||
await this.addRepo({ name: "stable", url: "https://kubernetes-charts.storage.googleapis.com/" })
|
||||
return await this.repositories()
|
||||
}
|
||||
try {
|
||||
const repositories = yaml.safeLoad(repoFile)
|
||||
const result = repositories['repositories'].map((repository: HelmRepo) => {
|
||||
return {
|
||||
name: repository.name,
|
||||
url: repository.url,
|
||||
cacheFilePath: `${this.helmEnv.HELM_REPOSITORY_CACHE}/${repository['name']}-index.yaml`
|
||||
}
|
||||
});
|
||||
if (result.length == 0) {
|
||||
await this.addRepo({ name: "stable", url: "https://kubernetes-charts.storage.googleapis.com/" })
|
||||
return await this.repositories()
|
||||
}
|
||||
return result
|
||||
} catch (error) {
|
||||
logger.debug(error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
public async repository(name: string) {
|
||||
const repositories = await this.repositories()
|
||||
return repositories.find((repo: HelmRepo) => {
|
||||
return repo.name == name
|
||||
})
|
||||
}
|
||||
|
||||
public async update() {
|
||||
const helm = await helmCli.binaryPath()
|
||||
logger.debug(`${helm} repo update`)
|
||||
|
||||
const {stdout } = await promiseExec(`"${helm}" repo update`).catch((error) => { return { stdout: error.stdout } })
|
||||
return stdout
|
||||
}
|
||||
|
||||
protected async addRepositories(repositories: HelmRepo[]){
|
||||
const currentRepositories = await this.repositories()
|
||||
repositories.forEach(async (repo: HelmRepo) => {
|
||||
try {
|
||||
const repoExists = currentRepositories.find((currentRepo: HelmRepo) => {
|
||||
return currentRepo.url == repo.url
|
||||
})
|
||||
if(!repoExists) {
|
||||
await this.addRepo(repo)
|
||||
}
|
||||
}
|
||||
catch(error) {
|
||||
logger.error(JSON.stringify(error))
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected async pruneRepositories(repositoriesToKeep: HelmRepo[]) {
|
||||
const repositories = await this.repositories()
|
||||
repositories.filter((repo: HelmRepo) => {
|
||||
return repositoriesToKeep.find((repoToKeep: HelmRepo) => {
|
||||
return repo.name == repoToKeep.name
|
||||
}) === undefined
|
||||
}).forEach(async (repo: HelmRepo) => {
|
||||
try {
|
||||
const output = await this.removeRepo(repo)
|
||||
logger.debug(output)
|
||||
} catch(error) {
|
||||
logger.error(error)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
public async addRepo(repository: HelmRepo) {
|
||||
const helm = await helmCli.binaryPath()
|
||||
logger.debug(`${helm} repo add ${repository.name} ${repository.url}`)
|
||||
|
||||
const {stdout } = await promiseExec(`"${helm}" repo add ${repository.name} ${repository.url}`).catch((error) => { throw(error.stderr)})
|
||||
return stdout
|
||||
}
|
||||
|
||||
public async removeRepo(repository: HelmRepo): Promise<string> {
|
||||
const helm = await helmCli.binaryPath()
|
||||
logger.debug(`${helm} repo remove ${repository.name} ${repository.url}`)
|
||||
|
||||
const { stdout, stderr } = await promiseExec(`"${helm}" repo remove ${repository.name}`).catch((error) => { throw(error.stderr)})
|
||||
return stdout
|
||||
}
|
||||
}
|
||||
|
||||
export const repoManager = HelmRepoManager.getInstance()
|
||||
@@ -0,0 +1,105 @@
|
||||
import { Cluster } from "./cluster";
|
||||
import logger from "./logger";
|
||||
import { repoManager } from "./helm-repo-manager";
|
||||
import { HelmChartManager } from "./helm-chart-manager";
|
||||
import { releaseManager } from "./helm-release-manager";
|
||||
|
||||
class HelmService {
|
||||
public async installChart(cluster: Cluster, data: {chart: string; values: {}; name: string; namespace: string; version: string}) {
|
||||
const installResult = await releaseManager.installChart(data.chart, data.values, data.name, data.namespace, data.version, cluster.kubeconfigPath())
|
||||
return installResult
|
||||
}
|
||||
|
||||
public async listCharts() {
|
||||
const charts: any = {}
|
||||
await repoManager.init()
|
||||
const repositories = await repoManager.repositories()
|
||||
for (const repo of repositories) {
|
||||
charts[repo.name] = {}
|
||||
const manager = new HelmChartManager(repo)
|
||||
let entries = await manager.charts()
|
||||
entries = this.excludeDeprecated(entries)
|
||||
for(const key in entries) {
|
||||
entries[key] = entries[key][0]
|
||||
}
|
||||
charts[repo.name] = entries
|
||||
}
|
||||
return charts
|
||||
}
|
||||
|
||||
public async getChart(repoName: string, chartName: string, version = "") {
|
||||
const result = {
|
||||
readme: "",
|
||||
versions: {}
|
||||
}
|
||||
const repo = await repoManager.repository(repoName)
|
||||
const chartManager = new HelmChartManager(repo)
|
||||
const chart = await chartManager.chart(chartName)
|
||||
result.readme = await chartManager.getReadme(chartName, version)
|
||||
result.versions = chart
|
||||
return result
|
||||
}
|
||||
|
||||
public async getChartValues(repoName: string, chartName: string, version = "") {
|
||||
const repo = await repoManager.repository(repoName)
|
||||
const chartManager = new HelmChartManager(repo)
|
||||
return chartManager.getValues(chartName, version)
|
||||
}
|
||||
|
||||
public async listReleases(cluster: Cluster, namespace: string = null) {
|
||||
await repoManager.init()
|
||||
const releases = await releaseManager.listReleases(cluster.kubeconfigPath(), namespace)
|
||||
return releases
|
||||
}
|
||||
|
||||
public async getRelease(cluster: Cluster, releaseName: string, namespace: string) {
|
||||
logger.debug("Fetch release")
|
||||
const release = await releaseManager.getRelease(releaseName, namespace, cluster)
|
||||
return release
|
||||
}
|
||||
|
||||
public async getReleaseValues(cluster: Cluster, releaseName: string, namespace: string) {
|
||||
logger.debug("Fetch release values")
|
||||
const values = await releaseManager.getValues(releaseName, namespace, cluster.kubeconfigPath())
|
||||
return values
|
||||
}
|
||||
|
||||
public async getReleaseHistory(cluster: Cluster, releaseName: string, namespace: string) {
|
||||
logger.debug("Fetch release history")
|
||||
const history = await releaseManager.getHistory(releaseName, namespace, cluster.kubeconfigPath())
|
||||
return(history)
|
||||
}
|
||||
|
||||
public async deleteRelease(cluster: Cluster, releaseName: string, namespace: string) {
|
||||
logger.debug("Delete release")
|
||||
const release = await releaseManager.deleteRelease(releaseName, namespace, cluster.kubeconfigPath())
|
||||
return release
|
||||
}
|
||||
|
||||
public async updateRelease(cluster: Cluster, releaseName: string, namespace: string, data: {chart: string; values: {}; version: string}) {
|
||||
logger.debug("Upgrade release")
|
||||
const release = await releaseManager.upgradeRelease(releaseName, data.chart, data.values, namespace, data.version, cluster)
|
||||
return release
|
||||
}
|
||||
|
||||
public async rollback(cluster: Cluster, releaseName: string, namespace: string, revision: number) {
|
||||
logger.debug("Rollback release")
|
||||
const output = await releaseManager.rollback(releaseName, namespace, revision, cluster.kubeconfigPath())
|
||||
return({ message: output })
|
||||
}
|
||||
|
||||
protected excludeDeprecated(entries: any) {
|
||||
for(const key in entries) {
|
||||
entries[key] = entries[key].filter((entry: any) => {
|
||||
if(Array.isArray(entry)) {
|
||||
return entry[0]['deprecated'] != true
|
||||
}
|
||||
return entry["deprecated"] != true
|
||||
})
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const helmService = new HelmService()
|
||||
@@ -0,0 +1,127 @@
|
||||
import "../common/system-ca"
|
||||
import { app, dialog, protocol } from "electron"
|
||||
import { PromiseIpc } from "electron-promise-ipc"
|
||||
import * as path from "path"
|
||||
import { format as formatUrl } from "url"
|
||||
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 { tracker } from "./tracker"
|
||||
import { ClusterManager } from "./cluster-manager";
|
||||
import AppUpdater from "./app-updater"
|
||||
import { shellSync } from "./shell-sync"
|
||||
import { getFreePort } from "./port"
|
||||
import { mangleProxyEnv } from "./proxy-env"
|
||||
import { findMainWebContents } from "./webcontents"
|
||||
import { HelmRepoManager, HelmRepo } from "./helm-repo-manager"
|
||||
|
||||
mangleProxyEnv()
|
||||
if (app.commandLine.getSwitchValue("proxy-server") !== "") {
|
||||
process.env.HTTPS_PROXY = app.commandLine.getSwitchValue("proxy-server")
|
||||
}
|
||||
const isDevelopment = process.env.NODE_ENV !== "production"
|
||||
const promiseIpc = new PromiseIpc({ timeout: 2000 })
|
||||
|
||||
let windowManager: WindowManager = null;
|
||||
let clusterManager: ClusterManager = null;
|
||||
const vmURL = (isDevelopment) ? `http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}` : formatUrl({
|
||||
pathname: path.join(__dirname, "index.html"),
|
||||
protocol: "file",
|
||||
slashes: true,
|
||||
})
|
||||
|
||||
async function main() {
|
||||
await shellSync()
|
||||
|
||||
const updater = new AppUpdater()
|
||||
updater.start();
|
||||
|
||||
tracker.event("app", "start");
|
||||
protocol.registerFileProtocol('store', (request, callback) => {
|
||||
const url = request.url.substr(8)
|
||||
callback( path.normalize(`${app.getPath("userData")}/${url}`) )
|
||||
}, (error) => {
|
||||
if (error) console.error('Failed to register protocol')
|
||||
})
|
||||
let port: number = null
|
||||
// find free port
|
||||
try {
|
||||
port = await getFreePort(49152, 65535)
|
||||
} catch (error) {
|
||||
logger.error(error)
|
||||
await dialog.showErrorBox("Lens Error", "Could not find a free port for the cluster proxy")
|
||||
app.quit();
|
||||
}
|
||||
|
||||
// create cluster manager
|
||||
clusterManager = new ClusterManager(clusterStore.getAllClusterObjects(), port)
|
||||
|
||||
// run proxy
|
||||
try {
|
||||
proxy.listen(port, clusterManager)
|
||||
} catch (error) {
|
||||
logger.error(`Could not start proxy (127.0.0:${port}): ${error.message}`)
|
||||
await dialog.showErrorBox("Lens Error", `Could not start proxy (127.0.0:${port}): ${error.message || "unknown error"}`)
|
||||
app.quit();
|
||||
}
|
||||
|
||||
// boot windowmanager
|
||||
windowManager = new WindowManager();
|
||||
windowManager.showMain(vmURL)
|
||||
|
||||
initMenu({
|
||||
logoutHook: async () => {
|
||||
// IPC send needs webContents as we're sending it to renderer
|
||||
promiseIpc.send('logout', findMainWebContents(), {}).then((data: any) => {
|
||||
logger.debug("logout IPC sent");
|
||||
})
|
||||
},
|
||||
showPreferencesHook: async () => {
|
||||
// IPC send needs webContents as we're sending it to renderer
|
||||
promiseIpc.send('navigate', findMainWebContents(), {name: 'preferences-page'}).then((data: any) => {
|
||||
logger.debug("navigate: preferences IPC sent");
|
||||
})
|
||||
},
|
||||
addClusterHook: async () => {
|
||||
promiseIpc.send('navigate', findMainWebContents(), { name: "add-cluster-page" }).then((data: any) => {
|
||||
logger.debug("navigate: add-cluster-page IPC sent");
|
||||
})
|
||||
},
|
||||
showWhatsNewHook: async () => {
|
||||
promiseIpc.send('navigate', findMainWebContents(), { name: "whats-new-page" }).then((data: any) => {
|
||||
logger.debug("navigate: whats-new-page IPC sent");
|
||||
})
|
||||
},
|
||||
clusterSettingsHook: async () => {
|
||||
promiseIpc.send('navigate', findMainWebContents(), { name: "cluster-settings-page" }).then((data: any) => {
|
||||
logger.debug("navigate: cluster-settings-page IPC sent");
|
||||
})
|
||||
},
|
||||
}, promiseIpc);
|
||||
}
|
||||
|
||||
app.on("ready", main)
|
||||
app.on('window-all-closed', function() {
|
||||
// On OS X it is common for applications and their menu bar
|
||||
// to stay active until the user quits explicitly with Cmd + Q
|
||||
if (process.platform != 'darwin') {
|
||||
app.quit();
|
||||
} else {
|
||||
windowManager = null
|
||||
if (clusterManager) clusterManager.stop()
|
||||
}
|
||||
})
|
||||
app.on("activate", () => {
|
||||
if (!windowManager) {
|
||||
logger.debug("activate main window")
|
||||
windowManager = new WindowManager(false)
|
||||
windowManager.showMain(vmURL)
|
||||
}
|
||||
})
|
||||
app.on("will-quit", async (event) => {
|
||||
event.preventDefault(); // To allow mixpanel sending to be executed
|
||||
if (clusterManager) clusterManager.stop()
|
||||
app.exit(0);
|
||||
})
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
import * as k8s from "@kubernetes/client-node"
|
||||
import * as os from "os"
|
||||
import { all } from "q";
|
||||
import * as yaml from "js-yaml"
|
||||
import { V1beta1ValidatingWebhookConfiguration } from "@kubernetes/client-node";
|
||||
import logger from "./logger";
|
||||
|
||||
const kc = new k8s.KubeConfig()
|
||||
|
||||
function resolveTilde(filePath: string) {
|
||||
if (filePath[0] === "~" && (filePath[1] === "/" || filePath.length === 1)) {
|
||||
return filePath.replace("~", os.homedir());
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
export function loadConfig(kubeconfig: string): k8s.KubeConfig {
|
||||
if (kubeconfig) {
|
||||
kc.loadFromFile(resolveTilde(kubeconfig))
|
||||
} else {
|
||||
kc.loadFromDefault();
|
||||
}
|
||||
return kc
|
||||
}
|
||||
|
||||
/**
|
||||
* KubeConfig is valid when there's atleast one of each defined:
|
||||
* - User
|
||||
* - Cluster
|
||||
* - Context
|
||||
*
|
||||
* @param config KubeConfig to check
|
||||
*/
|
||||
export function valideConfig(config: k8s.KubeConfig): boolean {
|
||||
logger.debug(`validating kube config: ${JSON.stringify(config)}`)
|
||||
if(!config.users || config.users.length == 0) {
|
||||
throw new Error("No users provided in config")
|
||||
}
|
||||
|
||||
if(!config.clusters || config.clusters.length == 0) {
|
||||
throw new Error("No clusters provided in config")
|
||||
}
|
||||
|
||||
if(!config.contexts || config.contexts.length == 0) {
|
||||
throw new Error("No contexts provided in config")
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Breaks kube config into several configs. Each context as it own KubeConfig object
|
||||
*
|
||||
* @param configString yaml string of kube config
|
||||
*/
|
||||
export function splitConfig(kubeConfig: k8s.KubeConfig): k8s.KubeConfig[] {
|
||||
const configs: k8s.KubeConfig[] = []
|
||||
if(!kubeConfig.contexts) {
|
||||
return configs;
|
||||
}
|
||||
kubeConfig.contexts.forEach(ctx => {
|
||||
const kc = new k8s.KubeConfig();
|
||||
kc.clusters = [kubeConfig.getCluster(ctx.cluster)].filter(n => n);
|
||||
kc.users = [kubeConfig.getUser(ctx.user)].filter(n => n)
|
||||
kc.contexts = [kubeConfig.getContextObject(ctx.name)].filter(n => n)
|
||||
kc.setCurrentContext(ctx.name);
|
||||
|
||||
configs.push(kc);
|
||||
});
|
||||
return configs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads KubeConfig from a yaml string and breaks it into several configs. Each context
|
||||
*
|
||||
* @param configString yaml string of kube config
|
||||
*/
|
||||
export function loadAndSplitConfig(configString: string): k8s.KubeConfig[] {
|
||||
const allConfigs = new k8s.KubeConfig();
|
||||
allConfigs.loadFromString(configString);
|
||||
return splitConfig(allConfigs);
|
||||
}
|
||||
|
||||
export function dumpConfigYaml(kc: k8s.KubeConfig): string {
|
||||
const config = {
|
||||
apiVersion: "v1",
|
||||
kind: "Config",
|
||||
preferences: {},
|
||||
'current-context': kc.currentContext,
|
||||
clusters: kc.clusters.map(c => {
|
||||
return {
|
||||
name: c.name,
|
||||
cluster: {
|
||||
'certificate-authority-data': c.caData,
|
||||
'certificate-authority': c.caFile,
|
||||
server: c.server,
|
||||
'insecure-skip-tls-verify': c.skipTLSVerify
|
||||
}
|
||||
}
|
||||
}),
|
||||
contexts: kc.contexts.map(c => {
|
||||
return {
|
||||
name: c.name,
|
||||
context: {
|
||||
cluster: c.cluster,
|
||||
user: c.user,
|
||||
namespace: c.namespace
|
||||
}
|
||||
}
|
||||
}),
|
||||
users: kc.users.map(u => {
|
||||
return {
|
||||
name: u.name,
|
||||
user: {
|
||||
'client-certificate-data': u.certData,
|
||||
'client-certificate': u.certFile,
|
||||
'client-key-data': u.keyData,
|
||||
'client-key': u.keyFile,
|
||||
'auth-provider': u.authProvider,
|
||||
exec: u.exec,
|
||||
token: u.token,
|
||||
username: u.username,
|
||||
password: u.password
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
console.log("dumping kc:", config);
|
||||
|
||||
// skipInvalid: true makes dump ignore undefined values
|
||||
return yaml.safeDump(config, {skipInvalid: true});
|
||||
}
|
||||
|
||||
export function podHasIssues(pod: k8s.V1Pod) {
|
||||
// Logic adapted from dashboard
|
||||
const notReady = !!pod.status.conditions.find(condition => {
|
||||
return condition.type == "Ready" && condition.status !== "True"
|
||||
});
|
||||
|
||||
return (
|
||||
notReady ||
|
||||
pod.status.phase !== "Running" ||
|
||||
pod.spec.priority > 500000 // We're interested in high prio pods events regardless of their running status
|
||||
)
|
||||
}
|
||||
|
||||
// Logic adapted from dashboard
|
||||
// see: https://github.com/kontena/kontena-k8s-dashboard/blob/7d8f9cb678cc817a22dd1886c5e79415b212b9bf/client/api/endpoints/nodes.api.ts#L147
|
||||
export function getNodeWarningConditions(node: k8s.V1Node) {
|
||||
return node.status.conditions.filter(c =>
|
||||
c.status.toLowerCase() === "true" && c.type !== "Ready" && c.type !== "HostUpgrades"
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import { spawn, ChildProcess } from "child_process"
|
||||
import logger from "./logger"
|
||||
import * as tcpPortUsed from "tcp-port-used"
|
||||
import { Kubectl, bundledKubectl } from "./kubectl"
|
||||
import { Cluster } from "./cluster"
|
||||
import { readFileSync, watch } from "fs"
|
||||
import { PromiseIpc } from "electron-promise-ipc"
|
||||
import { findMainWebContents } from "./webcontents"
|
||||
import * as url from "url"
|
||||
|
||||
export class KubeAuthProxy {
|
||||
public lastError: string
|
||||
|
||||
protected cluster: Cluster
|
||||
protected env: NodeJS.ProcessEnv = null
|
||||
protected proxyProcess: ChildProcess
|
||||
protected port: number
|
||||
protected kubectl: Kubectl
|
||||
protected promiseIpc: any
|
||||
|
||||
constructor(cluster: Cluster, port: number, env: NodeJS.ProcessEnv) {
|
||||
this.env = env
|
||||
this.port = port
|
||||
this.cluster = cluster
|
||||
this.kubectl = bundledKubectl
|
||||
this.promiseIpc = new PromiseIpc({ timeout: 2000 })
|
||||
}
|
||||
|
||||
public async run(): Promise<void> {
|
||||
if (this.proxyProcess) {
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve()
|
||||
})
|
||||
}
|
||||
const proxyBin = await this.kubectl.kubectlPath()
|
||||
const configWatcher = watch(this.cluster.kubeconfigPath(), (eventType: string, filename: string) => {
|
||||
if (eventType === "change") {
|
||||
const kc = readFileSync(this.cluster.kubeconfigPath()).toString()
|
||||
if (kc.trim().length > 0) { // Prevent updating empty configs back to store
|
||||
this.cluster.updateKubeconfig(kc)
|
||||
} else {
|
||||
logger.warn(`kubeconfig watch on ${this.cluster.kubeconfigPath()} resulted into empty config, ignoring...`)
|
||||
}
|
||||
}
|
||||
})
|
||||
configWatcher.on("error", () => {})
|
||||
const clusterUrl = url.parse(this.cluster.apiUrl)
|
||||
let args = [
|
||||
"proxy",
|
||||
"-p", this.port.toString(),
|
||||
"--kubeconfig", this.cluster.kubeconfigPath(),
|
||||
"--accept-hosts", clusterUrl.hostname,
|
||||
]
|
||||
if (process.env.DEBUG_PROXY === "true") {
|
||||
args = args.concat(["-v", "9"])
|
||||
}
|
||||
this.proxyProcess = spawn(proxyBin, args, {
|
||||
env: this.env
|
||||
})
|
||||
this.proxyProcess.on("exit", (code) => {
|
||||
logger.error(`proxy ${this.cluster.contextName} exited with code ${code}`)
|
||||
this.sendIpcLogMessage( `proxy exited with code ${code}`, "stderr")
|
||||
this.proxyProcess = null
|
||||
configWatcher.close()
|
||||
})
|
||||
this.proxyProcess.stdout.on('data', (data) => {
|
||||
let logItem = data.toString()
|
||||
if (logItem.startsWith("Starting to serve on")) {
|
||||
logItem = "Authentication proxy started\n"
|
||||
}
|
||||
logger.debug(`proxy ${this.cluster.contextName} stdout: ${logItem}`)
|
||||
this.sendIpcLogMessage(logItem, "stdout")
|
||||
})
|
||||
this.proxyProcess.stderr.on('data', (data) => {
|
||||
this.lastError = this.parseError(data.toString())
|
||||
logger.debug(`proxy ${this.cluster.contextName} stderr: ${data}`)
|
||||
this.sendIpcLogMessage(data.toString(), "stderr")
|
||||
})
|
||||
|
||||
return tcpPortUsed.waitUntilUsed(this.port, 500, 10000)
|
||||
}
|
||||
|
||||
protected parseError(data: string) {
|
||||
const error = data.split("http: proxy error:").slice(1).join("").trim()
|
||||
let errorMsg = error
|
||||
const jsonError = error.split("Response: ")[1]
|
||||
if (jsonError) {
|
||||
try {
|
||||
const parsedError = JSON.parse(jsonError)
|
||||
errorMsg = parsedError.error_description || parsedError.error || jsonError
|
||||
} catch(_) {
|
||||
errorMsg = jsonError.trim()
|
||||
}
|
||||
}
|
||||
return errorMsg
|
||||
}
|
||||
|
||||
protected sendIpcLogMessage(data: string, stream: string) {
|
||||
this.promiseIpc.send(`kube-auth:${this.cluster.id}`, findMainWebContents(), { data: data, stream: stream })
|
||||
}
|
||||
|
||||
public exit() {
|
||||
if (this.proxyProcess) {
|
||||
logger.debug(`Stopping local proxy: ${this.cluster.contextName}`)
|
||||
this.proxyProcess.kill()
|
||||
this.proxyProcess = null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { app } from "electron"
|
||||
import * as fs from "fs"
|
||||
import { ensureDir, randomFileName} from "./file-helpers"
|
||||
import logger from "./logger"
|
||||
|
||||
export class KubeconfigManager {
|
||||
protected configDir = app.getPath("temp")
|
||||
protected kubeconfig: string
|
||||
protected tempFile: string
|
||||
|
||||
constructor(kubeconfig: string) {
|
||||
this.kubeconfig = kubeconfig
|
||||
this.tempFile = this.createTemporaryKubeconfig()
|
||||
}
|
||||
|
||||
public getPath() {
|
||||
return this.tempFile
|
||||
}
|
||||
|
||||
protected createTemporaryKubeconfig(): string {
|
||||
ensureDir(this.configDir)
|
||||
const path = `${this.configDir}/${randomFileName("kubeconfig")}`
|
||||
logger.debug('Creating temporary kubeconfig: ' + path)
|
||||
fs.writeFileSync(path, this.kubeconfig)
|
||||
return path
|
||||
}
|
||||
|
||||
public unlink() {
|
||||
logger.debug('Deleting temporary kubeconfig: ' + this.tempFile)
|
||||
fs.unlinkSync(this.tempFile)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
import { app, remote } from "electron"
|
||||
import * as path from "path"
|
||||
import * as fs from "fs"
|
||||
import * as request from "request"
|
||||
import * as requestPromise from "request-promise-native"
|
||||
import logger from "./logger"
|
||||
import { ensureDir, pathExists } from "fs-extra"
|
||||
import * as md5File from "md5-file"
|
||||
import { globalRequestOpts } from "../common/request"
|
||||
import * as lockFile from "proper-lockfile"
|
||||
import { helmCli } from "./helm-cli"
|
||||
|
||||
const kubectlMap: Map<string, string> = new Map([
|
||||
["1.7", "1.8.15"],
|
||||
["1.8", "1.9.10"],
|
||||
["1.9", "1.10.13"],
|
||||
["1.10", "1.11.10"],
|
||||
["1.11", "1.12.10"],
|
||||
["1.12", "1.13.12"],
|
||||
["1.13", "1.13.12"],
|
||||
["1.14", "1.14.10"],
|
||||
["1.15", "1.15.10"],
|
||||
["1.16", "1.16.7"],
|
||||
["1.17", "1.17.3"],
|
||||
["1.18", "1.18.0-beta.1"]
|
||||
])
|
||||
|
||||
const initScriptVersionString = "# lens-initscript v3\n"
|
||||
|
||||
const isDevelopment = process.env.NODE_ENV !== "production"
|
||||
let bundledPath: string = null
|
||||
|
||||
if(isDevelopment) {
|
||||
bundledPath = path.join(process.cwd(), "binaries", "client", process.platform, process.arch, "kubectl")
|
||||
} else {
|
||||
bundledPath = path.join(process.resourcesPath, process.arch, "kubectl")
|
||||
}
|
||||
|
||||
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")
|
||||
public static readonly bundledKubectlPath = bundledPath
|
||||
public static readonly bundledKubectlVersion: string = require("../../package.json").config.bundledKubectlVersion
|
||||
private static bundledInstance: Kubectl;
|
||||
|
||||
// Returns the single bundled Kubectl instance
|
||||
public static bundled() {
|
||||
if(!Kubectl.bundledInstance) Kubectl.bundledInstance = new Kubectl(Kubectl.bundledKubectlVersion)
|
||||
return Kubectl.bundledInstance
|
||||
}
|
||||
|
||||
constructor(clusterVersion: string) {
|
||||
const versionParts = /^v?(\d+\.\d+)(.*)/.exec(clusterVersion)
|
||||
const minorVersion = versionParts[1]
|
||||
/* minorVersion is the first two digits of kube server version
|
||||
if the version map includes that, use that version, if not, fallback to the exact x.y.z of kube version */
|
||||
if(kubectlMap.has(minorVersion)) {
|
||||
this.kubectlVersion = kubectlMap.get(minorVersion)
|
||||
logger.debug("Set kubectl version " + this.kubectlVersion + " for cluster version " + clusterVersion + " using version map")
|
||||
} else {
|
||||
this.kubectlVersion = versionParts[1] + versionParts[2]
|
||||
logger.debug("Set kubectl version " + this.kubectlVersion + " for cluster version " + clusterVersion + " using fallback")
|
||||
}
|
||||
|
||||
let arch = null
|
||||
|
||||
if(process.arch == "x64") {
|
||||
arch = "amd64"
|
||||
} else if(process.arch == "x86" || process.arch == "ia32") {
|
||||
arch = "386"
|
||||
} else {
|
||||
arch = process.arch
|
||||
}
|
||||
|
||||
const platformName = process.platform === "win32" ? "windows" : process.platform
|
||||
const binaryName = process.platform === "win32" ? "kubectl.exe" : "kubectl"
|
||||
|
||||
this.url = `https://storage.googleapis.com/kubernetes-release/release/v${this.kubectlVersion}/bin/${platformName}/${arch}/${binaryName}`
|
||||
|
||||
this.dirname = path.normalize(path.join(Kubectl.kubectlDir, this.kubectlVersion))
|
||||
this.path = path.join(this.dirname, binaryName)
|
||||
}
|
||||
|
||||
public async kubectlPath(): Promise<string> {
|
||||
try {
|
||||
await this.ensureKubectl()
|
||||
return this.path
|
||||
} catch(err) {
|
||||
logger.error("Failed to ensure kubectl, fallback to the bundled version")
|
||||
logger.error(err)
|
||||
return Kubectl.bundledKubectlPath
|
||||
}
|
||||
}
|
||||
|
||||
public async binDir() {
|
||||
try {
|
||||
await this.ensureKubectl()
|
||||
return this.dirname
|
||||
} catch(err) {
|
||||
logger.error(err)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
protected async urlEtag() {
|
||||
const response = await requestPromise({
|
||||
method: "HEAD",
|
||||
uri: this.url,
|
||||
resolveWithFullResponse: true,
|
||||
timeout: 4000,
|
||||
...this.getRequestOpts()
|
||||
}).catch((error) => { logger.error(error) })
|
||||
|
||||
if (response && response.headers["etag"]) {
|
||||
return response.headers["etag"].replace(/"/g, "")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
public async checkBinary(checkTag = true) {
|
||||
const exists = await pathExists(this.path)
|
||||
if (exists) {
|
||||
if (!checkTag) {
|
||||
return true
|
||||
}
|
||||
const hash = md5File.sync(this.path)
|
||||
const etag = await this.urlEtag()
|
||||
if (etag === "") {
|
||||
logger.debug("Cannot resolve kubectl remote etag")
|
||||
return true
|
||||
}
|
||||
if (hash == etag) {
|
||||
logger.debug("Kubectl md5sum matches the remote etag")
|
||||
return true
|
||||
}
|
||||
|
||||
logger.error("Kubectl md5sum " + hash + " does not match the remote etag " + etag + ", unlinking and downloading again")
|
||||
await fs.promises.unlink(this.path)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
protected async checkBundled(): Promise<boolean> {
|
||||
if(this.kubectlVersion === Kubectl.bundledKubectlVersion) {
|
||||
try {
|
||||
const exist = await pathExists(this.path)
|
||||
if (!exist) {
|
||||
await fs.promises.copyFile(Kubectl.bundledKubectlPath, this.path)
|
||||
await fs.promises.chmod(this.path, 0o755)
|
||||
}
|
||||
return true
|
||||
} catch(err) {
|
||||
logger.error("Could not copy the bundled kubectl to app-data: " + err)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
protected async ensureKubectl(): Promise<boolean> {
|
||||
await ensureDir(this.dirname, 0o755)
|
||||
return lockFile.lock(this.dirname).then(async (release) => {
|
||||
logger.debug(`Acquired a lock for ${this.kubectlVersion}`)
|
||||
const bundled = await this.checkBundled()
|
||||
const isValid = await this.checkBinary(!bundled)
|
||||
if(!isValid) {
|
||||
await this.downloadKubectl().catch((error) => { logger.error(error) });
|
||||
}
|
||||
await this.writeInitScripts().catch((error) => { logger.error("Failed to write init scripts"); logger.error(error) })
|
||||
logger.debug(`Releasing lock for ${this.kubectlVersion}`)
|
||||
release()
|
||||
return true
|
||||
}).catch((e) => {
|
||||
logger.error(`Failed to get a lock for ${this.kubectlVersion}`)
|
||||
logger.error(e)
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
public async downloadKubectl() {
|
||||
await ensureDir(path.dirname(this.path), 0o755)
|
||||
|
||||
logger.info(`Downloading kubectl ${this.kubectlVersion} from ${this.url} to ${this.path}`)
|
||||
return new Promise((resolve, reject) => {
|
||||
const stream = request({
|
||||
gzip: true,
|
||||
...this.getRequestOpts()
|
||||
})
|
||||
const file = fs.createWriteStream(this.path)
|
||||
stream.on("complete", () => {
|
||||
logger.debug("kubectl binary download finished")
|
||||
file.end(() => {})
|
||||
})
|
||||
stream.on("error", (error) => {
|
||||
logger.error(error)
|
||||
fs.unlink(this.path, () => {})
|
||||
reject(error)
|
||||
})
|
||||
file.on("close", () => {
|
||||
logger.debug("kubectl binary download closed")
|
||||
fs.chmod(this.path, 0o755, () => {})
|
||||
resolve()
|
||||
})
|
||||
stream.pipe(file)
|
||||
})
|
||||
}
|
||||
|
||||
protected async scriptIsLatest(scriptPath: string) {
|
||||
const scriptExists = await pathExists(scriptPath)
|
||||
if(!scriptExists) return false
|
||||
|
||||
try {
|
||||
const filehandle = await fs.promises.open(scriptPath, 'r')
|
||||
const buffer = Buffer.alloc(40)
|
||||
await filehandle.read(buffer, 0, 40, 0)
|
||||
await filehandle.close()
|
||||
return buffer.toString().startsWith(initScriptVersionString)
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
protected async writeInitScripts() {
|
||||
const helmPath = helmCli.getBinaryDir()
|
||||
const fsPromises = fs.promises;
|
||||
const bashScriptPath = path.join(this.dirname, '.bash_set_path')
|
||||
const bashScriptIsLatest = await this.scriptIsLatest(bashScriptPath)
|
||||
if(!bashScriptIsLatest) {
|
||||
let bashScript = "" + initScriptVersionString
|
||||
bashScript += "tempkubeconfig=\"$KUBECONFIG\"\n"
|
||||
bashScript += "test -f \"/etc/profile\" && . \"/etc/profile\"\n"
|
||||
bashScript += "if test -f \"$HOME/.bash_profile\"; then\n"
|
||||
bashScript += " . \"$HOME/.bash_profile\"\n"
|
||||
bashScript += "elif test -f \"$HOME/.bash_login\"; then\n"
|
||||
bashScript += " . \"$HOME/.bash_login\"\n"
|
||||
bashScript += "elif test -f \"$HOME/.profile\"; then\n"
|
||||
bashScript += " . \"$HOME/.profile\"\n"
|
||||
bashScript += "fi\n"
|
||||
bashScript += `export PATH="${this.dirname}:${helmPath}:$PATH"\n`
|
||||
bashScript += "export KUBECONFIG=\"$tempkubeconfig\"\n"
|
||||
bashScript += "unset tempkubeconfig\n"
|
||||
await fsPromises.writeFile(bashScriptPath, bashScript.toString(), { mode: 0o644 })
|
||||
}
|
||||
|
||||
const zshScriptPath = path.join(this.dirname, '.zlogin')
|
||||
const zshScriptIsLatest = await this.scriptIsLatest(zshScriptPath)
|
||||
if(!zshScriptIsLatest) {
|
||||
let zshScript = "" + initScriptVersionString
|
||||
|
||||
zshScript += "tempkubeconfig=\"$KUBECONFIG\"\n"
|
||||
// restore previous ZDOTDIR
|
||||
zshScript += "export ZDOTDIR=\"$OLD_ZDOTDIR\"\n"
|
||||
// source all the files
|
||||
zshScript += "test -f \"$OLD_ZDOTDIR/.zshenv\" && . \"$OLD_ZDOTDIR/.zshenv\"\n"
|
||||
zshScript += "test -f \"$OLD_ZDOTDIR/.zprofile\" && . \"$OLD_ZDOTDIR/.zprofile\"\n"
|
||||
zshScript += "test -f \"$OLD_ZDOTDIR/.zlogin\" && . \"$OLD_ZDOTDIR/.zlogin\"\n"
|
||||
zshScript += "test -f \"$OLD_ZDOTDIR/.zshrc\" && . \"$OLD_ZDOTDIR/.zshrc\"\n"
|
||||
|
||||
// voodoo to replace any previous occurences of kubectl path in the PATH
|
||||
zshScript += `kubectlpath=\"${this.dirname}"\n`
|
||||
zshScript += `helmpath=\"${helmPath}"\n`
|
||||
zshScript += "p=\":$kubectlpath:\"\n"
|
||||
zshScript += "d=\":$PATH:\"\n"
|
||||
zshScript += "d=${d//$p/:}\n"
|
||||
zshScript += "d=${d/#:/}\n"
|
||||
zshScript += "export PATH=\"$kubectlpath:$helmpath:${d/%:/}\"\n"
|
||||
zshScript += "export KUBECONFIG=\"$tempkubeconfig\"\n"
|
||||
zshScript += "unset tempkubeconfig\n"
|
||||
zshScript += "unset OLD_ZDOTDIR\n"
|
||||
await fsPromises.writeFile(zshScriptPath, zshScript.toString(), { mode: 0o644 })
|
||||
}
|
||||
}
|
||||
|
||||
protected getRequestOpts() {
|
||||
return globalRequestOpts({
|
||||
url: this.url
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const bundledKubectl = Kubectl.bundled()
|
||||
export { bundledKubectl }
|
||||
@@ -0,0 +1,17 @@
|
||||
import * as http from "http";
|
||||
|
||||
export abstract class LensApi {
|
||||
protected respondJson(res: http.ServerResponse, content: {}, status = 200) {
|
||||
this.respond(res, JSON.stringify(content), "application/json", status)
|
||||
}
|
||||
|
||||
protected respondText(res: http.ServerResponse, content: string, status = 200) {
|
||||
this.respond(res, content, "text/plain", status)
|
||||
}
|
||||
|
||||
protected respond(res: http.ServerResponse, content: string, contentType: string, status = 200) {
|
||||
res.setHeader("Content-Type", contentType)
|
||||
res.statusCode = status
|
||||
res.end(content)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
import { app, remote } from "electron"
|
||||
import * as path from "path"
|
||||
import * as fs from "fs"
|
||||
import * as request from "request"
|
||||
import logger from "./logger"
|
||||
import { ensureDir, pathExists } from "fs-extra"
|
||||
import * as tar from "tar"
|
||||
import { userStore } from "../common/user-store"
|
||||
import { globalRequestOpts} from "../common/request"
|
||||
|
||||
export type LensBinaryOpts = {
|
||||
version: string;
|
||||
originalBinaryName: string;
|
||||
newBinaryName?: string;
|
||||
}
|
||||
export class LensBinary {
|
||||
|
||||
public binaryVersion: string
|
||||
protected directory: string
|
||||
protected url: string
|
||||
protected path: string;
|
||||
protected tarPath: string;
|
||||
protected dirname: string
|
||||
protected binaryName: string
|
||||
protected platformName: string
|
||||
protected arch: string
|
||||
protected originalBinaryName: string
|
||||
|
||||
constructor(opts: LensBinaryOpts) {
|
||||
this.originalBinaryName = opts.originalBinaryName
|
||||
this.binaryName = opts.newBinaryName || opts.originalBinaryName
|
||||
|
||||
this.binaryVersion = opts.version
|
||||
|
||||
let arch = null
|
||||
|
||||
if(process.arch == "x64") {
|
||||
arch = "amd64"
|
||||
} else if(process.arch == "x86" || process.arch == "ia32") {
|
||||
arch = "386"
|
||||
} else {
|
||||
arch = process.arch
|
||||
}
|
||||
this.arch = arch
|
||||
const binaryDir = path.join((app || remote.app).getPath("userData"), "binaries", this.binaryName)
|
||||
this.platformName = process.platform === "win32" ? "windows" : process.platform
|
||||
if (process.platform === "win32") {
|
||||
this.binaryName = this.binaryName+".exe"
|
||||
this.originalBinaryName = this.originalBinaryName+".exe"
|
||||
}
|
||||
|
||||
this.dirname = path.normalize(path.join(binaryDir, this.binaryVersion))
|
||||
const tarName = this.getTarName()
|
||||
if (tarName) {
|
||||
this.tarPath = path.join(this.dirname, tarName)
|
||||
}
|
||||
}
|
||||
|
||||
public async binaryPath() {
|
||||
await this.ensureBinary()
|
||||
return this.getBinaryPath()
|
||||
}
|
||||
|
||||
protected getTarName(): string|null {
|
||||
return null
|
||||
}
|
||||
|
||||
protected getUrl() {
|
||||
return ""
|
||||
}
|
||||
|
||||
protected getBinaryPath() {
|
||||
return ""
|
||||
}
|
||||
|
||||
protected getOriginalBinaryPath() {
|
||||
return ""
|
||||
}
|
||||
|
||||
public getBinaryDir() {
|
||||
return path.dirname(this.getBinaryPath())
|
||||
}
|
||||
|
||||
public async binDir() {
|
||||
try {
|
||||
await this.ensureBinary()
|
||||
return this.dirname
|
||||
} catch(err) {
|
||||
logger.error(err)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
protected async checkBinary() {
|
||||
const exists = await pathExists(this.getBinaryPath())
|
||||
return exists
|
||||
}
|
||||
|
||||
protected async ensureBinary() {
|
||||
const isValid = await this.checkBinary()
|
||||
if(!isValid) {
|
||||
await this.downloadBinary().catch((error) => { logger.error(error) });
|
||||
if (this.tarPath) await this.untarBinary()
|
||||
if(this.originalBinaryName != this.binaryName ) await this.renameBinary()
|
||||
}
|
||||
}
|
||||
|
||||
protected async untarBinary() {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
logger.debug(`Extracting ${this.originalBinaryName} binary`)
|
||||
tar.x({
|
||||
file: this.tarPath,
|
||||
cwd: this.dirname
|
||||
}).then((_ => {
|
||||
resolve()
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
protected async renameBinary() {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
logger.debug(`Renaming ${this.originalBinaryName} binary to ${this.binaryName}`)
|
||||
fs.rename(this.getOriginalBinaryPath(), this.getBinaryPath(), (err) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
public async downloadBinary() {
|
||||
const binaryPath = this.tarPath || this.getBinaryPath()
|
||||
await ensureDir(this.getBinaryDir(), 0o755)
|
||||
|
||||
const file = fs.createWriteStream(binaryPath)
|
||||
const url = this.getUrl()
|
||||
|
||||
logger.info(`Downloading ${this.originalBinaryName} ${this.binaryVersion} from ${url} to ${binaryPath}`)
|
||||
const requestOpts: request.UriOptions & request.CoreOptions = {
|
||||
uri: url,
|
||||
gzip: true
|
||||
}
|
||||
|
||||
const stream = request(globalRequestOpts(requestOpts))
|
||||
|
||||
stream.on("complete", () => {
|
||||
logger.info(`${this.originalBinaryName} binary download finished`)
|
||||
file.end(() => {})
|
||||
})
|
||||
|
||||
stream.on("error", (error) => {
|
||||
logger.error(error)
|
||||
fs.unlink(this.path, () => {})
|
||||
throw(error)
|
||||
})
|
||||
return new Promise((resolve, reject) => {
|
||||
file.on("close", () => {
|
||||
logger.debug(`${this.originalBinaryName} binary download closed`)
|
||||
if(!this.tarPath) fs.chmod(binaryPath, 0o755, () => {})
|
||||
resolve()
|
||||
})
|
||||
stream.pipe(file)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import * as path from "path"
|
||||
import { spawn, ChildProcess } from "child_process"
|
||||
import logger from "./logger"
|
||||
import * as tcpPortUsed from "tcp-port-used"
|
||||
|
||||
declare const __static: string;
|
||||
const isDevelopment = process.env.NODE_ENV !== "production"
|
||||
let serverPath: string = null
|
||||
if (isDevelopment) {
|
||||
serverPath = path.join(process.cwd(), "binaries", "server", process.platform, "lens-server")
|
||||
} else {
|
||||
serverPath = path.join(process.resourcesPath, "lens-server")
|
||||
if (process.platform !== "win32") {
|
||||
serverPath = `${serverPath}.txt`
|
||||
}
|
||||
}
|
||||
if (process.platform === "win32") {
|
||||
serverPath = `${serverPath}-${process.arch}.exe`
|
||||
}
|
||||
|
||||
|
||||
export class LensServer {
|
||||
protected serverUrl: string = null
|
||||
protected env: NodeJS.ProcessEnv = null
|
||||
protected localServer: ChildProcess
|
||||
|
||||
constructor(serverUrl: string, env: NodeJS.ProcessEnv) {
|
||||
this.serverUrl = serverUrl
|
||||
this.env = env
|
||||
}
|
||||
|
||||
public async run(): Promise<void> {
|
||||
if (this.localServer) {
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve()
|
||||
})
|
||||
}
|
||||
this.localServer = spawn(serverPath, [], {
|
||||
env: this.env,
|
||||
cwd: __static
|
||||
})
|
||||
this.localServer.on("exit", (code) => {
|
||||
logger.error(`server ${this.serverUrl} exited with code ${code}`)
|
||||
this.localServer = null
|
||||
})
|
||||
this.localServer.stdout.on('data', (data) => {
|
||||
logger.debug(`server ${this.serverUrl} stdout: ${data}`)
|
||||
})
|
||||
this.localServer.stderr.on('data', (data) => {
|
||||
logger.debug(`server ${this.serverUrl} stderr: ${data}`)
|
||||
})
|
||||
|
||||
return tcpPortUsed.waitUntilUsed(parseInt(this.env.LOCAL_SERVER_PORT), 500, 10000)
|
||||
}
|
||||
|
||||
public exit() {
|
||||
if (this.localServer) {
|
||||
logger.debug(`Stopping local server: ${this.serverUrl}`)
|
||||
this.localServer.kill()
|
||||
this.localServer = null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import * as winston from "winston"
|
||||
|
||||
const options = {
|
||||
colorize: true,
|
||||
handleExceptions: false,
|
||||
json: false,
|
||||
level: process.env.DEBUG === "true" ? "debug" : "info",
|
||||
}
|
||||
|
||||
const logger = winston.createLogger({
|
||||
format: winston.format.combine(
|
||||
winston.format.colorize(),
|
||||
winston.format.simple(),
|
||||
),
|
||||
transports: [
|
||||
new winston.transports.Console(options),
|
||||
],
|
||||
});
|
||||
|
||||
export default logger
|
||||
@@ -0,0 +1,212 @@
|
||||
import {app, dialog, Menu, MenuItemConstructorOptions, shell, webContents, BrowserWindow, MenuItem} from "electron"
|
||||
|
||||
export interface MenuOptions {
|
||||
logoutHook: any;
|
||||
addClusterHook: any;
|
||||
clusterSettingsHook: any;
|
||||
showWhatsNewHook: any;
|
||||
showPreferencesHook: any;
|
||||
// all the above are really () => void type functions
|
||||
}
|
||||
|
||||
function setClusterSettingsEnabled(enabled: boolean) {
|
||||
const isMac = process.platform === 'darwin';
|
||||
const menuIndex = isMac ? 1 : 0
|
||||
Menu.getApplicationMenu().items[menuIndex].submenu.items[1].enabled = enabled
|
||||
}
|
||||
|
||||
function showAbout(_menuitem: MenuItem, browserWindow: BrowserWindow) {
|
||||
const appDetails = [
|
||||
`Version: ${app.getVersion()}`,
|
||||
]
|
||||
appDetails.push(`Copyright 2020 Lakend Labs, Inc.`)
|
||||
let title = "Lens"
|
||||
if (process.platform === "win32") {
|
||||
title = ` ${title}`
|
||||
}
|
||||
dialog.showMessageBoxSync(browserWindow, {
|
||||
title,
|
||||
type: "info",
|
||||
buttons: ["Close"],
|
||||
message: `Lens`,
|
||||
detail: appDetails.join("\r\n")
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the menu based on the example at: https://electronjs.org/docs/api/menu#main-process
|
||||
* Menu items are constructed piece-by-piece to have slightly better control on individual sub-menus
|
||||
*
|
||||
* @param ipc the main promiceIpc handle. Needed to be able to hook IPC sending into logout click handler.
|
||||
*/
|
||||
export default function initMenu(opts: MenuOptions, promiseIpc: any) {
|
||||
const isMac = process.platform === 'darwin';
|
||||
const isDevelopment = process.env.NODE_ENV === 'development';
|
||||
|
||||
const mt: MenuItemConstructorOptions[] = [];
|
||||
const macAppMenu: MenuItemConstructorOptions = {
|
||||
label: app.getName(),
|
||||
submenu: [
|
||||
{
|
||||
label: "About Lens",
|
||||
click: showAbout
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{
|
||||
label: 'Preferences',
|
||||
click: opts.showPreferencesHook,
|
||||
enabled: true
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{ role: 'services' },
|
||||
{ type: 'separator' },
|
||||
{ role: 'hide' },
|
||||
{ role: 'hideOthers' },
|
||||
{ role: 'unhide' },
|
||||
{ type: 'separator' },
|
||||
{ role: 'quit' }
|
||||
]
|
||||
};
|
||||
if(isMac) {
|
||||
mt.push(macAppMenu);
|
||||
}
|
||||
|
||||
let fileMenu: MenuItemConstructorOptions;
|
||||
if(isMac) {
|
||||
fileMenu = {
|
||||
label: 'File',
|
||||
submenu: [{
|
||||
label: 'Add Cluster...',
|
||||
click: opts.addClusterHook,
|
||||
},
|
||||
{
|
||||
label: 'Cluster Settings',
|
||||
click: opts.clusterSettingsHook,
|
||||
enabled: false
|
||||
}
|
||||
]
|
||||
}
|
||||
} else {
|
||||
fileMenu = {
|
||||
label: 'File',
|
||||
submenu: [
|
||||
{
|
||||
label: 'Add Cluster...',
|
||||
click: opts.addClusterHook,
|
||||
},
|
||||
{
|
||||
label: 'Cluster Settings',
|
||||
click: opts.clusterSettingsHook,
|
||||
enabled: false
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{
|
||||
label: 'Preferences',
|
||||
click: opts.showPreferencesHook,
|
||||
enabled: true
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{ role: 'quit' }
|
||||
]
|
||||
}
|
||||
}
|
||||
mt.push(fileMenu);
|
||||
|
||||
const editMenu: MenuItemConstructorOptions = {
|
||||
label: 'Edit',
|
||||
submenu: [
|
||||
{ role: 'undo' },
|
||||
{ role: 'redo' },
|
||||
{ type: 'separator' },
|
||||
{ role: 'cut' },
|
||||
{ role: 'copy' },
|
||||
{ role: 'paste' },
|
||||
{ role: 'delete' },
|
||||
{ type: 'separator' },
|
||||
{ role: 'selectAll' },
|
||||
]
|
||||
};
|
||||
mt.push(editMenu);
|
||||
|
||||
const viewMenu: MenuItemConstructorOptions = {
|
||||
label: 'View',
|
||||
submenu: [
|
||||
{
|
||||
label: 'Back',
|
||||
accelerator: 'CmdOrCtrl+[',
|
||||
click () {
|
||||
webContents.getFocusedWebContents().executeJavaScript('window.history.back()')
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Forward',
|
||||
accelerator: 'CmdOrCtrl+]',
|
||||
click () {
|
||||
webContents.getFocusedWebContents().executeJavaScript('window.history.forward()')
|
||||
}
|
||||
},
|
||||
{ role: 'reload' },
|
||||
{ role: 'forceReload' },
|
||||
...(isDevelopment ? [
|
||||
{ role: 'toggleDevTools'} as MenuItemConstructorOptions,
|
||||
{
|
||||
label: 'Open Dashboard Devtools',
|
||||
click() {
|
||||
webContents.getFocusedWebContents().openDevTools()
|
||||
}
|
||||
}
|
||||
] : []),
|
||||
{ type: 'separator' },
|
||||
{ role: 'resetZoom' },
|
||||
{ role: 'zoomIn' },
|
||||
{ role: 'zoomOut' },
|
||||
{ type: 'separator' },
|
||||
{ role: 'togglefullscreen' }
|
||||
]
|
||||
};
|
||||
mt.push(viewMenu);
|
||||
|
||||
const helpMenu: MenuItemConstructorOptions = {
|
||||
role: 'help',
|
||||
submenu: [
|
||||
{
|
||||
label: 'License',
|
||||
click: async () => {
|
||||
shell.openExternal('https://lakendlabs.com/licenses/lens-eula.md');
|
||||
},
|
||||
},
|
||||
{
|
||||
label: 'Community Slack',
|
||||
click: async () => {
|
||||
shell.openExternal('https://join.slack.com/t/kontenacommunity/shared_invite/enQtOTc5NjAyNjYyOTk4LWU1NDQ0ZGFkOWJkNTRhYTc2YjVmZDdkM2FkNGM5MjhiYTRhMDU2NDQ1MzIyMDA4ZGZlNmExOTc0N2JmY2M3ZGI');
|
||||
},
|
||||
},
|
||||
{
|
||||
label: 'Report an Issue',
|
||||
click: async () => {
|
||||
shell.openExternal('https://github.com/kontena/lens/issues');
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "What's new?",
|
||||
click: opts.showWhatsNewHook,
|
||||
},
|
||||
...(process.platform !== "darwin" ? [{
|
||||
label: "About Lens",
|
||||
click: showAbout
|
||||
} as MenuItemConstructorOptions] : [])
|
||||
]
|
||||
};
|
||||
mt.push(helpMenu);
|
||||
|
||||
const menu = Menu.buildFromTemplate(mt);
|
||||
Menu.setApplicationMenu(menu);
|
||||
|
||||
promiseIpc.on("enableClusterSettingsMenuItem", (clusterId: string) => {
|
||||
setClusterSettingsEnabled(true)
|
||||
});
|
||||
|
||||
promiseIpc.on("disableClusterSettingsMenuItem", () => {
|
||||
setClusterSettingsEnabled(false)
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,143 @@
|
||||
import * as WebSocket from "ws"
|
||||
import * as pty from "node-pty"
|
||||
import { ShellSession } from "./shell-session";
|
||||
import { v4 as uuid } from "uuid"
|
||||
import * as k8s from "@kubernetes/client-node"
|
||||
import logger from "./logger";
|
||||
import { KubeConfig, V1Pod } from "@kubernetes/client-node";
|
||||
import { tracker } from "./tracker"
|
||||
import { Cluster, ClusterPreferences } from "./cluster"
|
||||
|
||||
export class NodeShellSession extends ShellSession {
|
||||
protected nodeName: string;
|
||||
protected podId: string
|
||||
protected kc: KubeConfig
|
||||
|
||||
constructor(socket: WebSocket, pathToKubeconfig: string, cluster: Cluster, nodeName: string) {
|
||||
super(socket, pathToKubeconfig, cluster)
|
||||
this.nodeName = nodeName
|
||||
this.podId = `node-shell-${uuid()}`
|
||||
this.kc = cluster.contextHandler.kc
|
||||
}
|
||||
|
||||
public async open() {
|
||||
const shell = await this.kubectl.kubectlPath()
|
||||
let args = []
|
||||
if (this.createNodeShellPod(this.podId, this.nodeName)) {
|
||||
await this.waitForRunningPod(this.podId).catch((error) => {
|
||||
this.exit(1001)
|
||||
})
|
||||
}
|
||||
args = ["exec", "-i", "-t", "-n", "kube-system", this.podId, "--", "sh", "-c", "((clear && bash) || (clear && ash) || (clear && sh))"]
|
||||
|
||||
const shellEnv = this.getShellEnv()
|
||||
this.shellProcess = pty.spawn(shell, args, {
|
||||
cols: 80,
|
||||
cwd: this.cwd() || shellEnv["HOME"],
|
||||
env: shellEnv,
|
||||
name: "xterm-256color",
|
||||
rows: 30,
|
||||
});
|
||||
this.pipeStdout()
|
||||
this.pipeStdin()
|
||||
this.closeWebsocketOnProcessExit()
|
||||
this.exitProcessOnWebsocketClose()
|
||||
|
||||
tracker.event("node-shell", "open")
|
||||
}
|
||||
|
||||
protected exit(code = 1000) {
|
||||
if (this.podId) {
|
||||
this.deleteNodeShellPod()
|
||||
}
|
||||
super.exit(code)
|
||||
}
|
||||
|
||||
protected async createNodeShellPod(podId: string, nodeName: string) {
|
||||
const kc = this.getKubeConfig();
|
||||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
|
||||
const pod = {
|
||||
metadata: {
|
||||
name: podId,
|
||||
namespace: "kube-system"
|
||||
},
|
||||
spec: {
|
||||
restartPolicy: "Never",
|
||||
terminationGracePeriodSeconds: 0,
|
||||
hostPID: true,
|
||||
hostIPC: true,
|
||||
hostNetwork: true,
|
||||
tolerations: [{
|
||||
operator: "Exists"
|
||||
}],
|
||||
containers: [{
|
||||
name: "shell",
|
||||
image: "docker.io/alpine:3.9",
|
||||
securityContext: {
|
||||
privileged: true,
|
||||
},
|
||||
command: ["nsenter"],
|
||||
args: ["-t", "1", "-m", "-u", "-i", "-n", "sleep", "14000"]
|
||||
}],
|
||||
nodeSelector: {
|
||||
"kubernetes.io/hostname": nodeName
|
||||
}
|
||||
}
|
||||
} as k8s.V1Pod;
|
||||
await k8sApi.createNamespacedPod("kube-system", pod).catch((error) => {
|
||||
logger.error(error)
|
||||
return false
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
protected getKubeConfig() {
|
||||
if (this.kc) {
|
||||
return this.kc
|
||||
}
|
||||
this.kc = new k8s.KubeConfig();
|
||||
this.kc.loadFromFile(this.kubeconfigPath)
|
||||
return this.kc
|
||||
}
|
||||
|
||||
protected waitForRunningPod(podId: string) {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
const kc = this.getKubeConfig();
|
||||
const watch = new k8s.Watch(kc);
|
||||
|
||||
const req = watch.watch(`/api/v1/namespaces/kube-system/pods`, {},
|
||||
// callback is called for each received object.
|
||||
(_type, obj) => {
|
||||
if (obj.metadata.name == podId && obj.status.phase === "Running") {
|
||||
resolve(true)
|
||||
}
|
||||
},
|
||||
// done callback is called if the watch terminates normally
|
||||
(err) => {
|
||||
logger.error(err)
|
||||
reject(false)
|
||||
}
|
||||
);
|
||||
setTimeout(() => { req.abort(); reject(false); }, 120 * 1000);
|
||||
})
|
||||
}
|
||||
protected deleteNodeShellPod() {
|
||||
const kc = this.getKubeConfig();
|
||||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
|
||||
k8sApi.deleteNamespacedPod(this.podId, "kube-system")
|
||||
}
|
||||
}
|
||||
|
||||
export async function open(socket: WebSocket, pathToKubeconfig: string, cluster: Cluster, nodeName?: string): Promise<ShellSession> {
|
||||
return new Promise(async(resolve, reject) => {
|
||||
let shell = null
|
||||
if (nodeName) {
|
||||
shell = new NodeShellSession(socket, pathToKubeconfig, cluster, nodeName)
|
||||
}
|
||||
else {
|
||||
shell = new ShellSession(socket, pathToKubeconfig, cluster)
|
||||
}
|
||||
shell.open()
|
||||
resolve(shell)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import logger from "./logger"
|
||||
import { createServer } from "net"
|
||||
|
||||
// Adapted from https://gist.github.com/mikeal/1840641#gistcomment-2896667
|
||||
function checkPort(port: number) {
|
||||
const server = createServer()
|
||||
server.unref()
|
||||
return new Promise((resolve, reject) =>
|
||||
server
|
||||
.on('error', error => reject(error))
|
||||
.on('listening', () => server.close(() => resolve(port)))
|
||||
.listen({host: "127.0.0.1", port: port}))
|
||||
}
|
||||
|
||||
export async function getFreePort(firstPort: number, lastPort: number): Promise<number> {
|
||||
let port = firstPort
|
||||
|
||||
while(true) {
|
||||
try {
|
||||
logger.debug("Checking port " + port + " availability ...")
|
||||
await checkPort(port)
|
||||
return(port)
|
||||
} catch(error) {
|
||||
if(++port > lastPort) throw("Could not find a free port")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import * as util from "util"
|
||||
import { exec } from "child_process";
|
||||
|
||||
export const promiseExec = util.promisify(exec)
|
||||
@@ -0,0 +1,18 @@
|
||||
import { app } from "electron"
|
||||
|
||||
const switchValue = app.commandLine.getSwitchValue("proxy-server")
|
||||
|
||||
export function mangleProxyEnv() {
|
||||
let httpsProxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || ""
|
||||
|
||||
delete process.env.HTTPS_PROXY
|
||||
delete process.env.HTTP_PROXY
|
||||
|
||||
if (switchValue !== "") {
|
||||
httpsProxy = switchValue
|
||||
}
|
||||
|
||||
if (httpsProxy !== "") {
|
||||
process.env.APP_HTTPS_PROXY = httpsProxy
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
import * as http from "http";
|
||||
import * as httpProxy from "http-proxy";
|
||||
import { Socket } from "net";
|
||||
import * as url from "url";
|
||||
import * as WebSocket from "ws"
|
||||
import { ContextHandler } from "./context-handler";
|
||||
import logger from "./logger"
|
||||
import * as shell from "./node-shell-session"
|
||||
import { ClusterManager } from "./cluster-manager"
|
||||
import { Router } from "./router"
|
||||
|
||||
export class LensProxy {
|
||||
public static readonly localShellSessions = true
|
||||
|
||||
public port: number;
|
||||
protected clusterUrl: url.UrlWithStringQuery
|
||||
protected clusterManager: ClusterManager
|
||||
protected retryCounters: Map<string, number> = new Map()
|
||||
protected router: Router
|
||||
|
||||
constructor(port: number, clusterManager: ClusterManager) {
|
||||
this.port = port
|
||||
this.clusterManager = clusterManager
|
||||
this.router = new Router()
|
||||
}
|
||||
|
||||
public run() {
|
||||
const proxyServer = this.buildProxyServer();
|
||||
proxyServer.listen(this.port, "127.0.0.1")
|
||||
}
|
||||
|
||||
protected buildProxyServer() {
|
||||
const proxy = this.createProxy();
|
||||
const proxyServer = http.createServer(function(req: http.IncomingMessage, res: http.ServerResponse) {
|
||||
this.handleRequest(proxy, req, res);
|
||||
}.bind(this));
|
||||
proxyServer.on("upgrade", function(req: http.IncomingMessage, socket: Socket, head: Buffer) {
|
||||
if (this.isRemoteShellRequired(req)) {
|
||||
this.proxyWsUpgrade(proxy, req, socket, head)
|
||||
} else {
|
||||
this.handleWsUpgrade(req, socket, head)
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
proxyServer.on("error", (err) => {
|
||||
logger.error(err)
|
||||
});
|
||||
|
||||
return proxyServer;
|
||||
}
|
||||
|
||||
protected createProxy() {
|
||||
const proxy = httpProxy.createProxyServer();
|
||||
|
||||
proxy.on("proxyRes", (proxyRes, req, res) => {
|
||||
if (proxyRes.statusCode === 502) {
|
||||
const cluster = this.clusterManager.getClusterForRequest(req)
|
||||
if (cluster && cluster.contextHandler.proxyServerError()) {
|
||||
res.writeHead(proxyRes.statusCode, {
|
||||
"Content-Type": "text/plain"
|
||||
})
|
||||
res.end(cluster.contextHandler.proxyServerError().toString())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (req.method !== "GET") {
|
||||
return
|
||||
}
|
||||
const key = `${req.headers.host}${req.url}`
|
||||
if (this.retryCounters.has(key)) {
|
||||
logger.debug("Resetting proxy retry cache for url: " + key)
|
||||
this.retryCounters.delete(key)
|
||||
}
|
||||
})
|
||||
proxy.on("error", (error, req, res, target) => {
|
||||
if (target) {
|
||||
logger.debug("Failed proxy to target: " + JSON.stringify(target))
|
||||
if (req.method === "GET" && (!res.statusCode || res.statusCode >= 500)) {
|
||||
const retryCounterKey = `${req.headers.host}${req.url}`
|
||||
const retryCount = this.retryCounters.get(retryCounterKey) || 0
|
||||
if (retryCount < 20) {
|
||||
logger.debug("Retrying proxy request to url: " + retryCounterKey)
|
||||
setTimeout(() => {
|
||||
this.retryCounters.set(retryCounterKey, retryCount + 1)
|
||||
this.handleRequest(proxy, req, res)
|
||||
}, (250 * retryCount))
|
||||
}
|
||||
}
|
||||
|
||||
//return
|
||||
}
|
||||
res.writeHead(500, {
|
||||
'Content-Type': 'text/plain'
|
||||
})
|
||||
res.end('Oops, something went wrong.')
|
||||
})
|
||||
|
||||
return proxy;
|
||||
}
|
||||
|
||||
protected createWsListener() {
|
||||
const ws = new WebSocket.Server({ noServer: true})
|
||||
ws.on("connection", ((con: WebSocket, req: http.IncomingMessage) => {
|
||||
const cluster = this.clusterManager.getClusterForRequest(req)
|
||||
const contextHandler = cluster.contextHandler
|
||||
const nodeParam = this.getNodeParam(req.url)
|
||||
|
||||
contextHandler.withTemporaryKubeconfig((kubeconfigPath) => {
|
||||
return new Promise<boolean>(async (resolve, reject) => {
|
||||
const shellSession = await shell.open(con, kubeconfigPath, cluster, nodeParam)
|
||||
shellSession.on("exit", () => {
|
||||
resolve(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
}).bind(this))
|
||||
return ws
|
||||
}
|
||||
|
||||
protected getNodeParam(requestUrl: string) {
|
||||
const reqUrl = url.parse(requestUrl, true)
|
||||
const urlParams = reqUrl.query
|
||||
let nodeParam: string = null
|
||||
for (const [key, value] of Object.entries(urlParams)) {
|
||||
if (key === "node") {
|
||||
nodeParam = value.toString()
|
||||
}
|
||||
}
|
||||
return nodeParam
|
||||
}
|
||||
|
||||
protected async getProxyTarget(req: http.IncomingMessage, contextHandler: ContextHandler): Promise<httpProxy.ServerOptions> {
|
||||
if (req.url.startsWith("/api-kube/")) {
|
||||
delete req.headers.authorization
|
||||
req.url = req.url.replace("/api-kube", "")
|
||||
return await contextHandler.getApiTarget()
|
||||
} else {
|
||||
return await contextHandler.getProxyTarget()
|
||||
}
|
||||
}
|
||||
|
||||
protected async handleRequest(proxy: httpProxy, req: http.IncomingMessage, res: http.ServerResponse) {
|
||||
const cluster = this.clusterManager.getClusterForRequest(req)
|
||||
if (!cluster) {
|
||||
logger.error("Got request to unknown cluster")
|
||||
logger.debug(req.headers.host + req.url)
|
||||
res.statusCode = 503
|
||||
res.end()
|
||||
return
|
||||
}
|
||||
const contextHandler = cluster.contextHandler
|
||||
try {
|
||||
contextHandler.applyHeaders(req)
|
||||
} catch (error) {
|
||||
res.statusCode = 503
|
||||
res.end()
|
||||
return
|
||||
}
|
||||
contextHandler.ensureServer().then(async () => {
|
||||
if (await this.router.route(cluster, req, res)) return
|
||||
const proxyTarget = await this.getProxyTarget(req, contextHandler)
|
||||
proxy.web(req, res, proxyTarget)
|
||||
})
|
||||
}
|
||||
|
||||
protected async proxyWsUpgrade(proxy: httpProxy, req: http.IncomingMessage, socket: Socket, head: Buffer) {
|
||||
const cluster = this.clusterManager.getClusterForRequest(req)
|
||||
const contextHandler = cluster.contextHandler
|
||||
contextHandler.applyHeaders(req);
|
||||
const reqUrl = url.parse(req.url, true)
|
||||
const urlParams = reqUrl.query
|
||||
for (const [key, value] of Object.entries(urlParams)) {
|
||||
if (key !== "token") {
|
||||
req.headers["x-lens-param-" + key] = value
|
||||
}
|
||||
}
|
||||
proxy.ws(req, socket, head, await contextHandler.getProxyTarget());
|
||||
}
|
||||
|
||||
protected async handleWsUpgrade(req: http.IncomingMessage, socket: Socket, head: Buffer) {
|
||||
const wsServer = this.createWsListener();
|
||||
const cluster = this.clusterManager.getClusterForRequest(req)
|
||||
const contextHandler = cluster.contextHandler
|
||||
contextHandler.applyHeaders(req);
|
||||
wsServer.handleUpgrade(req, socket, head, (con) => {
|
||||
wsServer.emit("connection", con, req);
|
||||
});
|
||||
}
|
||||
|
||||
protected isRemoteShellRequired(req: http.IncomingMessage) {
|
||||
if (!LensProxy.localShellSessions) {
|
||||
return true
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function listen(port: number, clusterManager: ClusterManager) {
|
||||
const proxyServer = new LensProxy(port, clusterManager)
|
||||
proxyServer.run();
|
||||
return proxyServer;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { LensApiRequest } from "./router"
|
||||
import * as resourceApplier from "./resource-applier"
|
||||
import { LensApi } from "./lens-api"
|
||||
|
||||
class ResourceApplierApi extends LensApi {
|
||||
public async applyResource(request: LensApiRequest) {
|
||||
const { response, cluster, payload } = request
|
||||
try {
|
||||
const resource = await resourceApplier.apply(cluster, cluster.kubeconfigPath(), payload)
|
||||
this.respondJson(response, [resource], 200)
|
||||
} catch(error) {
|
||||
this.respondText(response, error, 422)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const resourceApplierApi = new ResourceApplierApi()
|
||||
@@ -0,0 +1,101 @@
|
||||
import { exec } from "child_process";
|
||||
import * as fs from "fs";
|
||||
import * as yaml from "js-yaml";
|
||||
import * as path from "path";
|
||||
import * as tempy from "tempy";
|
||||
import logger from "./logger"
|
||||
import { Cluster } from "./cluster";
|
||||
import { tracker } from "./tracker";
|
||||
|
||||
type KubeObject = {
|
||||
status: {};
|
||||
metadata?: {
|
||||
resourceVersion: number;
|
||||
annotations?: {
|
||||
"kubectl.kubernetes.io/last-applied-configuration": string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export class ResourceApplier {
|
||||
protected kubeconfigPath: string;
|
||||
protected cluster: Cluster
|
||||
|
||||
constructor(cluster: Cluster, pathToKubeconfig: string) {
|
||||
this.kubeconfigPath = pathToKubeconfig
|
||||
this.cluster = cluster
|
||||
}
|
||||
|
||||
public async apply(resource: any): Promise<string> {
|
||||
this.sanitizeObject((resource as KubeObject))
|
||||
try {
|
||||
tracker.event("resource", "apply")
|
||||
return await this.kubectlApply(yaml.safeDump(resource))
|
||||
} catch(error) {
|
||||
throw (error)
|
||||
}
|
||||
}
|
||||
|
||||
protected async kubectlApply(content: string): Promise<string> {
|
||||
const kubectl = await this.cluster.kubeCtl.kubectlPath()
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const fileName = tempy.file({name: "resource.yaml"})
|
||||
fs.writeFileSync(fileName, content)
|
||||
const cmd = `"${kubectl}" apply --kubeconfig ${this.kubeconfigPath} -o json -f ${fileName}`
|
||||
logger.debug("shooting manifests with: " + cmd);
|
||||
const execEnv: NodeJS.ProcessEnv = Object.assign({}, process.env)
|
||||
const httpsProxy = this.cluster.preferences?.httpsProxy
|
||||
if (httpsProxy) {
|
||||
execEnv["HTTPS_PROXY"] = httpsProxy
|
||||
}
|
||||
exec(cmd, { env: execEnv },
|
||||
(error, stdout, stderr) => {
|
||||
if (stderr != "") {
|
||||
fs.unlinkSync(fileName)
|
||||
reject(stderr)
|
||||
return
|
||||
}
|
||||
fs.unlinkSync(fileName)
|
||||
resolve(JSON.parse(stdout))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
public async kubectlApplyAll(resources: string[]): Promise<string> {
|
||||
const kubectl = await this.cluster.kubeCtl.kubectlPath()
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const tmpDir = tempy.directory()
|
||||
// Dump each resource into tmpDir
|
||||
for (const i in resources) {
|
||||
fs.writeFileSync(path.join(tmpDir, `${i}.yaml`), resources[i])
|
||||
}
|
||||
const cmd = `"${kubectl}" apply --kubeconfig ${this.kubeconfigPath} -o json -f ${tmpDir}`
|
||||
console.log("shooting manifests with:", cmd);
|
||||
exec(cmd, (error, stdout, stderr) => {
|
||||
if(error) {
|
||||
reject("Error applying manifests:" + error);
|
||||
}
|
||||
if (stderr != "") {
|
||||
reject(stderr)
|
||||
return
|
||||
}
|
||||
resolve(stdout)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
protected sanitizeObject(resource: KubeObject) {
|
||||
delete resource['status']
|
||||
if (resource['metadata']) {
|
||||
if (resource['metadata']['annotations'] && resource['metadata']['annotations']['kubectl.kubernetes.io/last-applied-configuration']) {
|
||||
delete resource['metadata']['annotations']['kubectl.kubernetes.io/last-applied-configuration']
|
||||
}
|
||||
delete resource['metadata']['resourceVersion']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function apply(cluster: Cluster, pathToKubeconfig: string, resource: any) {
|
||||
const resourceApplier = new ResourceApplier(cluster, pathToKubeconfig)
|
||||
return await resourceApplier.apply(resource)
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import * as http from "http";
|
||||
import { Cluster } from "./cluster";
|
||||
import { helmApi } from "./helm-api"
|
||||
import { resourceApplierApi } from "./resource-applier-api"
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const Call = require('@hapi/call');
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const Subtext = require('@hapi/subtext');
|
||||
|
||||
interface RouteParams {
|
||||
[key: string]: string | undefined;
|
||||
}
|
||||
|
||||
export type LensApiRequest = {
|
||||
cluster: Cluster;
|
||||
payload: any;
|
||||
raw: {
|
||||
req: http.IncomingMessage;
|
||||
};
|
||||
params: RouteParams;
|
||||
response: http.ServerResponse;
|
||||
query: URLSearchParams;
|
||||
path: string;
|
||||
}
|
||||
|
||||
export class Router {
|
||||
protected router: any
|
||||
|
||||
public constructor() {
|
||||
this.router = new Call.Router();
|
||||
this.addRoutes()
|
||||
}
|
||||
|
||||
public async route(cluster: Cluster, req: http.IncomingMessage, res: http.ServerResponse) {
|
||||
const url = new URL(req.url, "http://localhost")
|
||||
const path = url.pathname
|
||||
const method = req.method.toLowerCase()
|
||||
const matchingRoute = this.router.route(method, path)
|
||||
|
||||
if (matchingRoute.isBoom !== true) { // route() returns error if route not found -> object.isBoom === true
|
||||
const request = await this.getRequest({ req, res, cluster, url, params: matchingRoute.params })
|
||||
await matchingRoute.route(request)
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
protected async getRequest(opts: { req: http.IncomingMessage; res: http.ServerResponse; cluster: Cluster; url: URL; params: RouteParams }) {
|
||||
const { req, res, url, cluster, params } = opts
|
||||
const { payload } = await Subtext.parse(req, null, { parse: true, output: 'data' });
|
||||
const request: LensApiRequest = {
|
||||
cluster: cluster,
|
||||
path: url.pathname,
|
||||
raw: {
|
||||
req: req,
|
||||
},
|
||||
response: res,
|
||||
query: url.searchParams,
|
||||
payload: payload,
|
||||
params: params
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
protected addRoutes() {
|
||||
// Helm API
|
||||
this.router.add({ method: 'get', path: '/api-helm/v2/charts' }, helmApi.listCharts.bind(helmApi))
|
||||
this.router.add({ method: 'get', path: '/api-helm/v2/charts/{repo}/{chart}' }, helmApi.getChart.bind(helmApi))
|
||||
this.router.add({ method: 'get', path: '/api-helm/v2/charts/{repo}/{chart}/values' }, helmApi.getChartValues.bind(helmApi))
|
||||
|
||||
this.router.add({ method: 'post', path: '/api-helm/v2/releases' }, helmApi.installChart.bind(helmApi))
|
||||
this.router.add({ method: 'put', path: '/api-helm/v2/releases/{namespace}/{release}' }, helmApi.updateRelease.bind(helmApi))
|
||||
this.router.add({ method: 'put', path: '/api-helm/v2/releases/{namespace}/{release}/rollback' }, helmApi.rollbackRelease.bind(helmApi))
|
||||
this.router.add({ method: 'get', path: '/api-helm/v2/releases/{namespace?}' }, helmApi.listReleases.bind(helmApi))
|
||||
this.router.add({ method: 'get', path: '/api-helm/v2/releases/{namespace}/{release}' }, helmApi.getRelease.bind(helmApi))
|
||||
this.router.add({ method: 'get', path: '/api-helm/v2/releases/{namespace}/{release}/values' }, helmApi.getReleaseValues.bind(helmApi))
|
||||
this.router.add({ method: 'get', path: '/api-helm/v2/releases/{namespace}/{release}/history' }, helmApi.getReleaseHistory.bind(helmApi))
|
||||
this.router.add({ method: 'delete', path: '/api-helm/v2/releases/{namespace}/{release}' }, helmApi.deleteRelease.bind(helmApi))
|
||||
|
||||
// Resource Applier API
|
||||
this.router.add({ method: 'post', path: '/api-resource/stack' }, resourceApplierApi.applyResource.bind(resourceApplierApi))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
import * as pty from "node-pty"
|
||||
import * as WebSocket from "ws"
|
||||
import { EventEmitter } from "events";
|
||||
import * as path from "path"
|
||||
import shellEnv = require("shell-env")
|
||||
import { app } from "electron"
|
||||
import { Kubectl } from "./kubectl"
|
||||
import { tracker } from "./tracker"
|
||||
import { Cluster, ClusterPreferences } from "./cluster"
|
||||
import { helmCli } from "./helm-cli"
|
||||
|
||||
export class ShellSession extends EventEmitter {
|
||||
protected websocket: WebSocket
|
||||
protected shellProcess: pty.IPty
|
||||
protected kubeconfigPath: string
|
||||
protected nodeShellPod: string;
|
||||
protected kubectl: Kubectl;
|
||||
protected kubectlBinDir: string;
|
||||
protected helmBinDir: string;
|
||||
protected preferences: ClusterPreferences;
|
||||
|
||||
constructor(socket: WebSocket, pathToKubeconfig: string, cluster: Cluster) {
|
||||
super()
|
||||
this.websocket = socket
|
||||
this.kubeconfigPath = pathToKubeconfig
|
||||
this.kubectl = new Kubectl(cluster.version)
|
||||
this.preferences = cluster.preferences || {}
|
||||
}
|
||||
|
||||
public async open() {
|
||||
this.kubectlBinDir = await this.kubectl.binDir()
|
||||
this.helmBinDir = helmCli.getBinaryDir()
|
||||
await helmCli.binaryPath()
|
||||
const env = this.getShellEnv()
|
||||
const shell = env.PTYSHELL
|
||||
const args = await this.getShellArgs(shell)
|
||||
this.shellProcess = pty.spawn(shell, args, {
|
||||
cols: 80,
|
||||
cwd: this.cwd() || env.HOME,
|
||||
env: env,
|
||||
name: "xterm-256color",
|
||||
rows: 30,
|
||||
});
|
||||
|
||||
this.pipeStdout()
|
||||
this.pipeStdin()
|
||||
this.closeWebsocketOnProcessExit()
|
||||
this.exitProcessOnWebsocketClose()
|
||||
|
||||
tracker.event("shell", "open")
|
||||
}
|
||||
|
||||
protected cwd(): string {
|
||||
if(!this.preferences || !this.preferences.terminalCWD || this.preferences.terminalCWD === "") {
|
||||
return null
|
||||
}
|
||||
return this.preferences.terminalCWD
|
||||
}
|
||||
|
||||
protected async getShellArgs(shell: string): Promise<Array<string>> {
|
||||
switch(path.basename(shell)) {
|
||||
case "powershell.exe":
|
||||
return ["-NoExit", "-command", `& {Set-Location $Env:USERPROFILE; $Env:PATH="${this.kubectlBinDir};${this.helmBinDir};$Env:PATH"}`]
|
||||
break
|
||||
case "bash":
|
||||
return ["--init-file", path.join(this.kubectlBinDir, '.bash_set_path')]
|
||||
break
|
||||
case "fish":
|
||||
return ["--login", "--init-command", `export PATH="${this.kubectlBinDir}:${this.helmBinDir}:$PATH"; export KUBECONFIG="${this.kubeconfigPath}"`]
|
||||
break
|
||||
case "zsh":
|
||||
return ["--login"]
|
||||
default:
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
protected getShellEnv() {
|
||||
const env = JSON.parse(JSON.stringify(shellEnv.sync()))
|
||||
const pathStr = [this.kubectlBinDir, this.helmBinDir, process.env.PATH].join(path.delimiter)
|
||||
|
||||
if(process.platform === "win32") {
|
||||
env["SystemRoot"] = process.env.SystemRoot
|
||||
env["PTYSHELL"] = "powershell.exe"
|
||||
env["PATH"] = pathStr
|
||||
} else if(typeof(process.env.SHELL) != "undefined") {
|
||||
env["PTYSHELL"] = process.env.SHELL
|
||||
env["PATH"] = pathStr
|
||||
} else {
|
||||
env["PTYSHELL"] = "" // blank runs the system default shell
|
||||
}
|
||||
|
||||
if(path.basename(env["PTYSHELL"]) === "zsh") {
|
||||
env["OLD_ZDOTDIR"] = env.ZDOTDIR || env.HOME
|
||||
env["ZDOTDIR"] = this.kubectlBinDir
|
||||
}
|
||||
|
||||
env["PTYPID"] = process.pid.toString()
|
||||
env["KUBECONFIG"] = this.kubeconfigPath
|
||||
env["TERM_PROGRAM"] = app.getName()
|
||||
env["TERM_PROGRAM_VERSION"] = app.getVersion()
|
||||
if (this.preferences.httpsProxy) {
|
||||
env["HTTPS_PROXY"] = this.preferences.httpsProxy
|
||||
}
|
||||
if (env.DEBUG) { // do not pass debug option to bash
|
||||
delete env["DEBUG"]
|
||||
}
|
||||
|
||||
return(env)
|
||||
}
|
||||
|
||||
protected pipeStdout() {
|
||||
// send shell output to websocket
|
||||
this.shellProcess.on("data", ((data: string) => {
|
||||
this.sendResponse(data)
|
||||
}).bind(this));
|
||||
}
|
||||
|
||||
protected pipeStdin() {
|
||||
// write websocket messages to shellProcess
|
||||
this.websocket.on("message", function(data: string) {
|
||||
const message = Buffer.from(data.slice(1, data.length), "base64").toString()
|
||||
switch (data[0]) {
|
||||
case "0":
|
||||
this.shellProcess.write(message)
|
||||
break;
|
||||
case "4":
|
||||
const resizeMsgObj = JSON.parse(message)
|
||||
this.shellProcess.resize(resizeMsgObj["Width"], resizeMsgObj["Height"])
|
||||
break;
|
||||
case "9":
|
||||
this.token = message
|
||||
this.emit('newToken', this.token)
|
||||
break;
|
||||
}
|
||||
}.bind(this))
|
||||
}
|
||||
|
||||
protected exit(code = 1000) {
|
||||
this.websocket.close(code)
|
||||
this.emit('exit')
|
||||
}
|
||||
|
||||
protected closeWebsocketOnProcessExit() {
|
||||
this.shellProcess.on("exit", (_code) => {
|
||||
this.exit()
|
||||
});
|
||||
}
|
||||
|
||||
protected exitProcessOnWebsocketClose() {
|
||||
this.websocket.on("close", () => {
|
||||
if (this.shellProcess) {
|
||||
this.shellProcess.kill();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
protected sendResponse(msg: string) {
|
||||
this.websocket.send("1" + Buffer.from(msg).toString("base64"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import shellEnv = require("shell-env")
|
||||
import logger from "./logger"
|
||||
|
||||
export async function shellSync() {
|
||||
const env = await shellEnv()
|
||||
|
||||
// Overwrite PATH on darwin
|
||||
if (process.env.NODE_ENV === "production" && process.platform === "darwin") {
|
||||
process.env["PATH"] = env.PATH
|
||||
}
|
||||
|
||||
let key = null
|
||||
for(key in env) {
|
||||
if(!env.hasOwnProperty(key) || process.env[key]) continue // skip existing and prototype keys
|
||||
logger.debug("Imported " + key + " from login shell to process environment")
|
||||
process.env[key] = env[key]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { Tracker } from "../common/tracker"
|
||||
import { app, remote } from "electron"
|
||||
|
||||
export const tracker = new Tracker(app || remote.app);
|
||||
@@ -0,0 +1,7 @@
|
||||
import { webContents } from "electron"
|
||||
/**
|
||||
* Helper to find the correct web contents handle for main window
|
||||
*/
|
||||
export function findMainWebContents() {
|
||||
return webContents.getAllWebContents().find(w => w.getType() === "window");
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import { BrowserWindow, shell, Menu } from "electron"
|
||||
import { PromiseIpc } from "electron-promise-ipc"
|
||||
import * as windowStateKeeper from "electron-window-state"
|
||||
import * as path from "path"
|
||||
import { tracker } from "./tracker";
|
||||
|
||||
declare const __static: string;
|
||||
|
||||
export class WindowManager {
|
||||
public mainWindow: BrowserWindow = null;
|
||||
public splashWindow: BrowserWindow = null;
|
||||
protected promiseIpc: any
|
||||
protected windowState: windowStateKeeper.State;
|
||||
|
||||
constructor(showSplash = true) {
|
||||
this.promiseIpc = new PromiseIpc({ timeout: 2000 })
|
||||
// Manage main window size&position with persistence
|
||||
this.windowState = windowStateKeeper({
|
||||
defaultHeight: 900,
|
||||
defaultWidth: 1440,
|
||||
});
|
||||
|
||||
this.splashWindow = new BrowserWindow({
|
||||
width: 500,
|
||||
height: 300,
|
||||
backgroundColor: "#1e2124",
|
||||
center: true,
|
||||
frame: false,
|
||||
resizable: false,
|
||||
show: false
|
||||
})
|
||||
if (showSplash) {
|
||||
this.splashWindow.loadFile(path.join(__static, "/splash.html"))
|
||||
this.splashWindow.show()
|
||||
}
|
||||
|
||||
this.mainWindow = new BrowserWindow({
|
||||
x: this.windowState.x,
|
||||
y: this.windowState.y,
|
||||
width: this.windowState.width,
|
||||
height: this.windowState.height,
|
||||
backgroundColor: "#1e2124",
|
||||
titleBarStyle: "hidden",
|
||||
webPreferences: {
|
||||
nodeIntegration: true,
|
||||
webviewTag: true
|
||||
},
|
||||
show: false
|
||||
});
|
||||
|
||||
// Hook window state manager into window lifecycle
|
||||
this.windowState.manage(this.mainWindow);
|
||||
|
||||
// handle close event
|
||||
this.mainWindow.on("close", () => {
|
||||
this.mainWindow = null;
|
||||
})
|
||||
|
||||
// handle external links
|
||||
this.mainWindow.webContents.on("will-navigate", (event, link) => {
|
||||
if (link.startsWith("http://localhost")) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
shell.openExternal(link);
|
||||
})
|
||||
|
||||
// handle developer console
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
this.mainWindow.webContents.on("devtools-opened", () => {
|
||||
if (this.mainWindow.getBrowserView()) {
|
||||
this.mainWindow.getBrowserView().webContents.openDevTools({mode: "detach"})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
this.mainWindow.on("focus", () => {
|
||||
tracker.event("app", "focus")
|
||||
})
|
||||
}
|
||||
|
||||
public showMain(url: string) {
|
||||
this.mainWindow.loadURL( url ).then(() => {
|
||||
this.splashWindow.hide()
|
||||
this.splashWindow.loadURL("data:text/html;charset=utf-8,").then(() => {
|
||||
this.splashWindow.close()
|
||||
this.mainWindow.show()
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user