Kubeconfigs as file references (#466)

Signed-off-by: Jussi Nummelin <jussi.nummelin@gmail.com>
Co-authored-by: Lauri Nevala <lauri.nevala@gmail.com>
Co-authored-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
authored and GitHub committed 2020-07-03 16:53:23 +03:00
1 parent c15aa7972c
commit 55687b7d35
23 files changed
+272 -162

No files matched your search

+35 -5
View File
@@ -2,14 +2,17 @@ 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 kubeconfig: string
protected tempFile: string
protected cluster: Cluster
constructor(kubeconfig: string) {
this.kubeconfig = kubeconfig
constructor(cluster: Cluster) {
this.cluster = cluster
this.tempFile = this.createTemporaryKubeconfig()
}
@@ -17,11 +20,38 @@ export class KubeconfigManager {
return this.tempFile
}
/**
* Creates new "temporary" kubeconfig that point to the kubectl-proxy.
* This way any user of the config does not need to know anything about the auth etc. details.
*/
protected createTemporaryKubeconfig(): string {
ensureDir(this.configDir)
const path = `${this.configDir}/${randomFileName("kubeconfig")}`
logger.debug('Creating temporary kubeconfig: ' + path)
fs.writeFileSync(path, this.kubeconfig)
const originalKc = new KubeConfig()
originalKc.loadFromFile(this.cluster.kubeConfigPath)
const kc = {
clusters: [
{
name: this.cluster.contextName,
server: `http://127.0.0.1:${this.cluster.contextHandler.proxyPort}`
}
],
users: [
{
name: "proxy"
}
],
contexts: [
{
name: this.cluster.contextName,
cluster: this.cluster.contextName,
namespace: originalKc.getContextObject(this.cluster.contextName).namespace,
user: "proxy"
}
],
currentContext: this.cluster.contextName
} as KubeConfig
fs.writeFileSync(path, k8s.dumpConfigYaml(kc))
return path
}