forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplorer.ts
More file actions
435 lines (391 loc) · 18.3 KB
/
explorer.ts
File metadata and controls
435 lines (391 loc) · 18.3 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import { Context, KubernetesObject } from '@kubernetes/client-node';
import * as fs from 'fs';
import * as path from 'path';
import {
Disposable,
Event,
EventEmitter,
ThemeIcon,
TreeDataProvider,
TreeItem,
TreeItemCollapsibleState,
TreeView,
Uri,
commands,
extensions,
version,
window
} from 'vscode';
import { CommandText } from './base/command';
import * as Helm from './helm/helm';
import { HelmRepo } from './helm/helmChartType';
import { Oc } from './oc/ocWrapper';
import { Component } from './openshift/component';
import { getServiceKindStubs } from './openshift/serviceHelpers';
import { KubeConfigUtils, getKubeConfigFiles } from './util/kubeUtils';
import { Platform } from './util/platform';
import { Progress } from './util/progress';
import { FileContentChangeNotifier, WatchUtil } from './util/watch';
import { vsCommand } from './vscommand';
import { CustomResourceDefinitionStub } from './webview/common/createServiceTypes';
import { OpenShiftTerminalManager } from './webview/openshift-terminal/openShiftTerminal';
import { LoginUtil } from './util/loginUtil';
type ExplorerItem = KubernetesObject | Helm.HelmRelease | Context | TreeItem | OpenShiftObject | HelmRepo;
export type OpenShiftObject = {
kind: string,
metadata: {
name: string
},
}
type PackageJSON = {
version: string;
bugs: string;
};
function createOrSetProjectItem(projectName: string): ExplorerItem {
return {
label: `${projectName}`,
description: 'Missing project. Create new or set active Project',
tooltip: `${projectName} - Missing project. Create new or set active Project`,
iconPath: new ThemeIcon('warning'),
command: {
title: 'Create new or set active Project',
command: 'openshift.project.set'
}
};
}
export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Disposable {
private static instance: OpenShiftExplorer;
private treeView: TreeView<ExplorerItem>;
private kubeConfigWatchers: FileContentChangeNotifier[];
private kubeContext: Context;
private kubeConfig: KubeConfigUtils;
private eventEmitter: EventEmitter<ExplorerItem | undefined> =
new EventEmitter<ExplorerItem | undefined>();
readonly onDidChangeTreeData: Event<ExplorerItem | undefined> = this
.eventEmitter.event;
public onDidChangeContextEmitter = new EventEmitter<string>();
private constructor() {
try {
this.kubeConfig = new KubeConfigUtils();
this.kubeContext = this.kubeConfig.getContextObject(this.kubeConfig.currentContext);
} catch (err) {
// ignore config loading error and let odo report it on first call
}
try {
const kubeconfigFiles = getKubeConfigFiles();
this.kubeConfigWatchers = kubeconfigFiles.map(kubeconfigFile => WatchUtil.watchFileForContextChange(path.dirname(kubeconfigFile), path.basename(kubeconfigFile)));
} catch (err) {
void window.showWarningMessage('Couldn\'t install watcher for Kubernetes configuration file. OpenShift Application Explorer view won\'t be updated automatically.');
}
for (const fsw of this.kubeConfigWatchers) {
fsw.emitter?.on('file-changed', () => {
const ku2 = new KubeConfigUtils();
const newCtx = ku2.getContextObject(ku2.currentContext);
if (Boolean(this.kubeContext) !== Boolean(newCtx)
|| (this.kubeContext.cluster !== newCtx.cluster
|| this.kubeContext.user !== newCtx.user
|| this.kubeContext.namespace !== newCtx.namespace)) {
this.refresh();
this.onDidChangeContextEmitter.fire(newCtx.name);
}
this.kubeContext = newCtx;
this.kubeConfig = ku2;
});
}
this.treeView = window.createTreeView<ExplorerItem>('openshiftProjectExplorer', {
treeDataProvider: this,
});
Component.onDidStateChanged((_context) => {
this.refresh();
});
}
static getInstance(): OpenShiftExplorer {
if (!OpenShiftExplorer.instance) {
OpenShiftExplorer.instance = new OpenShiftExplorer();
}
return OpenShiftExplorer.instance;
}
private static generateOpenshiftProjectContextValue(namespace: string): Thenable<string> {
const contextValue = `openshift.project.${namespace}`;
return Oc.Instance.canDeleteNamespace(namespace)
.then(result => (result ? `${contextValue}.can-delete` : contextValue));
}
// eslint-disable-next-line class-methods-use-this
async getTreeItem(element: ExplorerItem): Promise<TreeItem> {
if ('command' in element) {
return element;
}
if ('label' in element) {
return {
contextValue: 'openshift.openConfigFile',
label: element.label,
collapsibleState: TreeItemCollapsibleState.None,
tooltip: element.label as string,
description: element.description,
iconPath: new ThemeIcon('file')
};
}
// check if element is Context instance
if ('name' in element && 'cluster' in element && 'user' in element) { // Context instance could be without namespace
void commands.executeCommand('setContext', 'isLoggedIn', true);
return {
contextValue: 'openshift.k8sContext',
label: this.kubeConfig.getCluster(element.cluster).server,
collapsibleState: TreeItemCollapsibleState.Collapsed,
iconPath: path.resolve(__dirname, '../../images/context/cluster-node.png')
};
}
if ('name' in element && 'url' in element) {
return {
contextValue: 'openshift.helm.repo',
label: element.name,
tooltip: element.url,
collapsibleState: TreeItemCollapsibleState.None,
iconPath: new ThemeIcon('repo')
};
}
// It's a Helm installation
if ('chart' in element) {
return {
contextValue: 'openshift.k8sObject.helm',
label: element.name,
collapsibleState: TreeItemCollapsibleState.None,
description: 'Helm Release',
iconPath: path.resolve(__dirname, '../../images/context/helm.png'),
};
}
// otherwise it is a KubernetesObject instance
if ('kind' in element) {
if (element.kind === 'project') {
return OpenShiftExplorer.generateOpenshiftProjectContextValue(element.metadata.name)
.then(namespace => {
return {
contextValue: namespace,
label: element.metadata.name,
collapsibleState: TreeItemCollapsibleState.Collapsed,
iconPath: path.resolve(__dirname, '../../images/context/project-node.png')
}
});
} else if (element.kind === 'helm') {
return {
contextValue: 'openshift.helm.repos',
label: element.metadata.name,
collapsibleState: TreeItemCollapsibleState.Collapsed,
description: 'Repositories',
iconPath: path.resolve(__dirname, '../../images/context/helm.png')
}
}
const routeURL = await Oc.Instance.getRouteURL(element.metadata.name);
return {
contextValue: `openshift.k8sObject.${element.kind}${routeURL ? '.route' : ''}`,
label: element.metadata.name,
description: `${element.kind.substring(0, 1).toLocaleUpperCase()}${element.kind.substring(1)}`,
collapsibleState: TreeItemCollapsibleState.None,
iconPath: path.resolve(__dirname, '../../images/context/component-node.png'),
command: {
title: 'Load',
command: 'openshift.resource.load',
arguments: [element]
}
};
}
return {
label: 'Unknown element'
}
}
// eslint-disable-next-line class-methods-use-this
async getChildren(element?: ExplorerItem): Promise<ExplorerItem[]> {
let result: ExplorerItem[] = [];
if (!element) {
try {
if (!await LoginUtil.Instance.requireLogin()) {
result = [this.kubeContext];
if (this.kubeContext) {
const config = getKubeConfigFiles();
void commands.executeCommand('setContext', 'canCreateNamespace', await Oc.Instance.canCreateNamespace());
result.unshift({ label: process.env.KUBECONFIG ? 'Custom KubeConfig' : 'Default KubeConfig', description: config.join(':') })
}
}
} catch (err) {
// ignore because ether server is not accessible or user is logged out
}
OpenShiftExplorer.getInstance().onDidChangeContextEmitter.fire(new KubeConfigUtils().currentContext);
} else if ('name' in element) { // we are dealing with context here
// user is logged into cluster from current context
// and project should be show as child node of current context
// there are several possible scenarios
// (1) there is no namespace set in context and default namespace/project exists
// * example is kubernetes provisioned with docker-desktop
// * could also be the case with CRC provisioned for the first time
// (2) there is no namespace set in context and default namespace/project does not exist
// * example is sandbox context created when login to sandbox first time
// (3) there is namespace set in context and namespace exists in the cluster
// (4) there is namespace set in context and namespace does not exist in the cluster
const namespaces = await Oc.Instance.getProjects();
const helmContext = {
kind: 'helm',
metadata: {
name: 'Helm'
},
} as OpenShiftObject
if (this.kubeContext.namespace) {
if (namespaces.find(item => item.name === this.kubeContext.namespace)) {
result = [{
kind: 'project',
metadata: {
name: this.kubeContext.namespace,
},
} as KubernetesObject]
} else {
result = [createOrSetProjectItem(this.kubeContext.namespace)];
}
} else {
// get list of projects or namespaces
// find default namespace
if (namespaces.find(item => item?.name === 'default')) {
result = [{
kind: 'project',
metadata: {
name: 'default',
},
} as KubernetesObject]
} else {
const projectName = this.kubeConfig.extractProjectNameFromCurrentContext() || 'default';
result = [createOrSetProjectItem(projectName)];
}
}
// The 'Create Service' menu visibility
let serviceKinds: CustomResourceDefinitionStub[] = [];
try {
if (await Oc.Instance.canGetKubernetesObjects('csv')) {
serviceKinds = await getServiceKindStubs();
}
} catch (_) {
// operator framework is not installed on cluster; do nothing
}
void commands.executeCommand('setContext', 'showCreateService', serviceKinds.length > 0);
// Add Helm Context
result.push(helmContext);
} else if ('kind' in element && element.kind === 'helm') {
const cliData = await Helm.getHelmRepos();
if (!cliData.error && !cliData.stderr) {
const helmRepos = JSON.parse(cliData.stdout) as HelmRepo[];
if (helmRepos?.length > 0) {
result = [...helmRepos.sort(Helm.ascRepoName)];
}
}
} else {
let serviceKinds: CustomResourceDefinitionStub[] = [];
try {
if (await Oc.Instance.canGetKubernetesObjects('csv')) {
serviceKinds = await getServiceKindStubs();
}
} catch (_) {
// operator framework is not installed on cluster; do nothing
}
const collectableServices: CustomResourceDefinitionStub[] = [];
await Promise.all(serviceKinds.map(async serviceKind => {
if (await Oc.Instance.canGetKubernetesObjects(serviceKind.name)) {
collectableServices.push(serviceKind);
}
}));
const toCollect = [
Oc.Instance.getKubernetesObjects('Deployment'),
Helm.getHelmReleases(),
...collectableServices
.map(serviceKind => Oc.Instance.getKubernetesObjects(serviceKind.name))
];
if (await Oc.Instance.isOpenShiftCluster()) {
toCollect.push(
Oc.Instance.getKubernetesObjects('DeploymentConfig'),
)
}
result = await Promise.all(toCollect).then(listOfLists => listOfLists.flatMap(a => a as ExplorerItem[]));
}
if (!element) {
await commands.executeCommand('setContext', 'openshift.app.explorer.init', result.length === 0);
} else {
// don't show Open In Developer Dashboard if not openshift cluster
const isOpenshiftCluster = await Oc.Instance.isOpenShiftCluster();
void commands.executeCommand('setContext', 'isOpenshiftCluster', isOpenshiftCluster);
}
return result;
}
refresh(target?: ExplorerItem): void {
this.eventEmitter.fire(target);
}
dispose(): void {
for (const fsw of this.kubeConfigWatchers) {
fsw?.watcher?.close();
}
this.treeView.dispose();
}
@vsCommand('openshift.resource.load')
public static loadResource(component: KubernetesObject) {
void commands.executeCommand('extension.vsKubernetesLoad', { namespace: component.metadata.namespace, kindName: `${component.kind}/${component.metadata.name}` });
}
@vsCommand('openshift.resource.delete')
public static async deleteResource(component: KubernetesObject) {
await Oc.Instance.deleteKubernetesObject(component.kind, component.metadata.name);
void window.showInformationMessage(`Deleted the '${component.kind}' named '${component.metadata.name}'`);
OpenShiftExplorer.instance.refresh();
}
@vsCommand('openshift.resource.watchLogs')
public static async watchLogs(component: KubernetesObject) {
// wait until logs are available before starting to stream them
await Progress.execFunctionWithProgress(`Opening ${component.kind}/${component.metadata.name} logs...`, (_) => {
return new Promise<void>(resolve => {
let intervalId: NodeJS.Timer = undefined;
function checkForPod() {
void Oc.Instance.getLogs('Deployment', component.metadata.name).then((logs) => {
clearInterval(intervalId);
resolve();
}).catch(_e => {});
}
intervalId = setInterval(checkForPod, 200);
});
});
void OpenShiftTerminalManager.getInstance().createTerminal(new CommandText('oc', `logs -f ${component.kind}/${component.metadata.name}`), `Watching '${component.metadata.name}' logs`);
}
@vsCommand('openshift.resource.unInstall')
public static async unInstallHelmChart(release: Helm.HelmRelease) {
return Progress.execFunctionWithProgress(`Uninstalling ${release.name}`, async () => {
await Helm.unInstallHelmChart(release.name);
OpenShiftExplorer.getInstance().refresh();
});
}
@vsCommand('openshift.resource.openInConsole')
public static openInConsole(component: KubernetesObject) {
void commands.executeCommand('extension.vsKubernetesLoad', { namespace: component.metadata.namespace, kindName: `${component.kind}/${component.metadata.name}` });
}
@vsCommand('openshift.explorer.reportIssue')
static async reportIssue(): Promise<unknown> {
const extensionPath = path.resolve(__dirname, '..', '..');
const templatePath = path.join(extensionPath, 'resources', 'issueReport.md');
const template = fs.readFileSync(templatePath, 'utf-8');
return commands.executeCommand('workbench.action.openIssueReporter', {
extensionId: 'redhat.vscode-openshift-connector',
issueBody: template
});
}
@vsCommand('openshift.open.configFile')
async openConfigFile(context: TreeItem): Promise<void> {
if (context.description && typeof context.description === 'string') {
await commands.executeCommand('vscode.open', Uri.file(context.description));
}
}
static issueUrl(): string {
const packageJSON: PackageJSON = extensions.getExtension('redhat.vscode-openshift-connector')
.packageJSON as PackageJSON;
const body = [
`VS Code version: ${version}`,
`OS: ${Platform.OS}`,
`Extension version: ${packageJSON.version}`,
].join('\n');
return `${packageJSON.bugs}/new?labels=kind/bug&title=&body=**Environment**\n${body}\n**Description**`;
}
}