mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Use kubeconfigs as file references without copying them into store
Signed-off-by: Jussi Nummelin <jussi.nummelin@gmail.com>
This commit is contained in:
parent
c15aa7972c
commit
c46a5f6f20
@ -6,6 +6,7 @@ import * as version260Beta2 from "../migrations/cluster-store/2.6.0-beta.2"
|
|||||||
import * as version260Beta3 from "../migrations/cluster-store/2.6.0-beta.3"
|
import * as version260Beta3 from "../migrations/cluster-store/2.6.0-beta.3"
|
||||||
import * as version270Beta0 from "../migrations/cluster-store/2.7.0-beta.0"
|
import * as version270Beta0 from "../migrations/cluster-store/2.7.0-beta.0"
|
||||||
import * as version270Beta1 from "../migrations/cluster-store/2.7.0-beta.1"
|
import * as version270Beta1 from "../migrations/cluster-store/2.7.0-beta.1"
|
||||||
|
import * as version350Beta1 from "../migrations/cluster-store/3.5.0-beta.1"
|
||||||
import { getAppVersion } from "./utils/app-version";
|
import { getAppVersion } from "./utils/app-version";
|
||||||
|
|
||||||
export class ClusterStore {
|
export class ClusterStore {
|
||||||
@ -25,7 +26,8 @@ export class ClusterStore {
|
|||||||
"2.6.0-beta.2": version260Beta2.migration,
|
"2.6.0-beta.2": version260Beta2.migration,
|
||||||
"2.6.0-beta.3": version260Beta3.migration,
|
"2.6.0-beta.3": version260Beta3.migration,
|
||||||
"2.7.0-beta.0": version270Beta0.migration,
|
"2.7.0-beta.0": version270Beta0.migration,
|
||||||
"2.7.0-beta.1": version270Beta1.migration
|
"2.7.0-beta.1": version270Beta1.migration,
|
||||||
|
"3.5.0-beta.1": version350Beta1.migration
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -72,7 +74,8 @@ export class ClusterStore {
|
|||||||
const index = clusters.findIndex((cl) => cl.id === cluster.id)
|
const index = clusters.findIndex((cl) => cl.id === cluster.id)
|
||||||
const storable = {
|
const storable = {
|
||||||
id: cluster.id,
|
id: cluster.id,
|
||||||
kubeConfig: cluster.kubeConfig,
|
kubeConfigPath: cluster.kubeConfigPath,
|
||||||
|
contextName: cluster.contextName,
|
||||||
preferences: cluster.preferences,
|
preferences: cluster.preferences,
|
||||||
workspace: cluster.workspace
|
workspace: cluster.workspace
|
||||||
}
|
}
|
||||||
@ -95,7 +98,8 @@ export class ClusterStore {
|
|||||||
public reloadCluster(cluster: ClusterBaseInfo): void {
|
public reloadCluster(cluster: ClusterBaseInfo): void {
|
||||||
const storedCluster = this.getCluster(cluster.id);
|
const storedCluster = this.getCluster(cluster.id);
|
||||||
if (storedCluster) {
|
if (storedCluster) {
|
||||||
cluster.kubeConfig = storedCluster.kubeConfig
|
cluster.kubeConfigPath = storedCluster.kubeConfigPath
|
||||||
|
cluster.contextName = storedCluster.contextName
|
||||||
cluster.preferences = storedCluster.preferences
|
cluster.preferences = storedCluster.preferences
|
||||||
cluster.workspace = storedCluster.workspace
|
cluster.workspace = storedCluster.workspace
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,12 +44,14 @@ export class ClusterManager {
|
|||||||
this.clusters = new Map()
|
this.clusters = new Map()
|
||||||
clusters.forEach((clusterInfo) => {
|
clusters.forEach((clusterInfo) => {
|
||||||
try {
|
try {
|
||||||
const kc = this.loadKubeConfig(clusterInfo.kubeConfig)
|
const kc = this.loadKubeConfig(clusterInfo.kubeConfigPath)
|
||||||
|
kc.setCurrentContext(clusterInfo.contextName)
|
||||||
logger.debug(`Starting to load target definitions for ${ kc.currentContext }`)
|
logger.debug(`Starting to load target definitions for ${ kc.currentContext }`)
|
||||||
const cluster = new Cluster({
|
const cluster = new Cluster({
|
||||||
id: clusterInfo.id,
|
id: clusterInfo.id,
|
||||||
port: this.port,
|
port: this.port,
|
||||||
kubeConfig: clusterInfo.kubeConfig,
|
kubeConfigPath: clusterInfo.kubeConfigPath,
|
||||||
|
contextName: clusterInfo.contextName,
|
||||||
preferences: clusterInfo.preferences,
|
preferences: clusterInfo.preferences,
|
||||||
workspace: clusterInfo.workspace
|
workspace: clusterInfo.workspace
|
||||||
})
|
})
|
||||||
@ -77,33 +79,35 @@ export class ClusterManager {
|
|||||||
clusters.map(cluster => cluster.stopServer())
|
clusters.map(cluster => cluster.stopServer())
|
||||||
}
|
}
|
||||||
|
|
||||||
protected loadKubeConfig(config: string): KubeConfig {
|
protected loadKubeConfig(configPath: string): KubeConfig {
|
||||||
const kc = new KubeConfig();
|
const kc = new KubeConfig();
|
||||||
kc.loadFromString(config);
|
kc.loadFromFile(configPath)
|
||||||
return kc;
|
return kc;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async addNewCluster(clusterData: ClusterBaseInfo): Promise<Cluster> {
|
protected async addNewCluster(clusterData: ClusterBaseInfo): Promise<Cluster> {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
|
|
||||||
|
logger.info(`*******addNewCluster: ${JSON.stringify(clusterData)}`)
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const configs: KubeConfig[] = k8s.loadAndSplitConfig(clusterData.kubeConfig)
|
const kc = this.loadKubeConfig(clusterData.kubeConfigPath)
|
||||||
if(configs.length == 0) {
|
k8s.validateConfig(kc)
|
||||||
reject("No cluster contexts defined")
|
kc.setCurrentContext(clusterData.contextName)
|
||||||
}
|
const cluster = new Cluster({
|
||||||
configs.forEach(c => {
|
id: uuid(),
|
||||||
k8s.validateConfig(c)
|
port: this.port,
|
||||||
const cluster = new Cluster({
|
kubeConfigPath: clusterData.kubeConfigPath,
|
||||||
id: uuid(),
|
contextName: clusterData.contextName,
|
||||||
port: this.port,
|
preferences: clusterData.preferences,
|
||||||
kubeConfig: k8s.dumpConfigYaml(c),
|
workspace: clusterData.workspace
|
||||||
preferences: clusterData.preferences,
|
})
|
||||||
workspace: clusterData.workspace
|
cluster.init(kc)
|
||||||
})
|
cluster.save()
|
||||||
cluster.init(c)
|
this.clusters.set(cluster.id, cluster)
|
||||||
cluster.save()
|
resolve(cluster)
|
||||||
this.clusters.set(cluster.id, cluster)
|
|
||||||
resolve(cluster)
|
|
||||||
});
|
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
logger.error(error)
|
logger.error(error)
|
||||||
reject(error)
|
reject(error)
|
||||||
|
|||||||
@ -6,9 +6,9 @@ import logger from "./logger"
|
|||||||
import { AuthorizationV1Api, CoreV1Api, KubeConfig, V1ResourceAttributes } from "@kubernetes/client-node"
|
import { AuthorizationV1Api, CoreV1Api, KubeConfig, V1ResourceAttributes } from "@kubernetes/client-node"
|
||||||
import * as fm from "./feature-manager";
|
import * as fm from "./feature-manager";
|
||||||
import { Kubectl } from "./kubectl";
|
import { Kubectl } from "./kubectl";
|
||||||
|
import { KubeconfigManager } from "./kubeconfig-manager"
|
||||||
import { PromiseIpc } from "electron-promise-ipc"
|
import { PromiseIpc } from "electron-promise-ipc"
|
||||||
import request from "request-promise-native"
|
import request from "request-promise-native"
|
||||||
import { KubeconfigManager } from "./kubeconfig-manager"
|
|
||||||
import { apiPrefix } from "../common/vars";
|
import { apiPrefix } from "../common/vars";
|
||||||
|
|
||||||
enum ClusterStatus {
|
enum ClusterStatus {
|
||||||
@ -19,7 +19,8 @@ enum ClusterStatus {
|
|||||||
|
|
||||||
export interface ClusterBaseInfo {
|
export interface ClusterBaseInfo {
|
||||||
id: string;
|
id: string;
|
||||||
kubeConfig: string;
|
kubeConfigPath: string;
|
||||||
|
contextName: string;
|
||||||
preferences?: ClusterPreferences;
|
preferences?: ClusterPreferences;
|
||||||
port?: number;
|
port?: number;
|
||||||
workspace?: string;
|
workspace?: string;
|
||||||
@ -73,7 +74,7 @@ export class Cluster implements ClusterInfo {
|
|||||||
public isAdmin: boolean;
|
public isAdmin: boolean;
|
||||||
public features: FeatureStatusMap;
|
public features: FeatureStatusMap;
|
||||||
public kubeCtl: Kubectl
|
public kubeCtl: Kubectl
|
||||||
public kubeConfig: string;
|
public kubeConfigPath: string;
|
||||||
public eventCount: number;
|
public eventCount: number;
|
||||||
public preferences: ClusterPreferences;
|
public preferences: ClusterPreferences;
|
||||||
|
|
||||||
@ -85,16 +86,16 @@ export class Cluster implements ClusterInfo {
|
|||||||
constructor(clusterInfo: ClusterBaseInfo) {
|
constructor(clusterInfo: ClusterBaseInfo) {
|
||||||
if (clusterInfo) Object.assign(this, clusterInfo)
|
if (clusterInfo) Object.assign(this, clusterInfo)
|
||||||
if (!this.preferences) this.preferences = {}
|
if (!this.preferences) this.preferences = {}
|
||||||
this.kubeconfigManager = new KubeconfigManager(this.kubeConfig)
|
this.kubeconfigManager = new KubeconfigManager(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
public kubeconfigPath() {
|
public proxyKubeconfigPath() {
|
||||||
return this.kubeconfigManager.getPath()
|
return this.kubeconfigManager.getPath()
|
||||||
}
|
}
|
||||||
|
|
||||||
public async init(kc: KubeConfig) {
|
public async init(kc: KubeConfig) {
|
||||||
this.contextHandler = new ContextHandler(kc, this)
|
this.contextHandler = new ContextHandler(kc, this)
|
||||||
this.contextName = kc.currentContext
|
//this.contextName = kc.currentContext
|
||||||
this.url = this.contextHandler.url
|
this.url = this.contextHandler.url
|
||||||
this.apiUrl = kc.getCurrentCluster().server
|
this.apiUrl = kc.getCurrentCluster().server
|
||||||
}
|
}
|
||||||
@ -138,15 +139,13 @@ export class Cluster implements ClusterInfo {
|
|||||||
this.eventCount = await this.getEventCount();
|
this.eventCount = await this.getEventCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
public updateKubeconfig(kubeconfig: string) {
|
// public updateKubeconfig(kubeconfig: string) {
|
||||||
const storedCluster = clusterStore.getCluster(this.id)
|
// const storedCluster = clusterStore.getCluster(this.id)
|
||||||
if (!storedCluster) {
|
// if (!storedCluster) { return }
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
this.kubeConfig = kubeconfig
|
// this.kubeConfig = kubeconfig
|
||||||
this.save()
|
// this.save()
|
||||||
}
|
// }
|
||||||
|
|
||||||
public getPrometheusApiPrefix() {
|
public getPrometheusApiPrefix() {
|
||||||
if (!this.preferences.prometheus?.prefix) {
|
if (!this.preferences.prometheus?.prefix) {
|
||||||
@ -175,7 +174,7 @@ export class Cluster implements ClusterInfo {
|
|||||||
isAdmin: this.isAdmin,
|
isAdmin: this.isAdmin,
|
||||||
features: this.features,
|
features: this.features,
|
||||||
kubeCtl: this.kubeCtl,
|
kubeCtl: this.kubeCtl,
|
||||||
kubeConfig: this.kubeConfig,
|
kubeConfigPath: this.kubeConfigPath,
|
||||||
preferences: this.preferences
|
preferences: this.preferences
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,9 @@ import { getFreePort } from "./port"
|
|||||||
import { KubeAuthProxy } from "./kube-auth-proxy"
|
import { KubeAuthProxy } from "./kube-auth-proxy"
|
||||||
import { Cluster, ClusterPreferences } from "./cluster"
|
import { Cluster, ClusterPreferences } from "./cluster"
|
||||||
import { prometheusProviders } from "../common/prometheus-providers"
|
import { prometheusProviders } from "../common/prometheus-providers"
|
||||||
import { PrometheusProvider, PrometheusService } from "./prometheus/provider-registry"
|
import { PrometheusService, PrometheusProvider } from "./prometheus/provider-registry"
|
||||||
|
import { PrometheusLens } from "./prometheus/lens"
|
||||||
|
import { KubeconfigManager } from "./kubeconfig-manager"
|
||||||
|
|
||||||
export class ContextHandler {
|
export class ContextHandler {
|
||||||
public contextName: string
|
public contextName: string
|
||||||
@ -35,36 +37,37 @@ export class ContextHandler {
|
|||||||
|
|
||||||
constructor(kc: KubeConfig, cluster: Cluster) {
|
constructor(kc: KubeConfig, cluster: Cluster) {
|
||||||
this.id = cluster.id
|
this.id = cluster.id
|
||||||
this.kc = new KubeConfig()
|
this.kc = kc
|
||||||
this.kc.users = [
|
logger.info(`**** ctxHandler.kc: ${JSON.stringify(kc, null, 2)}`)
|
||||||
{
|
// this.kc.users = [
|
||||||
name: kc.getCurrentUser().name,
|
// {
|
||||||
token: this.id
|
// name: kc.getCurrentUser().name,
|
||||||
}
|
// token: this.id
|
||||||
]
|
// }
|
||||||
this.kc.contexts = [
|
// ]
|
||||||
{
|
// this.kc.contexts = [
|
||||||
name: kc.currentContext,
|
// {
|
||||||
cluster: kc.getCurrentCluster().name,
|
// name: kc.currentContext,
|
||||||
user: kc.getCurrentUser().name,
|
// cluster: kc.getCurrentCluster().name,
|
||||||
namespace: kc.getContextObject(kc.currentContext).namespace
|
// user: kc.getCurrentUser().name,
|
||||||
}
|
// namespace: kc.getContextObject(kc.currentContext).namespace
|
||||||
]
|
// }
|
||||||
this.kc.setCurrentContext(kc.currentContext)
|
// ]
|
||||||
|
this.kc.setCurrentContext(cluster.contextName)
|
||||||
|
|
||||||
this.cluster = cluster
|
this.cluster = cluster
|
||||||
this.clusterUrl = url.parse(kc.getCurrentCluster().server)
|
this.clusterUrl = url.parse(kc.getCurrentCluster().server)
|
||||||
this.contextName = kc.currentContext;
|
this.contextName = cluster.contextName;
|
||||||
this.defaultNamespace = kc.getContextObject(kc.currentContext).namespace
|
this.defaultNamespace = kc.getContextObject(kc.currentContext).namespace
|
||||||
this.url = `http://${this.id}.localhost:${cluster.port}/`
|
this.url = `http://${this.id}.localhost:${cluster.port}/`
|
||||||
this.kubernetesApi = `http://127.0.0.1:${cluster.port}/${this.id}`
|
this.kubernetesApi = `http://127.0.0.1:${cluster.port}/${this.id}`
|
||||||
this.kc.clusters = [
|
// this.kc.clusters = [
|
||||||
{
|
// {
|
||||||
name: kc.getCurrentCluster().name,
|
// name: kc.getCurrentCluster().name,
|
||||||
server: this.kubernetesApi,
|
// server: this.kubernetesApi,
|
||||||
skipTLSVerify: true
|
// skipTLSVerify: true
|
||||||
}
|
// }
|
||||||
]
|
// ]
|
||||||
this.setClusterPreferences(cluster.preferences)
|
this.setClusterPreferences(cluster.preferences)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +177,7 @@ export class ContextHandler {
|
|||||||
|
|
||||||
public async withTemporaryKubeconfig(callback: (kubeconfig: string) => Promise<any>) {
|
public async withTemporaryKubeconfig(callback: (kubeconfig: string) => Promise<any>) {
|
||||||
try {
|
try {
|
||||||
await callback(this.cluster.kubeconfigPath())
|
await callback(this.cluster.proxyKubeconfigPath())
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw(error)
|
throw(error)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -54,7 +54,7 @@ export class HelmReleaseManager {
|
|||||||
await fs.promises.writeFile(fileName, yaml.safeDump(values))
|
await fs.promises.writeFile(fileName, yaml.safeDump(values))
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { stdout, stderr } = await promiseExec(`"${helm}" upgrade ${name} ${chart} --version ${version} -f ${fileName} --namespace ${namespace} --kubeconfig ${cluster.kubeconfigPath()}`).catch((error) => { throw(error.stderr)})
|
const { stdout, stderr } = await promiseExec(`"${helm}" upgrade ${name} ${chart} --version ${version} -f ${fileName} --namespace ${namespace} --kubeconfig ${cluster.proxyKubeconfigPath()}`).catch((error) => { throw(error.stderr)})
|
||||||
return {
|
return {
|
||||||
log: stdout,
|
log: stdout,
|
||||||
release: this.getRelease(name, namespace, cluster)
|
release: this.getRelease(name, namespace, cluster)
|
||||||
@ -66,7 +66,7 @@ export class HelmReleaseManager {
|
|||||||
|
|
||||||
public async getRelease(name: string, namespace: string, cluster: Cluster) {
|
public async getRelease(name: string, namespace: string, cluster: Cluster) {
|
||||||
const helm = await helmCli.binaryPath()
|
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 {stdout, stderr} = await promiseExec(`"${helm}" status ${name} --output json --namespace ${namespace} --kubeconfig ${cluster.proxyKubeconfigPath()}`).catch((error) => { throw(error.stderr)})
|
||||||
const release = JSON.parse(stdout)
|
const release = JSON.parse(stdout)
|
||||||
release.resources = await this.getResources(name, namespace, cluster)
|
release.resources = await this.getResources(name, namespace, cluster)
|
||||||
return release
|
return release
|
||||||
@ -100,7 +100,7 @@ export class HelmReleaseManager {
|
|||||||
protected async getResources(name: string, namespace: string, cluster: Cluster) {
|
protected async getResources(name: string, namespace: string, cluster: Cluster) {
|
||||||
const helm = await helmCli.binaryPath()
|
const helm = await helmCli.binaryPath()
|
||||||
const kubectl = await cluster.kubeCtl.kubectlPath()
|
const kubectl = await cluster.kubeCtl.kubectlPath()
|
||||||
const pathToKubeconfig = cluster.kubeconfigPath()
|
const pathToKubeconfig = cluster.proxyKubeconfigPath()
|
||||||
const { stdout } = await promiseExec(`"${helm}" get manifest ${name} --namespace ${namespace} --kubeconfig ${pathToKubeconfig} | "${kubectl}" get -n ${namespace} --kubeconfig ${pathToKubeconfig} -f - -o=json`).catch((error) => {
|
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: JSON.stringify({items: []})}
|
||||||
})
|
})
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import { releaseManager } from "./helm-release-manager";
|
|||||||
|
|
||||||
class HelmService {
|
class HelmService {
|
||||||
public async installChart(cluster: Cluster, data: {chart: string; values: {}; name: string; namespace: string; version: string}) {
|
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())
|
const installResult = await releaseManager.installChart(data.chart, data.values, data.name, data.namespace, data.version, cluster.proxyKubeconfigPath())
|
||||||
return installResult
|
return installResult
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ class HelmService {
|
|||||||
|
|
||||||
public async listReleases(cluster: Cluster, namespace: string = null) {
|
public async listReleases(cluster: Cluster, namespace: string = null) {
|
||||||
await repoManager.init()
|
await repoManager.init()
|
||||||
const releases = await releaseManager.listReleases(cluster.kubeconfigPath(), namespace)
|
const releases = await releaseManager.listReleases(cluster.proxyKubeconfigPath(), namespace)
|
||||||
return releases
|
return releases
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,19 +60,19 @@ class HelmService {
|
|||||||
|
|
||||||
public async getReleaseValues(cluster: Cluster, releaseName: string, namespace: string) {
|
public async getReleaseValues(cluster: Cluster, releaseName: string, namespace: string) {
|
||||||
logger.debug("Fetch release values")
|
logger.debug("Fetch release values")
|
||||||
const values = await releaseManager.getValues(releaseName, namespace, cluster.kubeconfigPath())
|
const values = await releaseManager.getValues(releaseName, namespace, cluster.proxyKubeconfigPath())
|
||||||
return values
|
return values
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getReleaseHistory(cluster: Cluster, releaseName: string, namespace: string) {
|
public async getReleaseHistory(cluster: Cluster, releaseName: string, namespace: string) {
|
||||||
logger.debug("Fetch release history")
|
logger.debug("Fetch release history")
|
||||||
const history = await releaseManager.getHistory(releaseName, namespace, cluster.kubeconfigPath())
|
const history = await releaseManager.getHistory(releaseName, namespace, cluster.proxyKubeconfigPath())
|
||||||
return(history)
|
return(history)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteRelease(cluster: Cluster, releaseName: string, namespace: string) {
|
public async deleteRelease(cluster: Cluster, releaseName: string, namespace: string) {
|
||||||
logger.debug("Delete release")
|
logger.debug("Delete release")
|
||||||
const release = await releaseManager.deleteRelease(releaseName, namespace, cluster.kubeconfigPath())
|
const release = await releaseManager.deleteRelease(releaseName, namespace, cluster.proxyKubeconfigPath())
|
||||||
return release
|
return release
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ class HelmService {
|
|||||||
|
|
||||||
public async rollback(cluster: Cluster, releaseName: string, namespace: string, revision: number) {
|
public async rollback(cluster: Cluster, releaseName: string, namespace: string, revision: number) {
|
||||||
logger.debug("Rollback release")
|
logger.debug("Rollback release")
|
||||||
const output = await releaseManager.rollback(releaseName, namespace, revision, cluster.kubeconfigPath())
|
const output = await releaseManager.rollback(releaseName, namespace, revision, cluster.proxyKubeconfigPath())
|
||||||
return({ message: output })
|
return({ message: output })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -70,13 +70,13 @@ export function splitConfig(kubeConfig: k8s.KubeConfig): k8s.KubeConfig[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads KubeConfig from a yaml string and breaks it into several configs. Each context
|
* Loads KubeConfig from a yaml and breaks it into several configs. Each context per KubeConfig object
|
||||||
*
|
*
|
||||||
* @param configString yaml string of kube config
|
* @param configPath path to kube config yaml file
|
||||||
*/
|
*/
|
||||||
export function loadAndSplitConfig(configString: string): k8s.KubeConfig[] {
|
export function loadAndSplitConfig(configPath: string): k8s.KubeConfig[] {
|
||||||
const allConfigs = new k8s.KubeConfig();
|
const allConfigs = new k8s.KubeConfig();
|
||||||
allConfigs.loadFromString(configString);
|
allConfigs.loadFromFile(configPath);
|
||||||
return splitConfig(allConfigs);
|
return splitConfig(allConfigs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -31,26 +31,18 @@ export class KubeAuthProxy {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const proxyBin = await this.kubectl.kubectlPath()
|
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...`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
const clusterUrl = url.parse(this.cluster.apiUrl)
|
|
||||||
let args = [
|
let args = [
|
||||||
"proxy",
|
"proxy",
|
||||||
"--port", this.port.toString(),
|
"-p", this.port.toString(),
|
||||||
"--kubeconfig", this.cluster.kubeconfigPath(),
|
"--kubeconfig", this.cluster.kubeConfigPath,
|
||||||
"--accept-hosts", clusterUrl.hostname,
|
"--context", this.cluster.contextName,
|
||||||
|
"--accept-hosts", ".*",
|
||||||
|
"--reject-paths", "^[^/]",
|
||||||
]
|
]
|
||||||
if (process.env.DEBUG_PROXY === "true") {
|
if (process.env.DEBUG_PROXY === "true") {
|
||||||
args = args.concat(["-v", "9"])
|
args = args.concat(["-v", "9"])
|
||||||
}
|
}
|
||||||
|
logger.debug(`spawning kubectl proxy with args: ${args}`)
|
||||||
this.proxyProcess = spawn(proxyBin, args, {
|
this.proxyProcess = spawn(proxyBin, args, {
|
||||||
env: this.env
|
env: this.env
|
||||||
})
|
})
|
||||||
@ -60,7 +52,6 @@ export class KubeAuthProxy {
|
|||||||
logger.debug("failed to send IPC log message: " + err.message)
|
logger.debug("failed to send IPC log message: " + err.message)
|
||||||
})
|
})
|
||||||
this.proxyProcess = null
|
this.proxyProcess = null
|
||||||
configWatcher.close()
|
|
||||||
})
|
})
|
||||||
this.proxyProcess.stdout.on('data', (data) => {
|
this.proxyProcess.stdout.on('data', (data) => {
|
||||||
let logItem = data.toString()
|
let logItem = data.toString()
|
||||||
|
|||||||
@ -2,14 +2,17 @@ import { app } from "electron"
|
|||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
import { ensureDir, randomFileName} from "./file-helpers"
|
import { ensureDir, randomFileName} from "./file-helpers"
|
||||||
import logger from "./logger"
|
import logger from "./logger"
|
||||||
|
import { Cluster } from "./cluster"
|
||||||
|
import * as k8s from "./k8s"
|
||||||
|
import { KubeConfig } from "@kubernetes/client-node"
|
||||||
|
|
||||||
export class KubeconfigManager {
|
export class KubeconfigManager {
|
||||||
protected configDir = app.getPath("temp")
|
protected configDir = app.getPath("temp")
|
||||||
protected kubeconfig: string
|
|
||||||
protected tempFile: string
|
protected tempFile: string
|
||||||
|
protected cluster: Cluster
|
||||||
|
|
||||||
constructor(kubeconfig: string) {
|
constructor(cluster: Cluster) {
|
||||||
this.kubeconfig = kubeconfig
|
this.cluster = cluster
|
||||||
this.tempFile = this.createTemporaryKubeconfig()
|
this.tempFile = this.createTemporaryKubeconfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,7 +24,32 @@ export class KubeconfigManager {
|
|||||||
ensureDir(this.configDir)
|
ensureDir(this.configDir)
|
||||||
const path = `${this.configDir}/${randomFileName("kubeconfig")}`
|
const path = `${this.configDir}/${randomFileName("kubeconfig")}`
|
||||||
logger.debug('Creating temporary kubeconfig: ' + path)
|
logger.debug('Creating temporary kubeconfig: ' + path)
|
||||||
fs.writeFileSync(path, this.kubeconfig)
|
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
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ class ResourceApplierApi extends LensApi {
|
|||||||
public async applyResource(request: LensApiRequest) {
|
public async applyResource(request: LensApiRequest) {
|
||||||
const { response, cluster, payload } = request
|
const { response, cluster, payload } = request
|
||||||
try {
|
try {
|
||||||
const resource = await resourceApplier.apply(cluster, cluster.kubeconfigPath(), payload)
|
const resource = await resourceApplier.apply(cluster, cluster.proxyKubeconfigPath(), payload)
|
||||||
this.respondJson(response, [resource], 200)
|
this.respondJson(response, [resource], 200)
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
this.respondText(response, error, 422)
|
this.respondText(response, error, 422)
|
||||||
|
|||||||
@ -87,7 +87,7 @@ class PortForwardRoute extends LensApi {
|
|||||||
namespace: params.namespace,
|
namespace: params.namespace,
|
||||||
name: params.service,
|
name: params.service,
|
||||||
port: params.port,
|
port: params.port,
|
||||||
kubeConfig: cluster.kubeconfigPath()
|
kubeConfig: cluster.proxyKubeconfigPath()
|
||||||
})
|
})
|
||||||
const started = await portForward.start()
|
const started = await portForward.start()
|
||||||
if (!started) {
|
if (!started) {
|
||||||
|
|||||||
32
src/migrations/cluster-store/3.5.0-beta.1.ts
Normal file
32
src/migrations/cluster-store/3.5.0-beta.1.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
// move embedded kubeconfig into separate file and add reference to it to cluster settings
|
||||||
|
import { app } from "electron"
|
||||||
|
import { ensureDirSync, writeFileSync } from "fs-extra"
|
||||||
|
import * as path from "path"
|
||||||
|
|
||||||
|
export function migration(store: any) {
|
||||||
|
console.log("CLUSTER STORE, MIGRATION: 3.5.0-beta.1");
|
||||||
|
const clusters: any[] = []
|
||||||
|
|
||||||
|
const kubeConfigBase = path.join(app.getPath("userData"), "kubeconfigs")
|
||||||
|
ensureDirSync(kubeConfigBase)
|
||||||
|
let storedClusters = store.get("clusters") as any[]
|
||||||
|
if (!storedClusters) return
|
||||||
|
|
||||||
|
console.log("num clusters to migrate: ", storedClusters.length)
|
||||||
|
for (let cluster of storedClusters ) {
|
||||||
|
// take the embedded kubeconfig and dump it into a file
|
||||||
|
const kubeConfigFile = path.join(kubeConfigBase, cluster.id)
|
||||||
|
writeFileSync(kubeConfigFile, cluster.kubeConfig)
|
||||||
|
delete cluster.kubeConfig
|
||||||
|
cluster.kubeConfigPath = kubeConfigFile
|
||||||
|
// TODO Need to parse the context name from the config
|
||||||
|
|
||||||
|
|
||||||
|
// "overwrite" the cluster configs
|
||||||
|
clusters.push(cluster)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clusters.length > 0) {
|
||||||
|
store.set("clusters", clusters)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -113,6 +113,7 @@ import * as PrismEditor from 'vue-prism-editor'
|
|||||||
import * as k8s from "@kubernetes/client-node"
|
import * as k8s from "@kubernetes/client-node"
|
||||||
import { dumpConfigYaml } from "../../../main/k8s"
|
import { dumpConfigYaml } from "../../../main/k8s"
|
||||||
import ClustersMixin from "@/_vue/mixins/ClustersMixin";
|
import ClustersMixin from "@/_vue/mixins/ClustersMixin";
|
||||||
|
import * as path from "path"
|
||||||
|
|
||||||
class ClusterAccessError extends Error {}
|
class ClusterAccessError extends Error {}
|
||||||
|
|
||||||
@ -196,8 +197,13 @@ export default {
|
|||||||
try {
|
try {
|
||||||
const kc = new k8s.KubeConfig();
|
const kc = new k8s.KubeConfig();
|
||||||
kc.loadFromString(this.clusterconfig); // throws TypeError if we cannot parse kubeconfig
|
kc.loadFromString(this.clusterconfig); // throws TypeError if we cannot parse kubeconfig
|
||||||
|
|
||||||
|
// TODO Remove hardcoded path
|
||||||
|
const configPath = path.join(process.env.HOME, '.kube', 'config');
|
||||||
|
|
||||||
const clusterInfo = {
|
const clusterInfo = {
|
||||||
kubeConfig: dumpConfigYaml(kc),
|
kubeConfigPath: configPath,
|
||||||
|
contextName: kc.currentContext,
|
||||||
preferences: {
|
preferences: {
|
||||||
clusterName: kc.currentContext
|
clusterName: kc.currentContext
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user