forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkubeUtils.ts
More file actions
183 lines (164 loc) · 7.17 KB
/
kubeUtils.ts
File metadata and controls
183 lines (164 loc) · 7.17 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import { KubeConfig, findHomeDir, loadYaml } from '@kubernetes/client-node';
import { Cluster, User, Context } from '@kubernetes/client-node/dist/config_types';
import * as fs from 'fs';
import * as path from 'path';
import { QuickPickItem, window } from 'vscode';
import { Platform } from './platform';
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);
}
public findClusterURL(clusterNameOrURL: string): string {
let clusterObj: Cluster = this.findCluster(clusterNameOrURL);
clusterObj = clusterObj || this.clusters.find((cluster: Cluster) => cluster.name === clusterNameOrURL);
return clusterObj ? clusterObj.server : undefined;
}
public findContext(contextName: string): Context {
return this.getContexts().find((context: Context) => context.name === contextName);
}
public findContextForCluster(clusterName: string): Context {
return this.getContexts().find((context: Context) => context.cluster === clusterName);
}
public extractProjectNameFromCurrentContext():string {
const currentContextName = this.getCurrentContext();
return this.extractProjectNameFromContextName(currentContextName);
}
public extractProjectNameFromContextName(contextName: string):string {
if (contextName && contextName.includes('/') && !contextName.startsWith('/')) {
return contextName.split('/')[0];
}
return undefined;
}
public equalsToCurrentContext(contextName:string, cluster: string, namespace: string, user: string): boolean {
const currentContext = this.findContext(this.currentContext);
if (!currentContext) return false;
if (currentContext.name !== contextName) return false;
if (currentContext.cluster !== cluster) return false;
if (currentContext.namespace !== namespace) return false;
if (currentContext.user !== user) return false;
return true;
}
}
/**
* Returns the list of kube config files:
* - If KUBECONFIG is not set, just ~/.kube/config
* - If KUBECONFIG is set, follows the semantics for specifying multiple config files described here:
* https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#append-home-kube-config-to-your-kubeconfig-environment-variable
* BUT: it shows an error if multiple configs are specified, since the Kubernetes client we are using doesn't support this use case.
*/
export function getKubeConfigFiles(): string[] {
if (process.env.KUBECONFIG) {
const configuredFiles: string[] = process.env.KUBECONFIG.split(path.delimiter);
const filesThatExist: string[] = [];
for (const configFile of configuredFiles) {
if (fs.existsSync(configFile)) {
filesThatExist.push(configFile);
}
}
return filesThatExist;
}
return [path.join(Platform.getUserHomePath(), '.kube', 'config')];
}
/**
* If there are multiple kube config files set, force the user to pick one to use.
*/
export async function setKubeConfig(): Promise<void> {
const kubeConfigFiles = getKubeConfigFiles();
if (kubeConfigFiles.length > 1) {
let selectedFile;
while(!selectedFile) {
try {
const potentialSelection = await window.showQuickPick(kubeConfigFiles, { canPickMany: false, placeHolder: 'VSCode OpenShift only supports using one kube config. Please select which one to use.' });
if (potentialSelection) {
selectedFile = potentialSelection;
}
} catch (_) {
// do nothing
}
}
process.env.KUBECONFIG = selectedFile;
}
}