-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathkubeUtils.ts
104 lines (93 loc) · 3.83 KB
/
kubeUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as fs from 'fs';
import * as path from 'path';
import { QuickPickItem } from 'vscode';
import { KubeConfig, findHomeDir, loadYaml } from '@kubernetes/client-node';
import { User, Cluster } from '@kubernetes/client-node/dist/config_types';
function fileExists(file: string): boolean {
try {
fs.accessSync(file);
return true;
} catch ( _ ) {
return false;
}
}
export class KubeConfigUtils extends KubeConfig {
public readonly loadingError: any;
constructor() {
super();
try {
this.loadFromDefault();
} catch (error) {
throw new Error('Kubernetes configuration cannot be loaded. Please check configuration files for errors and fix them to continue.');
}
// k8s nodejs-client ignores all unknown properties,
// so cluster object's proxy-url attribute is not present
// after k8s config loaded
}
findHomeDir() {
return findHomeDir();
}
getProxy(contextName: string): string | undefined {
if (process.env.KUBECONFIG?.[1]) {
const cFiles = process.env.KUBECONFIG.split(path.delimiter).filter(file => file);
//const yaml =
for (let i=0; i < cFiles.length; i++) {
const proxyUrl = this.getClusterProxyFromFile(cFiles[i], contextName);
if (proxyUrl) return proxyUrl;
}
return;
}
const home = this.findHomeDir();
if (home) {
const config = path.join(home, '.kube', 'config');
if (fileExists(config)) {
return this.getClusterProxyFromFile(config, contextName);
}
}
}
getClusterProxyFromFile(file: string, contextName: string): string {
const fileContent = fs.readFileSync(file, 'utf8');
const yaml: any = loadYaml(fileContent);
const contextObj = yaml.contexts.find(
(context) => context.name === contextName);
const clusterObj = yaml.clusters.find(
(cluster) => cluster.name === contextObj?.context?.cluster);
return clusterObj?.cluster?.['proxy-url'];
}
public getServers(): QuickPickItem[] {
const currentCluster = this.getCurrentCluster();
const clusters = this.clusters || [];
const qpItems = clusters.map((c: Cluster) => ({
label: c.server,
description: currentCluster && c.name === currentCluster.name ? 'Current Context' : '',
}));
const filterMap = new Set();
return qpItems.filter((item) => {
const notDuplicate = !filterMap.has(item.label);
if(notDuplicate) {
filterMap.add(item.label);
}
return notDuplicate;
});
}
public getClusterUsers(clusterServer: string): QuickPickItem[] {
const currentUser = this.getCurrentUser();
const cluster = this.findCluster(clusterServer);
const users = this.getUsers();
const clusterUsers = users.filter((item) => cluster && item.name.includes(cluster.name));
return clusterUsers.map((u: User) => {
const userName = u.name.split('/')[0];
return {
label: userName === 'kube:admin' ? 'kubeadmin' : userName,
description: u === currentUser ? 'Current Context' : '',
};
});
}
public findCluster(clusterServer: string): Cluster {
return this.getClusters().find((cluster: Cluster) => cluster.server === clusterServer);
}
}