1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/main/kubeconfig-manager.ts
Jussi Nummelin c46a5f6f20 Use kubeconfigs as file references without copying them into store
Signed-off-by: Jussi Nummelin <jussi.nummelin@gmail.com>
2020-07-03 11:33:24 +03:00

61 lines
1.6 KiB
TypeScript

import { app } from "electron"
import fs from "fs"
import { ensureDir, randomFileName} from "./file-helpers"
import logger from "./logger"
import { Cluster } from "./cluster"
import * as k8s from "./k8s"
import { KubeConfig } from "@kubernetes/client-node"
export class KubeconfigManager {
protected configDir = app.getPath("temp")
protected tempFile: string
protected cluster: Cluster
constructor(cluster: Cluster) {
this.cluster = cluster
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)
logger.debug(`cluster url: ${this.cluster.url}`)
logger.debug(`cluster api url: ${this.cluster.apiUrl}`)
// TODO Make the names come from actual cluster.name etc.
let kc = {
clusters: [
{
name: this.cluster.contextName,
server: `http://127.0.0.1:${this.cluster.port}/${this.cluster.id}`
}
],
users: [
{
name: "proxy"
}
],
contexts: [
{
name: this.cluster.contextName,
cluster: this.cluster.contextName,
user: "proxy"
}
],
currentContext: this.cluster.contextName
} as KubeConfig
fs.writeFileSync(path, k8s.dumpConfigYaml(kc))
return path
}
public unlink() {
logger.debug('Deleting temporary kubeconfig: ' + this.tempFile)
fs.unlinkSync(this.tempFile)
}
}