forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.ts
More file actions
309 lines (265 loc) · 11.6 KB
/
view.ts
File metadata and controls
309 lines (265 loc) · 11.6 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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import {
Command,
Disposable,
Event,
EventEmitter,
ThemeIcon,
TreeDataProvider,
TreeItem,
TreeItemCollapsibleState,
TreeView,
Uri,
window,
workspace,
} from 'vscode';
import * as path from 'path';
import { Platform } from '../util/platform';
import { Context, KubernetesObject } from '@kubernetes/client-node';
import { KubeConfigUtils } from '../util/kubeUtils';
import { FileContentChangeNotifier, WatchUtil } from '../util/watch';
import { ServerlessFunction, serverlessInstance } from './functionImpl';
import { FunctionContextType, FunctionObject, FunctionStatus } from './types';
import ServerlessFunctionViewLoader from '../webview/serverless-function/serverlessFunctionLoader';
import { Functions } from './functions';
import { vsCommand } from '../vscommand';
import ManageRepositoryViewLoader from '../webview/serverless-manage-repository/manageRepositoryLoader';
const kubeConfigFolder: string = path.join(Platform.getUserHomePath(), '.kube');
type ExplorerItem = KubernetesObject | FunctionObject | Context | TreeItem;
export class ServerlessFunctionView implements TreeDataProvider<ExplorerItem>, Disposable {
private static instance: ServerlessFunctionView;
private treeView: TreeView<ExplorerItem>;
private fsw: FileContentChangeNotifier;
private kubeContext: Context;
private kubeConfig: KubeConfigUtils;
private currentNameSpace: string;
private eventEmitter: EventEmitter<ExplorerItem | undefined> =
new EventEmitter<ExplorerItem | undefined>();
readonly onDidChangeTreeData: Event<ExplorerItem | undefined> = this
.eventEmitter.event;
private serverlessFunction: ServerlessFunction = serverlessInstance();
private constructor() {
try {
this.kubeConfig = new KubeConfigUtils();
this.kubeContext = this.kubeConfig.getContextObject(this.kubeConfig.currentContext);
} catch (err) {
// ignore config loading error
}
try {
this.fsw = WatchUtil.watchFileForContextChange(kubeConfigFolder, 'config');
} catch (err) {
void window.showWarningMessage('Couldn\'t install watcher for Kubernetes configuration file. OpenShift Application Explorer view won\'t be updated automatically.');
}
this.fsw?.emitter?.on('file-changed', () => {
const ku2 = new KubeConfigUtils();
const newCtx = ku2.getContextObject(ku2.currentContext);
if (!this.kubeContext
|| (this.kubeContext.cluster !== newCtx.cluster
|| this.kubeContext.user !== newCtx.user
|| this.kubeContext.namespace !== newCtx.namespace)) {
this.refresh();
}
this.kubeContext = newCtx;
this.kubeConfig = ku2;
});
this.treeView = window.createTreeView<ExplorerItem>('openshiftServerlessFunctionsView', {
treeDataProvider: this,
});
}
static getInstance(): ServerlessFunctionView {
if (!ServerlessFunctionView.instance) {
ServerlessFunctionView.instance = new ServerlessFunctionView();
}
return ServerlessFunctionView.instance;
}
getTreeItem(element: ExplorerItem): TreeItem | Thenable<TreeItem> {
if ('kind' in element) {
if (element.kind === 'project') {
return {
label: element.metadata.name,
collapsibleState: TreeItemCollapsibleState.Expanded,
iconPath: new ThemeIcon('project')
}
}
} else if ('context' in element) {
const functionObj: FunctionObject = element;
const explorerItem: ExplorerItem = {
label: functionObj.name,
collapsibleState: TreeItemCollapsibleState.None
}
if (functionObj.context !== FunctionStatus.NONE) {
explorerItem.iconPath = new ThemeIcon('symbol-function'),
explorerItem.description = this.getDescription(functionObj.context),
explorerItem.tooltip = this.getTooltip(functionObj),
explorerItem.contextValue = this.getContext(functionObj),
explorerItem.command = this.getCommand(functionObj);
}
return explorerItem;
}
}
getContext(functionObj: FunctionObject): string {
if (functionObj.hadBuilt || functionObj.hasImage) {
if (Functions.getInstance().checkRunning(functionObj.folderURI.fsPath)) {
return FunctionContextType.RUNNING;
} else if (functionObj.context === FunctionStatus.CLUSTERLOCALBOTH) {
return FunctionContextType.LOCALDEPLOYFUNCTION
}
return FunctionContextType.LOCAlFUNCTIONSWithBuild;
}
if (functionObj.context === FunctionStatus.CLUSTERONLY) {
return FunctionContextType.DEPLOYFUNCTION;
} else if (functionObj.context === FunctionStatus.CLUSTERLOCALBOTH) {
return FunctionContextType.LOCALDEPLOYFUNCTION
}
return FunctionContextType.LOCAlFUNCTIONS;
}
getCommand(functionObj: FunctionObject): Command {
return { command: 'openshift.Serverless.openFunction', title: 'Open Function', arguments: [functionObj] };
}
getTooltip(functionObj: FunctionObject): string {
let text = '';
if (functionObj.name) {
text = `Name: ${functionObj.name}\n`;
}
if (functionObj.runtime) {
text += `Runtime: ${functionObj.name}\n`;
}
if (functionObj.folderURI) {
text += `Name: ${functionObj.folderURI.fsPath}\n`;
}
return text;
}
getDescription(context: string): string {
return context === FunctionStatus.CLUSTERLOCALBOTH ? 'Local/Cluster' : context === FunctionStatus.LOCALONLY ?
'Local Only' : context === FunctionStatus.CLUSTERONLY ? 'Cluster Only' : '';
}
setCurrentNameSpace(value: string) {
this.currentNameSpace = value;
}
public getCurrentNameSpace(): string {
return this.currentNameSpace;
}
async getChildren(element?: ExplorerItem): Promise<ExplorerItem[]> {
let result: ExplorerItem[] = [];
if (!element) {
result = [{
kind: 'project',
metadata: {
name: this.kubeContext?.namespace,
},
} as KubernetesObject];
this.setCurrentNameSpace(this.kubeContext?.namespace);
} else if ('kind' in element) {
if (element.kind === 'project') {
result = [...await this.serverlessFunction.getLocalFunctions()]
if (result.length === 0) {
const functionNode: FunctionObject = {
name: 'No Available Functions',
context: FunctionStatus.NONE
}
result = [functionNode]
}
}
}
return result;
}
refresh(target?: ExplorerItem): void {
this.eventEmitter.fire(target);
}
dispose(): void {
this.fsw?.watcher?.close();
this.treeView.dispose();
}
@vsCommand('openshift.Serverless.createFunction')
static async openServerlessFunction(): Promise<void> {
await ServerlessFunctionViewLoader.loadView('Serverless Function - Create');
}
@vsCommand('openshift.Serverless.manageRepository')
static async openManageRepository(): Promise<void> {
await ManageRepositoryViewLoader.loadView('Manage Repository');
}
@vsCommand('openshift.Serverless.refresh')
static refresh(target?: ExplorerItem) {
ServerlessFunctionView.getInstance().refresh(target);
}
@vsCommand('openshift.Serverless.build')
static async buildFunction(context: FunctionObject) {
await Functions.getInstance().build(context);
}
@vsCommand('openshift.Serverless.buildAndRun')
static buildAndRunFunction(context: FunctionObject) {
Functions.getInstance().run(context, true);
}
@vsCommand('openshift.Serverless.run')
static runFunction(context: FunctionObject) {
Functions.getInstance().run(context);
}
@vsCommand('openshift.Serverless.stopRun')
static stopRunFunction(context: FunctionObject) {
Functions.getInstance().stop(context);
}
@vsCommand('openshift.Serverless.deploy')
static deployFunction(context: FunctionObject) {
void Functions.getInstance().deploy(context);
}
@vsCommand('openshift.Serverless.undeploy')
static undeployFunction(context: FunctionObject) {
Functions.getInstance().undeploy(context);
}
@vsCommand('openshift.Serverless.invoke')
static async invokeFunction(context: FunctionObject) {
await ServerlessFunctionViewLoader.loadView(`${context.name} - Invoke`, true, context.context, context.folderURI, context.url);
}
@vsCommand('openshift.Serverless.openFunction')
static openFunction(context: FunctionObject) {
if (!context || !context.folderURI) {
return null;
}
const uriPath = Uri.file(path.join(context.folderURI.fsPath, 'func.yaml'));
workspace.openTextDocument(uriPath).then(
(doc) => {
if (doc) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
window.showTextDocument(doc, { preserveFocus: true, preview: true });
}
},
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
(err) => window.showErrorMessage(`Error loading document: ${err}`),
);
}
@vsCommand('openshift.Serverless.addEnv')
static async addEnv(context: FunctionObject) {
await Functions.getInstance().config(`Add environment variables '${context.name}'`, context, 'envs');
}
@vsCommand('openshift.Serverless.addLabel')
static async addLabel(context: FunctionObject) {
await Functions.getInstance().config(`Add Labels '${context.name}'`, context, 'labels');
}
@vsCommand('openshift.Serverless.addVolume')
static async addVolume(context: FunctionObject) {
await Functions.getInstance().config(`Add Volumes '${context.name}'`, context, 'volumes');
}
@vsCommand('openshift.Serverless.addGit')
static async addGit(context: FunctionObject) {
await Functions.getInstance().config(`Add Git '${context.name}'`, context, 'git');
}
@vsCommand('openshift.Serverless.removeEnv')
static removeEnv(context: FunctionObject) {
void Functions.getInstance().config(`Remove environment variables '${context.name}'`, context, 'envs', false);
}
@vsCommand('openshift.Serverless.removeLabel')
static removeLabel(context: FunctionObject) {
void Functions.getInstance().config(`Remove Labels '${context.name}'`, context, 'labels', false);
}
@vsCommand('openshift.Serverless.removeVolume')
static async removeVolume(context: FunctionObject) {
await Functions.getInstance().config(`Remove Volumes '${context.name}'`, context, 'volumes', false);
}
@vsCommand('openshift.Serverless.removeGit')
static async removeGit(context: FunctionObject) {
await Functions.getInstance().config(`Remove Git '${context.name}'`, context, 'git', false);
}
}